core, eth, light, miner, tests, trie: cross trie indexing

This commit is contained in:
Péter Szilágyi 2016-01-12 11:38:36 +02:00
parent 371be37050
commit c485ac81be
29 changed files with 496 additions and 251 deletions

View file

@ -1195,11 +1195,9 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
return i, err
}
// Write state changes to database
_, err = statedb.Commit()
if err != nil {
if _, err = statedb.CommitIndexed([]common.Hash{block.Hash()}); err != nil {
return i, err
}
// coalesce logs for later processing
coalescedLogs = append(coalescedLogs, logs...)

View file

@ -153,7 +153,7 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
blockchain.mu.Lock()
WriteTd(blockchain.chainDb, block.Hash(), new(big.Int).Add(block.Difficulty(), blockchain.GetTd(block.ParentHash())))
WriteBlock(blockchain.chainDb, block)
statedb.Commit()
statedb.CommitIndexed([]common.Hash{block.Hash()})
blockchain.mu.Unlock()
}
return nil

View file

@ -171,12 +171,14 @@ func GenerateChain(parent *types.Block, db ethdb.Database, n int, gen func(int,
gen(i, b)
}
AccumulateRewards(statedb, h, b.uncles)
root, err := statedb.Commit()
if err != nil {
h.Root = statedb.IntermediateRoot()
block := types.NewBlock(h, b.txs, b.uncles, b.receipts)
if _, err := statedb.CommitIndexed([]common.Hash{block.Hash()}); err != nil {
panic(fmt.Sprintf("state write error: %v", err))
}
h.Root = root
return types.NewBlock(h, b.txs, b.uncles, b.receipts), b.receipts
return block, b.receipts
}
for i := 0; i < n; i++ {
statedb, err := state.New(parent.Root(), db)

View file

@ -72,8 +72,6 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block,
statedb.SetState(address, common.HexToHash(key), common.HexToHash(value))
}
}
root, stateBatch := statedb.CommitBatch()
difficulty := common.String2Big(genesis.Difficulty)
block := types.NewBlock(&types.Header{
Nonce: types.EncodeNonce(common.String2Big(genesis.Nonce).Uint64()),
@ -84,7 +82,7 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block,
Difficulty: difficulty,
MixDigest: common.HexToHash(genesis.Mixhash),
Coinbase: common.HexToAddress(genesis.Coinbase),
Root: root,
Root: statedb.IntermediateRoot(),
}, nil, nil, nil)
if block := GetBlock(chainDb, block.Hash()); block != nil {
@ -95,8 +93,7 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block,
}
return block, nil
}
if err := stateBatch.Write(); err != nil {
if _, err := statedb.CommitIndexed([]common.Hash{block.Hash()}); err != nil {
return nil, fmt.Errorf("cannot write state: %v", err)
}
if err := WriteTd(chainDb, block.Hash(), difficulty); err != nil {
@ -123,15 +120,16 @@ func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big
statedb, _ := state.New(common.Hash{}, db)
obj := statedb.GetOrNewStateObject(addr)
obj.SetBalance(balance)
root, err := statedb.Commit()
if err != nil {
panic(fmt.Sprintf("cannot write state: %v", err))
}
block := types.NewBlock(&types.Header{
Difficulty: params.GenesisDifficulty,
GasLimit: params.GenesisGasLimit,
Root: root,
Root: statedb.IntermediateRoot(),
}, nil, nil, nil)
if _, err := statedb.CommitIndexed([]common.Hash{block.Hash()}); err != nil {
panic(fmt.Sprintf("cannot write state: %v", err))
}
return block
}

155
core/index_test.go Normal file
View file

@ -0,0 +1,155 @@
// 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 <http://www.gnu.org/licenses/>.
package core
import (
"bytes"
"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/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{}
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 num := uint64(0); num <= blockchain.CurrentBlock().NumberU64(); num++ {
// Gather all the indexes that should be present in the state trie
block := blockchain.GetBlockByNumber(num)
stateDb, err := state.New(block.Root(), db)
if err != nil {
t.Fatalf("failed to create state trie at %x: %v", block.Root(), err)
}
for it := state.NewNodeIterator(stateDb); it.Next(); {
if (it.Hash != common.Hash{}) && (it.Parent != common.Hash{}) {
indexes[string(trie.ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
}
}
// Add the block->state index entries
indexes[string(trie.ParentReferenceIndexKey(block.Hash().Bytes(), block.Root().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.Keys() {
if bytes.HasPrefix(key, trie.ParentReferenceIndexPrefix) {
if _, ok := indexes[string(key)]; !ok {
t.Errorf("index entry not reported %x", key)
}
}
}
}

View file

@ -26,9 +26,16 @@ import (
)
// Tests that all index entries are stored in the database after a state commit.
func TestStateIndex(t *testing.T) {
func TestStateIndexDangling(t *testing.T) {
testStateIndex(t, nil)
}
func TestStateIndexRooted(t *testing.T) {
testStateIndex(t, []common.Hash{common.BytesToHash([]byte{0x01}), common.BytesToHash([]byte{0x02, 0x03})})
}
func testStateIndex(t *testing.T, referrers []common.Hash) {
// Create some arbitrary test state to iterate
db, root, _ := makeTestState()
db, root, _ := makeTestState(referrers)
state, err := New(root, db)
if err != nil {
@ -41,6 +48,9 @@ func TestStateIndex(t *testing.T) {
indexes[string(trie.ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
}
}
for _, referrer := range referrers {
indexes[string(trie.ParentReferenceIndexKey(referrer.Bytes(), root.Bytes()))] = struct{}{}
}
// Cross check the indexes and the database itself
for index, _ := range indexes {
if _, err := db.Get([]byte(index)); err != nil {

View file

@ -28,7 +28,7 @@ import (
// 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()
db, root, _ := makeTestState(nil)
state, err := New(root, db)
if err != nil {

View file

@ -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 {

View file

@ -89,7 +89,7 @@ func TestNull(t *testing.T) {
//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
var value common.Hash
state.SetState(address, common.Hash{}, value)
state.Commit()
state.CommitIndexed(nil)
value = state.GetState(address, common.Hash{})
if !common.EmptyHash(value) {
t.Errorf("expected empty hash. got %x", value)

View file

@ -353,22 +353,22 @@ func (s *StateDB) IntermediateRoot() common.Hash {
return s.trie.Hash()
}
// Commit commits all state changes to the database.
func (s *StateDB) Commit() (root common.Hash, err error) {
root, batch := s.CommitBatch()
// CommitIndexed commits all state changes to the database.
func (s *StateDB) CommitIndexed(refs []common.Hash) (root common.Hash, err error) {
root, batch := s.CommitBatchIndexed(refs)
return root, batch.Write()
}
// CommitBatch commits all state changes to a write batch but does not
// CommitBatchIndexed commits all state changes to a write batch but does not
// execute the batch. It is used to validate state changes against
// the root hash stored in a block.
func (s *StateDB) CommitBatch() (root common.Hash, batch ethdb.Batch) {
func (s *StateDB) CommitBatchIndexed(refs []common.Hash) (root common.Hash, batch ethdb.Batch) {
batch = s.db.NewBatch()
root, _ = s.commit(batch)
root, _ = s.commit(batch, refs)
return root, batch
}
func (s *StateDB) commit(db trie.DatabaseWriter) (common.Hash, error) {
func (s *StateDB) commit(db trie.DatabaseWriter, refs []common.Hash) (common.Hash, error) {
s.refund = new(big.Int)
for _, stateObject := range s.stateObjects {
@ -383,7 +383,7 @@ func (s *StateDB) commit(db trie.DatabaseWriter) (common.Hash, error) {
// This updates the trie root internally, so
// getting the root hash of the storage trie
// through UpdateStateObject is fast.
if _, err := stateObject.trie.CommitTo(db); err != nil {
if _, err := stateObject.trie.CommitToIndexed(db, nil); err != nil {
return common.Hash{}, err
}
// Update the object in the account trie.
@ -391,7 +391,11 @@ func (s *StateDB) commit(db trie.DatabaseWriter) (common.Hash, error) {
}
stateObject.dirty = false
}
return s.trie.CommitTo(db)
referrers := make([][]byte, len(refs))
for i, ref := range refs {
referrers[i] = ref.Bytes()
}
return s.trie.CommitToIndexed(db, referrers)
}
func (self *StateDB) Refunds() *big.Int {

View file

@ -32,7 +32,7 @@ import (
type StateSync trie.TrieSync
// NewStateSync create a new state trie download scheduler.
func NewStateSync(root common.Hash, database ethdb.Database) *StateSync {
func NewStateSync(root common.Hash, database ethdb.Database, parent common.Hash) *StateSync {
var syncer *trie.TrieSync
callback := func(leaf []byte, parent common.Hash) error {
@ -50,7 +50,7 @@ func NewStateSync(root common.Hash, database ethdb.Database) *StateSync {
return nil
}
syncer = trie.NewTrieSync(root, database, callback)
syncer = trie.NewTrieSync(root, database, parent, callback)
return (*StateSync)(syncer)
}

View file

@ -37,7 +37,7 @@ type testAccount struct {
}
// makeTestState create a sample test state to test node-wise reconstruction.
func makeTestState() (ethdb.Database, common.Hash, []*testAccount) {
func makeTestState(referrers []common.Hash) (ethdb.Database, common.Hash, []*testAccount) {
// Create an empty state
db, _ := ethdb.NewMemDatabase()
state, _ := New(common.Hash{}, db)
@ -61,7 +61,7 @@ func makeTestState() (ethdb.Database, common.Hash, []*testAccount) {
state.UpdateStateObject(obj)
accounts = append(accounts, acc)
}
root, _ := state.Commit()
root, _ := state.CommitIndexed(referrers)
// Remove any potentially cached data from the test state creation
trie.ClearGlobalCache()
@ -72,7 +72,7 @@ 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) {
func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accounts []*testAccount, parent common.Hash) {
// Remove any potentially cached data from the state synchronisation
trie.ClearGlobalCache()
@ -84,7 +84,7 @@ func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accou
if err := checkStateConsistency(db, root); err != nil {
t.Fatalf("inconsistent state trie at %x: %v", root, err)
}
if err := checkStateIndex(db, root); err != nil {
if err := checkStateIndex(db, root, parent); err != nil {
t.Fatalf("index error at %x: %v", root, err)
}
for i, acc := range accounts {
@ -126,23 +126,37 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) (failure error)
// 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 {
func checkStateIndex(db ethdb.Database, root common.Hash, parent 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
return fmt.Errorf("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{}) {
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)
indexes[string(trie.ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
}
}
if parent != (common.Hash{}) {
indexes[string(trie.ParentReferenceIndexKey(parent.Bytes(), root.Bytes()))] = struct{}{}
}
// Cross check the indexes and the database itself
for index, _ := range indexes {
if _, err := db.Get([]byte(index)); err != nil {
return fmt.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 {
return fmt.Errorf("index entry not reported %x", key)
}
}
}
@ -150,26 +164,39 @@ func checkStateIndex(db ethdb.Database, root common.Hash) error {
}
// Tests that an empty state is not scheduled for syncing.
func TestEmptyStateSync(t *testing.T) {
func TestEmptyStateSyncDangling(t *testing.T) { testEmptyStateSync(t, common.Hash{}) }
func TestEmptyStateSyncRooted(t *testing.T) { testEmptyStateSync(t, common.BytesToHash([]byte{0x01})) }
func testEmptyStateSync(t *testing.T, origin common.Hash) {
empty := common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
db, _ := ethdb.NewMemDatabase()
if req := NewStateSync(empty, db).Missing(1); len(req) != 0 {
if req := NewStateSync(empty, db, origin).Missing(1); len(req) != 0 {
t.Errorf("content requested for empty state: %v", req)
}
}
// Tests that given a root hash, a state can sync iteratively on a single thread,
// requesting retrieval tasks and returning all of them in one go.
func TestIterativeStateSyncIndividual(t *testing.T) { testIterativeStateSync(t, 1) }
func TestIterativeStateSyncBatched(t *testing.T) { testIterativeStateSync(t, 100) }
func TestIterativeStateSyncIndividualDangling(t *testing.T) {
testIterativeStateSync(t, 1, common.Hash{})
}
func TestIterativeStateSyncIndividualRooted(t *testing.T) {
testIterativeStateSync(t, 1, common.BytesToHash([]byte{0x02}))
}
func TestIterativeStateSyncBatchedDangling(t *testing.T) {
testIterativeStateSync(t, 100, common.Hash{})
}
func TestIterativeStateSyncBatchedRooted(t *testing.T) {
testIterativeStateSync(t, 100, common.BytesToHash([]byte{0x03}))
}
func testIterativeStateSync(t *testing.T, batch int) {
func testIterativeStateSync(t *testing.T, batch int, origin common.Hash) {
// Create a random state to copy
srcDb, srcRoot, srcAccounts := makeTestState()
srcDb, srcRoot, srcAccounts := makeTestState(nil)
// Create a destination state and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewStateSync(srcRoot, dstDb)
sched := NewStateSync(srcRoot, dstDb, origin)
queue := append([]common.Hash{}, sched.Missing(batch)...)
for len(queue) > 0 {
@ -187,18 +214,25 @@ func testIterativeStateSync(t *testing.T, batch int) {
queue = append(queue[:0], sched.Missing(batch)...)
}
// Cross check that the two states are in sync
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
checkStateAccounts(t, dstDb, srcRoot, srcAccounts, origin)
}
// Tests that the trie scheduler can correctly reconstruct the state even if only
// partial results are returned, and the others sent only later.
func TestIterativeDelayedStateSync(t *testing.T) {
func TestIterativeDelayedStateSyncDangling(t *testing.T) {
testIterativeDelayedStateSync(t, common.Hash{})
}
func TestIterativeDelayedStateSyncRooted(t *testing.T) {
testIterativeDelayedStateSync(t, common.BytesToHash([]byte{0x04}))
}
func testIterativeDelayedStateSync(t *testing.T, origin common.Hash) {
// Create a random state to copy
srcDb, srcRoot, srcAccounts := makeTestState()
srcDb, srcRoot, srcAccounts := makeTestState(nil)
// Create a destination state and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewStateSync(srcRoot, dstDb)
sched := NewStateSync(srcRoot, dstDb, origin)
queue := append([]common.Hash{}, sched.Missing(0)...)
for len(queue) > 0 {
@ -217,22 +251,32 @@ func TestIterativeDelayedStateSync(t *testing.T) {
queue = append(queue[len(results):], sched.Missing(0)...)
}
// Cross check that the two states are in sync
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
checkStateAccounts(t, dstDb, srcRoot, srcAccounts, origin)
}
// Tests that given a root hash, a trie can sync iteratively on a single thread,
// requesting retrieval tasks and returning all of them in one go, however in a
// random order.
func TestIterativeRandomStateSyncIndividual(t *testing.T) { testIterativeRandomStateSync(t, 1) }
func TestIterativeRandomStateSyncBatched(t *testing.T) { testIterativeRandomStateSync(t, 100) }
func TestIterativeRandomStateSyncIndividualDangling(t *testing.T) {
testIterativeRandomStateSync(t, 1, common.Hash{})
}
func TestIterativeRandomStateSyncIndividualRooted(t *testing.T) {
testIterativeRandomStateSync(t, 1, common.BytesToHash([]byte{0x05}))
}
func TestIterativeRandomStateSyncBatchedDangling(t *testing.T) {
testIterativeRandomStateSync(t, 100, common.Hash{})
}
func TestIterativeRandomStateSyncBatchedRooted(t *testing.T) {
testIterativeRandomStateSync(t, 100, common.BytesToHash([]byte{0x06}))
}
func testIterativeRandomStateSync(t *testing.T, batch int) {
func testIterativeRandomStateSync(t *testing.T, batch int, origin common.Hash) {
// Create a random state to copy
srcDb, srcRoot, srcAccounts := makeTestState()
srcDb, srcRoot, srcAccounts := makeTestState(nil)
// Create a destination state and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewStateSync(srcRoot, dstDb)
sched := NewStateSync(srcRoot, dstDb, origin)
queue := make(map[common.Hash]struct{})
for _, hash := range sched.Missing(batch) {
@ -258,18 +302,25 @@ func testIterativeRandomStateSync(t *testing.T, batch int) {
}
}
// Cross check that the two states are in sync
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
checkStateAccounts(t, dstDb, srcRoot, srcAccounts, origin)
}
// Tests that the trie scheduler can correctly reconstruct the state even if only
// partial results are returned (Even those randomly), others sent only later.
func TestIterativeRandomDelayedStateSync(t *testing.T) {
func TestIterativeRandomDelayedStateSyncDangling(t *testing.T) {
testIterativeRandomDelayedStateSync(t, common.Hash{})
}
func TestIterativeRandomDelayedStateSyncRooted(t *testing.T) {
testIterativeRandomDelayedStateSync(t, common.BytesToHash([]byte{0x07}))
}
func testIterativeRandomDelayedStateSync(t *testing.T, origin common.Hash) {
// Create a random state to copy
srcDb, srcRoot, srcAccounts := makeTestState()
srcDb, srcRoot, srcAccounts := makeTestState(nil)
// Create a destination state and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewStateSync(srcRoot, dstDb)
sched := NewStateSync(srcRoot, dstDb, origin)
queue := make(map[common.Hash]struct{})
for _, hash := range sched.Missing(0) {
@ -300,18 +351,18 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
}
}
// Cross check that the two states are in sync
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
checkStateAccounts(t, dstDb, srcRoot, srcAccounts, origin)
}
// 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()
srcDb, srcRoot, srcAccounts := makeTestState(nil)
// Create a destination state and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewStateSync(srcRoot, dstDb)
sched := NewStateSync(srcRoot, dstDb, common.Hash{})
added := []common.Hash{}
queue := append([]common.Hash{}, sched.Missing(1)...)

View file

@ -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()
}

View file

@ -356,7 +356,7 @@ func (q *queue) Schedule(headers []*types.Header, from uint64) []*types.Header {
if q.mode == FastSync && header.Number.Uint64() == q.fastSyncPivot {
// Pivoting point of the fast sync, retrieve the state tries
q.stateSchedLock.Lock()
q.stateScheduler = state.NewStateSync(header.Root, q.stateDatabase)
q.stateScheduler = state.NewStateSync(header.Root, q.stateDatabase, header.Hash())
q.stateSchedLock.Unlock()
}
inserts = append(inserts, header)

View file

@ -65,7 +65,7 @@ func makeTestState() (common.Hash, ethdb.Database) {
so.Update()
st.UpdateStateObject(so)
}
root, _ := st.Commit()
root, _ := st.CommitIndexed(nil)
return root, sdb
}

View file

@ -89,19 +89,20 @@ func (t *LightTrie) Get(ctx context.Context, key []byte) (res []byte, err error)
return
}
// 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 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. In addition, 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 *LightTrie) Update(ctx context.Context, key, value []byte) (err error) {
func (t *LightTrie) UpdateIndexed(ctx context.Context, key, value []byte, references [][]byte) (err error) {
err = t.do(ctx, key, func() (err error) {
if t.trie == nil {
t.trie, err = trie.NewSecure(t.originalRoot, t.db)
}
if err == nil {
err = t.trie.TryUpdate(key, value)
err = t.trie.TryUpdateIndexed(key, value, references)
}
return
})

View file

@ -284,7 +284,7 @@ func (self *worker) wait() {
}
go self.mux.Post(core.NewMinedBlockEvent{block})
} else {
work.state.Commit()
work.state.CommitIndexed([]common.Hash{block.Hash()})
parent := self.chain.GetBlock(block.ParentHash())
if parent == nil {
glog.V(logger.Error).Infoln("Invalid block found during mining")

View file

@ -250,7 +250,7 @@ func (t *BlockTest) InsertPreState(db ethdb.Database, am *accounts.Manager) (*st
}
}
root, err := statedb.Commit()
root, err := statedb.CommitIndexed(nil)
if err != nil {
return nil, fmt.Errorf("error writing state: %v", err)
}

View file

@ -201,7 +201,7 @@ func runStateTest(test VmTest) error {
}
}
root, _ := statedb.Commit()
root, _ := statedb.CommitIndexed(nil)
if common.HexToHash(test.PostStateRoot) != root {
return fmt.Errorf("Post state root error. Expected %s, got %x", test.PostStateRoot, root)
}
@ -245,7 +245,7 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, vm.Log
if core.IsNonceErr(err) || core.IsInvalidTxErr(err) || core.IsGasLimitErr(err) {
statedb.Set(snapshot)
}
statedb.Commit()
statedb.CommitIndexed(nil)
return ret, vmenv.state.Logs(), vmenv.Gas, err
}

View file

@ -25,9 +25,12 @@ import (
)
// Tests that all index entries are stored in the database after a trie commit.
func TestTrieIndex(t *testing.T) {
func TestTrieIndexDangling(t *testing.T) { testTrieIndex(t, nil) }
func TestTrieIndexRooted(t *testing.T) { testTrieIndex(t, [][]byte{[]byte{0x00}, []byte{0x00, 0x01}}) }
func testTrieIndex(t *testing.T, referrers [][]byte) {
// Create some arbitrary test trie to iterate
db, trie, _ := makeTestTrie()
db, trie, _ := makeTestTrie(referrers)
// Gather all the indexes that should be present in the database
indexes := make(map[string]struct{})
@ -36,6 +39,9 @@ func TestTrieIndex(t *testing.T) {
indexes[string(ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
}
}
for _, referrer := range referrers {
indexes[string(ParentReferenceIndexKey(referrer, trie.Hash().Bytes()))] = struct{}{}
}
// Cross check the indexes and the database itself
for index, _ := range indexes {
if _, err := db.Get([]byte(index)); err != nil {

View file

@ -37,9 +37,9 @@ 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()
trie.CommitIndexed(nil)
it := NewIterator(trie)
for it.Next() {
@ -56,7 +56,7 @@ 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()
db, trie, _ := makeTestTrie(nil)
// Gather all the node hashes found by the iterator
hashes := make(map[common.Hash]struct{})

View file

@ -62,7 +62,7 @@ func (t *Trie) Prove(key []byte) []rlp.RawValue {
// Don't bother checking for errors here since hasher panics
// if encoding doesn't work and we're not writing to any database.
n, _ = t.hasher.replaceChildren(n, nil)
hn, _ := t.hasher.store(n, nil, false)
hn, _ := t.hasher.store(n, nil, false, nil)
if _, ok := hn.(hashNode); ok || i == 0 {
// If the node's database encoding is a hash (or is the
// root node), it becomes a proof element.

View file

@ -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

View file

@ -79,36 +79,6 @@ 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.
//
// 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) {
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.
//
// 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 {
hk := t.hashKey(key)
err := t.Trie.TryUpdate(hk, value)
if err != nil {
return err
}
t.Trie.db.Put(t.secKey(hk), key)
return nil
}
// UpdateIndexed is an extended version of Update, where state trie index entries
// are also generated for all entities referencing the current node.
//

View file

@ -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")

View file

@ -34,7 +34,7 @@ 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
referrers []common.Hash // External parents of this node to index in addition to internal ones
callback TrieSyncLeafCallback // Callback to invoke if a leaf node it reached on this branch
}
@ -61,13 +61,13 @@ type TrieSync struct {
}
// NewTrieSync creates a new trie data download scheduler.
func NewTrieSync(root common.Hash, database ethdb.Database, callback TrieSyncLeafCallback) *TrieSync {
func NewTrieSync(root common.Hash, database ethdb.Database, parent common.Hash, callback TrieSyncLeafCallback) *TrieSync {
ts := &TrieSync{
database: database,
requests: make(map[common.Hash]*request),
queue: prque.New(),
}
ts.AddSubTrie(root, 0, common.Hash{}, callback)
ts.AddSubTrie(root, 0, parent, callback)
return ts
}
@ -91,13 +91,15 @@ func (s *TrieSync) AddSubTrie(root common.Hash, depth int, parent common.Hash, c
}
// If this sub-trie has a designated parent, link them together
if parent != (common.Hash{}) {
ancestor := s.requests[parent]
if ancestor == nil {
panic(fmt.Sprintf("sub-trie ancestor not found: %x", parent))
if depth > 0 {
ancestor := s.requests[parent]
if ancestor == nil {
panic(fmt.Sprintf("sub-trie ancestor not found: %x", parent))
}
ancestor.deps++
req.parents = append(req.parents, ancestor)
}
ancestor.deps++
ancestor.externals = append(ancestor.externals, root)
req.parents = append(req.parents, ancestor)
req.referrers = append(req.referrers, parent)
}
s.schedule(req)
}
@ -121,13 +123,15 @@ func (s *TrieSync) AddRawEntry(hash common.Hash, depth int, parent common.Hash)
}
// If this sub-trie has a designated parent, link them together
if parent != (common.Hash{}) {
ancestor := s.requests[parent]
if ancestor == nil {
panic(fmt.Sprintf("raw-entry ancestor not found: %x", parent))
if depth > 0 {
ancestor := s.requests[parent]
if ancestor == nil {
panic(fmt.Sprintf("raw-entry ancestor not found: %x", parent))
}
ancestor.deps++
req.parents = append(req.parents, ancestor)
}
ancestor.deps++
ancestor.externals = append(ancestor.externals, hash)
req.parents = append(req.parents, ancestor)
req.referrers = append(req.referrers, parent)
}
s.schedule(req)
}
@ -276,8 +280,8 @@ func (s *TrieSync) commit(req *request, batch ethdb.Batch) (err error) {
return err
}
}
for _, ext := range req.externals {
if err := storeParentReferenceEntry(req.hash[:], ext[:], batch); err != nil {
for _, referrer := range req.referrers {
if err := storeParentReferenceEntry(referrer[:], req.hash[:], batch); err != nil {
return err
}
}

View file

@ -26,7 +26,7 @@ import (
)
// makeTestTrie create a sample test trie to test node-wise reconstruction.
func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
func makeTestTrie(referrers [][]byte) (ethdb.Database, *Trie, map[string][]byte) {
// Create an empty trie
db, _ := ethdb.NewMemDatabase()
trie, _ := New(common.Hash{}, db)
@ -37,20 +37,20 @@ func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
// 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.Update(key, val)
trie.UpdateIndexed(key, val, nil)
}
}
trie.Commit()
trie.CommitIndexed(referrers)
// Remove any potentially cached data from the test trie creation
globalCache.Clear()
@ -61,7 +61,7 @@ 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) {
func checkTrieContents(t *testing.T, db Database, root []byte, content map[string][]byte, parent common.Hash) {
// Remove any potentially cached data from the trie synchronisation
globalCache.Clear()
@ -73,7 +73,7 @@ func checkTrieContents(t *testing.T, db Database, root []byte, content map[strin
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 {
if err := checkTrieIndex(db, common.BytesToHash(root), parent); err != nil {
t.Fatalf("index error at %x: %v", root, err)
}
for key, val := range content {
@ -106,20 +106,34 @@ func checkTrieConsistency(db Database, root common.Hash) (failure error) {
// 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 {
func checkTrieIndex(db Database, root common.Hash, parent 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
}
// 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{}) {
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)
indexes[string(ParentReferenceIndexKey(it.Parent.Bytes(), it.Hash.Bytes()))] = struct{}{}
}
}
if parent != (common.Hash{}) {
indexes[string(ParentReferenceIndexKey(parent.Bytes(), trie.Hash().Bytes()))] = struct{}{}
}
// Cross check the indexes and the database itself
for index, _ := range indexes {
if _, err := db.Get([]byte(index)); err != nil {
return fmt.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 {
return fmt.Errorf("index entry not reported %x", key)
}
}
}
@ -127,13 +141,18 @@ func checkTrieIndex(db Database, root common.Hash) error {
}
// Tests that an empty trie is not scheduled for syncing.
func TestEmptyTrieSync(t *testing.T) {
func TestEmptyTrieSyncDangling(t *testing.T) { testEmptyTrieSync(t, common.Hash{}) }
func TestEmptyTrieSyncRooted(t *testing.T) {
testEmptyTrieSync(t, common.BytesToHash([]byte{0x01}))
}
func testEmptyTrieSync(t *testing.T, origin common.Hash) {
emptyA, _ := New(common.Hash{}, nil)
emptyB, _ := New(emptyRoot, nil)
for i, trie := range []*Trie{emptyA, emptyB} {
db, _ := ethdb.NewMemDatabase()
if req := NewTrieSync(common.BytesToHash(trie.Root()), db, nil).Missing(1); len(req) != 0 {
if req := NewTrieSync(common.BytesToHash(trie.Root()), db, origin, nil).Missing(1); len(req) != 0 {
t.Errorf("test %d: content requested for empty trie: %v", i, req)
}
}
@ -141,16 +160,26 @@ func TestEmptyTrieSync(t *testing.T) {
// Tests that given a root hash, a trie can sync iteratively on a single thread,
// requesting retrieval tasks and returning all of them in one go.
func TestIterativeTrieSyncIndividual(t *testing.T) { testIterativeTrieSync(t, 1) }
func TestIterativeTrieSyncBatched(t *testing.T) { testIterativeTrieSync(t, 100) }
func TestIterativeTrieSyncIndividualDangling(t *testing.T) {
testIterativeTrieSync(t, 1, common.Hash{})
}
func TestIterativeTrieSyncIndividualRooted(t *testing.T) {
testIterativeTrieSync(t, 1, common.BytesToHash([]byte{0x02}))
}
func TestIterativeTrieSyncBatchedDangling(t *testing.T) {
testIterativeTrieSync(t, 100, common.Hash{})
}
func TestIterativeTrieSyncBatchedRooted(t *testing.T) {
testIterativeTrieSync(t, 100, common.BytesToHash([]byte{0x03}))
}
func testIterativeTrieSync(t *testing.T, batch int) {
func testIterativeTrieSync(t *testing.T, batch int, origin common.Hash) {
// Create a random trie to copy
srcDb, srcTrie, srcData := makeTestTrie()
srcDb, srcTrie, srcData := makeTestTrie(nil)
// Create a destination trie and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, origin, nil)
queue := append([]common.Hash{}, sched.Missing(batch)...)
for len(queue) > 0 {
@ -168,18 +197,25 @@ func testIterativeTrieSync(t *testing.T, batch int) {
queue = append(queue[:0], sched.Missing(batch)...)
}
// Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
checkTrieContents(t, dstDb, srcTrie.Root(), srcData, origin)
}
// Tests that the trie scheduler can correctly reconstruct the state even if only
// partial results are returned, and the others sent only later.
func TestIterativeDelayedTrieSync(t *testing.T) {
func TestIterativeDelayedTrieSyncDangling(t *testing.T) {
testIterativeDelayedTrieSync(t, common.Hash{})
}
func TestIterativeDelayedTrieSyncRooted(t *testing.T) {
testIterativeDelayedTrieSync(t, common.BytesToHash([]byte{0x04}))
}
func testIterativeDelayedTrieSync(t *testing.T, origin common.Hash) {
// Create a random trie to copy
srcDb, srcTrie, srcData := makeTestTrie()
srcDb, srcTrie, srcData := makeTestTrie(nil)
// Create a destination trie and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, origin, nil)
queue := append([]common.Hash{}, sched.Missing(10000)...)
for len(queue) > 0 {
@ -198,22 +234,32 @@ func TestIterativeDelayedTrieSync(t *testing.T) {
queue = append(queue[len(results):], sched.Missing(10000)...)
}
// Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
checkTrieContents(t, dstDb, srcTrie.Root(), srcData, origin)
}
// Tests that given a root hash, a trie can sync iteratively on a single thread,
// requesting retrieval tasks and returning all of them in one go, however in a
// random order.
func TestIterativeRandomTrieSyncIndividual(t *testing.T) { testIterativeRandomTrieSync(t, 1) }
func TestIterativeRandomTrieSyncBatched(t *testing.T) { testIterativeRandomTrieSync(t, 100) }
func TestIterativeRandomTrieSyncIndividualDangling(t *testing.T) {
testIterativeRandomTrieSync(t, 1, common.Hash{})
}
func TestIterativeRandomTrieSyncIndividualRooted(t *testing.T) {
testIterativeRandomTrieSync(t, 1, common.BytesToHash([]byte{0x05}))
}
func TestIterativeRandomTrieSyncBatchedDangling(t *testing.T) {
testIterativeRandomTrieSync(t, 100, common.Hash{})
}
func TestIterativeRandomTrieSyncBatchedlRooted(t *testing.T) {
testIterativeRandomTrieSync(t, 100, common.BytesToHash([]byte{0x06}))
}
func testIterativeRandomTrieSync(t *testing.T, batch int) {
func testIterativeRandomTrieSync(t *testing.T, batch int, origin common.Hash) {
// Create a random trie to copy
srcDb, srcTrie, srcData := makeTestTrie()
srcDb, srcTrie, srcData := makeTestTrie(nil)
// Create a destination trie and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, origin, nil)
queue := make(map[common.Hash]struct{})
for _, hash := range sched.Missing(batch) {
@ -239,18 +285,25 @@ func testIterativeRandomTrieSync(t *testing.T, batch int) {
}
}
// Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
checkTrieContents(t, dstDb, srcTrie.Root(), srcData, origin)
}
// Tests that the trie scheduler can correctly reconstruct the state even if only
// partial results are returned (Even those randomly), others sent only later.
func TestIterativeRandomDelayedTrieSync(t *testing.T) {
func TestIterativeRandomDelayedTrieSyncDangling(t *testing.T) {
testIterativeRandomDelayedTrieSync(t, common.Hash{})
}
func TestIterativeRandomDelayedTrieSyncRooted(t *testing.T) {
testIterativeRandomDelayedTrieSync(t, common.BytesToHash([]byte{0x07}))
}
func testIterativeRandomDelayedTrieSync(t *testing.T, origin common.Hash) {
// Create a random trie to copy
srcDb, srcTrie, srcData := makeTestTrie()
srcDb, srcTrie, srcData := makeTestTrie(nil)
// Create a destination trie and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, origin, nil)
queue := make(map[common.Hash]struct{})
for _, hash := range sched.Missing(10000) {
@ -282,18 +335,25 @@ func TestIterativeRandomDelayedTrieSync(t *testing.T) {
}
}
// Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
checkTrieContents(t, dstDb, srcTrie.Root(), srcData, origin)
}
// Tests that a trie sync will not request nodes multiple times, even if they
// have such references.
func TestDuplicateAvoidanceTrieSync(t *testing.T) {
func TestDuplicateAvoidanceTrieSyncDangling(t *testing.T) {
testDuplicateAvoidanceTrieSync(t, common.Hash{})
}
func TestDuplicateAvoidanceTrieSyncRooted(t *testing.T) {
testDuplicateAvoidanceTrieSync(t, common.BytesToHash([]byte{0x08}))
}
func testDuplicateAvoidanceTrieSync(t *testing.T, origin common.Hash) {
// Create a random trie to copy
srcDb, srcTrie, srcData := makeTestTrie()
srcDb, srcTrie, srcData := makeTestTrie(nil)
// Create a destination trie and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, origin, nil)
queue := append([]common.Hash{}, sched.Missing(0)...)
requested := make(map[common.Hash]struct{})
@ -318,18 +378,18 @@ func TestDuplicateAvoidanceTrieSync(t *testing.T) {
queue = append(queue[:0], sched.Missing(0)...)
}
// Cross check that the two tries are in sync
checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
checkTrieContents(t, dstDb, srcTrie.Root(), srcData, origin)
}
// 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()
srcDb, srcTrie, _ := makeTestTrie(nil)
// Create a destination trie and sync with the scheduler
dstDb, _ := ethdb.NewMemDatabase()
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, common.Hash{}, nil)
added := []common.Hash{}
queue := append([]common.Hash{}, sched.Missing(1)...)

View file

@ -146,50 +146,30 @@ func (t *Trie) TryGet(key []byte) ([]byte, error) {
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 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. In addition, 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, references [][]byte) {
if err := t.TryUpdateIndexed(key, value, references); 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.
//
// 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 {
return t.tryUpdateIndexed(key, value, 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) 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)
}
}
// TryUpdateIndexed is an extended version of Update, where state trie index
// TryUpdateIndexed 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. In addition, 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) TryUpdateIndexed(key, value []byte, refs [][]byte) error {
return t.tryUpdateIndexed(key, value, refs)
func (t *Trie) TryUpdateIndexed(key, value []byte, references [][]byte) error {
return t.tryUpdateIndexed(key, value, references)
}
func (t *Trie) tryUpdateIndexed(key, value []byte, refs [][]byte) error {
@ -439,31 +419,32 @@ func (t *Trie) Root() []byte { return t.Hash().Bytes() }
// Hash returns the root hash of the trie. It does not write to the
// database and can be used even if the trie doesn't have one.
func (t *Trie) Hash() common.Hash {
root, _ := t.hashRoot(nil)
root, _ := t.hashRoot(nil, nil)
return common.BytesToHash(root.(hashNode))
}
// Commit writes all nodes to the trie's database.
// Nodes are stored with their sha3 hash as the key.
// CommitIndexed writes all nodes to the trie's database, and if any references
// were specified, adds those to the trie index as well. Nodes are stored with
// their sha3 hash as the key.
//
// Committing flushes nodes from memory.
// Subsequent Get calls will load nodes from the database.
func (t *Trie) Commit() (root common.Hash, err error) {
// Committing flushes nodes from memory. Subsequent Get calls will load nodes
// from the database.
func (t *Trie) CommitIndexed(referrers [][]byte) (root common.Hash, err error) {
if t.db == nil {
panic("Commit called on trie with nil database")
}
return t.CommitTo(t.db)
return t.CommitToIndexed(t.db, referrers)
}
// CommitTo writes all nodes to the given database.
// Nodes are stored with their sha3 hash as the key.
// CommitToIndexed writes all nodes to the given database, and if any references
// were specified, adds those to the trie index as well. Nodes are stored with
// their sha3 hash as the key.
//
// Committing flushes nodes from memory. Subsequent Get calls will
// load nodes from the trie's database. Calling code must ensure that
// the changes made to db are written back to the trie's attached
// database before using the trie.
func (t *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
n, err := t.hashRoot(db)
// Committing flushes nodes from memory. Subsequent Get calls will load nodes
// from the trie's database. Calling code must ensure that the changes made to
// db are written back to the trie's attached database before using the trie.
func (t *Trie) CommitToIndexed(db DatabaseWriter, referrers [][]byte) (root common.Hash, err error) {
n, err := t.hashRoot(db, referrers)
if err != nil {
return (common.Hash{}), err
}
@ -471,14 +452,14 @@ func (t *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
return common.BytesToHash(n.(hashNode)), nil
}
func (t *Trie) hashRoot(db DatabaseWriter) (node, error) {
func (t *Trie) hashRoot(db DatabaseWriter, referrers [][]byte) (node, error) {
if t.root == nil {
return hashNode(emptyRoot.Bytes()), nil
}
if t.hasher == nil {
t.hasher = newHasher()
}
return t.hasher.hash(t.root, db, true)
return t.hasher.hash(t.root, db, true, referrers)
}
type hasher struct {
@ -490,12 +471,12 @@ func newHasher() *hasher {
return &hasher{tmp: new(bytes.Buffer), sha: sha3.NewKeccak256()}
}
func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, error) {
func (h *hasher) hash(n node, db DatabaseWriter, force bool, referrers [][]byte) (node, error) {
hashed, err := h.replaceChildren(n, db)
if err != nil {
return hashNode{}, err
}
if n, err = h.store(hashed, db, force); err != nil {
if n, err = h.store(hashed, db, force, referrers); err != nil {
return hashNode{}, err
}
return n, nil
@ -509,7 +490,7 @@ func (h *hasher) replaceChildren(n node, db DatabaseWriter) (node, error) {
case shortNode:
n.Key = compactEncode(n.Key)
if _, ok := n.Val.(valueNode); !ok {
if n.Val, err = h.hash(n.Val, db, false); err != nil {
if n.Val, err = h.hash(n.Val, db, false, nil); err != nil {
return n, err
}
}
@ -521,7 +502,7 @@ func (h *hasher) replaceChildren(n node, db DatabaseWriter) (node, error) {
case fullNode:
for i := 0; i < 16; i++ {
if n[i] != nil {
if n[i], err = h.hash(n[i], db, false); err != nil {
if n[i], err = h.hash(n[i], db, false, nil); err != nil {
return n, err
}
} else {
@ -538,7 +519,7 @@ func (h *hasher) replaceChildren(n node, db DatabaseWriter) (node, error) {
}
}
func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
func (h *hasher) store(n node, db DatabaseWriter, force bool, referrers [][]byte) (node, error) {
// Don't store hashes or empty nodes.
if _, isHash := n.(hashNode); n == nil || isHash {
return n, nil
@ -559,6 +540,11 @@ func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
if err := storeParentReferences(key, n, db); err != nil {
return key, err
}
for _, referrer := range referrers {
if err := storeParentReferenceEntry(referrer, key, db); err != nil {
return key, err
}
}
if err := db.Put(key, h.tmp.Bytes()); err != nil {
return key, err
}

View file

@ -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)
}
@ -74,7 +74,7 @@ func TestMissingNode(t *testing.T) {
trie, _ := New(common.Hash{}, db)
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
root, _ := trie.Commit()
root, _ := trie.CommitIndexed(nil)
ClearGlobalCache()
@ -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)
}
@ -159,7 +159,7 @@ func TestInsert(t *testing.T) {
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
root, err := trie.Commit()
root, err := trie.CommitIndexed(nil)
if err != nil {
t.Fatalf("commit error: %v", err)
}
@ -188,7 +188,7 @@ func TestGet(t *testing.T) {
if i == 1 {
return
}
trie.Commit()
trie.CommitIndexed(nil)
}
}
@ -257,7 +257,7 @@ func TestReplication(t *testing.T) {
for _, val := range vals {
updateString(trie, val.k, val.v)
}
exp, err := trie.Commit()
exp, err := trie.CommitIndexed(nil)
if err != nil {
t.Fatalf("commit error: %v", err)
}
@ -272,7 +272,7 @@ func TestReplication(t *testing.T) {
t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v)
}
}
hash, err := trie2.Commit()
hash, err := trie2.CommitIndexed(nil)
if err != nil {
t.Fatalf("commit 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
}
@ -327,7 +327,7 @@ func TestParanoia(t *testing.T) {
for _, val := range vals {
updateString(trie, val.k, val.v)
}
trie.Commit()
trie.CommitIndexed(nil)
ok, t2 := paranoiaCheck(trie)
if !ok {
@ -347,7 +347,7 @@ func TestOutput(t *testing.T) {
fmt.Println("############################## FULL ################################")
fmt.Println(trie.root)
trie.Commit()
trie.CommitIndexed(nil)
fmt.Println("############################## SMALL ################################")
trie2, _ := New(trie.Hash(), trie.db)
getString(trie2, base+"20")
@ -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,11 +419,11 @@ 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 {
trie.Commit()
trie.CommitIndexed(nil)
}
b.ResetTimer()
@ -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) {