state_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. checker "gopkg.in/check.v1"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. )
  25. type StateSuite struct {
  26. state *StateDB
  27. }
  28. var _ = checker.Suite(&StateSuite{})
  29. var toAddr = common.BytesToAddress
  30. func (s *StateSuite) TestDump(c *checker.C) {
  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. s.state.Commit()
  42. // check that dump contains the state objects that are in trie
  43. got := string(s.state.Dump())
  44. want := `{
  45. "root": "71edff0130dd2385947095001c73d9e28d862fc286fca2b922ca6f6f3cddfdd2",
  46. "accounts": {
  47. "0000000000000000000000000000000000000001": {
  48. "balance": "22",
  49. "nonce": 0,
  50. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  51. "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
  52. "code": "",
  53. "storage": {}
  54. },
  55. "0000000000000000000000000000000000000002": {
  56. "balance": "44",
  57. "nonce": 0,
  58. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  59. "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
  60. "code": "",
  61. "storage": {}
  62. },
  63. "0000000000000000000000000000000000000102": {
  64. "balance": "0",
  65. "nonce": 0,
  66. "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  67. "codeHash": "87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3",
  68. "code": "03030303030303",
  69. "storage": {}
  70. }
  71. }
  72. }`
  73. if got != want {
  74. c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
  75. }
  76. }
  77. func (s *StateSuite) SetUpTest(c *checker.C) {
  78. db, _ := ethdb.NewMemDatabase()
  79. s.state, _ = New(common.Hash{}, db)
  80. }
  81. func TestNull(t *testing.T) {
  82. db, _ := ethdb.NewMemDatabase()
  83. state, _ := New(common.Hash{}, db)
  84. address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
  85. state.CreateAccount(address)
  86. //value := common.FromHex("0x823140710bf13990e4500136726d8b55")
  87. var value common.Hash
  88. state.SetState(address, common.Hash{}, value)
  89. state.Commit()
  90. value = state.GetState(address, common.Hash{})
  91. if !common.EmptyHash(value) {
  92. t.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.Copy()
  104. // set new state object value
  105. s.state.SetState(stateobjaddr, storageaddr, data2)
  106. // restore snapshot
  107. s.state.Set(snapshot)
  108. // get state storage value
  109. res := s.state.GetState(stateobjaddr, storageaddr)
  110. c.Assert(data1, checker.DeepEquals, res)
  111. }
  112. // use testing instead of checker because checker does not support
  113. // printing/logging in tests (-check.vv does not work)
  114. func TestSnapshot2(t *testing.T) {
  115. db, _ := ethdb.NewMemDatabase()
  116. state, _ := New(common.Hash{}, db)
  117. stateobjaddr0 := toAddr([]byte("so0"))
  118. stateobjaddr1 := toAddr([]byte("so1"))
  119. var storageaddr common.Hash
  120. data0 := common.BytesToHash([]byte{17})
  121. data1 := common.BytesToHash([]byte{18})
  122. state.SetState(stateobjaddr0, storageaddr, data0)
  123. state.SetState(stateobjaddr1, storageaddr, data1)
  124. // db, trie are already non-empty values
  125. so0 := state.GetStateObject(stateobjaddr0)
  126. so0.balance = big.NewInt(42)
  127. so0.nonce = 43
  128. so0.SetCode([]byte{'c', 'a', 'f', 'e'})
  129. so0.remove = false
  130. so0.deleted = false
  131. so0.dirty = true
  132. state.SetStateObject(so0)
  133. state.Commit()
  134. // and one with deleted == true
  135. so1 := state.GetStateObject(stateobjaddr1)
  136. so1.balance = big.NewInt(52)
  137. so1.nonce = 53
  138. so1.SetCode([]byte{'c', 'a', 'f', 'e', '2'})
  139. so1.remove = true
  140. so1.deleted = true
  141. so1.dirty = true
  142. state.SetStateObject(so1)
  143. so1 = state.GetStateObject(stateobjaddr1)
  144. if so1 != nil {
  145. t.Fatalf("deleted object not nil when getting")
  146. }
  147. snapshot := state.Copy()
  148. state.Set(snapshot)
  149. so0Restored := state.GetStateObject(stateobjaddr0)
  150. so0Restored.GetState(storageaddr)
  151. so1Restored := state.GetStateObject(stateobjaddr1)
  152. // non-deleted is equal (restored)
  153. compareStateObjects(so0Restored, so0, t)
  154. // deleted should be nil, both before and after restore of state copy
  155. if so1Restored != nil {
  156. t.Fatalf("deleted object not nil after restoring snapshot")
  157. }
  158. }
  159. func compareStateObjects(so0, so1 *StateObject, t *testing.T) {
  160. if so0.address != so1.address {
  161. t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address)
  162. }
  163. if so0.balance.Cmp(so1.balance) != 0 {
  164. t.Fatalf("Balance mismatch: have %v, want %v", so0.balance, so1.balance)
  165. }
  166. if so0.nonce != so1.nonce {
  167. t.Fatalf("Nonce mismatch: have %v, want %v", so0.nonce, so1.nonce)
  168. }
  169. if !bytes.Equal(so0.codeHash, so1.codeHash) {
  170. t.Fatalf("CodeHash mismatch: have %v, want %v", so0.codeHash, so1.codeHash)
  171. }
  172. if !bytes.Equal(so0.code, so1.code) {
  173. t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code)
  174. }
  175. if !bytes.Equal(so0.initCode, so1.initCode) {
  176. t.Fatalf("InitCode mismatch: have %v, want %v", so0.initCode, so1.initCode)
  177. }
  178. for k, v := range so1.storage {
  179. if so0.storage[k] != v {
  180. t.Fatalf("Storage key %s mismatch: have %v, want %v", k, so0.storage[k], v)
  181. }
  182. }
  183. for k, v := range so0.storage {
  184. if so1.storage[k] != v {
  185. t.Fatalf("Storage key %s mismatch: have %v, want none.", k, v)
  186. }
  187. }
  188. if so0.remove != so1.remove {
  189. t.Fatalf("Remove mismatch: have %v, want %v", so0.remove, so1.remove)
  190. }
  191. if so0.deleted != so1.deleted {
  192. t.Fatalf("Deleted mismatch: have %v, want %v", so0.deleted, so1.deleted)
  193. }
  194. if so0.dirty != so1.dirty {
  195. t.Fatalf("Dirty mismatch: have %v, want %v", so0.dirty, so1.dirty)
  196. }
  197. }