state_test.go 6.6 KB

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