Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync dead nodes #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/util/markle_patricia_trie_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type MerklePatriciaTrieI interface {

MergeMPTChanges(mpt2 MerklePatriciaTrieI) error
MergeChanges(newRoot Key, changes []*NodeChange, deletes []Node, startRoot Key) error
MergeDB(ndb NodeDB, root Key) error
MergeDB(ndb NodeDB, root Key, deadNodes []Node) error

Cache() *statecache.TransactionCache
}
Expand Down
24 changes: 20 additions & 4 deletions core/util/merkle_patricia_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type MerklePatriciaTrie struct {
Version Sequence
missingNodeKeys []Key
cache *statecache.TransactionCache
deleteNodes []Node // delete nodes that added when sync from remote
}

/*NewMerklePatriciaTrie - create a new patricia merkle trie */
Expand Down Expand Up @@ -228,11 +229,25 @@ func (mpt *MerklePatriciaTrie) GetChanges() (Key, []*NodeChange, []Node, Key) {
}

func (mpt *MerklePatriciaTrie) GetDeletes() []Node {
var nodes []Node
mpt.mutex.RLock()
nodes = mpt.ChangeCollector.GetDeletes()
nodes := mpt.ChangeCollector.GetDeletes()
nodesMap := make(map[string]struct{})
for _, nd := range nodes {
nodesMap[nd.GetHash()] = struct{}{}
}

newNodes := make([]Node, 0, len(nodes)+len(mpt.deleteNodes))
newNodes = append(newNodes, nodes...)
for _, nd := range mpt.deleteNodes {
if _, ok := nodesMap[nd.GetHash()]; ok {
// already added
continue
}
newNodes = append(newNodes, nd)
}

mpt.mutex.RUnlock()
return nodes
return newNodes
}

/*GetChangeCount - implement interface */
Expand Down Expand Up @@ -1107,13 +1122,14 @@ func (mpt *MerklePatriciaTrie) mergeChanges(newRoot Key, changes []*NodeChange,
}

// MergeDB - merges the state changes from the node db directly
func (mpt *MerklePatriciaTrie) MergeDB(ndb NodeDB, root Key) error {
func (mpt *MerklePatriciaTrie) MergeDB(ndb NodeDB, root Key, deadNodes []Node) error {
mpt.mutex.Lock()
defer mpt.mutex.Unlock()
handler := func(ctx context.Context, key Key, node Node) error {
_, _, err := mpt.insertNode(nil, node)
return err
}
mpt.root = root
mpt.deleteNodes = append(mpt.deleteNodes, deadNodes...)
return ndb.Iterate(context.TODO(), handler)
}