@@ -816,7 +816,8 @@ func testAccountIteratorDeletions(t *testing.T, newIterator func(db *Database, r
816
816
config := & Config {
817
817
NoAsyncGeneration : true ,
818
818
}
819
- db := New (rawdb .NewMemoryDatabase (), config , false )
819
+ memoryDB := rawdb .NewMemoryDatabase ()
820
+ db := New (memoryDB , config , false )
820
821
821
822
// Stack three diff layers on top with various overlaps
822
823
db .Update (common .HexToHash ("0x02" ), types .EmptyRootHash , 1 , trienode .NewMergedNodeSet (),
@@ -831,31 +832,55 @@ func testAccountIteratorDeletions(t *testing.T, newIterator func(db *Database, r
831
832
db .Update (common .HexToHash ("0x04" ), common .HexToHash ("0x03" ), 3 , trienode .NewMergedNodeSet (),
832
833
NewStateSetWithOrigin (randomAccountSet ("0x33" , "0x44" , "0x55" ), nil , nil , nil , false ))
833
834
834
- // The output should be 11,33,44,55
835
- it := newIterator (db , common .HexToHash ("0x04" ), common.Hash {})
836
- // Do a quick check
837
- verifyIterator (t , 4 , it , verifyAccount )
838
- it .Release ()
839
-
840
- // And a more detailed verification that we indeed do not see '0x22'
841
- it = newIterator (db , common .HexToHash ("0x04" ), common.Hash {})
842
- defer it .Release ()
843
- for it .Next () {
844
- hash := it .Hash ()
845
- if it .Account () == nil {
846
- t .Errorf ("iterator returned nil-value for hash %x" , hash )
847
- }
848
- if hash == deleted {
849
- t .Errorf ("expected deleted elem %x to not be returned by iterator" , deleted )
835
+ verify := func () {
836
+ // The output should be 11,33,44,55
837
+ it := newIterator (db , common .HexToHash ("0x04" ), common.Hash {})
838
+ // Do a quick check
839
+ verifyIterator (t , 4 , it , verifyAccount )
840
+ it .Release ()
841
+
842
+ // And a more detailed verification that we indeed do not see '0x22'
843
+ it = newIterator (db , common .HexToHash ("0x04" ), common.Hash {})
844
+ defer it .Release ()
845
+ for it .Next () {
846
+ hash := it .Hash ()
847
+ if it .Account () == nil {
848
+ t .Errorf ("iterator returned nil-value for hash %x" , hash )
849
+ }
850
+ if hash == deleted {
851
+ t .Errorf ("expected deleted elem %x to not be returned by iterator" , deleted )
852
+ }
850
853
}
851
854
}
855
+ verify ()
856
+
857
+ if err := db .Journal (common .HexToHash ("0x04" )); err != nil {
858
+ t .Fatalf ("Failed to journal the database, %v" , err )
859
+ }
860
+ if err := db .Close (); err != nil {
861
+ t .Fatalf ("Failed to close the database, %v" , err )
862
+ }
863
+ db = New (memoryDB , config , false )
864
+
865
+ verify ()
852
866
}
853
867
854
868
func TestStorageIteratorDeletions (t * testing.T ) {
855
869
config := & Config {
856
870
NoAsyncGeneration : true ,
857
871
}
858
- db := New (rawdb .NewMemoryDatabase (), config , false )
872
+ memoryDB := rawdb .NewMemoryDatabase ()
873
+ db := New (memoryDB , config , false )
874
+
875
+ restart := func (head common.Hash ) {
876
+ if err := db .Journal (head ); err != nil {
877
+ t .Fatalf ("Failed to journal the database, %v" , err )
878
+ }
879
+ if err := db .Close (); err != nil {
880
+ t .Fatalf ("Failed to close the database, %v" , err )
881
+ }
882
+ db = New (memoryDB , config , false )
883
+ }
859
884
860
885
// Stack three diff layers on top with various overlaps
861
886
db .Update (common .HexToHash ("0x02" ), types .EmptyRootHash , 1 , trienode .NewMergedNodeSet (),
@@ -874,6 +899,19 @@ func TestStorageIteratorDeletions(t *testing.T) {
874
899
verifyIterator (t , 3 , it , verifyStorage )
875
900
it .Release ()
876
901
902
+ // Ensure the iteration result aligns after the database restart
903
+ restart (common .HexToHash ("0x03" ))
904
+
905
+ // The output should be 02,04,05,06
906
+ it , _ = db .StorageIterator (common .HexToHash ("0x03" ), common .HexToHash ("0xaa" ), common.Hash {})
907
+ verifyIterator (t , 4 , it , verifyStorage )
908
+ it .Release ()
909
+
910
+ // The output should be 04,05,06
911
+ it , _ = db .StorageIterator (common .HexToHash ("0x03" ), common .HexToHash ("0xaa" ), common .HexToHash ("0x03" ))
912
+ verifyIterator (t , 3 , it , verifyStorage )
913
+ it .Release ()
914
+
877
915
// Destruct the whole storage
878
916
accounts := map [common.Hash ][]byte {
879
917
common .HexToHash ("0xaa" ): nil ,
@@ -885,6 +923,12 @@ func TestStorageIteratorDeletions(t *testing.T) {
885
923
verifyIterator (t , 0 , it , verifyStorage )
886
924
it .Release ()
887
925
926
+ // Ensure the iteration result aligns after the database restart
927
+ restart (common .HexToHash ("0x04" ))
928
+ it , _ = db .StorageIterator (common .HexToHash ("0x04" ), common .HexToHash ("0xaa" ), common.Hash {})
929
+ verifyIterator (t , 0 , it , verifyStorage )
930
+ it .Release ()
931
+
888
932
// Re-insert the slots of the same account
889
933
db .Update (common .HexToHash ("0x05" ), common .HexToHash ("0x04" ), 4 , trienode .NewMergedNodeSet (),
890
934
NewStateSetWithOrigin (randomAccountSet ("0xaa" ), randomStorageSet ([]string {"0xaa" }, [][]string {{"0x07" , "0x08" , "0x09" }}, nil ), nil , nil , false ))
@@ -894,6 +938,14 @@ func TestStorageIteratorDeletions(t *testing.T) {
894
938
verifyIterator (t , 3 , it , verifyStorage )
895
939
it .Release ()
896
940
941
+ // Ensure the iteration result aligns after the database restart
942
+ restart (common .HexToHash ("0x05" ))
943
+
944
+ // The output should be 07,08,09
945
+ it , _ = db .StorageIterator (common .HexToHash ("0x05" ), common .HexToHash ("0xaa" ), common.Hash {})
946
+ verifyIterator (t , 3 , it , verifyStorage )
947
+ it .Release ()
948
+
897
949
// Destruct the whole storage but re-create the account in the same layer
898
950
db .Update (common .HexToHash ("0x06" ), common .HexToHash ("0x05" ), 5 , trienode .NewMergedNodeSet (),
899
951
NewStateSetWithOrigin (randomAccountSet ("0xaa" ), randomStorageSet ([]string {"0xaa" }, [][]string {{"0x11" , "0x12" }}, [][]string {{"0x07" , "0x08" , "0x09" }}), nil , nil , false ))
@@ -903,6 +955,13 @@ func TestStorageIteratorDeletions(t *testing.T) {
903
955
it .Release ()
904
956
905
957
verifyIterator (t , 2 , db .tree .get (common .HexToHash ("0x06" )).(* diffLayer ).newBinaryStorageIterator (common .HexToHash ("0xaa" ), common.Hash {}), verifyStorage )
958
+
959
+ // Ensure the iteration result aligns after the database restart
960
+ restart (common .HexToHash ("0x06" ))
961
+ it , _ = db .StorageIterator (common .HexToHash ("0x06" ), common .HexToHash ("0xaa" ), common.Hash {})
962
+ verifyIterator (t , 2 , it , verifyStorage ) // The output should be 11,12
963
+ it .Release ()
964
+ verifyIterator (t , 2 , db .tree .get (common .HexToHash ("0x06" )).(* diffLayer ).newBinaryStorageIterator (common .HexToHash ("0xaa" ), common.Hash {}), verifyStorage )
906
965
}
907
966
908
967
// TestStaleIterator tests if the iterator could correctly terminate the iteration
0 commit comments