Remove the abci responses type - prune legacy responses (#8673)

Closes #8069 

* Type `ABCIResponses` was just wrapping type `ResponseFinalizeBlock`. This patch removes the former.
* Did some renaming to avoid confusion on the data structure we are working with.
* We also remove any stale ABCIResponses we may have in the state store at the time of pruning

**IMPORTANT**: There is an undesirable side-effect of the unwrapping. An empty `ResponseFinalizeBlock` yields a 0-length proto-buf serialized buffer. This was not the case with `ABCIResponses`. I have added an interim solution, but open for suggestions on more elegant ones.
This commit is contained in:
Sergio Mena
2022-06-02 21:13:08 +02:00
committed by GitHub
parent 08099ff669
commit ce6485fa70
21 changed files with 293 additions and 461 deletions
+2 -2
View File
@@ -424,11 +424,11 @@ func (h *Handshaker) ReplayBlocks(
case appBlockHeight == storeBlockHeight:
// We ran Commit, but didn't save the state, so replayBlock with mock app.
abciResponses, err := h.stateStore.LoadABCIResponses(storeBlockHeight)
finalizeBlockResponses, err := h.stateStore.LoadFinalizeBlockResponses(storeBlockHeight)
if err != nil {
return nil, err
}
mockApp, err := newMockProxyApp(h.logger, appHash, abciResponses)
mockApp, err := newMockProxyApp(h.logger, appHash, finalizeBlockResponses)
if err != nil {
return nil, err
}
+8 -9
View File
@@ -9,7 +9,6 @@ import (
"github.com/tendermint/tendermint/internal/mempool"
"github.com/tendermint/tendermint/internal/proxy"
"github.com/tendermint/tendermint/libs/log"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
"github.com/tendermint/tendermint/types"
)
@@ -52,7 +51,7 @@ func (emptyMempool) InitWAL() error { return nil }
func (emptyMempool) CloseWAL() {}
//-----------------------------------------------------------------------------
// mockProxyApp uses ABCIResponses to give the right results.
// mockProxyApp uses Responses to FinalizeBlock to give the right results.
//
// Useful because we don't want to call Commit() twice for the same block on
// the real app.
@@ -60,24 +59,24 @@ func (emptyMempool) CloseWAL() {}
func newMockProxyApp(
logger log.Logger,
appHash []byte,
abciResponses *tmstate.ABCIResponses,
finalizeBlockResponses *abci.ResponseFinalizeBlock,
) (abciclient.Client, error) {
return proxy.New(abciclient.NewLocalClient(logger, &mockProxyApp{
appHash: appHash,
abciResponses: abciResponses,
appHash: appHash,
finalizeBlockResponses: finalizeBlockResponses,
}), logger, proxy.NopMetrics()), nil
}
type mockProxyApp struct {
abci.BaseApplication
appHash []byte
txCount int
abciResponses *tmstate.ABCIResponses
appHash []byte
txCount int
finalizeBlockResponses *abci.ResponseFinalizeBlock
}
func (mock *mockProxyApp) FinalizeBlock(_ context.Context, req *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) {
r := mock.abciResponses.FinalizeBlock
r := mock.finalizeBlockResponses
mock.txCount++
if r == nil {
return &abci.ResponseFinalizeBlock{}, nil
+6 -3
View File
@@ -1979,7 +1979,8 @@ func TestFinalizeBlockCalled(t *testing.T) {
Status: abci.ResponseVerifyVoteExtension_ACCEPT,
}, nil)
}
m.On("FinalizeBlock", mock.Anything, mock.Anything).Return(&abci.ResponseFinalizeBlock{}, nil).Maybe()
r := &abci.ResponseFinalizeBlock{AppHash: []byte("the_hash")}
m.On("FinalizeBlock", mock.Anything, mock.Anything).Return(r, nil).Maybe()
m.On("Commit", mock.Anything).Return(&abci.ResponseCommit{}, nil).Maybe()
cs1, vss := makeState(ctx, t, makeStateArgs{config: config, application: m})
@@ -2060,7 +2061,8 @@ func TestExtendVoteCalledWhenEnabled(t *testing.T) {
}, nil)
}
m.On("Commit", mock.Anything).Return(&abci.ResponseCommit{}, nil).Maybe()
m.On("FinalizeBlock", mock.Anything, mock.Anything).Return(&abci.ResponseFinalizeBlock{}, nil).Maybe()
r := &abci.ResponseFinalizeBlock{AppHash: []byte("myHash")}
m.On("FinalizeBlock", mock.Anything, mock.Anything).Return(r, nil).Maybe()
c := factory.ConsensusParams()
if !testCase.enabled {
c.ABCI.VoteExtensionsEnableHeight = 0
@@ -2357,7 +2359,8 @@ func TestVoteExtensionEnableHeight(t *testing.T) {
Status: abci.ResponseVerifyVoteExtension_ACCEPT,
}, nil).Times(numValidators - 1)
}
m.On("FinalizeBlock", mock.Anything, mock.Anything).Return(&abci.ResponseFinalizeBlock{}, nil).Maybe()
r := &abci.ResponseFinalizeBlock{AppHash: []byte("hashyHash")}
m.On("FinalizeBlock", mock.Anything, mock.Anything).Return(r, nil).Maybe()
m.On("Commit", mock.Anything).Return(&abci.ResponseCommit{}, nil).Maybe()
c := factory.ConsensusParams()
c.ABCI.VoteExtensionsEnableHeight = testCase.enableHeight
+10
View File
@@ -22,6 +22,16 @@ import (
"github.com/tendermint/tendermint/types"
)
// key prefixes
// NB: Before modifying these, cross-check them with those in
// * internal/store/store.go [0..4, 13]
// * internal/state/store.go [5..8, 14]
// * internal/evidence/pool.go [9..10]
// * light/store/db/db.go [11..12]
// TODO(sergio): Move all these to their own package.
// TODO: what about these (they already collide):
// * scripts/scmigrate/migrate.go [3]
// * internal/p2p/peermanager.go [1]
const (
// prefixes are unique across all tm db's
prefixCommitted = int64(9)
+4 -7
View File
@@ -23,7 +23,6 @@ import (
indexermocks "github.com/tendermint/tendermint/internal/state/indexer/mocks"
statemocks "github.com/tendermint/tendermint/internal/state/mocks"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/proto/tendermint/state"
httpclient "github.com/tendermint/tendermint/rpc/client/http"
"github.com/tendermint/tendermint/types"
)
@@ -263,12 +262,10 @@ func TestBlockResults(t *testing.T) {
testGasUsed := int64(100)
stateStoreMock := &statemocks.Store{}
// tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
stateStoreMock.On("LoadABCIResponses", testHeight).Return(&state.ABCIResponses{
FinalizeBlock: &abcitypes.ResponseFinalizeBlock{
TxResults: []*abcitypes.ExecTxResult{
{
GasUsed: testGasUsed,
},
stateStoreMock.On("LoadFinalizeBlockResponses", testHeight).Return(&abcitypes.ResponseFinalizeBlock{
TxResults: []*abcitypes.ExecTxResult{
{
GasUsed: testGasUsed,
},
},
}, nil)
+6 -6
View File
@@ -187,23 +187,23 @@ func (env *Environment) BlockResults(ctx context.Context, req *coretypes.Request
return nil, err
}
results, err := env.StateStore.LoadABCIResponses(height)
results, err := env.StateStore.LoadFinalizeBlockResponses(height)
if err != nil {
return nil, err
}
var totalGasUsed int64
for _, res := range results.FinalizeBlock.GetTxResults() {
for _, res := range results.GetTxResults() {
totalGasUsed += res.GetGasUsed()
}
return &coretypes.ResultBlockResults{
Height: height,
TxsResults: results.FinalizeBlock.TxResults,
TxsResults: results.TxResults,
TotalGasUsed: totalGasUsed,
FinalizeBlockEvents: results.FinalizeBlock.Events,
ValidatorUpdates: results.FinalizeBlock.ValidatorUpdates,
ConsensusParamUpdates: results.FinalizeBlock.ConsensusParamUpdates,
FinalizeBlockEvents: results.Events,
ValidatorUpdates: results.ValidatorUpdates,
ConsensusParamUpdates: results.ConsensusParamUpdates,
}, nil
}
+10 -13
View File
@@ -13,7 +13,6 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
sm "github.com/tendermint/tendermint/internal/state"
"github.com/tendermint/tendermint/internal/state/mocks"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
"github.com/tendermint/tendermint/rpc/coretypes"
)
@@ -70,19 +69,17 @@ func TestBlockchainInfo(t *testing.T) {
}
func TestBlockResults(t *testing.T) {
results := &tmstate.ABCIResponses{
FinalizeBlock: &abci.ResponseFinalizeBlock{
TxResults: []*abci.ExecTxResult{
{Code: 0, Data: []byte{0x01}, Log: "ok", GasUsed: 10},
{Code: 0, Data: []byte{0x02}, Log: "ok", GasUsed: 5},
{Code: 1, Log: "not ok", GasUsed: 0},
},
results := &abci.ResponseFinalizeBlock{
TxResults: []*abci.ExecTxResult{
{Code: 0, Data: []byte{0x01}, Log: "ok", GasUsed: 10},
{Code: 0, Data: []byte{0x02}, Log: "ok", GasUsed: 5},
{Code: 1, Log: "not ok", GasUsed: 0},
},
}
env := &Environment{}
env.StateStore = sm.NewStore(dbm.NewMemDB())
err := env.StateStore.SaveABCIResponses(100, results)
err := env.StateStore.SaveFinalizeBlockResponses(100, results)
require.NoError(t, err)
mockstore := &mocks.BlockStore{}
mockstore.On("Height").Return(int64(100))
@@ -99,11 +96,11 @@ func TestBlockResults(t *testing.T) {
{101, true, nil},
{100, false, &coretypes.ResultBlockResults{
Height: 100,
TxsResults: results.FinalizeBlock.TxResults,
TxsResults: results.TxResults,
TotalGasUsed: 15,
FinalizeBlockEvents: results.FinalizeBlock.Events,
ValidatorUpdates: results.FinalizeBlock.ValidatorUpdates,
ConsensusParamUpdates: results.FinalizeBlock.ConsensusParamUpdates,
FinalizeBlockEvents: results.Events,
ValidatorUpdates: results.ValidatorUpdates,
ConsensusParamUpdates: results.ConsensusParamUpdates,
}},
}
+3 -3
View File
@@ -46,7 +46,7 @@ type (
Height int64
}
ErrNoABCIResponsesForHeight struct {
ErrNoFinalizeBlockResponsesForHeight struct {
Height int64
}
)
@@ -102,6 +102,6 @@ func (e ErrNoConsensusParamsForHeight) Error() string {
return fmt.Sprintf("could not find consensus params for height #%d", e.Height)
}
func (e ErrNoABCIResponsesForHeight) Error() string {
return fmt.Sprintf("could not find results for height #%d", e.Height)
func (e ErrNoFinalizeBlockResponsesForHeight) Error() string {
return fmt.Sprintf("could not find FinalizeBlock responses for height #%d", e.Height)
}
+6 -8
View File
@@ -14,7 +14,6 @@ import (
"github.com/tendermint/tendermint/internal/eventbus"
"github.com/tendermint/tendermint/internal/mempool"
"github.com/tendermint/tendermint/libs/log"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
tmtypes "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
@@ -233,12 +232,11 @@ func (blockExec *BlockExecutor) ApplyBlock(
"block_app_hash", fmt.Sprintf("%X", fBlockRes.AppHash),
)
abciResponses := &tmstate.ABCIResponses{
FinalizeBlock: fBlockRes,
}
// Save the results before we commit.
if err := blockExec.store.SaveABCIResponses(block.Height, abciResponses); err != nil {
err = blockExec.store.SaveFinalizeBlockResponses(block.Height, fBlockRes)
if err != nil && !errors.Is(err, ErrNoFinalizeBlockResponsesForHeight{block.Height}) {
// It is correct to have an empty ResponseFinalizeBlock for ApplyBlock,
// but not for saving it to the state store
return state, err
}
@@ -538,7 +536,7 @@ func (state State) Update(
// and update s.LastValidators and s.Validators.
nValSet := state.NextValidators.Copy()
// Update the validator set with the latest abciResponses.
// Update the validator set with the latest responses to FinalizeBlock.
lastHeightValsChanged := state.LastHeightValidatorsChanged
if len(validatorUpdates) > 0 {
err := nValSet.UpdateWithChangeSet(validatorUpdates)
@@ -552,7 +550,7 @@ func (state State) Update(
// Update validator proposer priority and set state variables.
nValSet.IncrementProposerPriority(1)
// Update the params with the latest abciResponses.
// Update the params with the latest responses to FinalizeBlock.
nextParams := state.ConsensusParams
lastHeightParamsChanged := state.LastHeightConsensusParamsChanged
if consensusParamUpdates != nil {
+14 -23
View File
@@ -19,7 +19,6 @@ import (
sf "github.com/tendermint/tendermint/internal/state/test/factory"
"github.com/tendermint/tendermint/internal/test/factory"
tmtime "github.com/tendermint/tendermint/libs/time"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
@@ -148,10 +147,10 @@ func makeHeaderPartsResponsesValPubKeyChange(
t *testing.T,
state sm.State,
pubkey crypto.PubKey,
) (types.Header, types.BlockID, *tmstate.ABCIResponses) {
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
block := sf.MakeBlock(state, state.LastBlockHeight+1, new(types.Commit))
abciResponses := &tmstate.ABCIResponses{}
finalizeBlockResponses := &abci.ResponseFinalizeBlock{}
// If the pubkey is new, remove the old and add the new.
_, val := state.NextValidators.GetByIndex(0)
if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
@@ -160,58 +159,50 @@ func makeHeaderPartsResponsesValPubKeyChange(
pbPk, err := encoding.PubKeyToProto(pubkey)
require.NoError(t, err)
abciResponses.FinalizeBlock = &abci.ResponseFinalizeBlock{
ValidatorUpdates: []abci.ValidatorUpdate{
{PubKey: vPbPk, Power: 0},
{PubKey: pbPk, Power: 10},
},
finalizeBlockResponses.ValidatorUpdates = []abci.ValidatorUpdate{
{PubKey: vPbPk, Power: 0},
{PubKey: pbPk, Power: 10},
}
}
return block.Header, types.BlockID{Hash: block.Hash(), PartSetHeader: types.PartSetHeader{}}, abciResponses
return block.Header, types.BlockID{Hash: block.Hash(), PartSetHeader: types.PartSetHeader{}}, finalizeBlockResponses
}
func makeHeaderPartsResponsesValPowerChange(
t *testing.T,
state sm.State,
power int64,
) (types.Header, types.BlockID, *tmstate.ABCIResponses) {
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
t.Helper()
block := sf.MakeBlock(state, state.LastBlockHeight+1, new(types.Commit))
finalizeBlockResponses := &abci.ResponseFinalizeBlock{}
abciResponses := &tmstate.ABCIResponses{}
abciResponses.FinalizeBlock = &abci.ResponseFinalizeBlock{}
// If the pubkey is new, remove the old and add the new.
_, val := state.NextValidators.GetByIndex(0)
if val.VotingPower != power {
vPbPk, err := encoding.PubKeyToProto(val.PubKey)
require.NoError(t, err)
abciResponses.FinalizeBlock = &abci.ResponseFinalizeBlock{
ValidatorUpdates: []abci.ValidatorUpdate{
{PubKey: vPbPk, Power: power},
},
finalizeBlockResponses.ValidatorUpdates = []abci.ValidatorUpdate{
{PubKey: vPbPk, Power: power},
}
}
return block.Header, types.BlockID{Hash: block.Hash(), PartSetHeader: types.PartSetHeader{}}, abciResponses
return block.Header, types.BlockID{Hash: block.Hash(), PartSetHeader: types.PartSetHeader{}}, finalizeBlockResponses
}
func makeHeaderPartsResponsesParams(
t *testing.T,
state sm.State,
params *types.ConsensusParams,
) (types.Header, types.BlockID, *tmstate.ABCIResponses) {
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
t.Helper()
block := sf.MakeBlock(state, state.LastBlockHeight+1, new(types.Commit))
pbParams := params.ToProto()
abciResponses := &tmstate.ABCIResponses{
FinalizeBlock: &abci.ResponseFinalizeBlock{ConsensusParamUpdates: &pbParams},
}
return block.Header, types.BlockID{Hash: block.Hash(), PartSetHeader: types.PartSetHeader{}}, abciResponses
finalizeBlockResponses := &abci.ResponseFinalizeBlock{ConsensusParamUpdates: &pbParams}
return block.Header, types.BlockID{Hash: block.Hash(), PartSetHeader: types.PartSetHeader{}}, finalizeBlockResponses
}
func randomGenesisDoc() *types.GenesisDoc {
+17 -16
View File
@@ -4,8 +4,9 @@ package mocks
import (
mock "github.com/stretchr/testify/mock"
abcitypes "github.com/tendermint/tendermint/abci/types"
state "github.com/tendermint/tendermint/internal/state"
tendermintstate "github.com/tendermint/tendermint/proto/tendermint/state"
types "github.com/tendermint/tendermint/types"
)
@@ -64,17 +65,15 @@ func (_m *Store) Load() (state.State, error) {
return r0, r1
}
// LoadABCIResponses provides a mock function with given fields: _a0
func (_m *Store) LoadABCIResponses(_a0 int64) (*tendermintstate.ABCIResponses, error) {
// LoadConsensusParams provides a mock function with given fields: _a0
func (_m *Store) LoadConsensusParams(_a0 int64) (types.ConsensusParams, error) {
ret := _m.Called(_a0)
var r0 *tendermintstate.ABCIResponses
if rf, ok := ret.Get(0).(func(int64) *tendermintstate.ABCIResponses); ok {
var r0 types.ConsensusParams
if rf, ok := ret.Get(0).(func(int64) types.ConsensusParams); ok {
r0 = rf(_a0)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*tendermintstate.ABCIResponses)
}
r0 = ret.Get(0).(types.ConsensusParams)
}
var r1 error
@@ -87,15 +86,17 @@ func (_m *Store) LoadABCIResponses(_a0 int64) (*tendermintstate.ABCIResponses, e
return r0, r1
}
// LoadConsensusParams provides a mock function with given fields: _a0
func (_m *Store) LoadConsensusParams(_a0 int64) (types.ConsensusParams, error) {
// LoadFinalizeBlockResponses provides a mock function with given fields: _a0
func (_m *Store) LoadFinalizeBlockResponses(_a0 int64) (*abcitypes.ResponseFinalizeBlock, error) {
ret := _m.Called(_a0)
var r0 types.ConsensusParams
if rf, ok := ret.Get(0).(func(int64) types.ConsensusParams); ok {
var r0 *abcitypes.ResponseFinalizeBlock
if rf, ok := ret.Get(0).(func(int64) *abcitypes.ResponseFinalizeBlock); ok {
r0 = rf(_a0)
} else {
r0 = ret.Get(0).(types.ConsensusParams)
if ret.Get(0) != nil {
r0 = ret.Get(0).(*abcitypes.ResponseFinalizeBlock)
}
}
var r1 error
@@ -159,12 +160,12 @@ func (_m *Store) Save(_a0 state.State) error {
return r0
}
// SaveABCIResponses provides a mock function with given fields: _a0, _a1
func (_m *Store) SaveABCIResponses(_a0 int64, _a1 *tendermintstate.ABCIResponses) error {
// SaveFinalizeBlockResponses provides a mock function with given fields: _a0, _a1
func (_m *Store) SaveFinalizeBlockResponses(_a0 int64, _a1 *abcitypes.ResponseFinalizeBlock) error {
ret := _m.Called(_a0, _a1)
var r0 error
if rf, ok := ret.Get(0).(func(int64, *tendermintstate.ABCIResponses) error); ok {
if rf, ok := ret.Get(0).(func(int64, *abcitypes.ResponseFinalizeBlock) error); ok {
r0 = rf(_a0, _a1)
} else {
r0 = ret.Error(0)
+31 -34
View File
@@ -21,7 +21,6 @@ import (
"github.com/tendermint/tendermint/crypto/merkle"
sm "github.com/tendermint/tendermint/internal/state"
statefactory "github.com/tendermint/tendermint/internal/state/test/factory"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
"github.com/tendermint/tendermint/types"
)
@@ -102,8 +101,8 @@ func TestStateSaveLoad(t *testing.T) {
loadedState, state)
}
// TestABCIResponsesSaveLoad tests saving and loading ABCIResponses.
func TestABCIResponsesSaveLoad1(t *testing.T) {
// TestFinalizeBlockResponsesSaveLoad1 tests saving and loading responses to FinalizeBlock.
func TestFinalizeBlockResponsesSaveLoad1(t *testing.T) {
tearDown, stateDB, state := setupTestCase(t)
defer tearDown(t)
stateStore := sm.NewStore(stateDB)
@@ -113,28 +112,27 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
// Build mock responses.
block := statefactory.MakeBlock(state, 2, new(types.Commit))
abciResponses := new(tmstate.ABCIResponses)
dtxs := make([]*abci.ExecTxResult, 2)
abciResponses.FinalizeBlock = new(abci.ResponseFinalizeBlock)
abciResponses.FinalizeBlock.TxResults = dtxs
finalizeBlockResponses := new(abci.ResponseFinalizeBlock)
finalizeBlockResponses.TxResults = dtxs
abciResponses.FinalizeBlock.TxResults[0] = &abci.ExecTxResult{Data: []byte("foo"), Events: nil}
abciResponses.FinalizeBlock.TxResults[1] = &abci.ExecTxResult{Data: []byte("bar"), Log: "ok", Events: nil}
finalizeBlockResponses.TxResults[0] = &abci.ExecTxResult{Data: []byte("foo"), Events: nil}
finalizeBlockResponses.TxResults[1] = &abci.ExecTxResult{Data: []byte("bar"), Log: "ok", Events: nil}
pbpk, err := encoding.PubKeyToProto(ed25519.GenPrivKey().PubKey())
require.NoError(t, err)
abciResponses.FinalizeBlock.ValidatorUpdates = []abci.ValidatorUpdate{{PubKey: pbpk, Power: 10}}
finalizeBlockResponses.ValidatorUpdates = []abci.ValidatorUpdate{{PubKey: pbpk, Power: 10}}
err = stateStore.SaveABCIResponses(block.Height, abciResponses)
err = stateStore.SaveFinalizeBlockResponses(block.Height, finalizeBlockResponses)
require.NoError(t, err)
loadedABCIResponses, err := stateStore.LoadABCIResponses(block.Height)
loadedFinalizeBlockResponses, err := stateStore.LoadFinalizeBlockResponses(block.Height)
require.NoError(t, err)
assert.Equal(t, abciResponses, loadedABCIResponses,
"ABCIResponses don't match:\ngot: %v\nexpected: %v\n",
loadedABCIResponses, abciResponses)
assert.Equal(t, finalizeBlockResponses, loadedFinalizeBlockResponses,
"FinalizeBlockResponses don't match:\ngot: %v\nexpected: %v\n",
loadedFinalizeBlockResponses, finalizeBlockResponses)
}
// TestResultsSaveLoad tests saving and loading ABCI results.
func TestABCIResponsesSaveLoad2(t *testing.T) {
// TestFinalizeBlockResponsesSaveLoad2 tests saving and loading responses to FinalizeBlock.
func TestFinalizeBlockResponsesSaveLoad2(t *testing.T) {
tearDown, stateDB, _ := setupTestCase(t)
defer tearDown(t)
@@ -190,32 +188,31 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
// Query all before, this should return error.
for i := range cases {
h := int64(i + 1)
res, err := stateStore.LoadABCIResponses(h)
res, err := stateStore.LoadFinalizeBlockResponses(h)
assert.Error(t, err, "%d: %#v", i, res)
}
// Add all cases.
for i, tc := range cases {
h := int64(i + 1) // last block height, one below what we save
responses := &tmstate.ABCIResponses{
FinalizeBlock: &abci.ResponseFinalizeBlock{
TxResults: tc.added,
},
responses := &abci.ResponseFinalizeBlock{
TxResults: tc.added,
AppHash: []byte("a_hash"),
}
err := stateStore.SaveABCIResponses(h, responses)
err := stateStore.SaveFinalizeBlockResponses(h, responses)
require.NoError(t, err)
}
// Query all before, should return expected value.
// Query all after, should return expected value.
for i, tc := range cases {
h := int64(i + 1)
res, err := stateStore.LoadABCIResponses(h)
res, err := stateStore.LoadFinalizeBlockResponses(h)
if assert.NoError(t, err, "%d", i) {
t.Log(res)
e, err := abci.MarshalTxResults(tc.expected)
require.NoError(t, err)
he := merkle.HashFromByteSlices(e)
rs, err := abci.MarshalTxResults(res.FinalizeBlock.TxResults)
rs, err := abci.MarshalTxResults(res.TxResults)
hrs := merkle.HashFromByteSlices(rs)
require.NoError(t, err)
assert.Equal(t, he, hrs, "%d", i)
@@ -282,12 +279,12 @@ func TestOneValidatorChangesSaveLoad(t *testing.T) {
power++
}
header, blockID, responses := makeHeaderPartsResponsesValPowerChange(t, state, power)
validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.FinalizeBlock.ValidatorUpdates)
validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.ValidatorUpdates)
require.NoError(t, err)
rs, err := abci.MarshalTxResults(responses.FinalizeBlock.TxResults)
rs, err := abci.MarshalTxResults(responses.TxResults)
require.NoError(t, err)
h := merkle.HashFromByteSlices(rs)
state, err = state.Update(blockID, &header, h, responses.FinalizeBlock.ConsensusParamUpdates, validatorUpdates)
state, err = state.Update(blockID, &header, h, responses.ConsensusParamUpdates, validatorUpdates)
require.NoError(t, err)
err = stateStore.Save(state)
require.NoError(t, err)
@@ -1024,12 +1021,12 @@ func TestManyValidatorChangesSaveLoad(t *testing.T) {
// Save state etc.
var validatorUpdates []*types.Validator
validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.FinalizeBlock.ValidatorUpdates)
validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.ValidatorUpdates)
require.NoError(t, err)
rs, err := abci.MarshalTxResults(responses.FinalizeBlock.TxResults)
rs, err := abci.MarshalTxResults(responses.TxResults)
require.NoError(t, err)
h := merkle.HashFromByteSlices(rs)
state, err = state.Update(blockID, &header, h, responses.FinalizeBlock.ConsensusParamUpdates, validatorUpdates)
state, err = state.Update(blockID, &header, h, responses.ConsensusParamUpdates, validatorUpdates)
require.NoError(t, err)
nextHeight := state.LastBlockHeight + 1
err = stateStore.Save(state)
@@ -1104,12 +1101,12 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) {
cp = params[changeIndex]
}
header, blockID, responses := makeHeaderPartsResponsesParams(t, state, &cp)
validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.FinalizeBlock.ValidatorUpdates)
validatorUpdates, err = types.PB2TM.ValidatorUpdates(responses.ValidatorUpdates)
require.NoError(t, err)
rs, err := abci.MarshalTxResults(responses.FinalizeBlock.TxResults)
rs, err := abci.MarshalTxResults(responses.TxResults)
require.NoError(t, err)
h := merkle.HashFromByteSlices(rs)
state, err = state.Update(blockID, &header, h, responses.FinalizeBlock.ConsensusParamUpdates, validatorUpdates)
state, err = state.Update(blockID, &header, h, responses.ConsensusParamUpdates, validatorUpdates)
require.NoError(t, err)
err = stateStore.Save(state)
+55 -35
View File
@@ -26,15 +26,23 @@ const (
//------------------------------------------------------------------------
// key prefixes
// NB: Before modifying these, cross-check them with those in
// internal/store/store.go
// TODO(thane): Move these and the ones in internal/store/store.go to their own package.
// * internal/store/store.go [0..4, 13]
// * internal/state/store.go [5..8, 14]
// * internal/evidence/pool.go [9..10]
// * light/store/db/db.go [11..12]
// TODO(thane): Move all these to their own package.
// TODO: what about these (they already collide):
// * scripts/scmigrate/migrate.go [3]
// * internal/p2p/peermanager.go [1]
const (
// prefixes are unique across all tm db's
prefixValidators = int64(5)
prefixConsensusParams = int64(6)
prefixABCIResponses = int64(7)
prefixState = int64(8)
prefixValidators = int64(5)
prefixConsensusParams = int64(6)
prefixABCIResponses = int64(7) // deprecated in v0.36
prefixState = int64(8)
prefixFinalizeBlockResponses = int64(14)
)
func encodeKey(prefix int64, height int64) []byte {
@@ -57,6 +65,10 @@ func abciResponsesKey(height int64) []byte {
return encodeKey(prefixABCIResponses, height)
}
func finalizeBlockResponsesKey(height int64) []byte {
return encodeKey(prefixFinalizeBlockResponses, height)
}
// stateKey should never change after being set in init()
var stateKey []byte
@@ -81,14 +93,14 @@ type Store interface {
Load() (State, error)
// LoadValidators loads the validator set at a given height
LoadValidators(int64) (*types.ValidatorSet, error)
// LoadABCIResponses loads the abciResponse for a given height
LoadABCIResponses(int64) (*tmstate.ABCIResponses, error)
// LoadFinalizeBlockResponses loads the responses to FinalizeBlock for a given height
LoadFinalizeBlockResponses(int64) (*abci.ResponseFinalizeBlock, error)
// LoadConsensusParams loads the consensus params for a given height
LoadConsensusParams(int64) (types.ConsensusParams, error)
// Save overwrites the previous state with the updated one
Save(State) error
// SaveABCIResponses saves ABCIResponses for a given height
SaveABCIResponses(int64, *tmstate.ABCIResponses) error
// SaveFinalizeBlockResponses saves responses to FinalizeBlock for a given height
SaveFinalizeBlockResponses(int64, *abci.ResponseFinalizeBlock) error
// SaveValidatorSet saves the validator set at a given height
SaveValidatorSets(int64, int64, *types.ValidatorSet) error
// Bootstrap is used for bootstrapping state when not starting from a initial height.
@@ -247,7 +259,7 @@ func (store dbStore) PruneStates(retainHeight int64) error {
return err
}
if err := store.pruneABCIResponses(retainHeight); err != nil {
if err := store.pruneFinalizeBlockResponses(retainHeight); err != nil {
return err
}
@@ -338,10 +350,15 @@ func (store dbStore) pruneConsensusParams(retainHeight int64) error {
)
}
// pruneABCIResponses calls a reverse iterator from base height to retain height batch deleting
// all abci responses in between
func (store dbStore) pruneABCIResponses(height int64) error {
return store.pruneRange(abciResponsesKey(1), abciResponsesKey(height))
// pruneFinalizeBlockResponses calls a reverse iterator from base height to retain height
// batch deleting all responses to FinalizeBlock, and legacy ABCI responses, in between
func (store dbStore) pruneFinalizeBlockResponses(height int64) error {
err := store.pruneRange(finalizeBlockResponsesKey(1), finalizeBlockResponsesKey(height))
if err == nil {
// Remove any stale legacy ABCI responses
err = store.pruneRange(abciResponsesKey(1), abciResponsesKey(height))
}
return err
}
// pruneRange is a generic function for deleting a range of keys in reverse order.
@@ -408,60 +425,63 @@ func (store dbStore) reverseBatchDelete(batch dbm.Batch, start, end []byte) ([]b
//------------------------------------------------------------------------
// LoadABCIResponses loads the ABCIResponses for the given height from the
// database. If not found, ErrNoABCIResponsesForHeight is returned.
// LoadFinalizeBlockResponses loads the responses to FinalizeBlock for the
// given height from the database. If not found,
// ErrNoFinalizeBlockResponsesForHeight is returned.
//
// This is useful for recovering from crashes where we called app.Commit and
// before we called s.Save(). It can also be used to produce Merkle proofs of
// the result of txs.
func (store dbStore) LoadABCIResponses(height int64) (*tmstate.ABCIResponses, error) {
buf, err := store.db.Get(abciResponsesKey(height))
// This is useful for recovering from crashes where we called app.Commit
// and before we called s.Save(). It can also be used to produce Merkle
// proofs of the result of txs.
func (store dbStore) LoadFinalizeBlockResponses(height int64) (*abci.ResponseFinalizeBlock, error) {
buf, err := store.db.Get(finalizeBlockResponsesKey(height))
if err != nil {
return nil, err
}
if len(buf) == 0 {
return nil, ErrNoABCIResponsesForHeight{height}
return nil, ErrNoFinalizeBlockResponsesForHeight{height}
}
abciResponses := new(tmstate.ABCIResponses)
err = abciResponses.Unmarshal(buf)
finalizeBlockResponses := new(abci.ResponseFinalizeBlock)
err = finalizeBlockResponses.Unmarshal(buf)
if err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
panic(fmt.Sprintf("data has been corrupted or its spec has changed: %+v", err))
}
// TODO: ensure that buf is completely read.
return abciResponses, nil
return finalizeBlockResponses, nil
}
// SaveABCIResponses persists the ABCIResponses to the database.
// SaveFinalizeBlockResponses persists to the database the responses to FinalizeBlock.
// This is useful in case we crash after app.Commit and before s.Save().
// Responses are indexed by height so they can also be loaded later to produce
// Merkle proofs.
//
// Exposed for testing.
func (store dbStore) SaveABCIResponses(height int64, abciResponses *tmstate.ABCIResponses) error {
return store.saveABCIResponses(height, abciResponses)
func (store dbStore) SaveFinalizeBlockResponses(height int64, finalizeBlockResponses *abci.ResponseFinalizeBlock) error {
return store.saveFinalizeBlockResponses(height, finalizeBlockResponses)
}
func (store dbStore) saveABCIResponses(height int64, abciResponses *tmstate.ABCIResponses) error {
func (store dbStore) saveFinalizeBlockResponses(height int64, finalizeBlockResponses *abci.ResponseFinalizeBlock) error {
var dtxs []*abci.ExecTxResult
// strip nil values,
for _, tx := range abciResponses.FinalizeBlock.TxResults {
for _, tx := range finalizeBlockResponses.TxResults {
if tx != nil {
dtxs = append(dtxs, tx)
}
}
abciResponses.FinalizeBlock.TxResults = dtxs
finalizeBlockResponses.TxResults = dtxs
bz, err := abciResponses.Marshal()
bz, err := finalizeBlockResponses.Marshal()
if err != nil {
return err
}
if len(bz) == 0 {
return ErrNoFinalizeBlockResponsesForHeight{height}
}
return store.db.SetSync(abciResponsesKey(height), bz)
return store.db.SetSync(finalizeBlockResponsesKey(height), bz)
}
// SaveValidatorSets is used to save the validator set over multiple heights.
+11 -13
View File
@@ -17,7 +17,6 @@ import (
sm "github.com/tendermint/tendermint/internal/state"
"github.com/tendermint/tendermint/internal/test/factory"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
"github.com/tendermint/tendermint/types"
)
@@ -236,15 +235,14 @@ func TestPruneStates(t *testing.T) {
err := stateStore.Save(state)
require.NoError(t, err)
err = stateStore.SaveABCIResponses(h, &tmstate.ABCIResponses{
FinalizeBlock: &abci.ResponseFinalizeBlock{
TxResults: []*abci.ExecTxResult{
{Data: []byte{1}},
{Data: []byte{2}},
{Data: []byte{3}},
},
err = stateStore.SaveFinalizeBlockResponses(h, &abci.ResponseFinalizeBlock{
TxResults: []*abci.ExecTxResult{
{Data: []byte{1}},
{Data: []byte{2}},
{Data: []byte{3}},
},
})
},
)
require.NoError(t, err)
}
@@ -265,9 +263,9 @@ func TestPruneStates(t *testing.T) {
require.NoError(t, err, h)
require.NotNil(t, params, h)
abci, err := stateStore.LoadABCIResponses(h)
finRes, err := stateStore.LoadFinalizeBlockResponses(h)
require.NoError(t, err, h)
require.NotNil(t, abci, h)
require.NotNil(t, finRes, h)
}
emptyParams := types.ConsensusParams{}
@@ -291,9 +289,9 @@ func TestPruneStates(t *testing.T) {
require.Equal(t, emptyParams, params, h)
}
abci, err := stateStore.LoadABCIResponses(h)
finRes, err := stateStore.LoadFinalizeBlockResponses(h)
require.Error(t, err, h)
require.Nil(t, abci, h)
require.Nil(t, finRes, h)
}
})
}
+9 -3
View File
@@ -650,8 +650,14 @@ func (bs *BlockStore) Close() error {
// key prefixes
// NB: Before modifying these, cross-check them with those in
// internal/state/store.go
// TODO(thane): Move these and the ones in internal/state/store.go to their own package.
// * internal/store/store.go [0..4, 13]
// * internal/state/store.go [5..8, 14]
// * internal/evidence/pool.go [9..10]
// * light/store/db/db.go [11..12]
// TODO(thane): Move all these to their own package.
// TODO: what about these (they already collide):
// * scripts/scmigrate/migrate.go [3] --> Looks OK, as it is also called "SeenCommit"
// * internal/p2p/peermanager.go [1]
const (
// prefixes are unique across all tm db's
prefixBlockMeta = int64(0)
@@ -659,7 +665,7 @@ const (
prefixBlockCommit = int64(2)
prefixSeenCommit = int64(3)
prefixBlockHash = int64(4)
prefixExtCommit = int64(9) // 5..8 are used by state/store
prefixExtCommit = int64(13)
)
func blockMetaKey(height int64) []byte {