github.com/Finschia/finschia-sdk@v0.48.1/store/rootmulti/rollback_test.go (about) 1 package rootmulti_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 abci "github.com/tendermint/tendermint/abci/types" 10 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 11 dbm "github.com/tendermint/tm-db" 12 13 ocabci "github.com/Finschia/ostracon/abci/types" 14 "github.com/Finschia/ostracon/libs/log" 15 16 "github.com/Finschia/finschia-sdk/simapp" 17 ) 18 19 func setup(withGenesis bool, invCheckPeriod uint, db dbm.DB) (*simapp.SimApp, simapp.GenesisState) { 20 encCdc := simapp.MakeTestEncodingConfig() 21 app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, invCheckPeriod, encCdc, simapp.EmptyAppOptions{}) 22 if withGenesis { 23 return app, simapp.NewDefaultGenesisState(encCdc.Marshaler) 24 } 25 return app, simapp.GenesisState{} 26 } 27 28 // Setup initializes a new SimApp. A Nop logger is set in SimApp. 29 func SetupWithDB(isCheckTx bool, db dbm.DB) *simapp.SimApp { 30 app, genesisState := setup(!isCheckTx, 5, db) 31 if !isCheckTx { 32 // init chain must be called to stop deliverState from being nil 33 stateBytes, err := json.MarshalIndent(genesisState, "", " ") 34 if err != nil { 35 panic(err) 36 } 37 38 // Initialize the chain 39 app.InitChain( 40 abci.RequestInitChain{ 41 Validators: []abci.ValidatorUpdate{}, 42 ConsensusParams: simapp.DefaultConsensusParams, 43 AppStateBytes: stateBytes, 44 }, 45 ) 46 } 47 48 return app 49 } 50 51 func TestRollback(t *testing.T) { 52 t.Skip() 53 db := dbm.NewMemDB() 54 app := SetupWithDB(false, db) 55 app.Commit() 56 ver0 := app.LastBlockHeight() 57 // commit 10 blocks 58 for i := int64(1); i <= 10; i++ { 59 header := tmproto.Header{ 60 Height: ver0 + i, 61 AppHash: app.LastCommitID().Hash, 62 } 63 app.BeginBlock(ocabci.RequestBeginBlock{Header: header}) 64 ctx := app.NewContext(false, header) 65 store := ctx.KVStore(app.GetKey("bank")) 66 store.Set([]byte("key"), []byte(fmt.Sprintf("value%d", i))) 67 app.Commit() 68 } 69 70 require.Equal(t, ver0+10, app.LastBlockHeight()) 71 store := app.NewContext(true, tmproto.Header{}).KVStore(app.GetKey("bank")) 72 require.Equal(t, []byte("value10"), store.Get([]byte("key"))) 73 74 // rollback 5 blocks 75 target := ver0 + 5 76 require.NoError(t, app.CommitMultiStore().RollbackToVersion(target)) 77 require.Equal(t, target, app.LastBlockHeight()) 78 79 // recreate app to have clean check state 80 encCdc := simapp.MakeTestEncodingConfig() 81 app = simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, encCdc, simapp.EmptyAppOptions{}) 82 store = app.NewContext(true, tmproto.Header{}).KVStore(app.GetKey("bank")) 83 require.Equal(t, []byte("value5"), store.Get([]byte("key"))) 84 85 // commit another 5 blocks with different values 86 for i := int64(6); i <= 10; i++ { 87 header := tmproto.Header{ 88 Height: ver0 + i, 89 AppHash: app.LastCommitID().Hash, 90 } 91 app.BeginBlock(ocabci.RequestBeginBlock{Header: header}) 92 ctx := app.NewContext(false, header) 93 store := ctx.KVStore(app.GetKey("bank")) 94 store.Set([]byte("key"), []byte(fmt.Sprintf("VALUE%d", i))) 95 app.Commit() 96 } 97 98 require.Equal(t, ver0+10, app.LastBlockHeight()) 99 store = app.NewContext(true, tmproto.Header{}).KVStore(app.GetKey("bank")) 100 require.Equal(t, []byte("VALUE10"), store.Get([]byte("key"))) 101 }