21
21
package core
22
22
23
23
import (
24
+ "bytes"
25
+ "encoding/hex"
24
26
"math/big"
25
27
"testing"
26
28
"time"
@@ -42,17 +44,51 @@ import (
42
44
43
45
var (
44
46
// testKey is a private key to use for funding a tester account.
45
- testKey , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
47
+ testKey , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
48
+ contractCode , _ = hex .DecodeString ("608060405260016000806101000a81548160ff02191690831515021790555034801561002a57600080fd5b506101688061003a6000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806389a2d8011461003b578063b0483f4814610059575b600080fd5b610043610075565b60405161005091906100f4565b60405180910390f35b610073600480360381019061006e91906100bc565b61008b565b005b60008060009054906101000a900460ff16905090565b806000806101000a81548160ff02191690831515021790555050565b6000813590506100b68161011b565b92915050565b6000602082840312156100ce57600080fd5b60006100dc848285016100a7565b91505092915050565b6100ee8161010f565b82525050565b600060208201905061010960008301846100e5565b92915050565b60008115159050919050565b6101248161010f565b811461012f57600080fd5b5056fea264697066735822122092f788b569bfc3786e90601b5dbec01cfc3d76094164fd66ca7d599c4239fc5164736f6c63430008000033" )
49
+ contractAddr = common .HexToAddress ("0xe74a3c7427cda785e0000d42a705b1f3fd371e09" )
50
+ contractSlot = common .HexToHash ("0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563" )
51
+ contractData1 , _ = hex .DecodeString ("b0483f480000000000000000000000000000000000000000000000000000000000000000" )
52
+ contractData2 , _ = hex .DecodeString ("b0483f480000000000000000000000000000000000000000000000000000000000000001" )
53
+ commonGas = 192138
46
54
// testAddr is the Ethereum address of the tester account.
47
55
testAddr = crypto .PubkeyToAddress (testKey .PublicKey )
56
+
57
+ checkBlocks = map [int ]checkBlockParam {
58
+ 12 : {
59
+ txs : []checkTransactionParam {
60
+ {
61
+ to : & contractAddr ,
62
+ slot : contractSlot ,
63
+ value : []byte {01 },
64
+ },
65
+ }},
66
+
67
+ 13 : {
68
+ txs : []checkTransactionParam {
69
+ {
70
+ to : & contractAddr ,
71
+ slot : contractSlot ,
72
+ value : []byte {},
73
+ },
74
+ }},
75
+ 14 : {
76
+ txs : []checkTransactionParam {
77
+ {
78
+ to : & contractAddr ,
79
+ slot : contractSlot ,
80
+ value : []byte {01 },
81
+ },
82
+ }},
83
+ }
48
84
// testBlocks is the test parameters array for specific blocks.
49
85
testBlocks = []testBlockParam {
50
86
{
51
87
// This txs params also used to default block.
52
88
blockNr : 11 ,
53
89
txs : []testTransactionParam {
54
90
{
55
- to : common.Address {0x01 },
91
+ to : & common.Address {0x01 },
56
92
value : big .NewInt (1 ),
57
93
gasPrice : big .NewInt (1 ),
58
94
data : nil ,
@@ -63,51 +99,74 @@ var (
63
99
blockNr : 12 ,
64
100
txs : []testTransactionParam {
65
101
{
66
- to : common.Address {0x01 },
102
+ to : & common.Address {0x01 },
67
103
value : big .NewInt (1 ),
68
104
gasPrice : big .NewInt (1 ),
69
105
data : nil ,
70
106
},
71
107
{
72
- to : common.Address {0x02 },
108
+ to : & common.Address {0x02 },
73
109
value : big .NewInt (2 ),
74
110
gasPrice : big .NewInt (2 ),
75
111
data : nil ,
76
112
},
113
+ {
114
+ to : nil ,
115
+ value : big .NewInt (0 ),
116
+ gasPrice : big .NewInt (2 ),
117
+ data : contractCode ,
118
+ },
77
119
},
78
120
},
79
121
{
80
122
blockNr : 13 ,
81
123
txs : []testTransactionParam {
82
124
{
83
- to : common.Address {0x01 },
125
+ to : & common.Address {0x01 },
84
126
value : big .NewInt (1 ),
85
127
gasPrice : big .NewInt (1 ),
86
128
data : nil ,
87
129
},
88
130
{
89
- to : common.Address {0x02 },
131
+ to : & common.Address {0x02 },
90
132
value : big .NewInt (2 ),
91
133
gasPrice : big .NewInt (2 ),
92
134
data : nil ,
93
135
},
94
136
{
95
- to : common.Address {0x03 },
137
+ to : & common.Address {0x03 },
96
138
value : big .NewInt (3 ),
97
139
gasPrice : big .NewInt (3 ),
98
140
data : nil ,
99
141
},
142
+ {
143
+ to : & contractAddr ,
144
+ value : big .NewInt (0 ),
145
+ gasPrice : big .NewInt (3 ),
146
+ data : contractData1 ,
147
+ },
100
148
},
101
149
},
102
150
{
103
151
blockNr : 14 ,
152
+ txs : []testTransactionParam {
153
+ {
154
+ to : & contractAddr ,
155
+ value : big .NewInt (0 ),
156
+ gasPrice : big .NewInt (3 ),
157
+ data : contractData2 ,
158
+ },
159
+ },
160
+ },
161
+ {
162
+ blockNr : 15 ,
104
163
txs : []testTransactionParam {},
105
164
},
106
165
}
107
166
)
108
167
109
168
type testTransactionParam struct {
110
- to common.Address
169
+ to * common.Address
111
170
value * big.Int
112
171
gasPrice * big.Int
113
172
data []byte
@@ -118,6 +177,16 @@ type testBlockParam struct {
118
177
txs []testTransactionParam
119
178
}
120
179
180
+ type checkTransactionParam struct {
181
+ to * common.Address
182
+ slot common.Hash
183
+ value []byte
184
+ }
185
+
186
+ type checkBlockParam struct {
187
+ txs []checkTransactionParam
188
+ }
189
+
121
190
// testBackend is a mock implementation of the live Ethereum message handler. Its
122
191
// purpose is to allow testing the request/reply workflows and wire serialization
123
192
// in the `eth` protocol without actually doing any data processing.
@@ -153,8 +222,15 @@ func newTestBackendWithGenerator(blocks int, lightProcess bool) *testBackend {
153
222
// Specific block setting, the index in this generator has 1 diff from specified blockNr.
154
223
if i + 1 == testBlock .blockNr {
155
224
for _ , testTransaction := range testBlock .txs {
156
- tx , err := types .SignTx (types .NewTransaction (block .TxNonce (testAddr ), testTransaction .to ,
157
- testTransaction .value , params .TxGas , testTransaction .gasPrice , testTransaction .data ), signer , testKey )
225
+ var transaction * types.Transaction
226
+ if testTransaction .to == nil {
227
+ transaction = types .NewContractCreation (block .TxNonce (testAddr ),
228
+ testTransaction .value , uint64 (commonGas ), testTransaction .gasPrice , testTransaction .data )
229
+ } else {
230
+ transaction = types .NewTransaction (block .TxNonce (testAddr ), * testTransaction .to ,
231
+ testTransaction .value , uint64 (commonGas ), testTransaction .gasPrice , testTransaction .data )
232
+ }
233
+ tx , err := types .SignTx (transaction , signer , testKey )
158
234
if err != nil {
159
235
panic (err )
160
236
}
@@ -168,8 +244,8 @@ func newTestBackendWithGenerator(blocks int, lightProcess bool) *testBackend {
168
244
// We want to simulate an empty middle block, having the same state as the
169
245
// first one. The last is needs a state change again to force a reorg.
170
246
for _ , testTransaction := range testBlocks [0 ].txs {
171
- tx , err := types .SignTx (types .NewTransaction (block .TxNonce (testAddr ), testTransaction .to ,
172
- testTransaction .value , params . TxGas , testTransaction .gasPrice , testTransaction .data ), signer , testKey )
247
+ tx , err := types .SignTx (types .NewTransaction (block .TxNonce (testAddr ), * testTransaction .to ,
248
+ testTransaction .value , uint64 ( commonGas ) , testTransaction .gasPrice , testTransaction .data ), signer , testKey )
173
249
if err != nil {
174
250
panic (err )
175
251
}
@@ -241,6 +317,14 @@ func TestProcessDiffLayer(t *testing.T) {
241
317
lightBackend .Chain ().HandleDiffLayer (diff , "testpid" , true )
242
318
}
243
319
_ , err := lightBackend .chain .insertChain ([]* types.Block {block }, true )
320
+ if checks , exist := checkBlocks [i ]; exist {
321
+ for _ , check := range checks .txs {
322
+ s , _ := lightBackend .Chain ().Snapshots ().Snapshot (block .Root ()).Storage (crypto .Keccak256Hash ((* check .to )[:]), check .slot )
323
+ if ! bytes .Equal (s , check .value ) {
324
+ t .Fatalf ("Expected value %x, get %x" , check .value , s )
325
+ }
326
+ }
327
+ }
244
328
if err != nil {
245
329
t .Errorf ("failed to insert block %v" , err )
246
330
}
@@ -385,13 +469,14 @@ func TestGetDiffAccounts(t *testing.T) {
385
469
t .Errorf ("the diff accounts does't include addr: %v" , testAddr )
386
470
}
387
471
}
388
-
389
472
for _ , transaction := range testBlock .txs {
473
+ if transaction .to == nil || len (transaction .data ) > 0 {
474
+ continue
475
+ }
390
476
for idx , account := range accounts {
391
- if transaction .to == account {
477
+ if * transaction .to == account {
392
478
break
393
479
}
394
-
395
480
if idx == len (accounts )- 1 {
396
481
t .Errorf ("the diff accounts does't include addr: %v" , transaction .to )
397
482
}
0 commit comments