state_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package state
  17. import (
  18. "math/big"
  19. "testing"
  20. checker "gopkg.in/check.v1"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. )
  24. type StateSuite struct {
  25. state *StateDB
  26. }
  27. var _ = checker.Suite(&StateSuite{})
  28. var toAddr = common.BytesToAddress
  29. func (s *StateSuite) TestDump(c *checker.C) {
  30. return
  31. // generate a few entries
  32. obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
  33. obj1.AddBalance(big.NewInt(22))
  34. obj2 := s.state.GetOrNewStateObject(toAddr([]byte{0x01, 0x02}))
  35. obj2.SetCode([]byte{3, 3, 3, 3, 3, 3, 3})
  36. obj3 := s.state.GetOrNewStateObject(toAddr([]byte{0x02}))
  37. obj3.SetBalance(big.NewInt(44))
  38. // write some of them to the trie
  39. s.state.UpdateStateObject(obj1)
  40. s.state.UpdateStateObject(obj2)
  41. // check that dump contains the state objects that are in trie
  42. got := string(s.state.Dump())
  43. want := `{
  44. "root": "6e277ae8357d013e50f74eedb66a991f6922f93ae03714de58b3d0c5e9eee53f",
  45. "accounts": {
  46. "1468288056310c82aa4c01a7e12a10f8111a0560e72b700555479031b86c357d": {
  47. "balance": "22",
  48. "nonce": 0,
  49. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  50. "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
  51. "storage": {}
  52. },
  53. "a17eacbc25cda025e81db9c5c62868822c73ce097cee2a63e33a2e41268358a1": {
  54. "balance": "0",
  55. "nonce": 0,
  56. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  57. "codeHash": "87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3",
  58. "storage": {}
  59. }
  60. }
  61. }`
  62. if got != want {
  63. c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
  64. }
  65. }
  66. func (s *StateSuite) SetUpTest(c *checker.C) {
  67. db, _ := ethdb.NewMemDatabase()
  68. s.state = New(common.Hash{}, db)
  69. }
  70. func TestNull(t *testing.T) {
  71. db, _ := ethdb.NewMemDatabase()
  72. state := New(common.Hash{}, db)
  73. address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
  74. state.CreateAccount(address)
  75. //value := common.FromHex("0x823140710bf13990e4500136726d8b55")
  76. var value common.Hash
  77. state.SetState(address, common.Hash{}, value)
  78. state.SyncIntermediate()
  79. state.Sync()
  80. value = state.GetState(address, common.Hash{})
  81. if !common.EmptyHash(value) {
  82. t.Errorf("expected empty hash. got %x", value)
  83. }
  84. }
  85. func (s *StateSuite) TestSnapshot(c *checker.C) {
  86. stateobjaddr := toAddr([]byte("aa"))
  87. var storageaddr common.Hash
  88. data1 := common.BytesToHash([]byte{42})
  89. data2 := common.BytesToHash([]byte{43})
  90. // set inital state object value
  91. s.state.SetState(stateobjaddr, storageaddr, data1)
  92. // get snapshot of current state
  93. snapshot := s.state.Copy()
  94. // set new state object value
  95. s.state.SetState(stateobjaddr, storageaddr, data2)
  96. // restore snapshot
  97. s.state.Set(snapshot)
  98. // get state storage value
  99. res := s.state.GetState(stateobjaddr, storageaddr)
  100. c.Assert(data1, checker.DeepEquals, res)
  101. }