statedb_test.go 4.4 KB

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