Skip to content

Commit

Permalink
Merge pull request #16 from 0chain/debug/demo-stuck
Browse files Browse the repository at this point in the history
Add GetAllMissingNodes()
  • Loading branch information
dabasov authored Sep 29, 2023
2 parents d463636 + e14be80 commit 472780d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/util/markle_patricia_trie_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type MerklePatriciaTrieI interface {
GetMissingNodeKeys() []Key
// only for testing and debugging
PrettyPrint(w io.Writer) error
GetAllMissingNodes() ([]Key, error)

Validate() error

Expand Down
43 changes: 42 additions & 1 deletion core/util/merkle_patricia_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ func (mpt *MerklePatriciaTrie) getNodeValue(path Path, node Node, v MPTSerializa
return err
}

func (mpt *MerklePatriciaTrie) GetAllMissingNodes() ([]Key, error) {
mpt.mutex.RLock()
defer mpt.mutex.RUnlock()
var missingNodes []Key
if err := mpt.pp2(mpt.root, 0, &missingNodes); err != nil {
return nil, err
}

return missingNodes, nil
}

func (mpt *MerklePatriciaTrie) getNodeValueRaw(path Path, node Node) ([]byte, error) {
switch nodeImpl := node.(type) {
case *LeafNode:
Expand Down Expand Up @@ -361,6 +372,11 @@ func (mpt *MerklePatriciaTrie) getNodeValueRaw(path Path, node Node) ([]byte, er
func (mpt *MerklePatriciaTrie) insert(value MPTSerializable, key Key, prefix, path Path) (Node, Key, error) {
node, err := mpt.getNode(key)
if err != nil {
Logger.Error("insert get node failed",
zap.String("key", ToHex(key)),
zap.String("prefix", ToHex(prefix)),
zap.Any("prefix bytes", prefix),
zap.Error(err))
return nil, nil, err
}
if len(path) == 0 {
Expand Down Expand Up @@ -832,7 +848,7 @@ func (mpt *MerklePatriciaTrie) insertNode(oldNode Node, newNode Node) (Node, Key
if oldNode != nil {
ohash = oldNode.GetHash()
}
Logger.Info("insert node", zap.String("nn", newNode.GetHash()), zap.String("on", ohash))
Logger.Debug("insert node", zap.String("nn", newNode.GetHash()), zap.String("on", ohash))
}

newNode.SetOrigin(mpt.Version)
Expand Down Expand Up @@ -909,6 +925,31 @@ func (mpt *MerklePatriciaTrie) pp(w io.Writer, key Key, depth byte, initpad bool
return nil
}

func (mpt *MerklePatriciaTrie) pp2(key Key, depth byte, missingNodes *[]Key) error {
node, err := mpt.getNode(key)
if err != nil {
if missingNodes != nil && err == ErrNodeNotFound {
ckey := make([]byte, len(key))
copy(ckey, key)
*missingNodes = append(*missingNodes, ckey)
}
return err
}
switch nodeImpl := node.(type) {
case *LeafNode:
case *ExtensionNode:
_ = mpt.pp2(nodeImpl.NodeKey, depth+2, missingNodes)
case *FullNode:
for _, cnode := range nodeImpl.Children {
if cnode == nil {
continue
}
_ = mpt.pp2(cnode, depth+2, missingNodes)
}
}
return nil
}

// HasMissingNodes returns immediately when a missing node is detected
func (mpt *MerklePatriciaTrie) HasMissingNodes(ctx context.Context) (bool, error) {
paths := make([]Path, 0, BatchSize)
Expand Down
3 changes: 3 additions & 0 deletions core/util/mpt_node_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ func (cc *ChangeCollector) UpdateChanges(ndb NodeDB, origin Sequence, includeDel
cc.mutex.RLock()
defer cc.mutex.RUnlock()
keys := make([]Key, len(cc.Changes))
keysStr := make([]string, len(keys))
nodes := make([]Node, len(cc.Changes))
idx := 0
for _, c := range cc.Changes {
nodes[idx] = c.New.Clone()
keys[idx] = nodes[idx].GetHashBytes()
keysStr[idx] = nodes[idx].GetHash()
idx++
}

Expand All @@ -150,6 +152,7 @@ func (cc *ChangeCollector) UpdateChanges(ndb NodeDB, origin Sequence, includeDel
if err != nil {
return err
}

logging.Logger.Debug("MPT - update changes done", zap.Int("changes", len(keys)))
if includeDeletes {
for _, d := range cc.Deletes {
Expand Down

0 comments on commit 472780d

Please sign in to comment.