diff --git a/core/chain_makers.go b/core/chain_makers.go
index 5a8f380a36..584d2faa7f 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -185,11 +185,15 @@ func GenerateChain(parent *types.Block, db ethdb.Database, n int, gen func(int,
panic(fmt.Sprintf("state write error: %v", err))
}
h.Root = root
+ for _, receipt := range b.receipts {
+ fmt.Printf("%+v\n", receipt)
+ }
return types.NewBlock(h, b.txs, b.uncles, b.receipts), b.receipts
}
for i := 0; i < n; i++ {
header := makeHeader(parent, statedb)
block, receipt := genblock(i, header)
+ fmt.Printf("%x\n\n", block.Header().ReceiptHash)
blocks[i] = block
receipts[i] = receipt
parent = block
diff --git a/core/index_test.go b/core/index_test.go
new file mode 100644
index 0000000000..9837f97b99
--- /dev/null
+++ b/core/index_test.go
@@ -0,0 +1,160 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package core
+
+import (
+ "bytes"
+ "fmt"
+ "math/big"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/core/vm"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/event"
+ "github.com/ethereum/go-ethereum/params"
+ "github.com/ethereum/go-ethereum/trie"
+)
+
+// blackHoleContract is a trivial Ethereum contract that can just store some data
+// points in its state trie. It's goal is to have a means to test functionality
+// depending on evolving state roots.
+//
+// contract BlackHole {
+// mapping (int256 => uint) public data;
+//
+// function set(int256 key, uint value) {
+// data[key] = value;
+// }
+// }
+var blackHoleContract = common.Hex2Bytes("6060604052605f8060106000396000f3606060405260e060020a60003504639398e0cd81146024578063a22c554014603b575b005b605560043560006020819052908152604090205481565b600435600090815260208190526040902060243590556022565b6060908152602090f3")
+var blackHoleSetter = common.Hex2Bytes("a22c5540")
+
+// Tests that state trie indexes are properly constructed for an entire chain of
+// imported blocks, cross referencing between various state roots, as well as
+// making sure that state updates do not lose reference entries.
+func TestChainIndex(t *testing.T) {
+ // Configure the test chain and ensure we have enough funds to play with
+ var (
+ key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
+ key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
+ key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
+ addr1 = crypto.PubkeyToAddress(key1.PublicKey)
+ addr2 = crypto.PubkeyToAddress(key2.PublicKey)
+ addr3 = crypto.PubkeyToAddress(key3.PublicKey)
+ db, _ = ethdb.NewMemDatabase()
+ )
+ genesis := WriteGenesisBlockForTesting(db, GenesisAccount{addr1, big.NewInt(10000000000)})
+
+ // Generate a chain will all kinds of events happening in it
+ var contract common.Address
+
+ chain, _ := GenerateChain(genesis, db, 8, func(i int, gen *BlockGen) {
+ switch i {
+ case 0:
+ // In block 1, addr1 sends addr2 some ether.
+ tx, _ := types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(key1)
+ gen.AddTx(tx)
+ case 1:
+ // In block 2, addr1 sends some more ether to addr2.
+ // addr2 passes it on to addr3.
+ tx1, _ := types.NewTransaction(gen.TxNonce(addr1), addr2, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key1)
+ tx2, _ := types.NewTransaction(gen.TxNonce(addr2), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key2)
+ gen.AddTx(tx1)
+ gen.AddTx(tx2)
+ case 2:
+ // Block 3 is empty but was mined by addr3.
+ gen.SetCoinbase(addr3)
+ gen.SetExtra([]byte("yeehaw"))
+ case 3:
+ // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
+ b2 := gen.PrevBlock(1).Header()
+ b2.Extra = []byte("foo")
+ gen.AddUncle(b2)
+ b3 := gen.PrevBlock(2).Header()
+ b3.Extra = []byte("foo")
+ gen.AddUncle(b3)
+ case 4:
+ // In block 5, we create a simple storage contract to add data entries to
+ tx, _ := types.NewContractCreation(gen.TxNonce(addr1), big.NewInt(31415), params.GenesisGasLimit, big.NewInt(1), blackHoleContract).SignECDSA(key1)
+ contract = crypto.CreateAddress(addr1, tx.Nonce())
+ gen.AddTx(tx)
+ case 5:
+ // In block 6, we store a single entry into the storage to check single update indexing
+ key := common.Hex2BytesFixed("01", 32)
+ val := common.Hex2BytesFixed("02", 32)
+
+ tx, _ := types.NewTransaction(gen.TxNonce(addr1), contract, nil, params.GenesisGasLimit, nil, append(append(blackHoleSetter, key...), val...)).SignECDSA(key1)
+ gen.AddTx(tx)
+ case 6:
+ // In block 7, we store a lot of entries into the storage to check multi update indexing
+ for i := int64(0); i < 50; i++ {
+ key := common.Hex2BytesFixed(common.Bytes2Hex(big.NewInt(i).Bytes()), 32)
+ val := common.Hex2BytesFixed(common.Bytes2Hex(big.NewInt(i+2).Bytes()), 32)
+
+ tx, _ := types.NewTransaction(gen.TxNonce(addr1), contract, nil, big.NewInt(45000), nil, append(append(blackHoleSetter, key...), val...)).SignECDSA(key1)
+ gen.AddTx(tx)
+ }
+ }
+ })
+ // Import the chain. This runs all block validation rules.
+ evmux := &event.TypeMux{}
+
+ vm.Debug = true
+
+ blockchain, _ := NewBlockChain(db, FakePow{}, evmux)
+ if i, err := blockchain.InsertChain(chain); err != nil {
+ t.Fatalf("block %d: insert failed: %v\n", i, err)
+ return
+ }
+ // Iterate over all the blocks and check the state trie indexes
+ indexes := make(map[string]struct{})
+
+ for block := uint64(0); block <= blockchain.CurrentBlock().NumberU64(); block++ {
+ // Gather all the indexes that should be present in the database
+ root := blockchain.GetBlockByNumber(block).Root()
+
+ stateDb, err := state.New(root, db)
+ if err != nil {
+ t.Fatalf("failed to create state trie at %x: %v", root, err)
+ }
+ fmt.Println(blockchain.GetBlockByNumber(block).Transactions())
+ for it := state.NewNodeIterator(stateDb); it.Next(); {
+ if (it.Hash != common.Hash{}) && (it.Parent != common.Hash{}) {
+ fmt.Printf("%d: %x -> %x\n", block, it.Parent, it.Hash)
+ indexes[string(trie.ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
+ }
+ }
+ }
+ // Cross check the indexes and the database itself
+ fmt.Println(len(indexes))
+ for index, _ := range indexes {
+ if _, err := db.Get([]byte(index)); err != nil {
+ t.Errorf("failed to retrieve reported index %x: %v", index, err)
+ }
+ }
+ for _, key := range db.Keys() {
+ if bytes.HasPrefix(key, trie.ParentReferenceIndexPrefix) {
+ if _, ok := indexes[string(key)]; !ok {
+ t.Errorf("index entry not reported %x", key)
+ }
+ }
+ }
+}
diff --git a/core/state/index_test.go b/core/state/index_test.go
new file mode 100644
index 0000000000..25f1f853f2
--- /dev/null
+++ b/core/state/index_test.go
@@ -0,0 +1,57 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package state
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/trie"
+)
+
+// Tests that all index entries are stored in the database after a state commit.
+func TestStateIndex(t *testing.T) {
+ // Create some arbitrary test state to iterate
+ db, root, _ := makeTestState()
+
+ state, err := New(root, db)
+ if err != nil {
+ t.Fatalf("failed to create state trie at %x: %v", root, err)
+ }
+ // Gather all the indexes that should be present in the database
+ indexes := make(map[string]struct{})
+ for it := NewNodeIterator(state); it.Next(); {
+ if (it.Hash != common.Hash{}) && (it.Parent != common.Hash{}) {
+ indexes[string(trie.ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
+ }
+ }
+ // Cross check the indexes and the database itself
+ for index, _ := range indexes {
+ if _, err := db.Get([]byte(index)); err != nil {
+ t.Errorf("failed to retrieve reported index %x: %v", index, err)
+ }
+ }
+ for _, key := range db.(*ethdb.MemDatabase).Keys() {
+ if bytes.HasPrefix(key, trie.ParentReferenceIndexPrefix) {
+ if _, ok := indexes[string(key)]; !ok {
+ t.Errorf("index entry not reported %x", key)
+ }
+ }
+ }
+}
diff --git a/core/state/iterator.go b/core/state/iterator.go
new file mode 100644
index 0000000000..a051e5e01a
--- /dev/null
+++ b/core/state/iterator.go
@@ -0,0 +1,143 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package state
+
+import (
+ "bytes"
+ "fmt"
+ "math/big"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/rlp"
+ "github.com/ethereum/go-ethereum/trie"
+)
+
+// NodeIterator is an iterator to traverse the entire state trie post-order,
+// including all of the contract code and contract state tries.
+type NodeIterator struct {
+ state *StateDB // State being iterated
+
+ stateIt *trie.NodeIterator // Primary iterator for the global state trie
+ dataIt *trie.NodeIterator // Secondary iterator for the data trie of a contract
+
+ accountHash common.Hash // Hash of the node containing the account
+ codeHash common.Hash // Hash of the contract source code
+ code []byte // Source code associated with a contract
+
+ Hash common.Hash // Hash of the current entry being iterated (nil if not standalone)
+ Entry interface{} // Current state entry being iterated (internal representation)
+ Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
+}
+
+// NewNodeIterator creates an post-order state node iterator.
+func NewNodeIterator(state *StateDB) *NodeIterator {
+ return &NodeIterator{
+ state: state,
+ }
+}
+
+// Next moves the iterator to the next node, returning whether there are any
+// further nodes.
+func (it *NodeIterator) Next() bool {
+ it.step()
+ return it.retrieve()
+}
+
+// step moves the iterator to the next entry of the state trie.
+func (it *NodeIterator) step() {
+ // Abort if we reached the end of the iteration
+ if it.state == nil {
+ return
+ }
+ // Initialize the iterator if we've just started
+ if it.stateIt == nil {
+ it.stateIt = trie.NewNodeIterator(it.state.trie.Trie)
+ }
+ // If we had data nodes previously, we surely have at least state nodes
+ if it.dataIt != nil {
+ if cont := it.dataIt.Next(); !cont {
+ it.dataIt = nil
+ }
+ return
+ }
+ // If we had source code previously, discard that
+ if it.code != nil {
+ it.code = nil
+ return
+ }
+ // Step to the next state trie node, terminating if we're out of nodes
+ if cont := it.stateIt.Next(); !cont {
+ it.state, it.stateIt = nil, nil
+ return
+ }
+ // If the state trie node is an internal entry, leave as is
+ if !it.stateIt.Leaf {
+ return
+ }
+ // Otherwise we've reached an account node, initiate data iteration
+ var account struct {
+ Nonce uint64
+ Balance *big.Int
+ Root common.Hash
+ CodeHash []byte
+ }
+ err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account)
+ if err != nil {
+ panic(err)
+ }
+ dataTrie, err := trie.New(account.Root, it.state.db)
+ if err != nil {
+ panic(err)
+ }
+ it.dataIt = trie.NewNodeIterator(dataTrie)
+ if !it.dataIt.Next() {
+ it.dataIt = nil
+ }
+ if bytes.Compare(account.CodeHash, emptyCodeHash) != 0 {
+ it.codeHash = common.BytesToHash(account.CodeHash)
+ it.code, err = it.state.db.Get(account.CodeHash)
+ if err != nil {
+ panic(fmt.Sprintf("code %x: %v", account.CodeHash, err))
+ }
+ }
+ it.accountHash = it.stateIt.Parent
+}
+
+// retrieve pulls and caches the current state entry the iterator is traversing.
+// The method returns whether there are any more data left for inspection.
+func (it *NodeIterator) retrieve() bool {
+ // Clear out any previously set values
+ it.Hash, it.Entry = common.Hash{}, nil
+
+ // If the iteration's done, return no available data
+ if it.state == nil {
+ return false
+ }
+ // Otherwise retrieve the current entry
+ switch {
+ case it.dataIt != nil:
+ it.Hash, it.Entry, it.Parent = it.dataIt.Hash, it.dataIt.Node, it.dataIt.Parent
+ if it.Parent == (common.Hash{}) {
+ it.Parent = it.accountHash
+ }
+ case it.code != nil:
+ it.Hash, it.Entry, it.Parent = it.codeHash, it.code, it.accountHash
+ case it.stateIt != nil:
+ it.Hash, it.Entry, it.Parent = it.stateIt.Hash, it.stateIt.Node, it.stateIt.Parent
+ }
+ return true
+}
diff --git a/core/state/iterator_test.go b/core/state/iterator_test.go
new file mode 100644
index 0000000000..df0135296a
--- /dev/null
+++ b/core/state/iterator_test.go
@@ -0,0 +1,61 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package state
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/trie"
+)
+
+// Tests that the node iterator indeed walks over the entire database contents.
+func TestNodeIteratorCoverage(t *testing.T) {
+ // Create some arbitrary test state to iterate
+ db, root, _ := makeTestState()
+
+ state, err := New(root, db)
+ if err != nil {
+ t.Fatalf("failed to create state trie at %x: %v", root, err)
+ }
+ // Gather all the node hashes found by the iterator
+ hashes := make(map[common.Hash]struct{})
+ for it := NewNodeIterator(state); it.Next(); {
+ if it.Hash != (common.Hash{}) {
+ hashes[it.Hash] = struct{}{}
+ }
+ }
+ // Cross check the hashes and the database itself
+ for hash, _ := range hashes {
+ if _, err := db.Get(hash.Bytes()); err != nil {
+ t.Errorf("failed to retrieve reported node %x: %v", hash, err)
+ }
+ }
+ for _, key := range db.(*ethdb.MemDatabase).Keys() {
+ if bytes.HasPrefix(key, []byte("secure-key-")) {
+ continue
+ }
+ if bytes.HasPrefix(key, trie.ParentReferenceIndexPrefix) {
+ continue
+ }
+ if _, ok := hashes[common.BytesToHash(key)]; !ok {
+ t.Errorf("state entry not reported %x", key)
+ }
+ }
+}
diff --git a/core/state/state_object.go b/core/state/state_object.go
index 47546112f4..292f32f301 100644
--- a/core/state/state_object.go
+++ b/core/state/state_object.go
@@ -118,7 +118,7 @@ func (c *StateObject) setAddr(addr []byte, value common.Hash) {
// if RLPing failed we better panic and not fail silently. This would be considered a consensus issue
panic(err)
}
- c.trie.Update(addr, v)
+ c.trie.UpdateIndexed(addr, v, nil)
}
func (self *StateObject) Storage() Storage {
diff --git a/core/state/statedb.go b/core/state/statedb.go
index 4133210577..28ed216fce 100644
--- a/core/state/statedb.go
+++ b/core/state/statedb.go
@@ -206,15 +206,17 @@ func (self *StateDB) Delete(addr common.Address) bool {
// Update the given state object and apply it to state trie
func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
+ refs := [][]byte{stateObject.Root()}
if len(stateObject.code) > 0 {
self.db.Put(stateObject.codeHash, stateObject.code)
+ refs = append(refs, stateObject.codeHash)
}
addr := stateObject.Address()
data, err := rlp.EncodeToBytes(stateObject)
if err != nil {
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
}
- self.trie.Update(addr[:], data)
+ self.trie.UpdateIndexed(addr[:], data, refs)
}
// Delete the given state object and delete it from the state trie
diff --git a/core/state/sync_test.go b/core/state/sync_test.go
index 0dab372ba0..5e59c8f9cd 100644
--- a/core/state/sync_test.go
+++ b/core/state/sync_test.go
@@ -18,10 +18,12 @@ package state
import (
"bytes"
+ "fmt"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie"
)
@@ -42,7 +44,7 @@ func makeTestState() (ethdb.Database, common.Hash, []*testAccount) {
// Fill it with some arbitrary data
accounts := []*testAccount{}
- for i := byte(0); i < 255; i++ {
+ for i := byte(0); i < 96; i++ {
obj := state.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
acc := &testAccount{address: common.BytesToAddress([]byte{i})}
@@ -61,6 +63,9 @@ func makeTestState() (ethdb.Database, common.Hash, []*testAccount) {
}
root, _ := state.Commit()
+ // Remove any potentially cached data from the test state creation
+ trie.ClearGlobalCache()
+
// Return the generated state
return db, root, accounts
}
@@ -68,9 +73,21 @@ func makeTestState() (ethdb.Database, common.Hash, []*testAccount) {
// checkStateAccounts cross references a reconstructed state with an expected
// account array.
func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accounts []*testAccount) {
- state, _ := New(root, db)
- for i, acc := range accounts {
+ // Remove any potentially cached data from the state synchronisation
+ trie.ClearGlobalCache()
+ // Check root availability and state contents
+ state, err := New(root, db)
+ if err != nil {
+ t.Fatalf("failed to create state trie at %x: %v", root, err)
+ }
+ if err := checkStateConsistency(db, root); err != nil {
+ t.Fatalf("inconsistent state trie at %x: %v", root, err)
+ }
+ if err := checkStateIndex(db, root); err != nil {
+ t.Fatalf("index error at %x: %v", root, err)
+ }
+ for i, acc := range accounts {
if balance := state.GetBalance(acc.address); balance.Cmp(acc.balance) != 0 {
t.Errorf("account %d: balance mismatch: have %v, want %v", i, balance, acc.balance)
}
@@ -83,6 +100,55 @@ func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accou
}
}
+// checkStateConsistency checks that all nodes in a state trie and indeed present.
+func checkStateConsistency(db ethdb.Database, root common.Hash) (failure error) {
+ // Capture any panics by the iterator
+ defer func() {
+ if r := recover(); r != nil {
+ failure = fmt.Errorf("%v", r)
+ }
+ }()
+ // Remove any potentially cached data from the test state creation or previous checks
+ trie.ClearGlobalCache()
+
+ // Create and iterate a state trie rooted in a sub-node
+ if _, err := db.Get(root.Bytes()); err != nil {
+ return
+ }
+ state, err := New(root, db)
+ if err != nil {
+ return
+ }
+ for it := NewNodeIterator(state); it.Next(); {
+ }
+ return nil
+}
+
+// checkStateIndex iterates over the entire state trie and checks that all required
+// database indexes have been generated by the synchronizer.
+func checkStateIndex(db ethdb.Database, root common.Hash) error {
+ // Remove any potentially cached data from the test state creation or previous checks
+ trie.ClearGlobalCache()
+
+ // Create and iterate a state trie rooted in a sub-node
+ if _, err := db.Get(root.Bytes()); err != nil {
+ return err
+ }
+ state, err := New(root, db)
+ if err != nil {
+ return err
+ }
+ for it := NewNodeIterator(state); it.Next(); {
+ if (it.Hash != common.Hash{}) && (it.Parent != common.Hash{}) {
+ parentRef := trie.ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes())
+ if _, err := db.Get(parentRef); err != nil {
+ return fmt.Errorf("parent reference lookup %x: %v", parentRef, err)
+ }
+ }
+ }
+ return nil
+}
+
// Tests that an empty state is not scheduled for syncing.
func TestEmptyStateSync(t *testing.T) {
empty := common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
@@ -236,3 +302,65 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
// Cross check that the two states are in sync
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
}
+
+// Tests that at any point in time during a sync, only complete sub-tries are in
+// the database.
+func TestIncompleteStateSync(t *testing.T) {
+ // Create a random state to copy
+ srcDb, srcRoot, srcAccounts := makeTestState()
+
+ // Create a destination state and sync with the scheduler
+ dstDb, _ := ethdb.NewMemDatabase()
+ sched := NewStateSync(srcRoot, dstDb)
+
+ added := []common.Hash{}
+ queue := append([]common.Hash{}, sched.Missing(1)...)
+ for len(queue) > 0 {
+ // Fetch a batch of state nodes
+ results := make([]trie.SyncResult, len(queue))
+ for i, hash := range queue {
+ data, err := srcDb.Get(hash.Bytes())
+ if err != nil {
+ t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
+ }
+ results[i] = trie.SyncResult{hash, data}
+ }
+ // Process each of the state nodes
+ if index, err := sched.Process(results); err != nil {
+ t.Fatalf("failed to process result #%d: %v", index, err)
+ }
+ for _, result := range results {
+ added = append(added, result.Hash)
+ }
+ // Check that all known sub-tries in the synced state is complete
+ for _, root := range added {
+ // Skim through the accounts and make sure the root hash is not a code node
+ codeHash := false
+ for _, acc := range srcAccounts {
+ if bytes.Compare(root.Bytes(), crypto.Sha3(acc.code)) == 0 {
+ codeHash = true
+ break
+ }
+ }
+ // If the root is a real trie node, check consistency
+ if !codeHash {
+ if err := checkStateConsistency(dstDb, root); err != nil {
+ t.Fatalf("state inconsistent: %v", err)
+ }
+ }
+ }
+ // Fetch the next batch to retrieve
+ queue = append(queue[:0], sched.Missing(1)...)
+ }
+ // Sanity check that removing any node from the database is detected
+ for _, node := range added[1:] {
+ key := node.Bytes()
+ value, _ := dstDb.Get(key)
+
+ dstDb.Delete(key)
+ if err := checkStateConsistency(dstDb, added[0]); err == nil {
+ t.Fatalf("trie inconsistency not caught, missing: %x", key)
+ }
+ dstDb.Put(key, value)
+ }
+}
diff --git a/core/types/derive_sha.go b/core/types/derive_sha.go
index 00c42c5bc6..68d803e448 100644
--- a/core/types/derive_sha.go
+++ b/core/types/derive_sha.go
@@ -35,7 +35,7 @@ func DeriveSha(list DerivableList) common.Hash {
for i := 0; i < list.Len(); i++ {
keybuf.Reset()
rlp.Encode(keybuf, uint(i))
- trie.Update(keybuf.Bytes(), list.GetRlp(i))
+ trie.UpdateIndexed(keybuf.Bytes(), list.GetRlp(i), nil)
}
return trie.Hash()
}
diff --git a/core/vm/contract.go b/core/vm/contract.go
index 95417e7471..5981dcca0d 100644
--- a/core/vm/contract.go
+++ b/core/vm/contract.go
@@ -27,6 +27,7 @@ type ContractRef interface {
ReturnGas(*big.Int, *big.Int)
Address() common.Address
SetCode([]byte)
+ EachStorage(cb func(key, value []byte))
}
// Contract represents an ethereum contract in the state database. It contains
@@ -124,3 +125,9 @@ func (self *Contract) SetCallCode(addr *common.Address, code []byte) {
self.Code = code
self.CodeAddr = addr
}
+
+// EachStorage iterates the contract's storage and calls a method for every key
+// value pair.
+func (self *Contract) EachStorage(cb func(key, value []byte)) {
+ self.caller.EachStorage(cb)
+}
diff --git a/core/vm/environment.go b/core/vm/environment.go
index 299d126747..4fee583bfb 100644
--- a/core/vm/environment.go
+++ b/core/vm/environment.go
@@ -121,4 +121,5 @@ type Account interface {
Address() common.Address
ReturnGas(*big.Int, *big.Int)
SetCode([]byte)
+ EachStorage(cb func(key, value []byte))
}
diff --git a/core/vm/jit_test.go b/core/vm/jit_test.go
index aa97e51844..8c50ed0f5c 100644
--- a/core/vm/jit_test.go
+++ b/core/vm/jit_test.go
@@ -125,14 +125,15 @@ type vmBench struct {
type account struct{}
-func (account) SubBalance(amount *big.Int) {}
-func (account) AddBalance(amount *big.Int) {}
-func (account) SetBalance(*big.Int) {}
-func (account) SetNonce(uint64) {}
-func (account) Balance() *big.Int { return nil }
-func (account) Address() common.Address { return common.Address{} }
-func (account) ReturnGas(*big.Int, *big.Int) {}
-func (account) SetCode([]byte) {}
+func (account) SubBalance(amount *big.Int) {}
+func (account) AddBalance(amount *big.Int) {}
+func (account) SetBalance(*big.Int) {}
+func (account) SetNonce(uint64) {}
+func (account) Balance() *big.Int { return nil }
+func (account) Address() common.Address { return common.Address{} }
+func (account) ReturnGas(*big.Int, *big.Int) {}
+func (account) SetCode([]byte) {}
+func (account) EachStorage(cb func(key, value []byte)) {}
func runVmBench(test vmBench, b *testing.B) {
var sender account
diff --git a/core/vm/vm.go b/core/vm/vm.go
index 4b03e55f04..8e07aaa899 100644
--- a/core/vm/vm.go
+++ b/core/vm/vm.go
@@ -376,12 +376,9 @@ func (self *Vm) log(pc uint64, op OpCode, gas, cost *big.Int, memory *Memory, st
stck[i] = new(big.Int).Set(item)
}
storage := make(map[common.Hash][]byte)
- /*
- object := contract.self.(*state.StateObject)
- object.EachStorage(func(k, v []byte) {
- storage[common.BytesToHash(k)] = v
- })
- */
+ contract.self.EachStorage(func(k, v []byte) {
+ storage[common.BytesToHash(k)] = v
+ })
self.env.AddStructLog(StructLog{pc, op, new(big.Int).Set(gas), cost, mem, stck, storage, err})
}
}
diff --git a/trie/index.go b/trie/index.go
new file mode 100644
index 0000000000..705e605ea5
--- /dev/null
+++ b/trie/index.go
@@ -0,0 +1,68 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package trie
+
+import "bytes"
+
+var (
+ // ParentReferenceIndexPrefix is the database key prefix storing parent references index entries
+ ParentReferenceIndexPrefix = []byte("ref-")
+)
+
+// ParentReferenceIndexKey constructs a child->parent database index key.
+func ParentReferenceIndexKey(parent []byte, child []byte) []byte {
+ return append(append(ParentReferenceIndexPrefix, child...), parent...)
+}
+
+// storeParentReferences expands a trie node to find all its stored children and
+// adds a database reference pointing to the parent to permit reference tracking.
+func storeParentReferences(key []byte, node node, db DatabaseWriter) error {
+ switch node := node.(type) {
+ case fullNode:
+ for _, child := range node {
+ if child != nil {
+ if err := storeParentReferences(key, child, db); err != nil {
+ return err
+ }
+ }
+ }
+ case shortNode:
+ if child := node.Val; child != nil {
+ return storeParentReferences(key, child, db)
+ }
+ case hashNode:
+ return db.Put(ParentReferenceIndexKey(key, node), nil)
+
+ case valueNode:
+ for _, child := range node.refs {
+ if bytes.Compare(child, emptyRoot.Bytes()) == 0 {
+ continue // don't index an empty trie
+ }
+ if err := db.Put(ParentReferenceIndexKey(key, child), nil); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+}
+
+// storeParentReferenceEntry manually inserts a parent->child reference entry into
+// the database index without requiring direct, trie-internal descendancy. This is
+// useful to store references to external entities, like accounts or blocks.
+func storeParentReferenceEntry(parent []byte, child []byte, db DatabaseWriter) error {
+ return db.Put(ParentReferenceIndexKey(parent, child), nil)
+}
diff --git a/trie/index_test.go b/trie/index_test.go
new file mode 100644
index 0000000000..1ecf8dd7bf
--- /dev/null
+++ b/trie/index_test.go
@@ -0,0 +1,52 @@
+// Copyright 2015 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package trie
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/ethdb"
+)
+
+// Tests that all index entries are stored in the database after a trie commit.
+func TestTrieIndex(t *testing.T) {
+ // Create some arbitrary test trie to iterate
+ db, trie, _ := makeTestTrie()
+
+ // Gather all the indexes that should be present in the database
+ indexes := make(map[string]struct{})
+ for it := NewNodeIterator(trie); it.Next(); {
+ if (it.Hash != common.Hash{}) && (it.Parent != common.Hash{}) {
+ indexes[string(ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
+ }
+ }
+ // Cross check the indexes and the database itself
+ for index, _ := range indexes {
+ if _, err := db.Get([]byte(index)); err != nil {
+ t.Errorf("failed to retrieve reported index %x: %v", index, err)
+ }
+ }
+ for _, key := range db.(*ethdb.MemDatabase).Keys() {
+ if bytes.HasPrefix(key, ParentReferenceIndexPrefix) {
+ if _, ok := indexes[string(key)]; !ok {
+ t.Errorf("index entry not reported %x", key)
+ }
+ }
+ }
+}
diff --git a/trie/iterator.go b/trie/iterator.go
index 5f205e0817..59ac3c5182 100644
--- a/trie/iterator.go
+++ b/trie/iterator.go
@@ -18,22 +18,27 @@ package trie
import (
"bytes"
+ "fmt"
+ "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
)
+// Iterator is a key-value trie iterator to traverse the data contents.
type Iterator struct {
trie *Trie
- Key []byte
- Value []byte
+ Key []byte // Current data key on which the iterator is positioned on
+ Value []byte // Current data value on which the iterator is positioned on
}
+// NewIterator creates a new key-value iterator.
func NewIterator(trie *Trie) *Iterator {
return &Iterator{trie: trie, Key: nil}
}
+// Next moves the iterator forward with one key-value entry.
func (self *Iterator) Next() bool {
isIterStart := false
if self.Key == nil {
@@ -81,11 +86,11 @@ func (self *Iterator) next(node interface{}, key []byte, isIterStart bool) []byt
switch bytes.Compare([]byte(k), key) {
case 0:
if isIterStart {
- self.Value = vnode
+ self.Value = vnode.Value
return k
}
case 1:
- self.Value = vnode
+ self.Value = vnode.Value
return k
}
} else {
@@ -120,13 +125,13 @@ func (self *Iterator) key(node interface{}) []byte {
// Leaf node
k := remTerm(node.Key)
if vnode, ok := node.Val.(valueNode); ok {
- self.Value = vnode
+ self.Value = vnode.Value
return k
}
return append(k, self.key(node.Val)...)
case fullNode:
if node[16] != nil {
- self.Value = node[16].(valueNode)
+ self.Value = node[16].(valueNode).Value
return []byte{16}
}
for i := 0; i < 16; i++ {
@@ -142,6 +147,126 @@ func (self *Iterator) key(node interface{}) []byte {
}
return self.key(rn)
}
-
return nil
}
+
+// nodeIteratorState represents the iteration state at one particular node of the
+// trie, which can be resumed at a later invocation.
+type nodeIteratorState struct {
+ hash common.Hash // Hash of the node being iterated (nil if not standalone)
+ node node // Trie node being iterated
+ parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
+ child int // Child to be processed next
+}
+
+// NodeIterator is an iterator to traverse the trie post-order.
+type NodeIterator struct {
+ trie *Trie // Trie being iterated
+ stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
+
+ Hash common.Hash // Hash of the current node being iterated (nil if not standalone)
+ Node node // Current node being iterated (internal representation)
+ Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
+ Leaf bool // Flag whether the current node is a value (data) node
+ LeafBlob []byte // Data blob contained within a leaf (otherwise nil)
+}
+
+// NewNodeIterator creates an post-order trie iterator.
+func NewNodeIterator(trie *Trie) *NodeIterator {
+ if bytes.Compare(trie.Root(), emptyRoot.Bytes()) == 0 {
+ return new(NodeIterator)
+ }
+ return &NodeIterator{trie: trie}
+}
+
+// Next moves the iterator to the next node, returning whether there are any
+// further nodes.
+func (it *NodeIterator) Next() bool {
+ it.step()
+ return it.retrieve()
+}
+
+// step moves the iterator to the next node of the trie.
+func (it *NodeIterator) step() {
+ // Abort if we reached the end of the iteration
+ if it.trie == nil {
+ return
+ }
+ // Initialize the iterator if we've just started, or pop off the old node otherwise
+ if len(it.stack) == 0 {
+ it.stack = append(it.stack, &nodeIteratorState{node: it.trie.root, child: -1})
+ if it.stack[0].node == nil {
+ panic(fmt.Sprintf("root node missing: %x", it.trie.Root()))
+ }
+ } else {
+ it.stack = it.stack[:len(it.stack)-1]
+ if len(it.stack) == 0 {
+ it.trie = nil
+ return
+ }
+ }
+ // Continue iteration to the next child
+ for {
+ parent := it.stack[len(it.stack)-1]
+ ancestor := parent.hash
+ if (ancestor == common.Hash{}) {
+ ancestor = parent.parent
+ }
+ if node, ok := parent.node.(fullNode); ok {
+ // Full node, traverse all children, then the node itself
+ if parent.child >= len(node) {
+ break
+ }
+ for parent.child++; parent.child < len(node); parent.child++ {
+ if current := node[parent.child]; current != nil {
+ it.stack = append(it.stack, &nodeIteratorState{node: current, parent: ancestor, child: -1})
+ break
+ }
+ }
+ } else if node, ok := parent.node.(shortNode); ok {
+ // Short node, traverse the pointer singleton child, then the node itself
+ if parent.child >= 0 {
+ break
+ }
+ parent.child++
+ it.stack = append(it.stack, &nodeIteratorState{node: node.Val, parent: ancestor, child: -1})
+ } else if hash, ok := parent.node.(hashNode); ok {
+ // Hash node, resolve the hash child from the database, then the node itself
+ if parent.child >= 0 {
+ break
+ }
+ parent.child++
+
+ node, err := it.trie.resolveHash(hash, nil, nil)
+ if err != nil {
+ panic(err)
+ }
+ it.stack = append(it.stack, &nodeIteratorState{hash: common.BytesToHash(hash), node: node, parent: ancestor, child: -1})
+ } else {
+ break
+ }
+ }
+}
+
+// retrieve pulls and caches the current trie node the iterator is traversing.
+// In case of a value node, the additional leaf blob is also populated with the
+// data contents for external interpretation.
+//
+// The method returns whether there are any more data left for inspection.
+func (it *NodeIterator) retrieve() bool {
+ // Clear out any previously set values
+ it.Hash, it.Node, it.Parent, it.Leaf, it.LeafBlob = common.Hash{}, nil, common.Hash{}, false, nil
+
+ // If the iteration's done, return no available data
+ if it.trie == nil {
+ return false
+ }
+ // Otherwise retrieve the current node and resolve leaf accessors
+ state := it.stack[len(it.stack)-1]
+
+ it.Hash, it.Node, it.Parent = state.hash, state.node, state.parent
+ if node, ok := it.Node.(valueNode); ok {
+ it.Leaf, it.LeafBlob = true, []byte(node.Value)
+ }
+ return true
+}
diff --git a/trie/iterator_test.go b/trie/iterator_test.go
index fdc60b4122..43ee56963d 100644
--- a/trie/iterator_test.go
+++ b/trie/iterator_test.go
@@ -16,7 +16,12 @@
package trie
-import "testing"
+import (
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/ethdb"
+)
func TestIterator(t *testing.T) {
trie := newEmpty()
@@ -32,7 +37,7 @@ func TestIterator(t *testing.T) {
v := make(map[string]bool)
for _, val := range vals {
v[val.k] = false
- trie.Update([]byte(val.k), []byte(val.v))
+ trie.UpdateIndexed([]byte(val.k), []byte(val.v), nil)
}
trie.Commit()
@@ -47,3 +52,30 @@ func TestIterator(t *testing.T) {
}
}
}
+
+// Tests that the node iterator indeed walks over the entire database contents.
+func TestNodeIteratorCoverage(t *testing.T) {
+ // Create some arbitrary test trie to iterate
+ db, trie, _ := makeTestTrie()
+
+ // Gather all the node hashes found by the iterator
+ hashes := make(map[common.Hash]struct{})
+ for it := NewNodeIterator(trie); it.Next(); {
+ if it.Hash != (common.Hash{}) {
+ hashes[it.Hash] = struct{}{}
+ }
+ }
+ // Cross check the hashes and the database itself
+ for hash, _ := range hashes {
+ if _, err := db.Get(hash.Bytes()); err != nil {
+ t.Errorf("failed to retrieve reported node %x: %v", hash, err)
+ }
+ }
+ for _, key := range db.(*ethdb.MemDatabase).Keys() {
+ if len(key) == common.HashLength {
+ if _, ok := hashes[common.BytesToHash(key)]; !ok {
+ t.Errorf("state entry not reported %x", key)
+ }
+ }
+ }
+}
diff --git a/trie/node.go b/trie/node.go
index 0bfa21dc43..01452a8531 100644
--- a/trie/node.go
+++ b/trie/node.go
@@ -38,9 +38,22 @@ type (
Val node
}
hashNode []byte
- valueNode []byte
+ valueNode struct {
+ Value []byte
+ refs [][]byte
+ }
)
+func (n valueNode) EncodeRLP(w io.Writer) error {
+ return rlp.Encode(w, n.Value)
+}
+
+func (n valueNode) DecodeRLP(s *rlp.Stream) (err error) {
+ fmt.Println("Decoding value node")
+ n.Value, err = s.Bytes()
+ return
+}
+
// Pretty printing.
func (n fullNode) String() string { return n.fstring("") }
func (n shortNode) String() string { return n.fstring("") }
@@ -65,7 +78,7 @@ func (n hashNode) fstring(ind string) string {
return fmt.Sprintf("<%x> ", []byte(n))
}
func (n valueNode) fstring(ind string) string {
- return fmt.Sprintf("%x ", []byte(n))
+ return fmt.Sprintf("%x ", []byte(n.Value))
}
func mustDecodeNode(dbkey, buf []byte) node {
@@ -109,7 +122,7 @@ func decodeShort(buf []byte) (node, error) {
if err != nil {
return nil, fmt.Errorf("invalid value node: %v", err)
}
- return shortNode{key, valueNode(val)}, nil
+ return shortNode{key, valueNode{Value: val}}, nil
}
r, _, err := decodeRef(rest)
if err != nil {
@@ -132,7 +145,7 @@ func decodeFull(buf []byte) (fullNode, error) {
return n, err
}
if len(val) > 0 {
- n[16] = valueNode(val)
+ n[16] = valueNode{Value: val}
}
return n, nil
}
diff --git a/trie/proof.go b/trie/proof.go
index 0f92649424..3c49309026 100644
--- a/trie/proof.go
+++ b/trie/proof.go
@@ -107,7 +107,7 @@ func VerifyProof(rootHash common.Hash, key []byte, proof []rlp.RawValue) (value
if i != len(proof)-1 {
return nil, errors.New("additional nodes at end of proof")
}
- return cld, nil
+ return cld.Value, nil
}
}
return nil, errors.New("unexpected end of proof")
diff --git a/trie/proof_test.go b/trie/proof_test.go
index 6b5bef05c4..0530dff357 100644
--- a/trie/proof_test.go
+++ b/trie/proof_test.go
@@ -119,14 +119,14 @@ func randomTrie(n int) (*Trie, map[string]*kv) {
for i := byte(0); i < 100; i++ {
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
value2 := &kv{common.LeftPadBytes([]byte{i + 10}, 32), []byte{i}, false}
- trie.Update(value.k, value.v)
- trie.Update(value2.k, value2.v)
+ trie.UpdateIndexed(value.k, value.v, nil)
+ trie.UpdateIndexed(value2.k, value2.v, nil)
vals[string(value.k)] = value
vals[string(value2.k)] = value2
}
for i := 0; i < n; i++ {
value := &kv{randBytes(32), randBytes(20), false}
- trie.Update(value.k, value.v)
+ trie.UpdateIndexed(value.k, value.v, nil)
vals[string(value.k)] = value
}
return trie, vals
diff --git a/trie/secure_trie.go b/trie/secure_trie.go
index caeef3c3a3..aabffb40f7 100644
--- a/trie/secure_trie.go
+++ b/trie/secure_trie.go
@@ -79,29 +79,27 @@ func (t *SecureTrie) TryGet(key []byte) ([]byte, error) {
return t.Trie.TryGet(t.hashKey(key))
}
-// Update associates key with value in the trie. Subsequent calls to
-// Get will return value. If value has length zero, any existing value
-// is deleted from the trie and calls to Get will return nil.
+// UpdateIndexed is an extended version of Update, where state trie index entries
+// are also generated for all entities referencing the current node.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
-func (t *SecureTrie) Update(key, value []byte) {
- if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
+func (t *SecureTrie) UpdateIndexed(key, value []byte, refs [][]byte) {
+ if err := t.TryUpdateIndexed(key, value, refs); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err)
}
}
-// TryUpdate associates key with value in the trie. Subsequent calls to
-// Get will return value. If value has length zero, any existing value
-// is deleted from the trie and calls to Get will return nil.
+// TryUpdateIndexed is an extended version of Update, where state trie index
+// entries are also generated for all entities referencing the current node.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
//
// If a node was not found in the database, a MissingNodeError is returned.
-func (t *SecureTrie) TryUpdate(key, value []byte) error {
+func (t *SecureTrie) TryUpdateIndexed(key, value []byte, refs [][]byte) error {
hk := t.hashKey(key)
- err := t.Trie.TryUpdate(hk, value)
+ err := t.Trie.TryUpdateIndexed(hk, value, refs)
if err != nil {
return err
}
diff --git a/trie/secure_trie_test.go b/trie/secure_trie_test.go
index 13c6cd02e9..59ee267efc 100644
--- a/trie/secure_trie_test.go
+++ b/trie/secure_trie_test.go
@@ -45,7 +45,7 @@ func TestSecureDelete(t *testing.T) {
}
for _, val := range vals {
if val.v != "" {
- trie.Update([]byte(val.k), []byte(val.v))
+ trie.UpdateIndexed([]byte(val.k), []byte(val.v), nil)
} else {
trie.Delete([]byte(val.k))
}
@@ -59,7 +59,7 @@ func TestSecureDelete(t *testing.T) {
func TestSecureGetKey(t *testing.T) {
trie := newEmptySecure()
- trie.Update([]byte("foo"), []byte("bar"))
+ trie.UpdateIndexed([]byte("foo"), []byte("bar"), nil)
key := []byte("foo")
value := []byte("bar")
diff --git a/trie/sync.go b/trie/sync.go
index d55399d06b..c3cd36cf79 100644
--- a/trie/sync.go
+++ b/trie/sync.go
@@ -34,6 +34,8 @@ type request struct {
depth int // Depth level within the trie the node is located to prioritise DFS
deps int // Number of dependencies before allowed to commit this node
+ externals []common.Hash // External children of this node to index in addition to internal ones
+
callback TrieSyncLeafCallback // Callback to invoke if a leaf node it reached on this branch
}
@@ -94,6 +96,7 @@ func (s *TrieSync) AddSubTrie(root common.Hash, depth int, parent common.Hash, c
panic(fmt.Sprintf("sub-trie ancestor not found: %x", parent))
}
ancestor.deps++
+ ancestor.externals = append(ancestor.externals, root)
req.parents = append(req.parents, ancestor)
}
s.schedule(req)
@@ -123,6 +126,7 @@ func (s *TrieSync) AddRawEntry(hash common.Hash, depth int, parent common.Hash)
panic(fmt.Sprintf("raw-entry ancestor not found: %x", parent))
}
ancestor.deps++
+ ancestor.externals = append(ancestor.externals, hash)
req.parents = append(req.parents, ancestor)
}
s.schedule(req)
@@ -229,7 +233,7 @@ func (s *TrieSync) children(req *request) ([]*request, error) {
// Notify any external watcher of a new key/value node
if req.callback != nil {
if node, ok := (*child.node).(valueNode); ok {
- if err := req.callback(node, req.hash); err != nil {
+ if err := req.callback(node.Value, req.hash); err != nil {
return nil, err
}
}
@@ -266,7 +270,17 @@ func (s *TrieSync) commit(req *request, batch ethdb.Batch) (err error) {
err = batch.Write()
}()
}
- // Write the node content to disk
+ // Write the node content and indices to disk
+ if req.object != nil {
+ if err := storeParentReferences(req.hash[:], *req.object, batch); err != nil {
+ return err
+ }
+ }
+ for _, ext := range req.externals {
+ if err := storeParentReferenceEntry(req.hash[:], ext[:], batch); err != nil {
+ return err
+ }
+ }
if err := batch.Put(req.hash[:], req.data); err != nil {
return err
}
diff --git a/trie/sync_test.go b/trie/sync_test.go
index 9c036a3a92..f1c67b5e6d 100644
--- a/trie/sync_test.go
+++ b/trie/sync_test.go
@@ -18,6 +18,7 @@ package trie
import (
"bytes"
+ "fmt"
"testing"
"github.com/ethereum/go-ethereum/common"
@@ -33,16 +34,27 @@ func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
// Fill it with some arbitrary data
content := make(map[string][]byte)
for i := byte(0); i < 255; i++ {
+ // Map the same data under multiple keys
key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
content[string(key)] = val
- trie.Update(key, val)
+ trie.UpdateIndexed(key, val, nil)
key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
content[string(key)] = val
- trie.Update(key, val)
+ trie.UpdateIndexed(key, val, nil)
+
+ // Add some other data to inflate th trie
+ for j := byte(3); j < 13; j++ {
+ key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
+ content[string(key)] = val
+ trie.UpdateIndexed(key, val, nil)
+ }
}
trie.Commit()
+ // Remove any potentially cached data from the test trie creation
+ globalCache.Clear()
+
// Return the generated trie
return db, trie, content
}
@@ -50,10 +62,20 @@ func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
// checkTrieContents cross references a reconstructed trie with an expected data
// content map.
func checkTrieContents(t *testing.T, db Database, root []byte, content map[string][]byte) {
+ // Remove any potentially cached data from the trie synchronisation
+ globalCache.Clear()
+
+ // Check root availability and trie contents
trie, err := New(common.BytesToHash(root), db)
if err != nil {
t.Fatalf("failed to create trie at %x: %v", root, err)
}
+ if err := checkTrieConsistency(db, common.BytesToHash(root)); err != nil {
+ t.Fatalf("inconsistent trie at %x: %v", root, err)
+ }
+ if err := checkTrieIndex(db, common.BytesToHash(root)); err != nil {
+ t.Fatalf("index error at %x: %v", root, err)
+ }
for key, val := range content {
if have := trie.Get([]byte(key)); bytes.Compare(have, val) != 0 {
t.Errorf("entry %x: content mismatch: have %x, want %x", key, have, val)
@@ -61,6 +83,49 @@ func checkTrieContents(t *testing.T, db Database, root []byte, content map[strin
}
}
+// checkTrieConsistency checks that all nodes in a trie and indeed present.
+func checkTrieConsistency(db Database, root common.Hash) (failure error) {
+ // Capture any panics by the iterator
+ defer func() {
+ if r := recover(); r != nil {
+ failure = fmt.Errorf("%v", r)
+ }
+ }()
+ // Remove any potentially cached data from the test trie creation or previous checks
+ globalCache.Clear()
+
+ // Create and iterate a trie rooted in a subnode
+ trie, err := New(root, db)
+ if err != nil {
+ return
+ }
+ for it := NewNodeIterator(trie); it.Next(); {
+ }
+ return nil
+}
+
+// checkTrieIndex iterates over the entire state trie and checks that all required
+// database indexes have been generated by the synchronizer.
+func checkTrieIndex(db Database, root common.Hash) error {
+ // Remove any potentially cached data from the test trie creation or previous checks
+ globalCache.Clear()
+
+ // Create and iterate a trie
+ trie, err := New(root, db)
+ if err != nil {
+ return err
+ }
+ for it := NewNodeIterator(trie); it.Next(); {
+ if (it.Hash != common.Hash{}) && (it.Parent != common.Hash{}) {
+ parentRef := ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes())
+ if _, err := db.Get(parentRef); err != nil {
+ return fmt.Errorf("parent reference lookup %x: %v", parentRef, err)
+ }
+ }
+ }
+ return nil
+}
+
// Tests that an empty trie is not scheduled for syncing.
func TestEmptyTrieSync(t *testing.T) {
emptyA, _ := New(common.Hash{}, nil)
@@ -102,7 +167,7 @@ func testIterativeTrieSync(t *testing.T, batch int) {
}
queue = append(queue[:0], sched.Missing(batch)...)
}
- // Cross check that the two tries re in sync
+ // Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
}
@@ -132,7 +197,7 @@ func TestIterativeDelayedTrieSync(t *testing.T) {
}
queue = append(queue[len(results):], sched.Missing(10000)...)
}
- // Cross check that the two tries re in sync
+ // Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
}
@@ -173,7 +238,7 @@ func testIterativeRandomTrieSync(t *testing.T, batch int) {
queue[hash] = struct{}{}
}
}
- // Cross check that the two tries re in sync
+ // Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
}
@@ -216,7 +281,7 @@ func TestIterativeRandomDelayedTrieSync(t *testing.T) {
queue[hash] = struct{}{}
}
}
- // Cross check that the two tries re in sync
+ // Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
}
@@ -252,6 +317,57 @@ func TestDuplicateAvoidanceTrieSync(t *testing.T) {
}
queue = append(queue[:0], sched.Missing(0)...)
}
- // Cross check that the two tries re in sync
+ // Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
}
+
+// Tests that at any point in time during a sync, only complete sub-tries are in
+// the database.
+func TestIncompleteTrieSync(t *testing.T) {
+ // Create a random trie to copy
+ srcDb, srcTrie, _ := makeTestTrie()
+
+ // Create a destination trie and sync with the scheduler
+ dstDb, _ := ethdb.NewMemDatabase()
+ sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
+
+ added := []common.Hash{}
+ queue := append([]common.Hash{}, sched.Missing(1)...)
+ for len(queue) > 0 {
+ // Fetch a batch of trie nodes
+ results := make([]SyncResult, len(queue))
+ for i, hash := range queue {
+ data, err := srcDb.Get(hash.Bytes())
+ if err != nil {
+ t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
+ }
+ results[i] = SyncResult{hash, data}
+ }
+ // Process each of the trie nodes
+ if index, err := sched.Process(results); err != nil {
+ t.Fatalf("failed to process result #%d: %v", index, err)
+ }
+ for _, result := range results {
+ added = append(added, result.Hash)
+ }
+ // Check that all known sub-tries in the synced trie is complete
+ for _, root := range added {
+ if err := checkTrieConsistency(dstDb, root); err != nil {
+ t.Fatalf("trie inconsistent: %v", err)
+ }
+ }
+ // Fetch the next batch to retrieve
+ queue = append(queue[:0], sched.Missing(1)...)
+ }
+ // Sanity check that removing any node from the database is detected
+ for _, node := range added[1:] {
+ key := node.Bytes()
+ value, _ := dstDb.Get(key)
+
+ dstDb.Delete(key)
+ if err := checkTrieConsistency(dstDb, added[0]); err == nil {
+ t.Fatalf("trie inconsistency not caught, missing: %x", key)
+ }
+ dstDb.Put(key, value)
+ }
+}
diff --git a/trie/trie.go b/trie/trie.go
index 9dfde45294..93dc48a55e 100644
--- a/trie/trie.go
+++ b/trie/trie.go
@@ -143,33 +143,35 @@ func (t *Trie) TryGet(key []byte) ([]byte, error) {
panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
}
}
- return tn.(valueNode), nil
+ return tn.(valueNode).Value, nil
}
-// Update associates key with value in the trie. Subsequent calls to
-// Get will return value. If value has length zero, any existing value
-// is deleted from the trie and calls to Get will return nil.
+// UpdateIndexed is an extended version of Update, where state trie index entries
+// are also generated for all entities referencing the current node.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
-func (t *Trie) Update(key, value []byte) {
- if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
+func (t *Trie) UpdateIndexed(key, value []byte, refs [][]byte) {
+ if err := t.TryUpdateIndexed(key, value, refs); err != nil && glog.V(logger.Error) {
glog.Errorf("Unhandled trie error: %v", err)
}
}
-// TryUpdate associates key with value in the trie. Subsequent calls to
-// Get will return value. If value has length zero, any existing value
-// is deleted from the trie and calls to Get will return nil.
+// TryUpdateIndexed is an extended version of Update, where state trie index
+// entries are also generated for all entities referencing the current node.
//
// The value bytes must not be modified by the caller while they are
// stored in the trie.
//
// If a node was not found in the database, a MissingNodeError is returned.
-func (t *Trie) TryUpdate(key, value []byte) error {
+func (t *Trie) TryUpdateIndexed(key, value []byte, refs [][]byte) error {
+ return t.tryUpdateIndexed(key, value, refs)
+}
+
+func (t *Trie) tryUpdateIndexed(key, value []byte, refs [][]byte) error {
k := compactHexDecode(key)
if len(value) != 0 {
- n, err := t.insert(t.root, nil, k, valueNode(value))
+ n, err := t.insert(t.root, nil, k, valueNode{Value: value, refs: refs})
if err != nil {
return err
}
@@ -489,7 +491,7 @@ func (h *hasher) replaceChildren(n node, db DatabaseWriter) (node, error) {
}
if n.Val == nil {
// Ensure that nil children are encoded as empty strings.
- n.Val = valueNode(nil)
+ n.Val = valueNode{Value: nil}
}
return n, nil
case fullNode:
@@ -500,11 +502,11 @@ func (h *hasher) replaceChildren(n node, db DatabaseWriter) (node, error) {
}
} else {
// Ensure that nil children are encoded as empty strings.
- n[i] = valueNode(nil)
+ n[i] = valueNode{Value: nil}
}
}
if n[16] == nil {
- n[16] = valueNode(nil)
+ n[16] = valueNode{Value: nil}
}
return n, nil
default:
@@ -530,8 +532,13 @@ func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
h.sha.Write(h.tmp.Bytes())
key := hashNode(h.sha.Sum(nil))
if db != nil {
- err := db.Put(key, h.tmp.Bytes())
- return key, err
+ if err := storeParentReferences(key, n, db); err != nil {
+ return key, err
+ }
+ if err := db.Put(key, h.tmp.Bytes()); err != nil {
+ return key, err
+ }
+ return key, nil
}
return key, nil
}
diff --git a/trie/trie_test.go b/trie/trie_test.go
index 35d043cdf1..fffb6c6315 100644
--- a/trie/trie_test.go
+++ b/trie/trie_test.go
@@ -54,7 +54,7 @@ func TestNull(t *testing.T) {
var trie Trie
key := make([]byte, 32)
value := common.FromHex("0x823140710bf13990e4500136726d8b55")
- trie.Update(key, value)
+ trie.UpdateIndexed(key, value, nil)
value = trie.Get(key)
}
@@ -97,7 +97,7 @@ func TestMissingNode(t *testing.T) {
}
trie, _ = New(root, db)
- err = trie.TryUpdate([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"))
+ err = trie.TryUpdateIndexed([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"), nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
@@ -130,7 +130,7 @@ func TestMissingNode(t *testing.T) {
}
trie, _ = New(root, db)
- err = trie.TryUpdate([]byte("120099"), []byte("zxcv"))
+ err = trie.TryUpdateIndexed([]byte("120099"), []byte("zxcv"), nil)
if _, ok := err.(*MissingNodeError); !ok {
t.Errorf("Wrong error: %v", err)
}
@@ -304,7 +304,7 @@ func paranoiaCheck(t1 *Trie) (bool, *Trie) {
t2 := new(Trie)
it := NewIterator(t1)
for it.Next() {
- t2.Update(it.Key, it.Value)
+ t2.UpdateIndexed(it.Key, it.Value, nil)
}
return t2.Hash() == t1.Hash(), t2
}
@@ -356,8 +356,8 @@ func TestOutput(t *testing.T) {
func TestLargeValue(t *testing.T) {
trie := newEmpty()
- trie.Update([]byte("key1"), []byte{99, 99, 99, 99})
- trie.Update([]byte("key2"), bytes.Repeat([]byte{1}, 32))
+ trie.UpdateIndexed([]byte("key1"), []byte{99, 99, 99, 99}, nil)
+ trie.UpdateIndexed([]byte("key2"), bytes.Repeat([]byte{1}, 32), nil)
trie.Hash()
}
@@ -374,8 +374,8 @@ func TestLargeData(t *testing.T) {
for i := byte(0); i < 255; i++ {
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false}
- trie.Update(value.k, value.v)
- trie.Update(value2.k, value2.v)
+ trie.UpdateIndexed(value.k, value.v, nil)
+ trie.UpdateIndexed(value2.k, value2.v, nil)
vals[string(value.k)] = value
vals[string(value2.k)] = value2
}
@@ -419,7 +419,7 @@ func benchGet(b *testing.B, commit bool) {
k := make([]byte, 32)
for i := 0; i < benchElemCount; i++ {
binary.LittleEndian.PutUint64(k, uint64(i))
- trie.Update(k, k)
+ trie.UpdateIndexed(k, k, nil)
}
binary.LittleEndian.PutUint64(k, benchElemCount/2)
if commit {
@@ -437,7 +437,7 @@ func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
k := make([]byte, 32)
for i := 0; i < b.N; i++ {
e.PutUint64(k, uint64(i))
- trie.Update(k, k)
+ trie.UpdateIndexed(k, k, nil)
}
return trie
}
@@ -447,7 +447,7 @@ func benchHash(b *testing.B, e binary.ByteOrder) {
k := make([]byte, 32)
for i := 0; i < benchElemCount; i++ {
e.PutUint64(k, uint64(i))
- trie.Update(k, k)
+ trie.UpdateIndexed(k, k, nil)
}
b.ResetTimer()
@@ -473,7 +473,7 @@ func getString(trie *Trie, k string) []byte {
}
func updateString(trie *Trie, k, v string) {
- trie.Update([]byte(k), []byte(v))
+ trie.UpdateIndexed([]byte(k), []byte(v), nil)
}
func deleteString(trie *Trie, k string) {