state_test.go 7.3 KB

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