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.

78 lines
2.2 KiB

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