@@ -385,7 +385,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
385
385
acc2Addr := crypto .PubkeyToAddress (acc2Key .PublicKey )
386
386
387
387
signer := types.HomesteadSigner {}
388
- // Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_markets_test )
388
+ // Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makers_test )
389
389
generator := func (i int , block * core.BlockGen ) {
390
390
switch i {
391
391
case 0 :
@@ -420,9 +420,8 @@ func testGetNodeData(t *testing.T, protocol uint) {
420
420
peer , _ := newTestPeer ("peer" , protocol , backend )
421
421
defer peer .close ()
422
422
423
- // Fetch for now the entire chain db
423
+ // Collect all state tree hashes.
424
424
var hashes []common.Hash
425
-
426
425
it := backend .db .NewIterator (nil , nil )
427
426
for it .Next () {
428
427
if key := it .Key (); len (key ) == common .HashLength {
@@ -431,6 +430,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
431
430
}
432
431
it .Release ()
433
432
433
+ // Request all hashes.
434
434
p2p .Send (peer .app , GetNodeDataMsg , GetNodeDataPacket66 {
435
435
RequestId : 123 ,
436
436
GetNodeDataPacket : hashes ,
@@ -442,38 +442,40 @@ func testGetNodeData(t *testing.T, protocol uint) {
442
442
if msg .Code != NodeDataMsg {
443
443
t .Fatalf ("response packet code mismatch: have %x, want %x" , msg .Code , NodeDataMsg )
444
444
}
445
- var (
446
- data [][]byte
447
- res NodeDataPacket66
448
- )
445
+ var res NodeDataPacket66
449
446
if err := msg .Decode (& res ); err != nil {
450
447
t .Fatalf ("failed to decode response node data: %v" , err )
451
448
}
452
- data = res .NodeDataPacket
453
- // Verify that all hashes correspond to the requested data, and reconstruct a state tree
449
+
450
+ // Verify that all hashes correspond to the requested data.
451
+ data := res .NodeDataPacket
454
452
for i , want := range hashes {
455
453
if hash := crypto .Keccak256Hash (data [i ]); hash != want {
456
454
t .Errorf ("data hash mismatch: have %x, want %x" , hash , want )
457
455
}
458
456
}
459
- statedb := rawdb .NewMemoryDatabase ()
457
+
458
+ // Reconstruct state tree from the received data.
459
+ reconstructDB := rawdb .NewMemoryDatabase ()
460
460
for i := 0 ; i < len (data ); i ++ {
461
- statedb . Put ( hashes [i ]. Bytes () , data [i ])
461
+ rawdb . WriteTrieNode ( reconstructDB , hashes [i ], data [i ])
462
462
}
463
+
464
+ // Sanity check whether all state matches.
463
465
accounts := []common.Address {testAddr , acc1Addr , acc2Addr }
464
466
for i := uint64 (0 ); i <= backend .chain .CurrentBlock ().NumberU64 (); i ++ {
465
- trie , _ := state . New ( backend .chain .GetBlockByNumber (i ).Root (), state . NewDatabase ( statedb ), nil )
466
-
467
+ root := backend .chain .GetBlockByNumber (i ).Root ()
468
+ reconstructed , _ := state . New ( root , state . NewDatabase ( reconstructDB ), nil )
467
469
for j , acc := range accounts {
468
- state , _ := backend .chain .State ( )
470
+ state , _ := backend .chain .StateAt ( root )
469
471
bw := state .GetBalance (acc )
470
- bh := trie .GetBalance (acc )
472
+ bh := reconstructed .GetBalance (acc )
471
473
472
- if (bw != nil && bh == nil ) || ( bw == nil && bh ! = nil ) {
473
- t .Errorf ("test %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
474
+ if (bw == nil ) != ( bh = = nil ) {
475
+ t .Errorf ("block %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
474
476
}
475
- if bw != nil && bh != nil && bw .Cmp (bw ) != 0 {
476
- t .Errorf ("test %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
477
+ if bw != nil && bh != nil && bw .Cmp (bh ) != 0 {
478
+ t .Errorf ("block %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
477
479
}
478
480
}
479
481
}
0 commit comments