state_test.go 11 KB

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