diff --git a/state/state.go b/state/state.go index 72c3e64da..7e22e04d1 100644 --- a/state/state.go +++ b/state/state.go @@ -42,17 +42,19 @@ type State struct { GenesisDoc *types.GenesisDoc ChainID string - // updated at end of SetBlockAndValidators - LastBlockHeight int // Genesis state has this set to 0. So, Block(H=0) does not exist. + // These fields are updated by SetBlockAndValidators. + // LastBlockHeight=0 at genesis (ie. block(H=0) does not exist) + // LastValidators is used to validate block.LastCommit. + LastBlockHeight int LastBlockID types.BlockID LastBlockTime time.Time Validators *types.ValidatorSet - LastValidators *types.ValidatorSet // block.LastCommit validated against this + LastValidators *types.ValidatorSet // AppHash is updated after Commit AppHash []byte - TxIndexer txindex.TxIndexer `json:"-"` // Transaction indexer. + TxIndexer txindex.TxIndexer `json:"-"` // Transaction indexer // When a block returns a validator set change via EndBlock, // the change only applies to the next block. @@ -224,7 +226,8 @@ func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader typ nextValSet.IncrementAccum(1) s.setBlockAndValidators(header.Height, - types.BlockID{header.Hash(), blockPartsHeader}, header.Time, + types.BlockID{header.Hash(), blockPartsHeader}, + header.Time, prevValSet, nextValSet) } @@ -258,7 +261,7 @@ func GetState(stateDB dbm.DB, genesisFile string) *State { return state } -//-------------------------------------------------- +//------------------------------------------------------------------------ // ABCIResponses retains the responses of the various ABCI calls during block processing. // It is persisted to disk before calling Commit. @@ -298,10 +301,11 @@ func (vi *ValidatorsInfo) Bytes() []byte { return wire.BinaryBytes(*vi) } -//----------------------------------------------------------------------------- +//------------------------------------------------------------------------ // Genesis -// MakeGenesisStateFromFile reads and unmarshals state from the given file. +// MakeGenesisStateFromFile reads and unmarshals state from the given +// file. // // Used during replay and in tests. func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State { diff --git a/state/state_test.go b/state/state_test.go index 713d76f80..4ff367fa3 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -8,7 +8,9 @@ import ( "github.com/stretchr/testify/assert" abci "github.com/tendermint/abci/types" + crypto "github.com/tendermint/go-crypto" + cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -17,46 +19,50 @@ import ( "github.com/tendermint/tendermint/types" ) -func TestStateCopyEquals(t *testing.T) { - assert := assert.New(t) +// setupTestCase does setup common to all test cases +func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, *State) { config := cfg.ResetTestRoot("state_") - - // Get State db stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) state := GetState(stateDB, config.GenesisFile()) state.SetLogger(log.TestingLogger()) + tearDown := func(t *testing.T) {} + + return tearDown, stateDB, state +} + +func TestStateCopy(t *testing.T) { + tearDown, _, state := setupTestCase(t) + defer tearDown(t) + assert := assert.New(t) + stateCopy := state.Copy() - assert.True(state.Equals(stateCopy), cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", stateCopy, state)) - stateCopy.LastBlockHeight += 1 + assert.True(state.Equals(stateCopy), + cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", stateCopy, state)) + stateCopy.LastBlockHeight++ assert.False(state.Equals(stateCopy), cmn.Fmt("expected states to be different. got same %v", state)) } func TestStateSaveLoad(t *testing.T) { + tearDown, stateDB, state := setupTestCase(t) + defer tearDown(t) assert := assert.New(t) - config := cfg.ResetTestRoot("state_") - // Get State db - stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) - state := GetState(stateDB, config.GenesisFile()) - state.SetLogger(log.TestingLogger()) - state.LastBlockHeight += 1 + state.LastBlockHeight++ state.Save() loadedState := LoadState(stateDB) - assert.True(state.Equals(loadedState), cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", loadedState, state)) + assert.True(state.Equals(loadedState), + cmn.Fmt("expected state and its copy to be identical. got %v\n expected %v\n", loadedState, state)) } func TestABCIResponsesSaveLoad(t *testing.T) { + tearDown, _, state := setupTestCase(t) + defer tearDown(t) assert := assert.New(t) - config := cfg.ResetTestRoot("state_") - stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) - state := GetState(stateDB, config.GenesisFile()) - state.SetLogger(log.TestingLogger()) - - state.LastBlockHeight += 1 + state.LastBlockHeight++ // build mock responses block := makeBlock(2, state) @@ -73,16 +79,14 @@ func TestABCIResponsesSaveLoad(t *testing.T) { state.SaveABCIResponses(abciResponses) abciResponses2 := state.LoadABCIResponses() - assert.Equal(abciResponses, abciResponses2, cmn.Fmt("ABCIResponses don't match: Got %v, Expected %v", abciResponses2, abciResponses)) + assert.Equal(abciResponses, abciResponses2, + cmn.Fmt("ABCIResponses don't match: Got %v, Expected %v", abciResponses2, abciResponses)) } func TestValidatorSimpleSaveLoad(t *testing.T) { + tearDown, _, state := setupTestCase(t) + defer tearDown(t) assert := assert.New(t) - config := cfg.ResetTestRoot("state_") - // Get State db - stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) - state := GetState(stateDB, config.GenesisFile()) - state.SetLogger(log.TestingLogger()) // cant load anything for height 0 v, err := state.LoadValidators(0) @@ -94,7 +98,7 @@ func TestValidatorSimpleSaveLoad(t *testing.T) { assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match") // increment height, save; should be able to load for next height - state.LastBlockHeight += 1 + state.LastBlockHeight++ state.saveValidatorsInfo() v, err = state.LoadValidators(state.LastBlockHeight + 1) assert.Nil(err, "expected no err") @@ -113,12 +117,9 @@ func TestValidatorSimpleSaveLoad(t *testing.T) { } func TestValidatorChangesSaveLoad(t *testing.T) { + tearDown, _, state := setupTestCase(t) + defer tearDown(t) assert := assert.New(t) - config := cfg.ResetTestRoot("state_") - // Get State db - stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir()) - state := GetState(stateDB, config.GenesisFile()) - state.SetLogger(log.TestingLogger()) // change vals at these heights changeHeights := []int{1, 2, 4, 5, 10, 15, 16, 17, 20} @@ -141,7 +142,7 @@ func TestValidatorChangesSaveLoad(t *testing.T) { // when we get to a change height, // use the next pubkey if changeIndex < len(changeHeights) && i == changeHeights[changeIndex] { - changeIndex += 1 + changeIndex++ pubkey = pubkeys[changeIndex] } header, parts, responses := makeHeaderPartsResponses(state, i, pubkey) @@ -157,7 +158,7 @@ func TestValidatorChangesSaveLoad(t *testing.T) { // we we get to the height after a change height // use the next pubkey (note our counter starts at 0 this time) if changeIndex < len(changeHeights) && i == changeHeights[changeIndex]+1 { - changeIndex += 1 + changeIndex++ pubkey = pubkeys[changeIndex] } testCases[i-1] = valChangeTestCase{i, pubkey} @@ -173,7 +174,9 @@ func TestValidatorChangesSaveLoad(t *testing.T) { } } -func makeHeaderPartsResponses(state *State, height int, pubkey crypto.PubKey) (*types.Header, types.PartSetHeader, *ABCIResponses) { +func makeHeaderPartsResponses(state *State, height int, + pubkey crypto.PubKey) (*types.Header, types.PartSetHeader, *ABCIResponses) { + block := makeBlock(height, state) _, val := state.Validators.GetByIndex(0) abciResponses := &ABCIResponses{