state_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.SyncIntermediate()
  80. state.Sync()
  81. value = state.GetState(address, common.Hash{})
  82. if !common.EmptyHash(value) {
  83. t.Errorf("expected empty hash. got %x", value)
  84. }
  85. }
  86. func (s *StateSuite) TestSnapshot(c *checker.C) {
  87. stateobjaddr := toAddr([]byte("aa"))
  88. var storageaddr common.Hash
  89. data1 := common.BytesToHash([]byte{42})
  90. data2 := common.BytesToHash([]byte{43})
  91. // set inital state object value
  92. s.state.SetState(stateobjaddr, storageaddr, data1)
  93. // get snapshot of current state
  94. snapshot := s.state.Copy()
  95. // set new state object value
  96. s.state.SetState(stateobjaddr, storageaddr, data2)
  97. // restore snapshot
  98. s.state.Set(snapshot)
  99. // get state storage value
  100. res := s.state.GetState(stateobjaddr, storageaddr)
  101. c.Assert(data1, checker.DeepEquals, res)
  102. }
  103. // use testing instead of checker because checker does not support
  104. // printing/logging in tests (-check.vv does not work)
  105. func TestSnapshot2(t *testing.T) {
  106. db, _ := ethdb.NewMemDatabase()
  107. state := New(common.Hash{}, db)
  108. stateobjaddr0 := toAddr([]byte("so0"))
  109. stateobjaddr1 := toAddr([]byte("so1"))
  110. var storageaddr common.Hash
  111. data0 := common.BytesToHash([]byte{17})
  112. data1 := common.BytesToHash([]byte{18})
  113. state.SetState(stateobjaddr0, storageaddr, data0)
  114. state.SetState(stateobjaddr1, storageaddr, data1)
  115. // db, trie are already non-empty values
  116. so0 := state.GetStateObject(stateobjaddr0)
  117. so0.balance = big.NewInt(42)
  118. so0.nonce = 43
  119. so0.gasPool = big.NewInt(44)
  120. so0.code = []byte{'c', 'a', 'f', 'e'}
  121. so0.codeHash = so0.CodeHash()
  122. so0.remove = true
  123. so0.deleted = false
  124. so0.dirty = false
  125. state.SetStateObject(so0)
  126. // and one with deleted == true
  127. so1 := state.GetStateObject(stateobjaddr1)
  128. so1.balance = big.NewInt(52)
  129. so1.nonce = 53
  130. so1.gasPool = big.NewInt(54)
  131. so1.code = []byte{'c', 'a', 'f', 'e', '2'}
  132. so1.codeHash = so1.CodeHash()
  133. so1.remove = true
  134. so1.deleted = true
  135. so1.dirty = true
  136. state.SetStateObject(so1)
  137. so1 = state.GetStateObject(stateobjaddr1)
  138. if so1 != nil {
  139. t.Fatalf("deleted object not nil when getting")
  140. }
  141. snapshot := state.Copy()
  142. state.Set(snapshot)
  143. so0Restored := state.GetStateObject(stateobjaddr0)
  144. so1Restored := state.GetStateObject(stateobjaddr1)
  145. // non-deleted is equal (restored)
  146. compareStateObjects(so0Restored, so0, t)
  147. // deleted should be nil, both before and after restore of state copy
  148. if so1Restored != nil {
  149. t.Fatalf("deleted object not nil after restoring snapshot")
  150. }
  151. }
  152. func compareStateObjects(so0, so1 *StateObject, t *testing.T) {
  153. if so0.address != so1.address {
  154. t.Fatalf("Address mismatch: have %v, want %v", so0.address, so1.address)
  155. }
  156. if so0.balance.Cmp(so1.balance) != 0 {
  157. t.Fatalf("Balance mismatch: have %v, want %v", so0.balance, so1.balance)
  158. }
  159. if so0.nonce != so1.nonce {
  160. t.Fatalf("Nonce mismatch: have %v, want %v", so0.nonce, so1.nonce)
  161. }
  162. if !bytes.Equal(so0.codeHash, so1.codeHash) {
  163. t.Fatalf("CodeHash mismatch: have %v, want %v", so0.codeHash, so1.codeHash)
  164. }
  165. if !bytes.Equal(so0.code, so1.code) {
  166. t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code)
  167. }
  168. if !bytes.Equal(so0.initCode, so1.initCode) {
  169. t.Fatalf("InitCode mismatch: have %v, want %v", so0.initCode, so1.initCode)
  170. }
  171. for k, v := range so1.storage {
  172. if so0.storage[k] != v {
  173. t.Fatalf("Storage key %s mismatch: have %v, want %v", k, so0.storage[k], v)
  174. }
  175. }
  176. for k, v := range so0.storage {
  177. if so1.storage[k] != v {
  178. t.Fatalf("Storage key %s mismatch: have %v, want none.", k, v)
  179. }
  180. }
  181. if so0.gasPool.Cmp(so1.gasPool) != 0 {
  182. t.Fatalf("GasPool mismatch: have %v, want %v", so0.gasPool, so1.gasPool)
  183. }
  184. if so0.remove != so1.remove {
  185. t.Fatalf("Remove mismatch: have %v, want %v", so0.remove, so1.remove)
  186. }
  187. if so0.deleted != so1.deleted {
  188. t.Fatalf("Deleted mismatch: have %v, want %v", so0.deleted, so1.deleted)
  189. }
  190. if so0.dirty != so1.dirty {
  191. t.Fatalf("Dirty mismatch: have %v, want %v", so0.dirty, so1.dirty)
  192. }
  193. }