@@ -46,6 +46,12 @@ func TestSavepoint(t *testing.T) {
46
46
testutil .AssertNoError (t , err , "Error upon historyDatabase.GetLastSavepoint()" )
47
47
testutil .AssertNil (t , savepoint )
48
48
49
+ // ShouldRecover should return true when no savepoint is found and recovery from block 0
50
+ status , blockNum , err := env .testHistoryDB .ShouldRecover (0 )
51
+ testutil .AssertNoError (t , err , "Error upon historyDatabase.ShouldRecover()" )
52
+ testutil .AssertEquals (t , status , true )
53
+ testutil .AssertEquals (t , blockNum , uint64 (0 ))
54
+
49
55
bg , gb := testutil .NewBlockGenerator (t , "testLedger" , false )
50
56
testutil .AssertNoError (t , env .testHistoryDB .Commit (gb ), "" )
51
57
// read the savepoint, it should now exist and return a Height object with BlockNum 0
@@ -63,6 +69,31 @@ func TestSavepoint(t *testing.T) {
63
69
savepoint , err = env .testHistoryDB .GetLastSavepoint ()
64
70
testutil .AssertNoError (t , err , "Error upon historyDatabase.GetLastSavepoint()" )
65
71
testutil .AssertEquals (t , savepoint .BlockNum , uint64 (1 ))
72
+
73
+ // Should Recover should return false
74
+ status , blockNum , err = env .testHistoryDB .ShouldRecover (1 )
75
+ testutil .AssertNoError (t , err , "Error upon historyDatabase.ShouldRecover()" )
76
+ testutil .AssertEquals (t , status , false )
77
+ testutil .AssertEquals (t , blockNum , uint64 (2 ))
78
+
79
+ // create the next block (block 2)
80
+ simulator , _ = env .txmgr .NewTxSimulator ()
81
+ simulator .SetState ("ns1" , "key1" , []byte ("value2" ))
82
+ simulator .Done ()
83
+ simRes , _ = simulator .GetTxSimulationResults ()
84
+ block2 := bg .NextBlock ([][]byte {simRes })
85
+
86
+ // assume that the peer failed to commit this block to historyDB and is being recovered now
87
+ env .testHistoryDB .CommitLostBlock (block2 )
88
+ savepoint , err = env .testHistoryDB .GetLastSavepoint ()
89
+ testutil .AssertNoError (t , err , "Error upon historyDatabase.GetLastSavepoint()" )
90
+ testutil .AssertEquals (t , savepoint .BlockNum , uint64 (2 ))
91
+
92
+ //Pass high blockNum, ShouldRecover should return true with 3 as blocknum to recover from
93
+ status , blockNum , err = env .testHistoryDB .ShouldRecover (10 )
94
+ testutil .AssertNoError (t , err , "Error upon historyDatabase.ShouldRecover()" )
95
+ testutil .AssertEquals (t , status , true )
96
+ testutil .AssertEquals (t , blockNum , uint64 (3 ))
66
97
}
67
98
68
99
func TestHistory (t * testing.T ) {
0 commit comments