You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1 KiB

  1. package state
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. abci "github.com/tendermint/abci/types"
  7. "github.com/tendermint/go-crypto"
  8. dbm "github.com/tendermint/go-db"
  9. "github.com/tendermint/tendermint/config/tendermint_test"
  10. )
  11. func TestStateCopyEquals(t *testing.T) {
  12. config := tendermint_test.ResetConfig("state_")
  13. // Get State db
  14. stateDB := dbm.NewDB("state", config.GetString("db_backend"), config.GetString("db_dir"))
  15. state := GetState(config, stateDB)
  16. stateCopy := state.Copy()
  17. if !state.Equals(stateCopy) {
  18. t.Fatal("expected state and its copy to be identical. got %v\n expected %v\n", stateCopy, state)
  19. }
  20. stateCopy.LastBlockHeight += 1
  21. if state.Equals(stateCopy) {
  22. t.Fatal("expected states to be different. got same %v", state)
  23. }
  24. }
  25. func TestStateSaveLoad(t *testing.T) {
  26. config := tendermint_test.ResetConfig("state_")
  27. // Get State db
  28. stateDB := dbm.NewDB("state", config.GetString("db_backend"), config.GetString("db_dir"))
  29. state := GetState(config, stateDB)
  30. state.LastBlockHeight += 1
  31. state.Save()
  32. loadedState := LoadState(stateDB)
  33. if !state.Equals(loadedState) {
  34. t.Fatal("expected state and its copy to be identical. got %v\n expected %v\n", loadedState, state)
  35. }
  36. }
  37. func TestABCIResponsesSaveLoad(t *testing.T) {
  38. assert := assert.New(t)
  39. config := tendermint_test.ResetConfig("state_")
  40. stateDB := dbm.NewDB("state", config.GetString("db_backend"), config.GetString("db_dir"))
  41. state := GetState(config, stateDB)
  42. state.LastBlockHeight += 1
  43. // build mock responses
  44. block := makeBlock(2, state)
  45. abciResponses := NewABCIResponses(block)
  46. abciResponses.DeliverTx[0] = &abci.ResponseDeliverTx{Data: []byte("foo")}
  47. abciResponses.DeliverTx[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok"}
  48. abciResponses.EndBlock = abci.ResponseEndBlock{Diffs: []*abci.Validator{
  49. {
  50. PubKey: crypto.GenPrivKeyEd25519().PubKey().Bytes(),
  51. Power: 10,
  52. },
  53. }}
  54. abciResponses.txs = nil
  55. state.SaveABCIResponses(abciResponses)
  56. abciResponses2 := state.LoadABCIResponses()
  57. assert.Equal(abciResponses, abciResponses2, fmt.Sprintf("ABCIResponses don't match: Got %v, Expected %v", abciResponses2, abciResponses))
  58. }