github.com/Finschia/finschia-sdk@v0.49.1/store/rootmulti/rollback_test.go (about)

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