const abciResponses key

This commit is contained in:
Anton Kaliaev 2018-09-27 11:19:02 +04:00
parent 5afe5c4af5
commit 0ff6526975
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
7 changed files with 30 additions and 61 deletions

View File

@ -345,7 +345,7 @@ func (h *Handshaker) ReplayBlocks(state sm.State, appHash []byte, appBlockHeight
} else if appBlockHeight == storeBlockHeight {
// We ran Commit, but didn't save the state, so replayBlock with mock app
abciResponses, err := sm.LoadABCIResponses(h.stateDB, storeBlockHeight)
abciResponses, err := sm.LoadABCIResponses(h.stateDB)
if err != nil {
return nil, err
}

View File

@ -1,6 +1,9 @@
package state
import "fmt"
import (
"errors"
"fmt"
)
type (
ErrInvalidBlock error
@ -39,10 +42,10 @@ type (
ErrNoConsensusParamsForHeight struct {
Height int64
}
)
ErrNoABCIResponsesForHeight struct {
Height int64
}
var (
ErrNoABCIResponses = errors.New("Could not find results")
)
func (e ErrUnknownBlock) Error() string {
@ -71,7 +74,3 @@ func (e ErrNoValSetForHeight) Error() string {
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)
}

View File

@ -82,12 +82,7 @@ func (blockExec *BlockExecutor) ApplyBlock(state State, blockID types.BlockID, b
fail.Fail() // XXX
// Save the results before we commit.
saveABCIResponses(blockExec.db, block.Height, abciResponses)
// Delete the results for the previous height (not needed anymore).
if block.Height > 0 {
deleteABCIResponses(blockExec.db, block.Height-1)
}
saveABCIResponses(blockExec.db,abciResponses)
fail.Fail() // XXX

View File

@ -45,17 +45,11 @@ func TestApplyBlock(t *testing.T) {
state, err = blockExec.ApplyBlock(state, blockID, block)
require.NoError(t, err)
// Test we store results for the current height and do not store results for
// the previous height.
abciResponses, err := LoadABCIResponses(stateDB, block.Height)
// test we save results
abciResponses, err := LoadABCIResponses(stateDB)
if assert.NoError(t, err) {
assert.NotNil(t, abciResponses)
}
abciResponses2, err := LoadABCIResponses(stateDB, block.Height-1)
assert.Nil(t, abciResponses2)
if assert.Error(t, err) {
assert.Equal(t, ErrNoABCIResponsesForHeight{block.Height - 1}, err)
}
// TODO check state and mempool
}

View File

@ -7,13 +7,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
crypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/types"
)
@ -82,8 +82,8 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
types.TM2PB.NewValidatorUpdate(ed25519.GenPrivKey().PubKey(), 10),
}}
saveABCIResponses(stateDB, block.Height, abciResponses)
loadedABCIResponses, err := LoadABCIResponses(stateDB, block.Height)
saveABCIResponses(stateDB, abciResponses)
loadedABCIResponses, err := LoadABCIResponses(stateDB)
assert.Nil(err)
assert.Equal(abciResponses, loadedABCIResponses,
fmt.Sprintf("ABCIResponses don't match:\ngot: %v\nexpected: %v\n",
@ -94,8 +94,6 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
func TestABCIResponsesSaveLoad2(t *testing.T) {
tearDown, stateDB, _ := setupTestCase(t)
defer tearDown(t)
// nolint: vetshadow
assert := assert.New(t)
cases := [...]struct {
// Height is implied to equal index+2,
@ -133,29 +131,19 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
},
}
// Query all before, this should return error.
for i := range cases {
h := int64(i + 1)
res, err := LoadABCIResponses(stateDB, h)
assert.Error(err, "%d: %#v", i, res)
}
res, err := LoadABCIResponses(stateDB)
assert.Error(t, err, "%#v", res)
// Add all cases.
for i, tc := range cases {
h := int64(i + 1) // last block height, one below what we save
responses := &ABCIResponses{
DeliverTx: tc.added,
EndBlock: &abci.ResponseEndBlock{},
}
saveABCIResponses(stateDB, h, responses)
}
saveABCIResponses(stateDB, responses)
// Query all before, should return expected value.
for i, tc := range cases {
h := int64(i + 1)
res, err := LoadABCIResponses(stateDB, h)
assert.NoError(err, "%d", i)
assert.Equal(tc.expected.Hash(), res.ResultsHash(), "%d", i)
res, err = LoadABCIResponses(stateDB)
assert.NoError(t, err, "%d", i)
assert.Equal(t, tc.expected.Hash(), res.ResultsHash(), "%d", i)
}
}

View File

@ -9,6 +9,10 @@ import (
"github.com/tendermint/tendermint/types"
)
var (
abciResponsesKey = []byte("abciResponsesKey")
)
//------------------------------------------------------------------------
func calcValidatorsKey(height int64) []byte {
@ -19,10 +23,6 @@ func calcConsensusParamsKey(height int64) []byte {
return []byte(fmt.Sprintf("consensusParamsKey:%v", height))
}
func calcABCIResponsesKey(height int64) []byte {
return []byte(fmt.Sprintf("abciResponsesKey:%v", height))
}
// LoadStateFromDBOrGenesisFile loads the most recent state from the database,
// or creates a new one from the given genesisFilePath and persists the result
// to the database.
@ -134,10 +134,10 @@ func (arz *ABCIResponses) ResultsHash() []byte {
// LoadABCIResponses loads the ABCIResponses for the given height from the database.
// 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 LoadABCIResponses(db dbm.DB, height int64) (*ABCIResponses, error) {
buf := db.Get(calcABCIResponsesKey(height))
func LoadABCIResponses(db dbm.DB) (*ABCIResponses, error) {
buf := db.Get(abciResponsesKey)
if len(buf) == 0 {
return nil, ErrNoABCIResponsesForHeight{height}
return nil, ErrNoABCIResponses
}
abciResponses := new(ABCIResponses)
@ -154,14 +154,8 @@ func LoadABCIResponses(db dbm.DB, height int64) (*ABCIResponses, error) {
// saveABCIResponses persists the ABCIResponses to the database.
// 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.
func saveABCIResponses(db dbm.DB, height int64, abciResponses *ABCIResponses) {
db.SetSync(calcABCIResponsesKey(height), abciResponses.Bytes())
}
// deleteABCIResponses deletes ABCIResponses for the given height.
func deleteABCIResponses(db dbm.DB, height int64) {
db.Delete(calcABCIResponsesKey(height))
func saveABCIResponses(db dbm.DB, abciResponses *ABCIResponses) {
db.SetSync(abciResponsesKey, abciResponses.Bytes())
}
//-----------------------------------------------------------------------------

View File

@ -9,7 +9,6 @@ import (
// TxIndexer interface defines methods to index and search transactions.
type TxIndexer interface {
// AddBatch analyzes, indexes and stores a batch of transactions.
AddBatch(b *Batch) error