@@ -39,11 +39,12 @@ import (
39
39
)
40
40
41
41
var (
42
- testKey , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
43
- testAddr = crypto .PubkeyToAddress (testKey .PublicKey )
44
- testSlot = common .HexToHash ("0xdeadbeef" )
45
- testValue = crypto .Keccak256Hash (testSlot [:])
46
- testBalance = big .NewInt (2e15 )
42
+ testKey , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
43
+ testAddr = crypto .PubkeyToAddress (testKey .PublicKey )
44
+ testContract = common .HexToAddress ("0xbeef" )
45
+ testSlot = common .HexToHash ("0xdeadbeef" )
46
+ testValue = crypto .Keccak256Hash (testSlot [:])
47
+ testBalance = big .NewInt (2e15 )
47
48
)
48
49
49
50
func newTestBackend (t * testing.T ) (* node.Node , []* types.Block ) {
@@ -78,8 +79,9 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
78
79
79
80
func generateTestChain () (* core.Genesis , []* types.Block ) {
80
81
genesis := & core.Genesis {
81
- Config : params .AllEthashProtocolChanges ,
82
- Alloc : core.GenesisAlloc {testAddr : {Balance : testBalance , Storage : map [common.Hash ]common.Hash {testSlot : testValue }}},
82
+ Config : params .AllEthashProtocolChanges ,
83
+ Alloc : core.GenesisAlloc {testAddr : {Balance : testBalance , Storage : map [common.Hash ]common.Hash {testSlot : testValue }},
84
+ testContract : {Nonce : 1 , Code : []byte {0x13 , 0x37 }}},
83
85
ExtraData : []byte ("test genesis" ),
84
86
Timestamp : 9000 ,
85
87
}
@@ -103,8 +105,11 @@ func TestGethClient(t *testing.T) {
103
105
test func (t * testing.T )
104
106
}{
105
107
{
106
- "TestGetProof" ,
107
- func (t * testing.T ) { testGetProof (t , client ) },
108
+ "TestGetProof1" ,
109
+ func (t * testing.T ) { testGetProof (t , client , testAddr ) },
110
+ }, {
111
+ "TestGetProof2" ,
112
+ func (t * testing.T ) { testGetProof (t , client , testContract ) },
108
113
}, {
109
114
"TestGetProofCanonicalizeKeys" ,
110
115
func (t * testing.T ) { testGetProofCanonicalizeKeys (t , client ) },
@@ -201,38 +206,41 @@ func testAccessList(t *testing.T, client *rpc.Client) {
201
206
}
202
207
}
203
208
204
- func testGetProof (t * testing.T , client * rpc.Client ) {
209
+ func testGetProof (t * testing.T , client * rpc.Client , addr common. Address ) {
205
210
ec := New (client )
206
211
ethcl := ethclient .NewClient (client )
207
- result , err := ec .GetProof (context .Background (), testAddr , []string {testSlot .String ()}, nil )
212
+ result , err := ec .GetProof (context .Background (), addr , []string {testSlot .String ()}, nil )
208
213
if err != nil {
209
214
t .Fatal (err )
210
215
}
211
- if ! bytes . Equal ( result .Address [:], testAddr [:]) {
212
- t .Fatalf ("unexpected address, want : %v got : %v" , testAddr , result .Address )
216
+ if result .Address != addr {
217
+ t .Fatalf ("unexpected address, have : %v want : %v" , result .Address , addr )
213
218
}
214
219
// test nonce
215
- nonce , _ := ethcl .NonceAt (context .Background (), result .Address , nil )
216
- if result .Nonce != nonce {
220
+ if nonce , _ := ethcl .NonceAt (context .Background (), addr , nil ); result .Nonce != nonce {
217
221
t .Fatalf ("invalid nonce, want: %v got: %v" , nonce , result .Nonce )
218
222
}
219
223
// test balance
220
- balance , _ := ethcl .BalanceAt (context .Background (), result .Address , nil )
221
- if result .Balance .Cmp (balance ) != 0 {
224
+ if balance , _ := ethcl .BalanceAt (context .Background (), addr , nil ); result .Balance .Cmp (balance ) != 0 {
222
225
t .Fatalf ("invalid balance, want: %v got: %v" , balance , result .Balance )
223
226
}
224
-
225
227
// test storage
226
228
if len (result .StorageProof ) != 1 {
227
229
t .Fatalf ("invalid storage proof, want 1 proof, got %v proof(s)" , len (result .StorageProof ))
228
230
}
229
- proof := result .StorageProof [0 ]
230
- slotValue , _ := ethcl .StorageAt (context .Background (), testAddr , testSlot , nil )
231
- if ! bytes .Equal (slotValue , proof .Value .Bytes ()) {
232
- t .Fatalf ("invalid storage proof value, want: %v, got: %v" , slotValue , proof .Value .Bytes ())
231
+ for _ , proof := range result .StorageProof {
232
+ if proof .Key != testSlot .String () {
233
+ t .Fatalf ("invalid storage proof key, want: %q, got: %q" , testSlot .String (), proof .Key )
234
+ }
235
+ slotValue , _ := ethcl .StorageAt (context .Background (), addr , common .HexToHash (proof .Key ), nil )
236
+ if have , want := common .BigToHash (proof .Value ), common .BytesToHash (slotValue ); have != want {
237
+ t .Fatalf ("addr %x, invalid storage proof value: have: %v, want: %v" , addr , have , want )
238
+ }
233
239
}
234
- if proof .Key != testSlot .String () {
235
- t .Fatalf ("invalid storage proof key, want: %q, got: %q" , testSlot .String (), proof .Key )
240
+ // test code
241
+ code , _ := ethcl .CodeAt (context .Background (), addr , nil )
242
+ if have , want := result .CodeHash , crypto .Keccak256Hash (code ); have != want {
243
+ t .Fatalf ("codehash wrong, have %v want %v " , have , want )
236
244
}
237
245
}
238
246
0 commit comments