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