state_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package state
  17. import (
  18. "bytes"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. checker "gopkg.in/check.v1"
  25. )
  26. type StateSuite struct {
  27. db *ethdb.MemDatabase
  28. state *StateDB
  29. }
  30. var _ = checker.Suite(&StateSuite{})
  31. var toAddr = common.BytesToAddress
  32. func (s *StateSuite) TestDump(c *checker.C) {
  33. // generate a few entries
  34. obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
  35. obj1.AddBalance(big.NewInt(22))
  36. obj2 := s.state.GetOrNewStateObject(toAddr([]byte{0x01, 0x02}))
  37. obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
  38. obj3 := s.state.GetOrNewStateObject(toAddr([]byte{0x02}))
  39. obj3.SetBalance(big.NewInt(44))
  40. // write some of them to the trie
  41. s.state.updateStateObject(obj1)
  42. s.state.updateStateObject(obj2)
  43. s.state.Commit(false)
  44. // check that dump contains the state objects that are in trie
  45. got := string(s.state.Dump())
  46. want := `{
  47. "root": "71edff0130dd2385947095001c73d9e28d862fc286fca2b922ca6f6f3cddfdd2",
  48. "accounts": {
  49. "0000000000000000000000000000000000000001": {
  50. "balance": "22",
  51. "nonce": 0,
  52. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  53. "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
  54. "code": "",
  55. "storage": {}
  56. },
  57. "0000000000000000000000000000000000000002": {
  58. "balance": "44",
  59. "nonce": 0,
  60. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  61. "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
  62. "code": "",
  63. "storage": {}
  64. },
  65. "0000000000000000000000000000000000000102": {
  66. "balance": "0",
  67. "nonce": 0,
  68. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  69. "codeHash": "87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3",
  70. "code": "03030303030303",
  71. "storage": {}
  72. }
  73. }
  74. }`
  75. if got != want {
  76. c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
  77. }
  78. }
  79. func (s *StateSuite) SetUpTest(c *checker.C) {
  80. s.db = ethdb.NewMemDatabase()
  81. s.state, _ = New(common.Hash{}, NewDatabase(s.db))
  82. }
  83. func (s *StateSuite) TestNull(c *checker.C) {
  84. address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
  85. s.state.CreateAccount(address)
  86. //value := common.FromHex("0x823140710bf13990e4500136726d8b55")
  87. var value common.Hash
  88. s.state.SetState(address, common.Hash{}, value)
  89. s.state.Commit(false)
  90. value = s.state.GetState(address, common.Hash{})
  91. if value != (common.Hash{}) {
  92. c.Errorf("expected empty hash. got %x", value)
  93. }
  94. }
  95. func (s *StateSuite) TestSnapshot(c *checker.C) {
  96. stateobjaddr := toAddr([]byte("aa"))
  97. var storageaddr common.Hash
  98. data1 := common.BytesToHash([]byte{42})
  99. data2 := common.BytesToHash([]byte{43})
  100. // set initial state object value
  101. s.state.SetState(stateobjaddr, storageaddr, data1)
  102. // get snapshot of current state
  103. snapshot := s.state.Snapshot()
  104. // set new state object value
  105. s.state.SetState(stateobjaddr, storageaddr, data2)
  106. // restore snapshot
  107. s.state.RevertToSnapshot(snapshot)
  108. // get state storage value
  109. res := s.state.GetState(stateobjaddr, storageaddr)
  110. c.Assert(data1, checker.DeepEquals, res)
  111. }
  112. func (s *StateSuite) TestSnapshotEmpty(c *checker.C) {
  113. s.state.RevertToSnapshot(s.state.Snapshot())
  114. }
  115. // use testing instead of checker because checker does not support
  116. // printing/logging in tests (-check.vv does not work)
  117. func TestSnapshot2(t *testing.T) {
  118. state, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase()))
  119. stateobjaddr0 := toAddr([]byte("so0"))
  120. stateobjaddr1 := toAddr([]byte("so1"))
  121. var storageaddr common.Hash
  122. data0 := common.BytesToHash([]byte{17})
  123. data1 := common.BytesToHash([]byte{18})
  124. state.SetState(stateobjaddr0, storageaddr, data0)
  125. state.SetState(stateobjaddr1, storageaddr, data1)
  126. // db, trie are already non-empty values
  127. so0 := state.getStateObject(stateobjaddr0)
  128. so0.SetBalance(big.NewInt(42))
  129. so0.SetNonce(43)
  130. so0.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e'}), []byte{'c', 'a', 'f', 'e'})
  131. so0.suicided = false
  132. so0.deleted = false
  133. state.setStateObject(so0)
  134. root, _ := state.Commit(false)
  135. state.Reset(root)
  136. // and one with deleted == true
  137. so1 := state.getStateObject(stateobjaddr1)
  138. so1.SetBalance(big.NewInt(52))
  139. so1.SetNonce(53)
  140. so1.SetCode(crypto.Keccak256Hash([]byte{'c', 'a', 'f', 'e', '2'}), []byte{'c', 'a', 'f', 'e', '2'})
  141. so1.suicided = true
  142. so1.deleted = true
  143. state.setStateObject(so1)
  144. so1 = state.getStateObject(stateobjaddr1)
  145. if so1 != nil {
  146. t.Fatalf("deleted object not nil when getting")
  147. }
  148. snapshot := state.Snapshot()
  149. state.RevertToSnapshot(snapshot)
  150. so0Restored := state.getStateObject(stateobjaddr0)
  151. // Update lazily-loaded values before comparing.
  152. so0Restored.GetState(state.db, storageaddr)
  153. so0Restored.Code(state.db)
  154. // non-deleted is equal (restored)
  155. compareStateObjects(so0Restored, so0, t)
  156. // deleted should be nil, both before and after restore of state copy
  157. so1Restored := state.getStateObject(stateobjaddr1)
  158. if so1Restored != nil {
  159. t.Fatalf("deleted object not nil after restoring snapshot: %+v", so1Restored)
  160. }
  161. }
  162. func compareStateObjects(so0, so1 *stateObject, t *testing.T) {
  163. if so0.Address() != so1.Address() {
  164. t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address)
  165. }
  166. if so0.Balance().Cmp(so1.Balance()) != 0 {
  167. t.Fatalf("Balance mismatch: have %v, want %v", so0.Balance(), so1.Balance())
  168. }
  169. if so0.Nonce() != so1.Nonce() {
  170. t.Fatalf("Nonce mismatch: have %v, want %v", so0.Nonce(), so1.Nonce())
  171. }
  172. if so0.data.Root != so1.data.Root {
  173. t.Errorf("Root mismatch: have %x, want %x", so0.data.Root[:], so1.data.Root[:])
  174. }
  175. if !bytes.Equal(so0.CodeHash(), so1.CodeHash()) {
  176. t.Fatalf("CodeHash mismatch: have %v, want %v", so0.CodeHash(), so1.CodeHash())
  177. }
  178. if !bytes.Equal(so0.code, so1.code) {
  179. t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code)
  180. }
  181. if len(so1.cachedStorage) != len(so0.cachedStorage) {
  182. t.Errorf("Storage size mismatch: have %d, want %d", len(so1.cachedStorage), len(so0.cachedStorage))
  183. }
  184. for k, v := range so1.cachedStorage {
  185. if so0.cachedStorage[k] != v {
  186. t.Errorf("Storage key %x mismatch: have %v, want %v", k, so0.cachedStorage[k], v)
  187. }
  188. }
  189. for k, v := range so0.cachedStorage {
  190. if so1.cachedStorage[k] != v {
  191. t.Errorf("Storage key %x mismatch: have %v, want none.", k, v)
  192. }
  193. }
  194. if so0.suicided != so1.suicided {
  195. t.Fatalf("suicided mismatch: have %v, want %v", so0.suicided, so1.suicided)
  196. }
  197. if so0.deleted != so1.deleted {
  198. t.Fatalf("Deleted mismatch: have %v, want %v", so0.deleted, so1.deleted)
  199. }
  200. }