@ -385,7 +385,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
acc2Addr := crypto . PubkeyToAddress ( acc2Key . PublicKey )
acc2Addr := crypto . PubkeyToAddress ( acc2Key . PublicKey )
signer := types . HomesteadSigner { }
signer := types . HomesteadSigner { }
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_market s_test)
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_maker s_test)
generator := func ( i int , block * core . BlockGen ) {
generator := func ( i int , block * core . BlockGen ) {
switch i {
switch i {
case 0 :
case 0 :
@ -420,9 +420,8 @@ func testGetNodeData(t *testing.T, protocol uint) {
peer , _ := newTestPeer ( "peer" , protocol , backend )
peer , _ := newTestPeer ( "peer" , protocol , backend )
defer peer . close ( )
defer peer . close ( )
// Fetch for now the entire chain db
// Collect all state tree hashes.
var hashes [ ] common . Hash
var hashes [ ] common . Hash
it := backend . db . NewIterator ( nil , nil )
it := backend . db . NewIterator ( nil , nil )
for it . Next ( ) {
for it . Next ( ) {
if key := it . Key ( ) ; len ( key ) == common . HashLength {
if key := it . Key ( ) ; len ( key ) == common . HashLength {
@ -431,6 +430,7 @@ func testGetNodeData(t *testing.T, protocol uint) {
}
}
it . Release ( )
it . Release ( )
// Request all hashes.
p2p . Send ( peer . app , GetNodeDataMsg , GetNodeDataPacket66 {
p2p . Send ( peer . app , GetNodeDataMsg , GetNodeDataPacket66 {
RequestId : 123 ,
RequestId : 123 ,
GetNodeDataPacket : hashes ,
GetNodeDataPacket : hashes ,
@ -442,38 +442,40 @@ func testGetNodeData(t *testing.T, protocol uint) {
if msg . Code != NodeDataMsg {
if msg . Code != NodeDataMsg {
t . Fatalf ( "response packet code mismatch: have %x, want %x" , msg . Code , NodeDataMsg )
t . Fatalf ( "response packet code mismatch: have %x, want %x" , msg . Code , NodeDataMsg )
}
}
var (
var res NodeDataPacket66
data [ ] [ ] byte
res NodeDataPacket66
)
if err := msg . Decode ( & res ) ; err != nil {
if err := msg . Decode ( & res ) ; err != nil {
t . Fatalf ( "failed to decode response node data: %v" , err )
t . Fatalf ( "failed to decode response node data: %v" , err )
}
}
data = res . NodeDataPacket
// Verify that all hashes correspond to the requested data, and reconstruct a state tree
// Verify that all hashes correspond to the requested data.
data := res . NodeDataPacket
for i , want := range hashes {
for i , want := range hashes {
if hash := crypto . Keccak256Hash ( data [ i ] ) ; hash != want {
if hash := crypto . Keccak256Hash ( data [ i ] ) ; hash != want {
t . Errorf ( "data hash mismatch: have %x, want %x" , hash , want )
t . Errorf ( "data hash mismatch: have %x, want %x" , hash , want )
}
}
}
}
statedb := rawdb . NewMemoryDatabase ( )
// Reconstruct state tree from the received data.
reconstructDB := rawdb . NewMemoryDatabase ( )
for i := 0 ; i < len ( data ) ; i ++ {
for i := 0 ; i < len ( data ) ; i ++ {
statedb . Put ( hashes [ i ] . Bytes ( ) , data [ i ] )
rawdb . WriteTrieNode ( reconstructDB , hashes [ i ] , data [ i ] )
}
}
// Sanity check whether all state matches.
accounts := [ ] common . Address { testAddr , acc1Addr , acc2Addr }
accounts := [ ] common . Address { testAddr , acc1Addr , acc2Addr }
for i := uint64 ( 0 ) ; i <= backend . chain . CurrentBlock ( ) . NumberU64 ( ) ; i ++ {
for i := uint64 ( 0 ) ; i <= backend . chain . CurrentBlock ( ) . NumberU64 ( ) ; i ++ {
trie , _ := state . New ( backend . chain . GetBlockByNumber ( i ) . Root ( ) , state . NewDatabase ( statedb ) , nil )
root := backend . chain . GetBlockByNumber ( i ) . Root ( )
reconstructed , _ := state . New ( root , state . NewDatabase ( reconstructDB ) , nil )
for j , acc := range accounts {
for j , acc := range accounts {
state , _ := backend . chain . State ( )
state , _ := backend . chain . StateAt ( root )
bw := state . GetBalance ( acc )
bw := state . GetBalance ( acc )
bh := trie . GetBalance ( acc )
bh := reconstructed . GetBalance ( acc )
if ( bw != nil && bh == nil ) || ( bw == nil && bh ! = nil ) {
if ( bw == nil ) != ( bh = = nil ) {
t . Errorf ( "test %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
t . Errorf ( "block %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
}
}
if bw != nil && bh != nil && bw . Cmp ( bw ) != 0 {
if bw != nil && bh != nil && bw . Cmp ( bh ) != 0 {
t . Errorf ( "test %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
t . Errorf ( "block %d, account %d: balance mismatch: have %v, want %v" , i , j , bh , bw )
}
}
}
}
}
}