tendermint/state/state_test.go

111 lines
3.6 KiB
Go
Raw Normal View History

package state
import (
"testing"
"github.com/stretchr/testify/assert"
2017-08-21 13:31:54 -07:00
abci "github.com/tendermint/abci/types"
2017-05-02 00:53:32 -07:00
crypto "github.com/tendermint/go-crypto"
2017-08-21 13:31:54 -07:00
cmn "github.com/tendermint/tmlibs/common"
2017-05-01 18:25:10 -07:00
dbm "github.com/tendermint/tmlibs/db"
2017-05-02 00:53:32 -07:00
"github.com/tendermint/tmlibs/log"
2017-08-21 13:31:54 -07:00
cfg "github.com/tendermint/tendermint/config"
)
func TestStateCopyEquals(t *testing.T) {
2017-08-21 13:31:54 -07:00
assert := assert.New(t)
2017-05-04 19:33:08 -07:00
config := cfg.ResetTestRoot("state_")
// Get State db
2017-05-04 19:33:08 -07:00
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
state := GetState(stateDB, config.GenesisFile())
2017-05-02 00:53:32 -07:00
state.SetLogger(log.TestingLogger())
stateCopy := state.Copy()
2017-08-21 13:31:54 -07:00
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
2017-08-21 13:31:54 -07:00
assert.False(state.Equals(stateCopy), cmn.Fmt("expected states to be different. got same %v", state))
}
func TestStateSaveLoad(t *testing.T) {
2017-08-21 13:31:54 -07:00
assert := assert.New(t)
2017-05-04 19:33:08 -07:00
config := cfg.ResetTestRoot("state_")
// Get State db
2017-05-04 19:33:08 -07:00
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
state := GetState(stateDB, config.GenesisFile())
2017-05-02 00:53:32 -07:00
state.SetLogger(log.TestingLogger())
state.LastBlockHeight += 1
state.Save()
loadedState := LoadState(stateDB)
2017-08-21 13:31:54 -07:00
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) {
assert := assert.New(t)
2017-05-04 19:33:08 -07:00
config := cfg.ResetTestRoot("state_")
stateDB := dbm.NewDB("state", config.DBBackend, config.DBDir())
state := GetState(stateDB, config.GenesisFile())
2017-05-02 00:53:32 -07:00
state.SetLogger(log.TestingLogger())
state.LastBlockHeight += 1
// build mock responses
block := makeBlock(2, state)
abciResponses := NewABCIResponses(block)
abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo")}
abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok"}
abciResponses.EndBlock = abci.ResponseEndBlock{Diffs: []*abci.Validator{
{
PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
Power: 10,
},
}}
abciResponses.txs = nil
state.SaveABCIResponses(abciResponses)
abciResponses2 := state.LoadABCIResponses()
2017-08-21 13:31:54 -07:00
assert.Equal(abciResponses, abciResponses2, cmn.Fmt("ABCIResponses don't match: Got %v, Expected %v", abciResponses2, abciResponses))
}
func TestValidatorsSaveLoad(t *testing.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)
assert.NotNil(err, "expected err at height 0")
// should be able to load for height 1
v, err = state.LoadValidators(1)
assert.Nil(err, "expected no err at height 1")
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.SaveValidators()
v, err = state.LoadValidators(state.LastBlockHeight + 1)
assert.Nil(err, "expected no err")
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 += 10
state.SaveValidators()
v, err = state.LoadValidators(state.LastBlockHeight + 1)
assert.Nil(err, "expected no err")
assert.Equal(v.Hash(), state.Validators.Hash(), "expected validator hashes to match")
// should be able to load for next next height
_, err = state.LoadValidators(state.LastBlockHeight + 2)
assert.NotNil(err, "expected err")
}