aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/responses_test.go
blob: 0f911c88697dd647bc0977d1c0fe7576a5f36f35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package rpc

import (
    "encoding/json"
    "fmt"
    "math/big"
    "regexp"
    "testing"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/core/types"
)

const (
    reHash       = `"0x[0-9a-f]{64}"`                    // 32 bytes
    reHashOpt    = `"(0x[0-9a-f]{64})"|null`             // 32 bytes or null
    reAddress    = `"0x[0-9a-f]{40}"`                    // 20 bytes
    reAddressOpt = `"0x[0-9a-f]{40}"|null`               // 20 bytes or null
    reNum        = `"0x([1-9a-f][0-9a-f]{0,15})|0"`      // must not have left-padded zeros
    reNumNonZero = `"0x([1-9a-f][0-9a-f]{0,15})"`        // non-zero required must not have left-padded zeros
    reNumOpt     = `"0x([1-9a-f][0-9a-f]{0,15})|0"|null` // must not have left-padded zeros or null
    reData       = `"0x[0-9a-f]*"`                       // can be "empty"
    // reListHash   = `[("\w":"0x[0-9a-f]{64}",?)*]`
    // reListObj    = `[("\w":(".+"|null),?)*]`
)

func TestNewBlockRes(t *testing.T) {
    parentHash := common.HexToHash("0x01")
    coinbase := common.HexToAddress("0x01")
    root := common.HexToHash("0x01")
    difficulty := common.Big1
    nonce := uint64(1)
    extra := ""
    block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra)
    tests := map[string]string{
        "number":          reNum,
        "hash":            reHash,
        "parentHash":      reHash,
        "nonce":           reData,
        "sha3Uncles":      reHash,
        "logsBloom":       reData,
        "transactionRoot": reHash,
        "stateRoot":       reHash,
        "miner":           reAddress,
        "difficulty":      `"0x1"`,
        "totalDifficulty": reNum,
        "size":            reNumNonZero,
        "extraData":       reData,
        "gasLimit":        reNum,
        // "minGasPrice":  "0x",
        "gasUsed":   reNum,
        "timestamp": reNum,
        // "transactions": reListHash,
        // "uncles":       reListHash,
    }

    to := common.HexToAddress("0x02")
    amount := big.NewInt(1)
    gasAmount := big.NewInt(1)
    gasPrice := big.NewInt(1)
    data := []byte{1, 2, 3}
    tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)

    v := NewBlockRes(block, false)
    v.Transactions = make([]*TransactionRes, 1)
    v.Transactions[0] = NewTransactionRes(tx)
    j, _ := json.Marshal(v)

    for k, re := range tests {
        match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
        if !match {
            t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
        }
    }
}

func TestNewBlockResWithTrans(t *testing.T) {
    parentHash := common.HexToHash("0x01")
    coinbase := common.HexToAddress("0x01")
    root := common.HexToHash("0x01")
    difficulty := common.Big1
    nonce := uint64(1)
    extra := ""
    block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra)
    tests := map[string]string{
        "number":          reNum,
        "hash":            reHash,
        "parentHash":      reHash,
        "nonce":           reData,
        "sha3Uncles":      reHash,
        "logsBloom":       reData,
        "transactionRoot": reHash,
        "stateRoot":       reHash,
        "miner":           reAddress,
        "difficulty":      `"0x1"`,
        "totalDifficulty": reNum,
        "size":            reNumNonZero,
        "extraData":       reData,
        "gasLimit":        reNum,
        // "minGasPrice":  "0x",
        "gasUsed":   reNum,
        "timestamp": reNum,
        // "transactions": `[{.*}]`,
        // "uncles":       reListHash,
    }

    to := common.HexToAddress("0x02")
    amount := big.NewInt(1)
    gasAmount := big.NewInt(1)
    gasPrice := big.NewInt(1)
    data := []byte{1, 2, 3}
    tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)

    v := NewBlockRes(block, true)
    v.Transactions = make([]*TransactionRes, 1)
    v.Transactions[0] = NewTransactionRes(tx)
    j, _ := json.Marshal(v)

    for k, re := range tests {
        match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
        if !match {
            t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
        }
    }
}

func TestNewTransactionRes(t *testing.T) {
    to := common.HexToAddress("0x02")
    amount := big.NewInt(1)
    gasAmount := big.NewInt(1)
    gasPrice := big.NewInt(1)
    data := []byte{1, 2, 3}
    tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)

    tests := map[string]string{
        "hash":             reHash,
        "nonce":            reNum,
        "blockHash":        reHashOpt,
        "blockNum":         reNumOpt,
        "transactionIndex": reNumOpt,
        "from":             reAddress,
        "to":               reAddressOpt,
        "value":            reNum,
        "gas":              reNum,
        "gasPrice":         reNum,
        "input":            reData,
    }

    v := NewTransactionRes(tx)
    v.BlockHash = newHexData(common.HexToHash("0x030201"))
    v.BlockNumber = newHexNum(5)
    v.TxIndex = newHexNum(0)
    j, _ := json.Marshal(v)
    for k, re := range tests {
        match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
        if !match {
            t.Error(fmt.Sprintf("`%s` output json does not match format %s. Source %s", k, re, j))
        }
    }

}

func TestNewLogRes(t *testing.T) {
    log := makeStateLog(0)
    tests := map[string]string{
        "address": reAddress,
        // "topics": "[.*]"
        "data":        reData,
        "blockNumber": reNum,
        // "hash":             reHash,
        // "logIndex":         reNum,
        // "blockHash":        reHash,
        // "transactionHash":  reHash,
        "transactionIndex": reNum,
    }

    v := NewLogRes(log)
    j, _ := json.Marshal(v)

    for k, re := range tests {
        match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
        if !match {
            t.Error(fmt.Sprintf("`%s` output json does not match format %s. Got %s", k, re, j))
        }
    }

}

func TestNewLogsRes(t *testing.T) {
    logs := make([]state.Log, 3)
    logs[0] = makeStateLog(1)
    logs[1] = makeStateLog(2)
    logs[2] = makeStateLog(3)
    tests := map[string]string{}

    v := NewLogsRes(logs)
    j, _ := json.Marshal(v)

    for k, re := range tests {
        match, _ := regexp.MatchString(fmt.Sprintf(`[{.*"%s":%s.*}]`, k, re), string(j))
        if !match {
            t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
        }
    }

}

func makeStateLog(num int) state.Log {
    address := common.HexToAddress("0x0")
    data := []byte{1, 2, 3}
    number := uint64(num)
    topics := make([]common.Hash, 3)
    topics = append(topics, common.HexToHash("0x00"))
    topics = append(topics, common.HexToHash("0x10"))
    topics = append(topics, common.HexToHash("0x20"))
    log := state.NewLog(address, topics, data, number)
    return log
}