@ -86,7 +86,7 @@ func testMissingNode(t *testing.T, memonly bool) {
root , nodes , _ := trie . Commit ( false )
triedb . Update ( NewWithNodeSet ( nodes ) )
if ! memonly {
triedb . Commit ( root , true , nil )
triedb . Commit ( root , false )
}
trie , _ = New ( TrieID ( root ) , triedb )
@ -791,29 +791,23 @@ func (b *spongeBatch) Reset() {}
func ( b * spongeBatch ) Replay ( w ethdb . KeyValueWriter ) error { return nil }
// TestCommitSequence tests that the trie.Commit operation writes the elements of the trie
// in the expected order, and calls the callbacks in the expected order .
// in the expected order.
// The test data was based on the 'master' code, and is basically random. It can be used
// to check whether changes to the trie modifies the write order or data in any way.
func TestCommitSequence ( t * testing . T ) {
for i , tc := range [ ] struct {
count int
expWriteSeqHash [ ] byte
expCallbackSeqHash [ ] byte
count int
expWriteSeqHash [ ] byte
} {
{ 20 , common . FromHex ( "873c78df73d60e59d4a2bcf3716e8bfe14554549fea2fc147cb54129382a8066" ) ,
common . FromHex ( "ff00f91ac05df53b82d7f178d77ada54fd0dca64526f537034a5dbe41b17df2a" ) } ,
{ 200 , common . FromHex ( "ba03d891bb15408c940eea5ee3d54d419595102648d02774a0268d892add9c8e" ) ,
common . FromHex ( "f3cd509064c8d319bbdd1c68f511850a902ad275e6ed5bea11547e23d492a926" ) } ,
{ 2000 , common . FromHex ( "f7a184f20df01c94f09537401d11e68d97ad0c00115233107f51b9c287ce60c7" ) ,
common . FromHex ( "ff795ea898ba1e4cfed4a33b4cf5535a347a02cf931f88d88719faf810f9a1c9" ) } ,
{ 20 , common . FromHex ( "873c78df73d60e59d4a2bcf3716e8bfe14554549fea2fc147cb54129382a8066" ) } ,
{ 200 , common . FromHex ( "ba03d891bb15408c940eea5ee3d54d419595102648d02774a0268d892add9c8e" ) } ,
{ 2000 , common . FromHex ( "f7a184f20df01c94f09537401d11e68d97ad0c00115233107f51b9c287ce60c7" ) } ,
} {
addresses , accounts := makeAccounts ( tc . count )
// This spongeDb is used to check the sequence of disk-db-writes
s := & spongeDb { sponge : sha3 . NewLegacyKeccak256 ( ) }
db := NewDatabase ( rawdb . NewDatabase ( s ) )
trie := NewEmpty ( db )
// Another sponge is used to check the callback-sequence
callbackSponge := sha3 . NewLegacyKeccak256 ( )
// Fill the trie with elements
for i := 0 ; i < tc . count ; i ++ {
trie . Update ( crypto . Keccak256 ( addresses [ i ] [ : ] ) , accounts [ i ] )
@ -822,16 +816,10 @@ func TestCommitSequence(t *testing.T) {
root , nodes , _ := trie . Commit ( false )
db . Update ( NewWithNodeSet ( nodes ) )
// Flush memdb -> disk (sponge)
db . Commit ( root , false , func ( c common . Hash ) {
// And spongify the callback-order
callbackSponge . Write ( c [ : ] )
} )
db . Commit ( root , false )
if got , exp := s . sponge . Sum ( nil ) , tc . expWriteSeqHash ; ! bytes . Equal ( got , exp ) {
t . Errorf ( "test %d, disk write sequence wrong:\ngot %x exp %x\n" , i , got , exp )
}
if got , exp := callbackSponge . Sum ( nil ) , tc . expCallbackSeqHash ; ! bytes . Equal ( got , exp ) {
t . Errorf ( "test %d, call back sequence wrong:\ngot: %x exp %x\n" , i , got , exp )
}
}
}
@ -839,24 +827,18 @@ func TestCommitSequence(t *testing.T) {
// but uses random blobs instead of 'accounts'
func TestCommitSequenceRandomBlobs ( t * testing . T ) {
for i , tc := range [ ] struct {
count int
expWriteSeqHash [ ] byte
expCallbackSeqHash [ ] byte
count int
expWriteSeqHash [ ] byte
} {
{ 20 , common . FromHex ( "8e4a01548551d139fa9e833ebc4e66fc1ba40a4b9b7259d80db32cff7b64ebbc" ) ,
common . FromHex ( "450238d73bc36dc6cc6f926987e5428535e64be403877c4560e238a52749ba24" ) } ,
{ 200 , common . FromHex ( "6869b4e7b95f3097a19ddb30ff735f922b915314047e041614df06958fc50554" ) ,
common . FromHex ( "0ace0b03d6cb8c0b82f6289ef5b1a1838306b455a62dafc63cada8e2924f2550" ) } ,
{ 2000 , common . FromHex ( "444200e6f4e2df49f77752f629a96ccf7445d4698c164f962bbd85a0526ef424" ) ,
common . FromHex ( "117d30dafaa62a1eed498c3dfd70982b377ba2b46dd3e725ed6120c80829e518" ) } ,
{ 20 , common . FromHex ( "8e4a01548551d139fa9e833ebc4e66fc1ba40a4b9b7259d80db32cff7b64ebbc" ) } ,
{ 200 , common . FromHex ( "6869b4e7b95f3097a19ddb30ff735f922b915314047e041614df06958fc50554" ) } ,
{ 2000 , common . FromHex ( "444200e6f4e2df49f77752f629a96ccf7445d4698c164f962bbd85a0526ef424" ) } ,
} {
prng := rand . New ( rand . NewSource ( int64 ( i ) ) )
// This spongeDb is used to check the sequence of disk-db-writes
s := & spongeDb { sponge : sha3 . NewLegacyKeccak256 ( ) }
db := NewDatabase ( rawdb . NewDatabase ( s ) )
trie := NewEmpty ( db )
// Another sponge is used to check the callback-sequence
callbackSponge := sha3 . NewLegacyKeccak256 ( )
// Fill the trie with elements
for i := 0 ; i < tc . count ; i ++ {
key := make ( [ ] byte , 32 )
@ -875,16 +857,10 @@ func TestCommitSequenceRandomBlobs(t *testing.T) {
root , nodes , _ := trie . Commit ( false )
db . Update ( NewWithNodeSet ( nodes ) )
// Flush memdb -> disk (sponge)
db . Commit ( root , false , func ( c common . Hash ) {
// And spongify the callback-order
callbackSponge . Write ( c [ : ] )
} )
db . Commit ( root , false )
if got , exp := s . sponge . Sum ( nil ) , tc . expWriteSeqHash ; ! bytes . Equal ( got , exp ) {
t . Fatalf ( "test %d, disk write sequence wrong:\ngot %x exp %x\n" , i , got , exp )
}
if got , exp := callbackSponge . Sum ( nil ) , tc . expCallbackSeqHash ; ! bytes . Equal ( got , exp ) {
t . Fatalf ( "test %d, call back sequence wrong:\ngot: %x exp %x\n" , i , got , exp )
}
}
}
@ -920,7 +896,7 @@ func TestCommitSequenceStackTrie(t *testing.T) {
root , nodes , _ := trie . Commit ( false )
// Flush memdb -> disk (sponge)
db . Update ( NewWithNodeSet ( nodes ) )
db . Commit ( root , false , nil )
db . Commit ( root , false )
// And flush stacktrie -> disk
stRoot , err := stTrie . Commit ( )
if err != nil {
@ -968,7 +944,7 @@ func TestCommitSequenceSmallRoot(t *testing.T) {
root , nodes , _ := trie . Commit ( false )
// Flush memdb -> disk (sponge)
db . Update ( NewWithNodeSet ( nodes ) )
db . Commit ( root , false , nil )
db . Commit ( root , false )
// And flush stacktrie -> disk
stRoot , err := stTrie . Commit ( )
if err != nil {