@ -242,3 +242,69 @@ func TestTrieTracePrevValue(t *testing.T) {
}
}
}
func TestDeleteAll ( t * testing . T ) {
db := NewDatabase ( rawdb . NewMemoryDatabase ( ) )
trie := NewEmpty ( db )
trie . tracer = newTracer ( )
// Insert a batch of entries, all the nodes should be marked as inserted
vals := [ ] struct { k , v string } {
{ "do" , "verb" } ,
{ "ether" , "wookiedoo" } ,
{ "horse" , "stallion" } ,
{ "shaman" , "horse" } ,
{ "doge" , "coin" } ,
{ "dog" , "puppy" } ,
{ "somethingveryoddindeedthis is" , "myothernodedata" } ,
}
for _ , val := range vals {
trie . Update ( [ ] byte ( val . k ) , [ ] byte ( val . v ) )
}
root , set , err := trie . Commit ( false )
if err != nil {
t . Fatal ( err )
}
if err := db . Update ( NewWithNodeSet ( set ) ) ; err != nil {
t . Fatal ( err )
}
// Delete entries from trie, ensure all values are detected
trie , _ = New ( TrieID ( root ) , db )
trie . tracer = newTracer ( )
trie . resolveAndTrack ( root . Bytes ( ) , nil )
// Iterate all existent nodes
var (
it = trie . NodeIterator ( nil )
nodes = make ( map [ string ] [ ] byte )
)
for it . Next ( true ) {
if it . Hash ( ) != ( common . Hash { } ) {
nodes [ string ( it . Path ( ) ) ] = common . CopyBytes ( it . NodeBlob ( ) )
}
}
// Perform deletion to purge the entire trie
for _ , val := range vals {
trie . Delete ( [ ] byte ( val . k ) )
}
root , set , err = trie . Commit ( false )
if err != nil {
t . Fatalf ( "Failed to delete trie %v" , err )
}
if root != emptyRoot {
t . Fatalf ( "Invalid trie root %v" , root )
}
for path , blob := range set . deletes {
prev , ok := nodes [ path ]
if ! ok {
t . Fatalf ( "Extra node deleted %v" , [ ] byte ( path ) )
}
if ! bytes . Equal ( prev , blob ) {
t . Fatalf ( "Unexpected previous value %v" , [ ] byte ( path ) )
}
}
if len ( set . deletes ) != len ( nodes ) {
t . Fatalf ( "Unexpected deletion set" )
}
}