state_test.go 8.9 KB

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