From 00d097861854b61280936106b8d3a4888846daad Mon Sep 17 00:00:00 2001 From: Sam Ricotta Date: Thu, 21 Jul 2022 11:11:11 +0200 Subject: [PATCH] wip --- internal/state/execution_test.go | 10 +++--- internal/state/rollback_test.go | 4 +-- internal/state/state_test.go | 18 +++++----- internal/state/store.go | 30 +++++++--------- internal/state/store_test.go | 58 ++++++++++++++++++++++++++++--- internal/state/validation_test.go | 6 ++-- 6 files changed, 84 insertions(+), 42 deletions(-) diff --git a/internal/state/execution_test.go b/internal/state/execution_test.go index a66b677f9..726d5d587 100644 --- a/internal/state/execution_test.go +++ b/internal/state/execution_test.go @@ -42,7 +42,7 @@ func TestApplyBlock(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // ignore for tests state, stateDB, _ := makeState(1, 1) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) blockStore := store.NewBlockStore(dbm.NewMemDB()) blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(), mmock.Mempool{}, sm.EmptyEvidencePool{}, blockStore) @@ -67,7 +67,7 @@ func TestBeginBlockValidators(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // no need to check error again state, stateDB, _ := makeState(2, 2) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) prevHash := state.LastBlockID.Hash prevParts := types.PartSetHeader{} @@ -130,7 +130,7 @@ func TestBeginBlockByzantineValidators(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // ignore for tests state, stateDB, privVals := makeState(1, 1) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) defaultEvidenceTime := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC) privVal := privVals[state.Validators.Validators[0].Address.String()] @@ -355,7 +355,7 @@ func TestEndBlockValidatorUpdates(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // ignore for tests state, stateDB, _ := makeState(1, 1) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) blockStore := store.NewBlockStore(dbm.NewMemDB()) blockExec := sm.NewBlockExecutor( @@ -428,7 +428,7 @@ func TestEndBlockValidatorUpdatesResultingInEmptySet(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // ignore for tests state, stateDB, _ := makeState(1, 1) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) blockStore := store.NewBlockStore(dbm.NewMemDB()) blockExec := sm.NewBlockExecutor( stateStore, diff --git a/internal/state/rollback_test.go b/internal/state/rollback_test.go index ad7f12afc..1600c02eb 100644 --- a/internal/state/rollback_test.go +++ b/internal/state/rollback_test.go @@ -78,7 +78,7 @@ func TestRollback(t *testing.T) { } func TestRollbackNoState(t *testing.T) { - stateStore := state.NewStore(dbm.NewMemDB()) + stateStore := state.NewStore(dbm.NewMemDB(), false) blockStore := &mocks.BlockStore{} _, _, err := state.Rollback(blockStore, stateStore) @@ -111,7 +111,7 @@ func TestRollbackDifferentStateHeight(t *testing.T) { } func setupStateStore(t *testing.T, height int64) state.Store { - stateStore := state.NewStore(dbm.NewMemDB()) + stateStore := state.NewStore(dbm.NewMemDB(), false) valSet, _ := factory.RandValidatorSet(5, 10) params := types.DefaultConsensusParams() diff --git a/internal/state/state_test.go b/internal/state/state_test.go index fdf681294..e9301c653 100644 --- a/internal/state/state_test.go +++ b/internal/state/state_test.go @@ -32,7 +32,7 @@ func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, sm.State) { dbType := dbm.BackendType(cfg.DBBackend) stateDB, err := dbm.NewDB("state", dbType, cfg.DBDir()) require.NoError(t, err) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) state, err := stateStore.Load() require.NoError(t, err) require.Empty(t, state) @@ -82,7 +82,7 @@ func TestMakeGenesisStateNilValidators(t *testing.T) { func TestStateSaveLoad(t *testing.T) { tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) assert := assert.New(t) state.LastBlockHeight++ @@ -101,7 +101,7 @@ func TestStateSaveLoad(t *testing.T) { func TestABCIResponsesSaveLoad1(t *testing.T) { tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) assert := assert.New(t) state.LastBlockHeight++ @@ -134,7 +134,7 @@ func TestABCIResponsesSaveLoad2(t *testing.T) { defer tearDown(t) assert := assert.New(t) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) cases := [...]struct { // Height is implied to equal index+2, @@ -222,7 +222,7 @@ func TestValidatorSimpleSaveLoad(t *testing.T) { defer tearDown(t) assert := assert.New(t) - statestore := sm.NewStore(stateDB) + statestore := sm.NewStore(stateDB, false) // Can't load anything for height 0. _, err := statestore.LoadValidators(0) @@ -255,7 +255,7 @@ func TestValidatorSimpleSaveLoad(t *testing.T) { func TestOneValidatorChangesSaveLoad(t *testing.T) { tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) // Change vals at these heights. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20} @@ -907,7 +907,7 @@ func TestStoreLoadValidatorsIncrementsProposerPriority(t *testing.T) { const valSetSize = 2 tearDown, stateDB, state := setupTestCase(t) t.Cleanup(func() { tearDown(t) }) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) state.Validators = genValSet(valSetSize) state.NextValidators = state.Validators.CopyIncrementProposerPriority(1) err := stateStore.Save(state) @@ -932,7 +932,7 @@ func TestManyValidatorChangesSaveLoad(t *testing.T) { const valSetSize = 7 tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) require.Equal(t, int64(0), state.LastBlockHeight) state.Validators = genValSet(valSetSize) state.NextValidators = state.Validators.CopyIncrementProposerPriority(1) @@ -996,7 +996,7 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) { tearDown, stateDB, state := setupTestCase(t) defer tearDown(t) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) // Change vals at these heights. changeHeights := []int64{1, 2, 4, 5, 10, 15, 16, 17, 20} diff --git a/internal/state/store.go b/internal/state/store.go index 4e6a22ec6..954481b81 100644 --- a/internal/state/store.go +++ b/internal/state/store.go @@ -4,7 +4,7 @@ import ( "bytes" "errors" "fmt" - "github.com/tendermint/tendermint/internal/store" + "github.com/gogo/protobuf/proto" "github.com/google/orderedcode" dbm "github.com/tendermint/tm-db" @@ -56,16 +56,9 @@ func abciResponsesKey(height int64) []byte { return encodeKey(prefixAllABCIResponses, height) } -func lastABCIResponseKey() []byte { - stateKey, err := orderedcode.Append(nil, prefixState) - if err != nil { - panic(err) - } - return stateKey -} - // stateKey should never change after being set in init() var stateKey []byte +var lastABCIResponseKey []byte func init() { var err error @@ -73,6 +66,11 @@ func init() { if err != nil { panic(err) } + + lastABCIResponseKey, err = orderedcode.Append(nil, prefixLastABCIResponse) + if err != nil { + panic(err) + } } //---------------------- @@ -106,7 +104,6 @@ type Store interface { PruneStates(int64) error // Close closes the connection with the database Close() error - } // dbStore wraps a db (github.com/tendermint/tm-db) @@ -120,7 +117,6 @@ type dbStore struct { var _ Store = (*dbStore)(nil) - // NewStore creates the dbStore of the state pkg. func NewStore(db dbm.DB, discardABCIResponses bool) Store { return dbStore{db, discardABCIResponses} @@ -459,9 +455,9 @@ func (store dbStore) LoadABCIResponses(height int64) (*tmstate.ABCIResponses, er // before we called s.Save(). It can also be used to produce Merkle proofs of // the result of txs. func (store dbStore) LoadLastABCIResponse(height int64) (*tmstate.ABCIResponsesInfo, error) { - bz, err := store.db.Get(lastABCIResponseKey()) + bz, err := store.db.Get(lastABCIResponseKey) if err != nil { - return err + return nil, err } if len(bz) == 0 { @@ -515,7 +511,7 @@ func (store dbStore) SaveABCIResponses(height int64, abciResponses *tmstate.ABCI // We always save the last ABCI response incase we crash after app.Commit and before s.Save(.) // This overwrites the previous saved ABCI Response - response:= &tmstate.ABCIResponsesInfo{ + response := &tmstate.ABCIResponsesInfo{ abciResponses, height, } @@ -524,7 +520,7 @@ func (store dbStore) SaveABCIResponses(height int64, abciResponses *tmstate.ABCI return err } - return store.db.SetSync(lastABCIResponseKey(), bz) + return store.db.SetSync(lastABCIResponseKey, bz) } // SaveValidatorSets is used to save the validator set over multiple heights. @@ -563,9 +559,7 @@ func (store dbStore) LoadValidators(height int64) (*types.ValidatorSet, error) { fmt.Errorf("couldn't find validators at height %d (height %d was originally requested): %w", lastStoredHeight, height, - err, - ) - 23 ^ + err) } vs, err := types.ValidatorSetFromProto(valInfo2.ValidatorSet) diff --git a/internal/state/store_test.go b/internal/state/store_test.go index 815d1f7a1..ae523c5c4 100644 --- a/internal/state/store_test.go +++ b/internal/state/store_test.go @@ -27,7 +27,7 @@ const ( func TestStoreBootstrap(t *testing.T) { stateDB := dbm.NewMemDB() - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) val, _ := factory.RandValidator(true, 10) val2, _ := factory.RandValidator(true, 10) val3, _ := factory.RandValidator(true, 10) @@ -53,7 +53,7 @@ func TestStoreBootstrap(t *testing.T) { func TestStoreLoadValidators(t *testing.T) { stateDB := dbm.NewMemDB() - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) val, _ := factory.RandValidator(true, 10) val2, _ := factory.RandValidator(true, 10) val3, _ := factory.RandValidator(true, 10) @@ -108,7 +108,7 @@ func BenchmarkLoadValidators(b *testing.B) { dbType := dbm.BackendType(cfg.DBBackend) stateDB, err := dbm.NewDB("state", dbType, cfg.DBDir()) require.NoError(b, err) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) state, err := sm.MakeGenesisStateFromFile(cfg.GenesisFile()) if err != nil { b.Fatal(err) @@ -142,7 +142,7 @@ func BenchmarkLoadValidators(b *testing.B) { func TestStoreLoadConsensusParams(t *testing.T) { stateDB := dbm.NewMemDB() - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) err := stateStore.Save(makeRandomStateFromConsensusParams(types.DefaultConsensusParams(), 1, 1)) require.NoError(t, err) params, err := stateStore.LoadConsensusParams(1) @@ -185,7 +185,7 @@ func TestPruneStates(t *testing.T) { t.Run(name, func(t *testing.T) { db := dbm.NewMemDB() - stateStore := sm.NewStore(db) + stateStore := sm.NewStore(db, true) pk := ed25519.GenPrivKey().PubKey() // Generate a bunch of state data. Validators change for heights ending with 3, and @@ -307,3 +307,51 @@ func TestABCIResponsesResultsHash(t *testing.T) { require.NoError(t, err) assert.NoError(t, proof.Verify(root, bz)) } + +func TestLastABCIResponses(t *testing.T) { + response1 := &tmstate.ABCIResponses{ + BeginBlock: &abci.ResponseBeginBlock{}, + DeliverTxs: []*abci.ResponseDeliverTx{ + {Code: 32, Data: []byte("Hello"), Log: "Huh?"}, + }, + EndBlock: &abci.ResponseEndBlock{}, + } + stateDB := dbm.NewMemDB() + stateStore := sm.NewStore(stateDB, false) + //stub the abciresponses + + height := int64(response1.Size()) + //save the last abci response + stateStore.SaveABCIResponses(height, response1) + //search for the last abciresponse + lastResponse, err := stateStore.LoadLastABCIResponse(height) + require.NoError(t, err) + fmt.Println(lastResponse) + // test if the last response saved + + //check to see if the saved response height is the same as the loaded height + assert.Equal(t, lastResponse.Height, int64(response1.Size())) + //test if the responses are the same + response2 := &tmstate.ABCIResponses{ + BeginBlock: &abci.ResponseBeginBlock{}, + DeliverTxs: []*abci.ResponseDeliverTx{ + {Code: 44, Data: []byte("Hello again"), Log: "????"}, + }, + EndBlock: &abci.ResponseEndBlock{}, + } + + //test to see if there are multiple responses + + //when the flag is on + stateStore = sm.NewStore(stateDB, true) + // add to the responses + height = int64(response1.Size()) + stateStore.SaveABCIResponses(height, response2) + lastResponse2, err := stateStore.LoadLastABCIResponse(height) + require.NoError(t, err) + fmt.Println(lastResponse2) + + //test to see if the responses other than the last is deleted + assert.Equal(t, lastResponse2.Height, int64(response2.Size())) + //check to see if the saved response height is the same as the loaded height +} diff --git a/internal/state/validation_test.go b/internal/state/validation_test.go index eb0cebbb7..2af3d27b8 100644 --- a/internal/state/validation_test.go +++ b/internal/state/validation_test.go @@ -33,7 +33,7 @@ func TestValidateBlockHeader(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // ignore for tests state, stateDB, privVals := makeState(3, 1) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) blockStore := store.NewBlockStore(dbm.NewMemDB()) blockExec := sm.NewBlockExecutor( stateStore, @@ -120,7 +120,7 @@ func TestValidateBlockCommit(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // ignore for tests state, stateDB, privVals := makeState(1, 1) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) blockStore := store.NewBlockStore(dbm.NewMemDB()) blockExec := sm.NewBlockExecutor( stateStore, @@ -241,7 +241,7 @@ func TestValidateBlockEvidence(t *testing.T) { defer proxyApp.Stop() //nolint:errcheck // ignore for tests state, stateDB, privVals := makeState(4, 1) - stateStore := sm.NewStore(stateDB) + stateStore := sm.NewStore(stateDB, false) blockStore := store.NewBlockStore(dbm.NewMemDB()) defaultEvidenceTime := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)