core, trie: port snap sync changes #24898 (#1096)

core, eth, les, trie: rework snap sync

Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
This commit is contained in:
Daniel Liu 2025-08-26 15:20:39 +08:00 committed by GitHub
parent 3d69b27ae4
commit 18d2dbad89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 840 additions and 483 deletions

View file

@ -136,7 +136,7 @@ func (l *liquidationPriceState) updateRoot(db Database) error {
if l.dbErr != nil {
return l.dbErr
}
root, err := l.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err := l.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil

View file

@ -245,7 +245,7 @@ func (te *tradingExchanges) CommitAsksTrie(db Database) error {
if te.dbErr != nil {
return te.dbErr
}
root, err := te.asksTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err := te.asksTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
@ -307,7 +307,7 @@ func (te *tradingExchanges) CommitBidsTrie(db Database) error {
if te.dbErr != nil {
return te.dbErr
}
root, err := te.bidsTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err := te.bidsTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
@ -783,7 +783,7 @@ func (t *tradingExchanges) CommitLiquidationPriceTrie(db Database) error {
if t.dbErr != nil {
return t.dbErr
}
root, err := t.liquidationPriceTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err := t.liquidationPriceTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var orderList orderList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil

View file

@ -589,7 +589,7 @@ func (t *TradingStateDB) Commit() (root common.Hash, err error) {
}
}
// Write trie changes.
root, err = t.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err = t.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var exchange tradingExchangeObject
if err := rlp.DecodeBytes(leaf, &exchange); err != nil {
return nil

View file

@ -472,7 +472,7 @@ func (le *lendingExchangeState) CommitInvestingTrie(db Database) error {
if le.dbErr != nil {
return le.dbErr
}
root, err := le.investingTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err := le.investingTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var orderList itemList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
@ -493,7 +493,7 @@ func (le *lendingExchangeState) CommitBorrowingTrie(db Database) error {
if le.dbErr != nil {
return le.dbErr
}
root, err := le.borrowingTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err := le.borrowingTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var orderList itemList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil
@ -514,7 +514,7 @@ func (le *lendingExchangeState) CommitLiquidationTimeTrie(db Database) error {
if le.dbErr != nil {
return le.dbErr
}
root, err := le.liquidationTimeTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err := le.liquidationTimeTrie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var orderList itemList
if err := rlp.DecodeBytes(leaf, &orderList); err != nil {
return nil

View file

@ -578,7 +578,7 @@ func (ls *LendingStateDB) Commit() (root common.Hash, err error) {
}
}
// Write trie changes.
root, err = ls.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, err = ls.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var exchange lendingObject
if err := rlp.DecodeBytes(leaf, &exchange); err != nil {
return nil

View file

@ -835,7 +835,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
// Write the account trie changes, measuing the amount of wasted time
start := time.Now()
root, accountCommitted, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
root, accountCommitted, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, _ []byte) error {
var account types.StateAccount
if err := rlp.DecodeBytes(leaf, &account); err != nil {
return nil

View file

@ -27,20 +27,20 @@ import (
)
// NewStateSync create a new state trie download scheduler.
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(paths [][]byte, leaf []byte) error) *trie.Sync {
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(keys [][]byte, leaf []byte) error) *trie.Sync {
// Register the storage slot callback if the external callback is specified.
var onSlot func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error
var onSlot func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error
if onLeaf != nil {
onSlot = func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error {
return onLeaf(paths, leaf)
onSlot = func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error {
return onLeaf(keys, leaf)
}
}
// Register the account callback to connect the state trie and the storage
// trie belongs to the contract.
var syncer *trie.Sync
onAccount := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error {
onAccount := func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error {
if onLeaf != nil {
if err := onLeaf(paths, leaf); err != nil {
if err := onLeaf(keys, leaf); err != nil {
return err
}
}
@ -48,8 +48,8 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(p
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
return err
}
syncer.AddSubTrie(obj.Root, hexpath, parent, onSlot)
syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), hexpath, parent)
syncer.AddSubTrie(obj.Root, path, parent, parentPath, onSlot)
syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), path, parent, parentPath)
return nil
}
syncer = trie.NewSync(root, database, onAccount)

View file

@ -133,8 +133,8 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) error {
// Tests that an empty state is not scheduled for syncing.
func TestEmptyStateSync(t *testing.T) {
sync := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), nil)
if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
t.Errorf(" content requested for empty state: %v, %v, %v", nodes, paths, codes)
if paths, nodes, codes := sync.Missing(1); len(paths) != 0 || len(nodes) != 0 || len(codes) != 0 {
t.Errorf("content requested for empty state: %v, %v, %v", nodes, paths, codes)
}
}
@ -159,6 +159,14 @@ func TestIterativeStateSyncBatchedByPath(t *testing.T) {
testIterativeStateSync(t, 100, false, true)
}
// stateElement represents the element in the state trie(bytecode or trie node).
type stateElement struct {
path string
hash common.Hash
code common.Hash
syncPath trie.SyncPath
}
func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
// Create a random state to copy
srcDb, srcRoot, srcAccounts := makeTestState()
@ -171,54 +179,73 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
dstDb := rawdb.NewMemoryDatabase()
sched := NewStateSync(srcRoot, dstDb, nil)
nodes, paths, codes := sched.Missing(count)
var (
hashQueue []common.Hash
pathQueue []trie.SyncPath
nodeElements []stateElement
codeElements []stateElement
)
if !bypath {
hashQueue = append(append(hashQueue[:0], nodes...), codes...)
} else {
hashQueue = append(hashQueue[:0], codes...)
pathQueue = append(pathQueue[:0], paths...)
paths, nodes, codes := sched.Missing(count)
for i := 0; i < len(paths); i++ {
nodeElements = append(nodeElements, stateElement{
path: paths[i],
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(paths[i])),
})
}
for len(hashQueue)+len(pathQueue) > 0 {
results := make([]trie.SyncResult, len(hashQueue)+len(pathQueue))
for i, hash := range hashQueue {
data, err := srcDb.TrieDB().Node(hash)
for i := 0; i < len(codes); i++ {
codeElements = append(codeElements, stateElement{
code: codes[i],
})
}
for len(nodeElements)+len(codeElements) > 0 {
var (
nodeResults = make([]trie.NodeSyncResult, len(nodeElements))
codeResults = make([]trie.CodeSyncResult, len(codeElements))
)
for i, element := range codeElements {
data, err := srcDb.ContractCode(common.Hash{}, element.code)
if err != nil {
data, err = srcDb.ContractCode(common.Hash{}, hash)
t.Fatalf("failed to retrieve contract bytecode for hash %x", element.code)
}
if err != nil {
t.Fatalf("failed to retrieve node data for hash %x", hash)
}
results[i] = trie.SyncResult{Hash: hash, Data: data}
codeResults[i] = trie.CodeSyncResult{Hash: element.code, Data: data}
}
for i, path := range pathQueue {
if len(path) == 1 {
data, _, err := srcTrie.TryGetNode(path[0])
if err != nil {
t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
for i, node := range nodeElements {
if bypath {
if len(node.syncPath) == 1 {
data, _, err := srcTrie.TryGetNode(node.syncPath[0])
if err != nil {
t.Fatalf("failed to retrieve node data for path %x: %v", node.syncPath[0], err)
}
nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data}
} else {
var acc types.StateAccount
if err := rlp.DecodeBytes(srcTrie.Get(node.syncPath[0]), &acc); err != nil {
t.Fatalf("failed to decode account on path %x: %v", node.syncPath[0], err)
}
stTrie, err := trie.New(common.BytesToHash(node.syncPath[0]), acc.Root, srcDb.TrieDB())
if err != nil {
t.Fatalf("failed to retriev storage trie for path %x: %v", node.syncPath[1], err)
}
data, _, err := stTrie.TryGetNode(node.syncPath[1])
if err != nil {
t.Fatalf("failed to retrieve node data for path %x: %v", node.syncPath[1], err)
}
nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data}
}
results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
} else {
var acc types.StateAccount
if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil {
t.Fatalf("failed to decode account on path %x: %v", path, err)
}
stTrie, err := trie.New(common.BytesToHash(path[0]), acc.Root, srcDb.TrieDB())
data, err := srcDb.TrieDB().Node(node.hash)
if err != nil {
t.Fatalf("failed to retriev storage trie for path %x: %v", path, err)
t.Fatalf("failed to retrieve node data for key %v", []byte(node.path))
}
data, _, err := stTrie.TryGetNode(path[1])
if err != nil {
t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
}
results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data}
}
}
for _, result := range results {
if err := sched.Process(result); err != nil {
for _, result := range codeResults {
if err := sched.ProcessCode(result); err != nil {
t.Errorf("failed to process result %v", err)
}
}
for _, result := range nodeResults {
if err := sched.ProcessNode(result); err != nil {
t.Errorf("failed to process result %v", err)
}
}
@ -228,12 +255,20 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
}
batch.Write()
nodes, paths, codes = sched.Missing(count)
if !bypath {
hashQueue = append(append(hashQueue[:0], nodes...), codes...)
} else {
hashQueue = append(hashQueue[:0], codes...)
pathQueue = append(pathQueue[:0], paths...)
paths, nodes, codes = sched.Missing(count)
nodeElements = nodeElements[:0]
for i := 0; i < len(paths); i++ {
nodeElements = append(nodeElements, stateElement{
path: paths[i],
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(paths[i])),
})
}
codeElements = codeElements[:0]
for i := 0; i < len(codes); i++ {
codeElements = append(codeElements, stateElement{
code: codes[i],
})
}
}
// Cross check that the two states are in sync
@ -250,26 +285,58 @@ func TestIterativeDelayedStateSync(t *testing.T) {
dstDb := rawdb.NewMemoryDatabase()
sched := NewStateSync(srcRoot, dstDb, nil)
nodes, _, codes := sched.Missing(0)
queue := append(append([]common.Hash{}, nodes...), codes...)
for len(queue) > 0 {
var (
nodeElements []stateElement
codeElements []stateElement
)
paths, nodes, codes := sched.Missing(0)
for i := 0; i < len(paths); i++ {
nodeElements = append(nodeElements, stateElement{
path: paths[i],
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(paths[i])),
})
}
for i := 0; i < len(codes); i++ {
codeElements = append(codeElements, stateElement{
code: codes[i],
})
}
for len(nodeElements)+len(codeElements) > 0 {
// Sync only half of the scheduled nodes
results := make([]trie.SyncResult, len(queue)/2+1)
for i, hash := range queue[:len(results)] {
data, err := srcDb.TrieDB().Node(hash)
if err != nil {
data, err = srcDb.ContractCode(common.Hash{}, hash)
var nodeProcessd int
var codeProcessd int
if len(codeElements) > 0 {
codeResults := make([]trie.CodeSyncResult, len(codeElements)/2+1)
for i, element := range codeElements[:len(codeResults)] {
data, err := srcDb.ContractCode(common.Hash{}, element.code)
if err != nil {
t.Fatalf("failed to retrieve contract bytecode for %x", element.code)
}
codeResults[i] = trie.CodeSyncResult{Hash: element.code, Data: data}
}
if err != nil {
t.Fatalf("failed to retrieve node data for %x", hash)
for _, result := range codeResults {
if err := sched.ProcessCode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
results[i] = trie.SyncResult{Hash: hash, Data: data}
codeProcessd = len(codeResults)
}
for _, result := range results {
if err := sched.Process(result); err != nil {
t.Fatalf("failed to process result %v", err)
if len(nodeElements) > 0 {
nodeResults := make([]trie.NodeSyncResult, len(nodeElements)/2+1)
for i, element := range nodeElements[:len(nodeResults)] {
data, err := srcDb.TrieDB().Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve contract bytecode for %x", element.code)
}
nodeResults[i] = trie.NodeSyncResult{Path: element.path, Data: data}
}
for _, result := range nodeResults {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
nodeProcessd = len(nodeResults)
}
batch := dstDb.NewBatch()
if err := sched.Commit(batch); err != nil {
@ -277,8 +344,21 @@ func TestIterativeDelayedStateSync(t *testing.T) {
}
batch.Write()
nodes, _, codes = sched.Missing(0)
queue = append(append(queue[len(results):], nodes...), codes...)
paths, nodes, codes = sched.Missing(0)
nodeElements = nodeElements[nodeProcessd:]
for i := 0; i < len(paths); i++ {
nodeElements = append(nodeElements, stateElement{
path: paths[i],
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(paths[i])),
})
}
codeElements = codeElements[codeProcessd:]
for i := 0; i < len(codes); i++ {
codeElements = append(codeElements, stateElement{
code: codes[i],
})
}
}
// Cross check that the two states are in sync
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
@ -298,40 +378,70 @@ func testIterativeRandomStateSync(t *testing.T, count int) {
dstDb := rawdb.NewMemoryDatabase()
sched := NewStateSync(srcRoot, dstDb, nil)
queue := make(map[common.Hash]struct{})
nodes, _, codes := sched.Missing(count)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
nodeQueue := make(map[string]stateElement)
codeQueue := make(map[common.Hash]struct{})
paths, nodes, codes := sched.Missing(count)
for i, path := range paths {
nodeQueue[path] = stateElement{
path: path,
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(path)),
}
}
for len(queue) > 0 {
for _, hash := range codes {
codeQueue[hash] = struct{}{}
}
for len(nodeQueue)+len(codeQueue) > 0 {
// Fetch all the queued nodes in a random order
results := make([]trie.SyncResult, 0, len(queue))
for hash := range queue {
data, err := srcDb.TrieDB().Node(hash)
if err != nil {
data, err = srcDb.ContractCode(common.Hash{}, hash)
if len(codeQueue) > 0 {
results := make([]trie.CodeSyncResult, 0, len(codeQueue))
for hash := range codeQueue {
data, err := srcDb.ContractCode(common.Hash{}, hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x", hash)
}
results = append(results, trie.CodeSyncResult{Hash: hash, Data: data})
}
if err != nil {
t.Fatalf("failed to retrieve node data for %x", hash)
for _, result := range results {
if err := sched.ProcessCode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
}
if len(nodeQueue) > 0 {
results := make([]trie.NodeSyncResult, 0, len(nodeQueue))
for path, element := range nodeQueue {
data, err := srcDb.TrieDB().Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x %v %v", element.hash, []byte(element.path), element.path)
}
results = append(results, trie.NodeSyncResult{Path: path, Data: data})
}
for _, result := range results {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
results = append(results, trie.SyncResult{Hash: hash, Data: data})
}
// Feed the retrieved results back and queue new tasks
for _, result := range results {
if err := sched.Process(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
batch := dstDb.NewBatch()
if err := sched.Commit(batch); err != nil {
t.Fatalf("failed to commit data: %v", err)
}
batch.Write()
queue = make(map[common.Hash]struct{})
nodes, _, codes = sched.Missing(count)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
nodeQueue = make(map[string]stateElement)
codeQueue = make(map[common.Hash]struct{})
paths, nodes, codes := sched.Missing(count)
for i, path := range paths {
nodeQueue[path] = stateElement{
path: path,
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(path)),
}
}
for _, hash := range codes {
codeQueue[hash] = struct{}{}
}
}
// Cross check that the two states are in sync
@ -348,34 +458,62 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
dstDb := rawdb.NewMemoryDatabase()
sched := NewStateSync(srcRoot, dstDb, nil)
queue := make(map[common.Hash]struct{})
nodes, _, codes := sched.Missing(0)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
nodeQueue := make(map[string]stateElement)
codeQueue := make(map[common.Hash]struct{})
paths, nodes, codes := sched.Missing(0)
for i, path := range paths {
nodeQueue[path] = stateElement{
path: path,
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(path)),
}
}
for len(queue) > 0 {
for _, hash := range codes {
codeQueue[hash] = struct{}{}
}
for len(nodeQueue)+len(codeQueue) > 0 {
// Sync only half of the scheduled nodes, even those in random order
results := make([]trie.SyncResult, 0, len(queue)/2+1)
for hash := range queue {
delete(queue, hash)
if len(codeQueue) > 0 {
results := make([]trie.CodeSyncResult, 0, len(codeQueue)/2+1)
for hash := range codeQueue {
delete(codeQueue, hash)
data, err := srcDb.TrieDB().Node(hash)
if err != nil {
data, err = srcDb.ContractCode(common.Hash{}, hash)
}
if err != nil {
t.Fatalf("failed to retrieve node data for %x", hash)
}
results = append(results, trie.SyncResult{Hash: hash, Data: data})
data, err := srcDb.ContractCode(common.Hash{}, hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x", hash)
}
results = append(results, trie.CodeSyncResult{Hash: hash, Data: data})
if len(results) >= cap(results) {
break
if len(results) >= cap(results) {
break
}
}
for _, result := range results {
if err := sched.ProcessCode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
}
// Feed the retrieved results back and queue new tasks
for _, result := range results {
if err := sched.Process(result); err != nil {
t.Fatalf("failed to process result %v", err)
if len(nodeQueue) > 0 {
results := make([]trie.NodeSyncResult, 0, len(nodeQueue)/2+1)
for path, element := range nodeQueue {
delete(nodeQueue, path)
data, err := srcDb.TrieDB().Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x", element.hash)
}
results = append(results, trie.NodeSyncResult{Path: path, Data: data})
if len(results) >= cap(results) {
break
}
}
// Feed the retrieved results back and queue new tasks
for _, result := range results {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
}
batch := dstDb.NewBatch()
@ -383,12 +521,17 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
t.Fatalf("failed to commit data: %v", err)
}
batch.Write()
for _, result := range results {
delete(queue, result.Hash)
paths, nodes, codes := sched.Missing(0)
for i, path := range paths {
nodeQueue[path] = stateElement{
path: path,
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(path)),
}
}
nodes, _, codes = sched.Missing(0)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
for _, hash := range codes {
codeQueue[hash] = struct{}{}
}
}
// Cross check that the two states are in sync
@ -415,28 +558,62 @@ func TestIncompleteStateSync(t *testing.T) {
dstDb := rawdb.NewMemoryDatabase()
sched := NewStateSync(srcRoot, dstDb, nil)
var added []common.Hash
nodes, _, codes := sched.Missing(1)
queue := append(append([]common.Hash{}, nodes...), codes...)
for len(queue) > 0 {
// Fetch a batch of state nodes
results := make([]trie.SyncResult, len(queue))
for i, hash := range queue {
data, err := srcDb.TrieDB().Node(hash)
if err != nil {
data, err = srcDb.ContractCode(common.Hash{}, hash)
}
if err != nil {
t.Fatalf("failed to retrieve node data for %x", hash)
}
results[i] = trie.SyncResult{Hash: hash, Data: data}
var (
addedCodes []common.Hash
addedNodes []common.Hash
)
nodeQueue := make(map[string]stateElement)
codeQueue := make(map[common.Hash]struct{})
paths, nodes, codes := sched.Missing(1)
for i, path := range paths {
nodeQueue[path] = stateElement{
path: path,
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(path)),
}
// Process each of the state nodes
for _, result := range results {
if err := sched.Process(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
for _, hash := range codes {
codeQueue[hash] = struct{}{}
}
for len(nodeQueue)+len(codeQueue) > 0 {
// Fetch a batch of state nodes
if len(codeQueue) > 0 {
results := make([]trie.CodeSyncResult, 0, len(codeQueue))
for hash := range codeQueue {
data, err := srcDb.ContractCode(common.Hash{}, hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x", hash)
}
results = append(results, trie.CodeSyncResult{Hash: hash, Data: data})
addedCodes = append(addedCodes, hash)
}
// Process each of the state nodes
for _, result := range results {
if err := sched.ProcessCode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
}
var nodehashes []common.Hash
if len(nodeQueue) > 0 {
results := make([]trie.NodeSyncResult, 0, len(nodeQueue))
for key, element := range nodeQueue {
data, err := srcDb.TrieDB().Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x", element.hash)
}
results = append(results, trie.NodeSyncResult{Path: key, Data: data})
if element.hash != srcRoot {
addedNodes = append(addedNodes, element.hash)
}
nodehashes = append(nodehashes, element.hash)
}
// Process each of the state nodes
for _, result := range results {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
}
batch := dstDb.NewBatch()
@ -444,43 +621,44 @@ func TestIncompleteStateSync(t *testing.T) {
t.Fatalf("failed to commit data: %v", err)
}
batch.Write()
for _, result := range results {
added = append(added, result.Hash)
// Check that all known sub-tries added so far are complete or missing entirely.
if _, ok := isCode[result.Hash]; ok {
continue
}
for _, root := range nodehashes {
// Can't use checkStateConsistency here because subtrie keys may have odd
// length and crash in LeafKey.
if err := checkTrieConsistency(dstDb, result.Hash); err != nil {
if err := checkTrieConsistency(dstDb, root); err != nil {
t.Fatalf("state inconsistent: %v", err)
}
}
// Fetch the next batch to retrieve
nodes, _, codes = sched.Missing(1)
queue = append(append(queue[:0], nodes...), codes...)
nodeQueue = make(map[string]stateElement)
codeQueue = make(map[common.Hash]struct{})
paths, nodes, codes := sched.Missing(1)
for i, path := range paths {
nodeQueue[path] = stateElement{
path: path,
hash: nodes[i],
syncPath: trie.NewSyncPath([]byte(path)),
}
}
for _, hash := range codes {
codeQueue[hash] = struct{}{}
}
}
// Sanity check that removing any node from the database is detected
for _, node := range added[1:] {
var (
key = node.Bytes()
_, code = isCode[node]
val []byte
)
if code {
val = rawdb.ReadCode(dstDb, node)
rawdb.DeleteCode(dstDb, node)
} else {
val = rawdb.ReadTrieNode(dstDb, node)
rawdb.DeleteTrieNode(dstDb, node)
for _, node := range addedCodes {
val := rawdb.ReadCode(dstDb, node)
rawdb.DeleteCode(dstDb, node)
if err := checkStateConsistency(dstDb, srcRoot); err == nil {
t.Errorf("trie inconsistency not caught, missing: %x", node)
}
if err := checkStateConsistency(dstDb, added[0]); err == nil {
t.Fatalf("trie inconsistency not caught, missing: %x", key)
}
if code {
rawdb.WriteCode(dstDb, node, val)
} else {
rawdb.WriteTrieNode(dstDb, node, val)
rawdb.WriteCode(dstDb, node, val)
}
for _, node := range addedNodes {
val := rawdb.ReadTrieNode(dstDb, node)
rawdb.DeleteTrieNode(dstDb, node)
if err := checkStateConsistency(dstDb, srcRoot); err == nil {
t.Errorf("trie inconsistency not caught, missing: %v", node.Hex())
}
rawdb.WriteTrieNode(dstDb, node, val)
}
}

View file

@ -35,7 +35,7 @@ import (
// a single data retrieval network packet.
type stateReq struct {
nItems uint16 // Number of items requested for download (max is 384, so uint16 is sufficient)
trieTasks map[common.Hash]*trieTask // Trie node download tasks to track previous attempts
trieTasks map[string]*trieTask // Trie node download tasks to track previous attempts
codeTasks map[common.Hash]*codeTask // Byte code download tasks to track previous attempts
timeout time.Duration // Maximum round trip time for this to complete
timer *time.Timer // Timer to fire when the RTT timeout expires
@ -257,8 +257,8 @@ type stateSync struct {
sched *trie.Sync // State trie sync scheduler defining the tasks
keccak hash.Hash // Keccak256 hasher to verify deliveries with
trieTasks map[common.Hash]*trieTask // Set of trie node tasks currently queued for retrieval
codeTasks map[common.Hash]*codeTask // Set of byte code tasks currently queued for retrieval
trieTasks map[string]*trieTask // Set of trie node tasks currently queued for retrieval, indexed by path
codeTasks map[common.Hash]*codeTask // Set of byte code tasks currently queued for retrieval, indexed by hash
numUncommitted int
bytesUncommitted int
@ -277,6 +277,7 @@ type stateSync struct {
// trieTask represents a single trie node download task, containing a set of
// peers already attempted retrieval from to detect stalled syncs and abort.
type trieTask struct {
hash common.Hash
path [][]byte
attempts map[string]struct{}
}
@ -295,7 +296,7 @@ func newStateSync(d *Downloader, root common.Hash) *stateSync {
d: d,
sched: state.NewStateSync(root, d.stateDB, nil),
keccak: sha3.NewLegacyKeccak256(),
trieTasks: make(map[common.Hash]*trieTask),
trieTasks: make(map[string]*trieTask),
codeTasks: make(map[common.Hash]*codeTask),
deliver: make(chan *stateReq),
cancel: make(chan struct{}),
@ -446,10 +447,11 @@ func (s *stateSync) assignTasks() {
func (s *stateSync) fillTasks(n int, req *stateReq) (nodes []common.Hash, paths []trie.SyncPath, codes []common.Hash) {
// Refill available tasks from the scheduler.
if fill := n - (len(s.trieTasks) + len(s.codeTasks)); fill > 0 {
nodes, paths, codes := s.sched.Missing(fill)
for i, hash := range nodes {
s.trieTasks[hash] = &trieTask{
path: paths[i],
paths, hashes, codes := s.sched.Missing(fill)
for i, path := range paths {
s.trieTasks[path] = &trieTask{
hash: hashes[i],
path: trie.NewSyncPath([]byte(path)),
attempts: make(map[string]struct{}),
}
}
@ -465,7 +467,7 @@ func (s *stateSync) fillTasks(n int, req *stateReq) (nodes []common.Hash, paths
paths = make([]trie.SyncPath, 0, n)
codes = make([]common.Hash, 0, n)
req.trieTasks = make(map[common.Hash]*trieTask, n)
req.trieTasks = make(map[string]*trieTask, n)
req.codeTasks = make(map[common.Hash]*codeTask, n)
for hash, t := range s.codeTasks {
@ -483,7 +485,7 @@ func (s *stateSync) fillTasks(n int, req *stateReq) (nodes []common.Hash, paths
req.codeTasks[hash] = t
delete(s.codeTasks, hash)
}
for hash, t := range s.trieTasks {
for path, t := range s.trieTasks {
// Stop when we've gathered enough requests
if len(nodes)+len(codes) == n {
break
@ -495,11 +497,11 @@ func (s *stateSync) fillTasks(n int, req *stateReq) (nodes []common.Hash, paths
// Assign the request to this peer
t.attempts[req.peer.id] = struct{}{}
nodes = append(nodes, hash)
nodes = append(nodes, t.hash)
paths = append(paths, t.path)
req.trieTasks[hash] = t
delete(s.trieTasks, hash)
req.trieTasks[path] = t
delete(s.trieTasks, path)
}
req.nItems = uint16(len(nodes) + len(codes))
return nodes, paths, codes
@ -521,7 +523,7 @@ func (s *stateSync) process(req *stateReq) (int, error) {
// Iterate over all the delivered data and inject one-by-one into the trie
for _, blob := range req.response {
hash, err := s.processNodeData(blob)
hash, err := s.processNodeData(req.trieTasks, req.codeTasks, blob)
switch err {
case nil:
s.numUncommitted++
@ -534,13 +536,10 @@ func (s *stateSync) process(req *stateReq) (int, error) {
default:
return successful, fmt.Errorf("invalid state node %s: %v", hash.TerminalString(), err)
}
// Delete from both queues (one delivery is enough for the syncer)
delete(req.trieTasks, hash)
delete(req.codeTasks, hash)
}
// Put unfulfilled tasks back into the retry queue
npeers := s.d.peers.Len()
for hash, task := range req.trieTasks {
for path, task := range req.trieTasks {
// If the node did deliver something, missing items may be due to a protocol
// limit or a previous timeout + delayed delivery. Both cases should permit
// the node to retry the missing items (to avoid single-peer stalls).
@ -550,10 +549,10 @@ func (s *stateSync) process(req *stateReq) (int, error) {
// If we've requested the node too many times already, it may be a malicious
// sync where nobody has the right data. Abort.
if len(task.attempts) >= npeers {
return successful, fmt.Errorf("trie node %s failed with all peers (%d tries, %d peers)", hash.TerminalString(), len(task.attempts), npeers)
return successful, fmt.Errorf("trie node %s failed with all peers (%d tries, %d peers)", task.hash.TerminalString(), len(task.attempts), npeers)
}
// Missing item, place into the retry queue.
s.trieTasks[hash] = task
s.trieTasks[path] = task
}
for hash, task := range req.codeTasks {
// If the node did deliver something, missing items may be due to a protocol
@ -576,13 +575,34 @@ func (s *stateSync) process(req *stateReq) (int, error) {
// processNodeData tries to inject a trie node data blob delivered from a remote
// peer into the state trie, returning whether anything useful was written or any
// error occurred.
func (s *stateSync) processNodeData(blob []byte) (common.Hash, error) {
res := trie.SyncResult{Data: blob}
//
// If multiple requests correspond to the same hash, this method will inject the
// blob as a result for the first one only, leaving the remaining duplicates to
// be fetched again.
func (s *stateSync) processNodeData(nodeTasks map[string]*trieTask, codeTasks map[common.Hash]*codeTask, blob []byte) (common.Hash, error) {
s.keccak.Reset()
s.keccak.Write(blob)
s.keccak.Sum(res.Hash[:0])
err := s.sched.Process(res)
return res.Hash, err
hash := common.BytesToHash(s.keccak.Sum(nil))
if _, present := codeTasks[hash]; present {
err := s.sched.ProcessCode(trie.CodeSyncResult{
Hash: hash,
Data: blob,
})
delete(codeTasks, hash)
return hash, err
}
for path, task := range nodeTasks {
if task.hash == hash {
err := s.sched.ProcessNode(trie.NodeSyncResult{
Path: path,
Data: blob,
})
delete(nodeTasks, path)
return hash, err
}
}
return common.Hash{}, trie.ErrNotRequested
}
// updateStats bumps the various state sync progress counters and displays a log

View file

@ -32,7 +32,8 @@ const leafChanSize = 200
type leaf struct {
size int // size of the rlp data (estimate)
hash common.Hash // hash of rlp data
node node // the Node to commit
node node // the node to commit
path []byte // the path from the root node
}
// committer is a type used for the trie Commit operation. A committer has some
@ -69,15 +70,15 @@ func (c *committer) Commit(n node, db *Database) (hashNode, int, error) {
if db == nil {
return nil, 0, errors.New("no Db provided")
}
h, committed, err := c.commit(n, db)
h, committed, err := c.commit(nil, n, db)
if err != nil {
return nil, 0, err
}
return h.(hashNode), committed, nil
}
// commit collapses a Node down into a hash Node and inserts it into the database
func (c *committer) commit(n node, db *Database) (node, int, error) {
// commit collapses a node down into a hash node and inserts it into the database
func (c *committer) commit(path []byte, n node, db *Database) (node, int, error) {
// if this path is clean, use available cached data
hash, dirty := n.cache()
if hash != nil && !dirty {
@ -93,7 +94,7 @@ func (c *committer) commit(n node, db *Database) (node, int, error) {
// otherwise it can only be hashNode or valueNode.
var childCommitted int
if _, ok := cn.Val.(*fullNode); ok {
childV, committed, err := c.commit(cn.Val, db)
childV, committed, err := c.commit(append(path, cn.Key...), cn.Val, db)
if err != nil {
return nil, 0, err
}
@ -101,20 +102,20 @@ func (c *committer) commit(n node, db *Database) (node, int, error) {
}
// The key needs to be copied, since we're delivering it to database
collapsed.Key = hexToCompact(cn.Key)
hashedNode := c.store(collapsed, db)
hashedNode := c.store(path, collapsed, db)
if hn, ok := hashedNode.(hashNode); ok {
return hn, childCommitted + 1, nil
}
return collapsed, childCommitted, nil
case *fullNode:
hashedKids, childCommitted, err := c.commitChildren(cn, db)
hashedKids, childCommitted, err := c.commitChildren(path, cn, db)
if err != nil {
return nil, 0, err
}
collapsed := cn.copy()
collapsed.Children = hashedKids
hashedNode := c.store(collapsed, db)
hashedNode := c.store(path, collapsed, db)
if hn, ok := hashedNode.(hashNode); ok {
return hn, childCommitted + 1, nil
}
@ -128,7 +129,7 @@ func (c *committer) commit(n node, db *Database) (node, int, error) {
}
// commitChildren commits the children of the given fullnode
func (c *committer) commitChildren(n *fullNode, db *Database) ([17]node, int, error) {
func (c *committer) commitChildren(path []byte, n *fullNode, db *Database) ([17]node, int, error) {
var (
committed int
children [17]node
@ -148,7 +149,7 @@ func (c *committer) commitChildren(n *fullNode, db *Database) ([17]node, int, er
// Commit the child recursively and store the "hashed" value.
// Note the returned node can be some embedded nodes, so it's
// possible the type is not hashNode.
hashed, childCommitted, err := c.commit(child, db)
hashed, childCommitted, err := c.commit(append(path, byte(i)), child, db)
if err != nil {
return children, 0, err
}
@ -162,10 +163,10 @@ func (c *committer) commitChildren(n *fullNode, db *Database) ([17]node, int, er
return children, committed, nil
}
// store hashes the Node n and if we have a storage layer specified, it writes
// the key/value pair to it and tracks any Node->child references as well as any
// Node->external trie references.
func (c *committer) store(n node, db *Database) node {
// store hashes the node n and if we have a storage layer specified, it writes
// the key/value pair to it and tracks any node->child references as well as any
// node->external trie references.
func (c *committer) store(path []byte, n node, db *Database) node {
// Larger nodes are replaced by their hash and stored in the database.
var (
hash, _ = n.cache()
@ -189,6 +190,7 @@ func (c *committer) store(n node, db *Database) node {
size: size,
hash: common.BytesToHash(hash),
node: n,
path: path,
}
} else if db != nil {
// No leaf-callback used, but there's still a database. Do serial
@ -213,13 +215,13 @@ func (c *committer) commitLoop(db *Database) {
switch n := n.(type) {
case *shortNode:
if child, ok := n.Val.(valueNode); ok {
c.onleaf(nil, nil, child, hash)
c.onleaf(nil, nil, child, hash, nil)
}
case *fullNode:
// For children in range [0, 15], it's impossible
// to contain valueNode. Only check the 17th child.
if n.Children[16] != nil {
c.onleaf(nil, nil, n.Children[16].(valueNode), hash)
c.onleaf(nil, nil, n.Children[16].(valueNode), hash, nil)
}
}
}

View file

@ -25,6 +25,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log"
)
// ErrNotRequested is returned by the trie sync when it's requested to process a
@ -40,19 +41,6 @@ var ErrAlreadyProcessed = errors.New("already processed")
// memory if the node was configured with a significant number of peers.
const maxFetchesPerDepth = 16384
// request represents a scheduled or already in-flight state retrieval request.
type request struct {
path []byte // Merkle path leading to this node for prioritization
hash common.Hash // Hash of the Node data content to retrieve
data []byte // Data content of the Node, cached until all subtrees complete
code bool // Whether this is a code entry
parents []*request // Parent state nodes referencing this entry (notify all upon completion)
deps int // Number of dependencies before allowed to commit this Node
callback LeafCallback // Callback to invoke if a leaf Node it reached on this branch
}
// SyncPath is a path tuple identifying a particular trie node either in a single
// trie (account) or a layered trie (account -> storage).
//
@ -72,9 +60,9 @@ type request struct {
// - Path 0x012345678901234567890123456789010123456789012345678901234567890199 -> {0x0123456789012345678901234567890101234567890123456789012345678901, 0x0099}
type SyncPath [][]byte
// newSyncPath converts an expanded trie path from nibble form into a compact
// NewSyncPath converts an expanded trie path from nibble form into a compact
// version that can be sent over the network.
func newSyncPath(path []byte) SyncPath {
func NewSyncPath(path []byte) SyncPath {
// If the hash is from the account trie, append a single item, if it
// is from the a storage trie, append a tuple. Note, the length 64 is
// clashing between account leaf and storage root. It's fine though
@ -86,30 +74,57 @@ func newSyncPath(path []byte) SyncPath {
return SyncPath{hexToKeybytes(path[:64]), hexToCompact(path[64:])}
}
// SyncResult is a response with requested data along with it's hash.
type SyncResult struct {
Hash common.Hash // Hash of the originally unknown trie Node
Data []byte // Data content of the retrieved Node
// nodeRequest represents a scheduled or already in-flight trie node retrieval request.
type nodeRequest struct {
hash common.Hash // Hash of the trie node to retrieve
path []byte // Merkle path leading to this node for prioritization
data []byte // Data content of the node, cached until all subtrees complete
parent *nodeRequest // Parent state node referencing this entry
deps int // Number of dependencies before allowed to commit this node
callback LeafCallback // Callback to invoke if a leaf node it reached on this branch
}
// codeRequest represents a scheduled or already in-flight bytecode retrieval request.
type codeRequest struct {
hash common.Hash // Hash of the contract bytecode to retrieve
path []byte // Merkle path leading to this node for prioritization
data []byte // Data content of the node, cached until all subtrees complete
parents []*nodeRequest // Parent state nodes referencing this entry (notify all upon completion)
}
// NodeSyncResult is a response with requested trie node along with its node path.
type NodeSyncResult struct {
Path string // Path of the originally unknown trie node
Data []byte // Data content of the retrieved trie node
}
// CodeSyncResult is a response with requested bytecode along with its hash.
type CodeSyncResult struct {
Hash common.Hash // Hash the originally unknown bytecode
Data []byte // Data content of the retrieved bytecode
}
// syncMemBatch is an in-memory buffer of successfully downloaded but not yet
// persisted data items.
type syncMemBatch struct {
nodes map[common.Hash][]byte // In-memory membatch of recently completed nodes
codes map[common.Hash][]byte // In-memory membatch of recently completed codes
nodes map[string][]byte // In-memory membatch of recently completed nodes
hashes map[string]common.Hash // Hashes of recently completed nodes
codes map[common.Hash][]byte // In-memory membatch of recently completed codes
}
// newSyncMemBatch allocates a new memory-buffer for not-yet persisted trie nodes.
func newSyncMemBatch() *syncMemBatch {
return &syncMemBatch{
nodes: make(map[common.Hash][]byte),
codes: make(map[common.Hash][]byte),
nodes: make(map[string][]byte),
hashes: make(map[string]common.Hash),
codes: make(map[common.Hash][]byte),
}
}
// hasNode reports the trie node with specific hash is already cached.
func (batch *syncMemBatch) hasNode(hash common.Hash) bool {
_, ok := batch.nodes[hash]
// hasNode reports the trie node with specific path is already cached.
func (batch *syncMemBatch) hasNode(path []byte) bool {
_, ok := batch.nodes[string(path)]
return ok
}
@ -123,12 +138,12 @@ func (batch *syncMemBatch) hasCode(hash common.Hash) bool {
// unknown trie hashes to retrieve, accepts Node data associated with said hashes
// and reconstructs the trie step by step until all is done.
type Sync struct {
database ethdb.KeyValueReader // Persistent database to check for existing entries
membatch *syncMemBatch // Memory buffer to avoid frequent database writes
nodeReqs map[common.Hash]*request // Pending requests pertaining to a trie node hash
codeReqs map[common.Hash]*request // Pending requests pertaining to a code hash
queue *prque.Prque[int64, any] // Priority queue with the pending requests
fetches map[int]int // Number of active fetches per trie node depth
database ethdb.KeyValueReader // Persistent database to check for existing entries
membatch *syncMemBatch // Memory buffer to avoid frequent database writes
nodeReqs map[string]*nodeRequest // Pending requests pertaining to a trie node path
codeReqs map[common.Hash]*codeRequest // Pending requests pertaining to a code hash
queue *prque.Prque[int64, any] // Priority queue with the pending requests
fetches map[int]int // Number of active fetches per trie node depth
}
// NewSync creates a new trie data download scheduler.
@ -136,51 +151,51 @@ func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallb
ts := &Sync{
database: database,
membatch: newSyncMemBatch(),
nodeReqs: make(map[common.Hash]*request),
codeReqs: make(map[common.Hash]*request),
queue: prque.New[int64, any](nil), // Ugh, can contain both string and hash, whyyy
nodeReqs: make(map[string]*nodeRequest),
codeReqs: make(map[common.Hash]*codeRequest),
queue: prque.New[int64, any](nil),
fetches: make(map[int]int),
}
ts.AddSubTrie(root, nil, common.Hash{}, callback)
ts.AddSubTrie(root, nil, common.Hash{}, nil, callback)
return ts
}
// AddSubTrie registers a new trie to the sync code, rooted at the designated parent.
func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, callback LeafCallback) {
// AddSubTrie registers a new trie to the sync code, rooted at the designated
// parent for completion tracking. The given path is a unique node path in
// hex format and contain all the parent path if it's layered trie node.
func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, parentPath []byte, callback LeafCallback) {
// Short circuit if the trie is empty or already known
if root == types.EmptyRootHash {
return
}
if s.membatch.hasNode(root) {
if s.membatch.hasNode(path) {
return
}
// If database says this is a duplicate, then at least the trie node is
// present, and we hold the assumption that it's NOT legacy contract code.
if rawdb.HasTrieNode(s.database, root) {
return
}
// Assemble the new sub-trie sync request
req := &request{
path: path,
req := &nodeRequest{
hash: root,
path: path,
callback: callback,
}
// If this sub-trie has a designated parent, link them together
if parent != (common.Hash{}) {
ancestor := s.nodeReqs[parent]
ancestor := s.nodeReqs[string(parentPath)]
if ancestor == nil {
panic(fmt.Sprintf("sub-trie ancestor not found: %x", parent))
}
ancestor.deps++
req.parents = append(req.parents, ancestor)
req.parent = ancestor
}
s.schedule(req)
s.scheduleNodeRequest(req)
}
// AddCodeEntry schedules the direct retrieval of a contract code that should not
// be interpreted as a trie node, but rather accepted and stored into the database
// as is.
func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash) {
func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash, parentPath []byte) {
// Short circuit if the entry is empty or already known
if hash == types.EmptyCodeHash {
return
@ -197,30 +212,29 @@ func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash) {
return
}
// Assemble the new sub-trie sync request
req := &request{
req := &codeRequest{
path: path,
hash: hash,
code: true,
}
// If this sub-trie has a designated parent, link them together
if parent != (common.Hash{}) {
ancestor := s.nodeReqs[parent] // the parent of codereq can ONLY be nodereq
ancestor := s.nodeReqs[string(parentPath)] // the parent of codereq can ONLY be nodereq
if ancestor == nil {
panic(fmt.Sprintf("raw-entry ancestor not found: %x", parent))
}
ancestor.deps++
req.parents = append(req.parents, ancestor)
}
s.schedule(req)
s.scheduleCodeRequest(req)
}
// Missing retrieves the known missing nodes from the trie for retrieval. To aid
// both eth/6x style fast sync and snap/1x style state sync, the paths of trie
// nodes are returned too, as well as separate hash list for codes.
func (s *Sync) Missing(max int) (nodes []common.Hash, paths []SyncPath, codes []common.Hash) {
func (s *Sync) Missing(max int) ([]string, []common.Hash, []common.Hash) {
var (
nodePaths []string
nodeHashes []common.Hash
nodePaths []SyncPath
codeHashes []common.Hash
)
for !s.queue.Empty() && (max == 0 || len(nodeHashes)+len(codeHashes) < max) {
@ -236,62 +250,77 @@ func (s *Sync) Missing(max int) (nodes []common.Hash, paths []SyncPath, codes []
s.queue.Pop()
s.fetches[depth]++
hash := item.(common.Hash)
if req, ok := s.nodeReqs[hash]; ok {
nodeHashes = append(nodeHashes, hash)
nodePaths = append(nodePaths, newSyncPath(req.path))
} else {
codeHashes = append(codeHashes, hash)
switch item.(type) {
case common.Hash:
codeHashes = append(codeHashes, item.(common.Hash))
case string:
path := item.(string)
req, ok := s.nodeReqs[path]
if !ok {
log.Error("Missing node request", "path", path)
continue // System very wrong, shouldn't happen
}
nodePaths = append(nodePaths, path)
nodeHashes = append(nodeHashes, req.hash)
}
}
return nodeHashes, nodePaths, codeHashes
return nodePaths, nodeHashes, codeHashes
}
// Process injects the received data for requested item. Note it can
// ProcessCode injects the received data for requested item. Note it can
// happpen that the single response commits two pending requests(e.g.
// there are two requests one for code and one for node but the hash
// is same). In this case the second response for the same hash will
// be treated as "non-requested" item or "already-processed" item but
// there is no downside.
func (s *Sync) Process(result SyncResult) error {
// If the item was not requested either for code or node, bail out
if s.nodeReqs[result.Hash] == nil && s.codeReqs[result.Hash] == nil {
func (s *Sync) ProcessCode(result CodeSyncResult) error {
// If the code was not requested or it's already processed, bail out
req := s.codeReqs[result.Hash]
if req == nil {
return ErrNotRequested
}
// There is an pending code request for this data, commit directly
var filled bool
if req := s.codeReqs[result.Hash]; req != nil && req.data == nil {
filled = true
req.data = result.Data
s.commit(req)
}
// There is an pending node request for this data, fill it.
if req := s.nodeReqs[result.Hash]; req != nil && req.data == nil {
filled = true
// Decode the Node data content and update the request
node, err := decodeNode(result.Hash[:], result.Data)
if err != nil {
return err
}
req.data = result.Data
// Create and schedule a request for all the children nodes
requests, err := s.children(req, node)
if err != nil {
return err
}
if len(requests) == 0 && req.deps == 0 {
s.commit(req)
} else {
req.deps += len(requests)
for _, child := range requests {
s.schedule(child)
}
}
}
if !filled {
if req.data != nil {
return ErrAlreadyProcessed
}
req.data = result.Data
return s.commitCodeRequest(req)
}
// ProcessNode injects the received data for requested item. Note it can
// happen that the single response commits two pending requests(e.g.
// there are two requests one for code and one for node but the hash
// is same). In this case the second response for the same hash will
// be treated as "non-requested" item or "already-processed" item but
// there is no downside.
func (s *Sync) ProcessNode(result NodeSyncResult) error {
// If the trie node was not requested or it's already processed, bail out
req := s.nodeReqs[result.Path]
if req == nil {
return ErrNotRequested
}
if req.data != nil {
return ErrAlreadyProcessed
}
// Decode the node data content and update the request
node, err := decodeNode(req.hash.Bytes(), result.Data)
if err != nil {
return err
}
req.data = result.Data
// Create and schedule a request for all the children nodes
requests, err := s.children(req, node)
if err != nil {
return err
}
if len(requests) == 0 && req.deps == 0 {
s.commitNodeRequest(req)
} else {
req.deps += len(requests)
for _, child := range requests {
s.scheduleNodeRequest(child)
}
}
return nil
}
@ -299,11 +328,11 @@ func (s *Sync) Process(result SyncResult) error {
// storage, returning any occurred error.
func (s *Sync) Commit(dbw ethdb.Batch) error {
// Dump the membatch into a database dbw
for key, value := range s.membatch.nodes {
rawdb.WriteTrieNode(dbw, key, value)
for path, value := range s.membatch.nodes {
rawdb.WriteTrieNode(dbw, s.membatch.hashes[path], value)
}
for key, value := range s.membatch.codes {
rawdb.WriteCode(dbw, key, value)
for hash, value := range s.membatch.codes {
rawdb.WriteCode(dbw, hash, value)
}
// Drop the membatch data and return
s.membatch = newSyncMemBatch()
@ -318,23 +347,31 @@ func (s *Sync) Pending() int {
// schedule inserts a new state retrieval request into the fetch queue. If there
// is already a pending request for this Node, the new request will be discarded
// and only a parent reference added to the old one.
func (s *Sync) schedule(req *request) {
var reqset = s.nodeReqs
if req.code {
reqset = s.codeReqs
func (s *Sync) scheduleNodeRequest(req *nodeRequest) {
s.nodeReqs[string(req.path)] = req
// Schedule the request for future retrieval. This queue is shared
// by both node requests and code requests.
prio := int64(len(req.path)) << 56 // depth >= 128 will never happen, storage leaves will be included in their parents
for i := 0; i < 14 && i < len(req.path); i++ {
prio |= int64(15-req.path[i]) << (52 - i*4) // 15-nibble => lexicographic order
}
s.queue.Push(string(req.path), prio)
}
// schedule inserts a new state retrieval request into the fetch queue. If there
// is already a pending request for this node, the new request will be discarded
// and only a parent reference added to the old one.
func (s *Sync) scheduleCodeRequest(req *codeRequest) {
// If we're already requesting this node, add a new reference and stop
if old, ok := reqset[req.hash]; ok {
if old, ok := s.codeReqs[req.hash]; ok {
old.parents = append(old.parents, req.parents...)
return
}
reqset[req.hash] = req
s.codeReqs[req.hash] = req
// Schedule the request for future retrieval. This queue is shared
// by both node requests and code requests. It can happen that there
// is a trie node and code has same hash. In this case two elements
// with same hash and same or different depth will be pushed. But it's
// ok the worst case is the second response will be treated as duplicated.
// by both node requests and code requests.
prio := int64(len(req.path)) << 56 // depth >= 128 will never happen, storage leaves will be included in their parents
for i := 0; i < 14 && i < len(req.path); i++ {
prio |= int64(15-req.path[i]) << (52 - i*4) // 15-nibble => lexicographic order
@ -344,8 +381,8 @@ func (s *Sync) schedule(req *request) {
// children retrieves all the missing children of a state trie entry for future
// retrieval scheduling.
func (s *Sync) children(req *request, object node) ([]*request, error) {
// Gather all the children of the Node, irrelevant whether known or not
func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) {
// Gather all the children of the node, irrelevant whether known or not
type child struct {
path []byte
node node
@ -375,7 +412,7 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
panic(fmt.Sprintf("unknown Node: %+v", node))
}
// Iterate over the children, and request all unknown ones
requests := make([]*request, 0, len(children))
requests := make([]*nodeRequest, 0, len(children))
for _, child := range children {
// Notify any external watcher of a new key/value Node
if req.callback != nil {
@ -387,28 +424,28 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
paths = append(paths, hexToKeybytes(child.path[:2*common.HashLength]))
paths = append(paths, hexToKeybytes(child.path[2*common.HashLength:]))
}
if err := req.callback(paths, child.path, node, req.hash); err != nil {
if err := req.callback(paths, child.path, node, req.hash, req.path); err != nil {
return nil, err
}
}
}
// If the child references another Node, resolve or schedule
if node, ok := (child.node).(hashNode); ok {
// Try to resolve the Node from the local database
hash := common.BytesToHash(node)
if s.membatch.hasNode(hash) {
// Try to resolve the node from the local database
if s.membatch.hasNode(child.path) {
continue
}
// If database says duplicate, then at least the trie node is present
// and we hold the assumption that it's NOT legacy contract code.
if rawdb.HasTrieNode(s.database, hash) {
chash := common.BytesToHash(node)
if rawdb.HasTrieNode(s.database, chash) {
continue
}
// Locally unknown Node, schedule for retrieval
requests = append(requests, &request{
// Locally unknown node, schedule for retrieval
requests = append(requests, &nodeRequest{
path: child.path,
hash: hash,
parents: []*request{req},
hash: chash,
parent: req,
callback: req.callback,
})
}
@ -419,22 +456,40 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
// commit finalizes a retrieval request and stores it into the membatch. If any
// of the referencing parent requests complete due to this commit, they are also
// committed themselves.
func (s *Sync) commit(req *request) (err error) {
// Write the Node content to the membatch
if req.code {
s.membatch.codes[req.hash] = req.data
delete(s.codeReqs, req.hash)
s.fetches[len(req.path)]--
} else {
s.membatch.nodes[req.hash] = req.data
delete(s.nodeReqs, req.hash)
s.fetches[len(req.path)]--
}
// Check all parents for completion
for _, parent := range req.parents {
parent.deps--
if parent.deps == 0 {
if err := s.commit(parent); err != nil {
func (s *Sync) commitNodeRequest(req *nodeRequest) error {
// Write the node content to the membatch
s.membatch.nodes[string(req.path)] = req.data
s.membatch.hashes[string(req.path)] = req.hash
delete(s.nodeReqs, string(req.path))
s.fetches[len(req.path)]--
// Check parent for completion
if req.parent != nil {
req.parent.deps--
if req.parent.deps == 0 {
if err := s.commitNodeRequest(req.parent); err != nil {
return err
}
}
}
return nil
}
// commit finalizes a retrieval request and stores it into the membatch. If any
// of the referencing parent requests complete due to this commit, they are also
// committed themselves.
func (s *Sync) commitCodeRequest(req *codeRequest) error {
// Write the node content to the membatch
s.membatch.codes[req.hash] = req.data
delete(s.codeReqs, req.hash)
s.fetches[len(req.path)]--
// Check all parents for completion
for _, parent := range req.parents {
parent.deps--
if parent.deps == 0 {
if err := s.commitNodeRequest(parent); err != nil {
return err
}
}

View file

@ -88,6 +88,13 @@ func checkTrieConsistency(db *Database, root common.Hash) error {
return it.Error()
}
// trieElement represents the element in the state trie(bytecode or trie node).
type trieElement struct {
path string
hash common.Hash
syncPath SyncPath
}
// Tests that an empty trie is not scheduled for syncing.
func TestEmptySync(t *testing.T) {
dbA := NewDatabase(memorydb.New())
@ -97,8 +104,8 @@ func TestEmptySync(t *testing.T) {
for i, trie := range []*Trie{emptyA, emptyB} {
sync := NewSync(trie.Hash(), memorydb.New(), nil)
if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
t.Errorf("test %d: content requested for empty trie: %v, %v, %v", i, nodes, paths, codes)
if paths, nodes, codes := sync.Missing(1); len(paths) != 0 || len(nodes) != 0 || len(codes) != 0 {
t.Errorf("test %d: content requested for empty trie: %v, %v, %v", i, paths, nodes, codes)
}
}
}
@ -119,35 +126,38 @@ func testIterativeSync(t *testing.T, count int, bypath bool) {
triedb := NewDatabase(diskdb)
sched := NewSync(srcTrie.Hash(), diskdb, nil)
nodes, paths, codes := sched.Missing(count)
var (
hashQueue []common.Hash
pathQueue []SyncPath
)
if !bypath {
hashQueue = append(append(hashQueue[:0], nodes...), codes...)
} else {
hashQueue = append(hashQueue[:0], codes...)
pathQueue = append(pathQueue[:0], paths...)
// The code requests are ignored here since there is no code
// at the testing trie.
paths, nodes, _ := sched.Missing(count)
var elements []trieElement
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
for len(hashQueue)+len(pathQueue) > 0 {
results := make([]SyncResult, len(hashQueue)+len(pathQueue))
for i, hash := range hashQueue {
data, err := srcDb.Node(hash)
if err != nil {
t.Fatalf("failed to retrieve node data for hash %x: %v", hash, err)
for len(elements) > 0 {
results := make([]NodeSyncResult, len(elements))
if !bypath {
for i, element := range elements {
data, err := srcDb.Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve node data for hash %x: %v", element.hash, err)
}
results[i] = NodeSyncResult{element.path, data}
}
results[i] = SyncResult{hash, data}
}
for i, path := range pathQueue {
data, _, err := srcTrie.TryGetNode(path[0])
if err != nil {
t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
} else {
for i, element := range elements {
data, _, err := srcTrie.TryGetNode(element.syncPath[len(element.syncPath)-1])
if err != nil {
t.Fatalf("failed to retrieve node data for path %x: %v", element.path, err)
}
results[i] = NodeSyncResult{element.path, data}
}
results[len(hashQueue)+i] = SyncResult{crypto.Keccak256Hash(data), data}
}
for _, result := range results {
if err := sched.Process(result); err != nil {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
@ -157,12 +167,14 @@ func testIterativeSync(t *testing.T, count int, bypath bool) {
}
batch.Write()
nodes, paths, codes = sched.Missing(count)
if !bypath {
hashQueue = append(append(hashQueue[:0], nodes...), codes...)
} else {
hashQueue = append(hashQueue[:0], codes...)
pathQueue = append(pathQueue[:0], paths...)
paths, nodes, _ = sched.Missing(count)
elements = elements[:0]
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
}
// Cross check that the two tries are in sync
@ -180,21 +192,29 @@ func TestIterativeDelayedSync(t *testing.T) {
triedb := NewDatabase(diskdb)
sched := NewSync(srcTrie.Hash(), diskdb, nil)
nodes, _, codes := sched.Missing(10000)
queue := append(append([]common.Hash{}, nodes...), codes...)
for len(queue) > 0 {
// The code requests are ignored here since there is no code
// at the testing trie.
paths, nodes, _ := sched.Missing(10000)
var elements []trieElement
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
for len(elements) > 0 {
// Sync only half of the scheduled nodes
results := make([]SyncResult, len(queue)/2+1)
for i, hash := range queue[:len(results)] {
data, err := srcDb.Node(hash)
results := make([]NodeSyncResult, len(elements)/2+1)
for i, element := range elements[:len(results)] {
data, err := srcDb.Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve Node data for %x: %v", hash, err)
t.Fatalf("failed to retrieve node data for %x: %v", element.hash, err)
}
results[i] = SyncResult{hash, data}
results[i] = NodeSyncResult{element.path, data}
}
for _, result := range results {
if err := sched.Process(result); err != nil {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
@ -204,8 +224,15 @@ func TestIterativeDelayedSync(t *testing.T) {
}
batch.Write()
nodes, _, codes = sched.Missing(10000)
queue = append(append(queue[len(results):], nodes...), codes...)
paths, nodes, _ = sched.Missing(10000)
elements = elements[len(results):]
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
}
// Cross check that the two tries are in sync
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
@ -226,24 +253,30 @@ func testIterativeRandomSync(t *testing.T, count int) {
triedb := NewDatabase(diskdb)
sched := NewSync(srcTrie.Hash(), diskdb, nil)
queue := make(map[common.Hash]struct{})
nodes, _, codes := sched.Missing(count)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
// The code requests are ignored here since there is no code
// at the testing trie.
paths, nodes, _ := sched.Missing(count)
queue := make(map[string]trieElement)
for i, path := range paths {
queue[path] = trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
}
}
for len(queue) > 0 {
// Fetch all the queued nodes in a random order
results := make([]SyncResult, 0, len(queue))
for hash := range queue {
data, err := srcDb.Node(hash)
results := make([]NodeSyncResult, 0, len(queue))
for path, element := range queue {
data, err := srcDb.Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve Node data for %x: %v", hash, err)
t.Fatalf("failed to retrieve node data for %x: %v", element.hash, err)
}
results = append(results, SyncResult{hash, data})
results = append(results, NodeSyncResult{path, data})
}
// Feed the retrieved results back and queue new tasks
for _, result := range results {
if err := sched.Process(result); err != nil {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
@ -253,10 +286,14 @@ func testIterativeRandomSync(t *testing.T, count int) {
}
batch.Write()
queue = make(map[common.Hash]struct{})
nodes, _, codes = sched.Missing(count)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
paths, nodes, _ = sched.Missing(count)
queue = make(map[string]trieElement)
for i, path := range paths {
queue[path] = trieElement{
path: path,
hash: nodes[i],
syncPath: NewSyncPath([]byte(path)),
}
}
}
// Cross check that the two tries are in sync
@ -274,20 +311,26 @@ func TestIterativeRandomDelayedSync(t *testing.T) {
triedb := NewDatabase(diskdb)
sched := NewSync(srcTrie.Hash(), diskdb, nil)
queue := make(map[common.Hash]struct{})
nodes, _, codes := sched.Missing(10000)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
// The code requests are ignored here since there is no code
// at the testing trie.
paths, nodes, _ := sched.Missing(10000)
queue := make(map[string]trieElement)
for i, path := range paths {
queue[path] = trieElement{
path: path,
hash: nodes[i],
syncPath: NewSyncPath([]byte(path)),
}
}
for len(queue) > 0 {
// Sync only half of the scheduled nodes, even those in random order
results := make([]SyncResult, 0, len(queue)/2+1)
for hash := range queue {
data, err := srcDb.Node(hash)
results := make([]NodeSyncResult, 0, len(queue)/2+1)
for path, element := range queue {
data, err := srcDb.Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve Node data for %x: %v", hash, err)
t.Fatalf("failed to retrieve node data for %x: %v", element.hash, err)
}
results = append(results, SyncResult{hash, data})
results = append(results, NodeSyncResult{path, data})
if len(results) >= cap(results) {
break
@ -295,7 +338,7 @@ func TestIterativeRandomDelayedSync(t *testing.T) {
}
// Feed the retrieved results back and queue new tasks
for _, result := range results {
if err := sched.Process(result); err != nil {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
@ -305,11 +348,15 @@ func TestIterativeRandomDelayedSync(t *testing.T) {
}
batch.Write()
for _, result := range results {
delete(queue, result.Hash)
delete(queue, result.Path)
}
nodes, _, codes = sched.Missing(10000)
for _, hash := range append(nodes, codes...) {
queue[hash] = struct{}{}
paths, nodes, _ = sched.Missing(10000)
for i, path := range paths {
queue[path] = trieElement{
path: path,
hash: nodes[i],
syncPath: NewSyncPath([]byte(path)),
}
}
}
// Cross check that the two tries are in sync
@ -327,26 +374,35 @@ func TestDuplicateAvoidanceSync(t *testing.T) {
triedb := NewDatabase(diskdb)
sched := NewSync(srcTrie.Hash(), diskdb, nil)
nodes, _, codes := sched.Missing(0)
queue := append(append([]common.Hash{}, nodes...), codes...)
// The code requests are ignored here since there is no code
// at the testing trie.
paths, nodes, _ := sched.Missing(0)
var elements []trieElement
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
requested := make(map[common.Hash]struct{})
for len(queue) > 0 {
results := make([]SyncResult, len(queue))
for i, hash := range queue {
data, err := srcDb.Node(hash)
for len(elements) > 0 {
results := make([]NodeSyncResult, len(elements))
for i, element := range elements {
data, err := srcDb.Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve Node data for %x: %v", hash, err)
t.Fatalf("failed to retrieve node data for %x: %v", element.hash, err)
}
if _, ok := requested[hash]; ok {
t.Errorf("hash %x already requested once", hash)
if _, ok := requested[element.hash]; ok {
t.Errorf("hash %x already requested once", element.hash)
}
requested[hash] = struct{}{}
requested[element.hash] = struct{}{}
results[i] = SyncResult{hash, data}
results[i] = NodeSyncResult{element.path, data}
}
for _, result := range results {
if err := sched.Process(result); err != nil {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
@ -356,8 +412,15 @@ func TestDuplicateAvoidanceSync(t *testing.T) {
}
batch.Write()
nodes, _, codes = sched.Missing(0)
queue = append(append(queue[:0], nodes...), codes...)
paths, nodes, _ = sched.Missing(0)
elements = elements[:0]
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
}
// Cross check that the two tries are in sync
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
@ -374,23 +437,34 @@ func TestIncompleteSync(t *testing.T) {
triedb := NewDatabase(diskdb)
sched := NewSync(srcTrie.Hash(), diskdb, nil)
var added []common.Hash
nodes, _, codes := sched.Missing(1)
queue := append(append([]common.Hash{}, nodes...), codes...)
for len(queue) > 0 {
// The code requests are ignored here since there is no code
// at the testing trie.
var (
added []common.Hash
elements []trieElement
root = srcTrie.Hash()
)
paths, nodes, _ := sched.Missing(1)
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
for len(elements) > 0 {
// Fetch a batch of trie nodes
results := make([]SyncResult, len(queue))
for i, hash := range queue {
data, err := srcDb.Node(hash)
results := make([]NodeSyncResult, len(elements))
for i, element := range elements {
data, err := srcDb.Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
t.Fatalf("failed to retrieve node data for %x: %v", element.hash, err)
}
results[i] = SyncResult{hash, data}
results[i] = NodeSyncResult{element.path, data}
}
// Process each of the trie nodes
for _, result := range results {
if err := sched.Process(result); err != nil {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
@ -399,27 +473,36 @@ func TestIncompleteSync(t *testing.T) {
t.Fatalf("failed to commit data: %v", err)
}
batch.Write()
for _, result := range results {
added = append(added, result.Hash)
hash := crypto.Keccak256Hash(result.Data)
if hash != root {
added = append(added, hash)
}
// Check that all known sub-tries in the synced trie are complete
if err := checkTrieConsistency(triedb, result.Hash); err != nil {
if err := checkTrieConsistency(triedb, hash); err != nil {
t.Fatalf("trie inconsistent: %v", err)
}
}
// Fetch the next batch to retrieve
nodes, _, codes = sched.Missing(1)
queue = append(append(queue[:0], nodes...), codes...)
paths, nodes, _ = sched.Missing(1)
elements = elements[:0]
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
}
}
// Sanity check that removing any node from the database is detected
for _, node := range added[1:] {
key := node.Bytes()
value, _ := diskdb.Get(key)
diskdb.Delete(key)
if err := checkTrieConsistency(triedb, added[0]); err == nil {
t.Fatalf("trie inconsistency not caught, missing: %x", key)
for _, hash := range added {
value, _ := diskdb.Get(hash.Bytes())
diskdb.Delete(hash.Bytes())
if err := checkTrieConsistency(triedb, root); err == nil {
t.Fatalf("trie inconsistency not caught, missing: %x", hash)
}
diskdb.Put(key, value)
diskdb.Put(hash.Bytes(), value)
}
}
@ -434,21 +517,33 @@ func TestSyncOrdering(t *testing.T) {
triedb := NewDatabase(diskdb)
sched := NewSync(srcTrie.Hash(), diskdb, nil)
nodes, paths, _ := sched.Missing(1)
queue := append([]common.Hash{}, nodes...)
reqs := append([]SyncPath{}, paths...)
// The code requests are ignored here since there is no code
// at the testing trie.
var (
reqs []SyncPath
elements []trieElement
)
paths, nodes, _ := sched.Missing(1)
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
reqs = append(reqs, NewSyncPath([]byte(paths[i])))
}
for len(queue) > 0 {
results := make([]SyncResult, len(queue))
for i, hash := range queue {
data, err := srcDb.Node(hash)
for len(elements) > 0 {
results := make([]NodeSyncResult, len(elements))
for i, element := range elements {
data, err := srcDb.Node(element.hash)
if err != nil {
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
t.Fatalf("failed to retrieve node data for %x: %v", element.hash, err)
}
results[i] = SyncResult{hash, data}
results[i] = NodeSyncResult{element.path, data}
}
for _, result := range results {
if err := sched.Process(result); err != nil {
if err := sched.ProcessNode(result); err != nil {
t.Fatalf("failed to process result %v", err)
}
}
@ -458,9 +553,16 @@ func TestSyncOrdering(t *testing.T) {
}
batch.Write()
nodes, paths, _ = sched.Missing(1)
queue = append(queue[:0], nodes...)
reqs = append(reqs, paths...)
paths, nodes, _ = sched.Missing(1)
elements = elements[:0]
for i := 0; i < len(paths); i++ {
elements = append(elements, trieElement{
path: paths[i],
hash: nodes[i],
syncPath: NewSyncPath([]byte(paths[i])),
})
reqs = append(reqs, NewSyncPath([]byte(paths[i])))
}
}
// Cross check that the two tries are in sync
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)

View file

@ -33,18 +33,18 @@ import (
// LeafCallback is a callback type invoked when a trie operation reaches a leaf
// node.
//
// The paths is a path tuple identifying a particular trie node either in a single
// trie (account) or a layered trie (account -> storage). Each path in the tuple
// The keys is a path tuple identifying a particular trie node either in a single
// trie (account) or a layered trie (account -> storage). Each key in the tuple
// is in the raw format(32 bytes).
//
// The hexpath is a composite hexary path identifying the trie node. All the key
// The path is a composite hexary path identifying the trie node. All the key
// bytes are converted to the hexary nibbles and composited with the parent path
// if the trie node is in a layered trie.
//
// It's used by state sync and commit to allow handling external references
// between account and storage tries. And also it's used in the state healing
// for extracting the raw states(leaf nodes) with corresponding paths.
type LeafCallback func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash) error
type LeafCallback func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error
// Trie is a Merkle Patricia Trie.
// The zero value is an empty trie with no database.

View file

@ -552,7 +552,7 @@ func BenchmarkCommitAfterHash(b *testing.B) {
benchmarkCommitAfterHash(b, nil)
})
var a types.StateAccount
onleaf := func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
onleaf := func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash, parentPath []byte) error {
rlp.DecodeBytes(leaf, &a)
return nil
}