state_test.go 8.2 KB

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