statedb_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. )
  23. // Tests that updating a state trie does not leak any database writes prior to
  24. // actually committing the state.
  25. func TestUpdateLeaks(t *testing.T) {
  26. // Create an empty state database
  27. db, _ := ethdb.NewMemDatabase()
  28. state, _ := New(common.Hash{}, db)
  29. // Update it with some accounts
  30. for i := byte(0); i < 255; i++ {
  31. obj := state.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  32. obj.AddBalance(big.NewInt(int64(11 * i)))
  33. obj.SetNonce(uint64(42 * i))
  34. if i%2 == 0 {
  35. obj.SetState(common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i}))
  36. }
  37. if i%3 == 0 {
  38. obj.SetCode([]byte{i, i, i, i, i})
  39. }
  40. state.UpdateStateObject(obj)
  41. }
  42. // Ensure that no data was leaked into the database
  43. for _, key := range db.Keys() {
  44. value, _ := db.Get(key)
  45. t.Errorf("State leaked into database: %x -> %x", key, value)
  46. }
  47. }
  48. // Tests that no intermediate state of an object is stored into the database,
  49. // only the one right before the commit.
  50. func TestIntermediateLeaks(t *testing.T) {
  51. // Create two state databases, one transitioning to the final state, the other final from the beginning
  52. transDb, _ := ethdb.NewMemDatabase()
  53. finalDb, _ := ethdb.NewMemDatabase()
  54. transState, _ := New(common.Hash{}, transDb)
  55. finalState, _ := New(common.Hash{}, finalDb)
  56. // Update the states with some objects
  57. for i := byte(0); i < 255; i++ {
  58. // Create a new state object with some data into the transition database
  59. obj := transState.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  60. obj.SetBalance(big.NewInt(int64(11 * i)))
  61. obj.SetNonce(uint64(42 * i))
  62. if i%2 == 0 {
  63. obj.SetState(common.BytesToHash([]byte{i, i, i, 0}), common.BytesToHash([]byte{i, i, i, i, 0}))
  64. }
  65. if i%3 == 0 {
  66. obj.SetCode([]byte{i, i, i, i, i, 0})
  67. }
  68. transState.UpdateStateObject(obj)
  69. // Overwrite all the data with new values in the transition database
  70. obj.SetBalance(big.NewInt(int64(11*i + 1)))
  71. obj.SetNonce(uint64(42*i + 1))
  72. if i%2 == 0 {
  73. obj.SetState(common.BytesToHash([]byte{i, i, i, 0}), common.Hash{})
  74. obj.SetState(common.BytesToHash([]byte{i, i, i, 1}), common.BytesToHash([]byte{i, i, i, i, 1}))
  75. }
  76. if i%3 == 0 {
  77. obj.SetCode([]byte{i, i, i, i, i, 1})
  78. }
  79. transState.UpdateStateObject(obj)
  80. // Create the final state object directly in the final database
  81. obj = finalState.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
  82. obj.SetBalance(big.NewInt(int64(11*i + 1)))
  83. obj.SetNonce(uint64(42*i + 1))
  84. if i%2 == 0 {
  85. obj.SetState(common.BytesToHash([]byte{i, i, i, 1}), common.BytesToHash([]byte{i, i, i, i, 1}))
  86. }
  87. if i%3 == 0 {
  88. obj.SetCode([]byte{i, i, i, i, i, 1})
  89. }
  90. finalState.UpdateStateObject(obj)
  91. }
  92. if _, err := transState.Commit(); err != nil {
  93. t.Fatalf("failed to commit transition state: %v", err)
  94. }
  95. if _, err := finalState.Commit(); err != nil {
  96. t.Fatalf("failed to commit final state: %v", err)
  97. }
  98. // Cross check the databases to ensure they are the same
  99. for _, key := range finalDb.Keys() {
  100. if _, err := transDb.Get(key); err != nil {
  101. val, _ := finalDb.Get(key)
  102. t.Errorf("entry missing from the transition database: %x -> %x", key, val)
  103. }
  104. }
  105. for _, key := range transDb.Keys() {
  106. if _, err := finalDb.Get(key); err != nil {
  107. val, _ := transDb.Get(key)
  108. t.Errorf("extra entry in the transition database: %x -> %x", key, val)
  109. }
  110. }
  111. }