statedb_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Copyright 2016 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/binary"
  20. "fmt"
  21. "math"
  22. "math/big"
  23. "math/rand"
  24. "reflect"
  25. "strings"
  26. "testing"
  27. "testing/quick"
  28. check "gopkg.in/check.v1"
  29. "github.com/ethereum/go-ethereum/common"
  30. "github.com/ethereum/go-ethereum/core/types"
  31. "github.com/ethereum/go-ethereum/ethdb"
  32. )
  33. // Tests that updating a state trie does not leak any database writes prior to
  34. // actually committing the state.
  35. func TestUpdateLeaks(t *testing.T) {
  36. // Create an empty state database
  37. db := ethdb.NewMemDatabase()
  38. state, _ := New(common.Hash{}, NewDatabase(db))
  39. // Update it with some accounts
  40. for i := byte(0); i < 255; i++ {
  41. addr := common.BytesToAddress([]byte{i})
  42. state.AddBalance(addr, big.NewInt(int64(11*i)))
  43. state.SetNonce(addr, uint64(42*i))
  44. if i%2 == 0 {
  45. state.SetState(addr, common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i}))
  46. }
  47. if i%3 == 0 {
  48. state.SetCode(addr, []byte{i, i, i, i, i})
  49. }
  50. state.IntermediateRoot(false)
  51. }
  52. // Ensure that no data was leaked into the database
  53. for _, key := range db.Keys() {
  54. value, _ := db.Get(key)
  55. t.Errorf("State leaked into database: %x -> %x", key, value)
  56. }
  57. }
  58. // Tests that no intermediate state of an object is stored into the database,
  59. // only the one right before the commit.
  60. func TestIntermediateLeaks(t *testing.T) {
  61. // Create two state databases, one transitioning to the final state, the other final from the beginning
  62. transDb := ethdb.NewMemDatabase()
  63. finalDb := ethdb.NewMemDatabase()
  64. transState, _ := New(common.Hash{}, NewDatabase(transDb))
  65. finalState, _ := New(common.Hash{}, NewDatabase(finalDb))
  66. modify := func(state *StateDB, addr common.Address, i, tweak byte) {
  67. state.SetBalance(addr, big.NewInt(int64(11*i)+int64(tweak)))
  68. state.SetNonce(addr, uint64(42*i+tweak))
  69. if i%2 == 0 {
  70. state.SetState(addr, common.Hash{i, i, i, 0}, common.Hash{})
  71. state.SetState(addr, common.Hash{i, i, i, tweak}, common.Hash{i, i, i, i, tweak})
  72. }
  73. if i%3 == 0 {
  74. state.SetCode(addr, []byte{i, i, i, i, i, tweak})
  75. }
  76. }
  77. // Modify the transient state.
  78. for i := byte(0); i < 255; i++ {
  79. modify(transState, common.Address{byte(i)}, i, 0)
  80. }
  81. // Write modifications to trie.
  82. transState.IntermediateRoot(false)
  83. // Overwrite all the data with new values in the transient database.
  84. for i := byte(0); i < 255; i++ {
  85. modify(transState, common.Address{byte(i)}, i, 99)
  86. modify(finalState, common.Address{byte(i)}, i, 99)
  87. }
  88. // Commit and cross check the databases.
  89. if _, err := transState.Commit(false); err != nil {
  90. t.Fatalf("failed to commit transition state: %v", err)
  91. }
  92. if _, err := finalState.Commit(false); err != nil {
  93. t.Fatalf("failed to commit final state: %v", err)
  94. }
  95. for _, key := range finalDb.Keys() {
  96. if _, err := transDb.Get(key); err != nil {
  97. val, _ := finalDb.Get(key)
  98. t.Errorf("entry missing from the transition database: %x -> %x", key, val)
  99. }
  100. }
  101. for _, key := range transDb.Keys() {
  102. if _, err := finalDb.Get(key); err != nil {
  103. val, _ := transDb.Get(key)
  104. t.Errorf("extra entry in the transition database: %x -> %x", key, val)
  105. }
  106. }
  107. }
  108. // TestCopy tests that copying a statedb object indeed makes the original and
  109. // the copy independent of each other. This test is a regression test against
  110. // https://github.com/ethereum/go-ethereum/pull/15549.
  111. func TestCopy(t *testing.T) {
  112. // Create a random state test to copy and modify "independently"
  113. orig, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase()))
  114. for i := byte(0); i < 255; i++ {
  115. obj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  116. obj.AddBalance(big.NewInt(int64(i)))
  117. orig.updateStateObject(obj)
  118. }
  119. orig.Finalise(false)
  120. // Copy the state, modify both in-memory
  121. copy := orig.Copy()
  122. for i := byte(0); i < 255; i++ {
  123. origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  124. copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  125. origObj.AddBalance(big.NewInt(2 * int64(i)))
  126. copyObj.AddBalance(big.NewInt(3 * int64(i)))
  127. orig.updateStateObject(origObj)
  128. copy.updateStateObject(copyObj)
  129. }
  130. // Finalise the changes on both concurrently
  131. done := make(chan struct{})
  132. go func() {
  133. orig.Finalise(true)
  134. close(done)
  135. }()
  136. copy.Finalise(true)
  137. <-done
  138. // Verify that the two states have been updated independently
  139. for i := byte(0); i < 255; i++ {
  140. origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  141. copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  142. if want := big.NewInt(3 * int64(i)); origObj.Balance().Cmp(want) != 0 {
  143. t.Errorf("orig obj %d: balance mismatch: have %v, want %v", i, origObj.Balance(), want)
  144. }
  145. if want := big.NewInt(4 * int64(i)); copyObj.Balance().Cmp(want) != 0 {
  146. t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, copyObj.Balance(), want)
  147. }
  148. }
  149. }
  150. func TestSnapshotRandom(t *testing.T) {
  151. config := &quick.Config{MaxCount: 1000}
  152. err := quick.Check((*snapshotTest).run, config)
  153. if cerr, ok := err.(*quick.CheckError); ok {
  154. test := cerr.In[0].(*snapshotTest)
  155. t.Errorf("%v:\n%s", test.err, test)
  156. } else if err != nil {
  157. t.Error(err)
  158. }
  159. }
  160. // A snapshotTest checks that reverting StateDB snapshots properly undoes all changes
  161. // captured by the snapshot. Instances of this test with pseudorandom content are created
  162. // by Generate.
  163. //
  164. // The test works as follows:
  165. //
  166. // A new state is created and all actions are applied to it. Several snapshots are taken
  167. // in between actions. The test then reverts each snapshot. For each snapshot the actions
  168. // leading up to it are replayed on a fresh, empty state. The behaviour of all public
  169. // accessor methods on the reverted state must match the return value of the equivalent
  170. // methods on the replayed state.
  171. type snapshotTest struct {
  172. addrs []common.Address // all account addresses
  173. actions []testAction // modifications to the state
  174. snapshots []int // actions indexes at which snapshot is taken
  175. err error // failure details are reported through this field
  176. }
  177. type testAction struct {
  178. name string
  179. fn func(testAction, *StateDB)
  180. args []int64
  181. noAddr bool
  182. }
  183. // newTestAction creates a random action that changes state.
  184. func newTestAction(addr common.Address, r *rand.Rand) testAction {
  185. actions := []testAction{
  186. {
  187. name: "SetBalance",
  188. fn: func(a testAction, s *StateDB) {
  189. s.SetBalance(addr, big.NewInt(a.args[0]))
  190. },
  191. args: make([]int64, 1),
  192. },
  193. {
  194. name: "AddBalance",
  195. fn: func(a testAction, s *StateDB) {
  196. s.AddBalance(addr, big.NewInt(a.args[0]))
  197. },
  198. args: make([]int64, 1),
  199. },
  200. {
  201. name: "SetNonce",
  202. fn: func(a testAction, s *StateDB) {
  203. s.SetNonce(addr, uint64(a.args[0]))
  204. },
  205. args: make([]int64, 1),
  206. },
  207. {
  208. name: "SetState",
  209. fn: func(a testAction, s *StateDB) {
  210. var key, val common.Hash
  211. binary.BigEndian.PutUint16(key[:], uint16(a.args[0]))
  212. binary.BigEndian.PutUint16(val[:], uint16(a.args[1]))
  213. s.SetState(addr, key, val)
  214. },
  215. args: make([]int64, 2),
  216. },
  217. {
  218. name: "SetCode",
  219. fn: func(a testAction, s *StateDB) {
  220. code := make([]byte, 16)
  221. binary.BigEndian.PutUint64(code, uint64(a.args[0]))
  222. binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))
  223. s.SetCode(addr, code)
  224. },
  225. args: make([]int64, 2),
  226. },
  227. {
  228. name: "CreateAccount",
  229. fn: func(a testAction, s *StateDB) {
  230. s.CreateAccount(addr)
  231. },
  232. },
  233. {
  234. name: "Suicide",
  235. fn: func(a testAction, s *StateDB) {
  236. s.Suicide(addr)
  237. },
  238. },
  239. {
  240. name: "AddRefund",
  241. fn: func(a testAction, s *StateDB) {
  242. s.AddRefund(uint64(a.args[0]))
  243. },
  244. args: make([]int64, 1),
  245. noAddr: true,
  246. },
  247. {
  248. name: "AddLog",
  249. fn: func(a testAction, s *StateDB) {
  250. data := make([]byte, 2)
  251. binary.BigEndian.PutUint16(data, uint16(a.args[0]))
  252. s.AddLog(&types.Log{Address: addr, Data: data})
  253. },
  254. args: make([]int64, 1),
  255. },
  256. }
  257. action := actions[r.Intn(len(actions))]
  258. var nameargs []string
  259. if !action.noAddr {
  260. nameargs = append(nameargs, addr.Hex())
  261. }
  262. for _, i := range action.args {
  263. action.args[i] = rand.Int63n(100)
  264. nameargs = append(nameargs, fmt.Sprint(action.args[i]))
  265. }
  266. action.name += strings.Join(nameargs, ", ")
  267. return action
  268. }
  269. // Generate returns a new snapshot test of the given size. All randomness is
  270. // derived from r.
  271. func (*snapshotTest) Generate(r *rand.Rand, size int) reflect.Value {
  272. // Generate random actions.
  273. addrs := make([]common.Address, 50)
  274. for i := range addrs {
  275. addrs[i][0] = byte(i)
  276. }
  277. actions := make([]testAction, size)
  278. for i := range actions {
  279. addr := addrs[r.Intn(len(addrs))]
  280. actions[i] = newTestAction(addr, r)
  281. }
  282. // Generate snapshot indexes.
  283. nsnapshots := int(math.Sqrt(float64(size)))
  284. if size > 0 && nsnapshots == 0 {
  285. nsnapshots = 1
  286. }
  287. snapshots := make([]int, nsnapshots)
  288. snaplen := len(actions) / nsnapshots
  289. for i := range snapshots {
  290. // Try to place the snapshots some number of actions apart from each other.
  291. snapshots[i] = (i * snaplen) + r.Intn(snaplen)
  292. }
  293. return reflect.ValueOf(&snapshotTest{addrs, actions, snapshots, nil})
  294. }
  295. func (test *snapshotTest) String() string {
  296. out := new(bytes.Buffer)
  297. sindex := 0
  298. for i, action := range test.actions {
  299. if len(test.snapshots) > sindex && i == test.snapshots[sindex] {
  300. fmt.Fprintf(out, "---- snapshot %d ----\n", sindex)
  301. sindex++
  302. }
  303. fmt.Fprintf(out, "%4d: %s\n", i, action.name)
  304. }
  305. return out.String()
  306. }
  307. func (test *snapshotTest) run() bool {
  308. // Run all actions and create snapshots.
  309. var (
  310. state, _ = New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase()))
  311. snapshotRevs = make([]int, len(test.snapshots))
  312. sindex = 0
  313. )
  314. for i, action := range test.actions {
  315. if len(test.snapshots) > sindex && i == test.snapshots[sindex] {
  316. snapshotRevs[sindex] = state.Snapshot()
  317. sindex++
  318. }
  319. action.fn(action, state)
  320. }
  321. // Revert all snapshots in reverse order. Each revert must yield a state
  322. // that is equivalent to fresh state with all actions up the snapshot applied.
  323. for sindex--; sindex >= 0; sindex-- {
  324. checkstate, _ := New(common.Hash{}, state.Database())
  325. for _, action := range test.actions[:test.snapshots[sindex]] {
  326. action.fn(action, checkstate)
  327. }
  328. state.RevertToSnapshot(snapshotRevs[sindex])
  329. if err := test.checkEqual(state, checkstate); err != nil {
  330. test.err = fmt.Errorf("state mismatch after revert to snapshot %d\n%v", sindex, err)
  331. return false
  332. }
  333. }
  334. return true
  335. }
  336. // checkEqual checks that methods of state and checkstate return the same values.
  337. func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
  338. for _, addr := range test.addrs {
  339. var err error
  340. checkeq := func(op string, a, b interface{}) bool {
  341. if err == nil && !reflect.DeepEqual(a, b) {
  342. err = fmt.Errorf("got %s(%s) == %v, want %v", op, addr.Hex(), a, b)
  343. return false
  344. }
  345. return true
  346. }
  347. // Check basic accessor methods.
  348. checkeq("Exist", state.Exist(addr), checkstate.Exist(addr))
  349. checkeq("HasSuicided", state.HasSuicided(addr), checkstate.HasSuicided(addr))
  350. checkeq("GetBalance", state.GetBalance(addr), checkstate.GetBalance(addr))
  351. checkeq("GetNonce", state.GetNonce(addr), checkstate.GetNonce(addr))
  352. checkeq("GetCode", state.GetCode(addr), checkstate.GetCode(addr))
  353. checkeq("GetCodeHash", state.GetCodeHash(addr), checkstate.GetCodeHash(addr))
  354. checkeq("GetCodeSize", state.GetCodeSize(addr), checkstate.GetCodeSize(addr))
  355. // Check storage.
  356. if obj := state.getStateObject(addr); obj != nil {
  357. state.ForEachStorage(addr, func(key, val common.Hash) bool {
  358. return checkeq("GetState("+key.Hex()+")", val, checkstate.GetState(addr, key))
  359. })
  360. checkstate.ForEachStorage(addr, func(key, checkval common.Hash) bool {
  361. return checkeq("GetState("+key.Hex()+")", state.GetState(addr, key), checkval)
  362. })
  363. }
  364. if err != nil {
  365. return err
  366. }
  367. }
  368. if state.GetRefund() != checkstate.GetRefund() {
  369. return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d",
  370. state.GetRefund(), checkstate.GetRefund())
  371. }
  372. if !reflect.DeepEqual(state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{})) {
  373. return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v",
  374. state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{}))
  375. }
  376. return nil
  377. }
  378. func (s *StateSuite) TestTouchDelete(c *check.C) {
  379. s.state.GetOrNewStateObject(common.Address{})
  380. root, _ := s.state.Commit(false)
  381. s.state.Reset(root)
  382. snapshot := s.state.Snapshot()
  383. s.state.AddBalance(common.Address{}, new(big.Int))
  384. if len(s.state.journal.dirties) != 1 {
  385. c.Fatal("expected one dirty state object")
  386. }
  387. s.state.RevertToSnapshot(snapshot)
  388. if len(s.state.journal.dirties) != 0 {
  389. c.Fatal("expected no dirty state object")
  390. }
  391. }
  392. // TestCopyOfCopy tests that modified objects are carried over to the copy, and the copy of the copy.
  393. // See https://github.com/ethereum/go-ethereum/pull/15225#issuecomment-380191512
  394. func TestCopyOfCopy(t *testing.T) {
  395. sdb, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase()))
  396. addr := common.HexToAddress("aaaa")
  397. sdb.SetBalance(addr, big.NewInt(42))
  398. if got := sdb.Copy().GetBalance(addr).Uint64(); got != 42 {
  399. t.Fatalf("1st copy fail, expected 42, got %v", got)
  400. }
  401. if got := sdb.Copy().Copy().GetBalance(addr).Uint64(); got != 42 {
  402. t.Fatalf("2nd copy fail, expected 42, got %v", got)
  403. }
  404. }