core: clone MPT node while restoring it multiple times

We need this to avoid collapse collisions. Example of such collapse
described in
https://github.com/nspcc-dev/neo-go/pull/2019#discussion_r689629704.
This commit is contained in:
Anna Shaleva 2021-08-27 16:58:27 +03:00
parent 5cd78c31af
commit 36808b8904
8 changed files with 138 additions and 1 deletions

View file

@ -371,7 +371,9 @@ func (s *Module) restoreNode(n mpt.Node) error {
}
var childrenPaths = make(map[util.Uint256][][]byte)
for _, path := range nPaths {
err := s.billet.RestoreHashNode(path, n)
// Must clone here in order to avoid future collapse collisions. If the node's refcount>1 then MPT pool
// will manage all paths for this node and call RestoreHashNode separately for each of the paths.
err := s.billet.RestoreHashNode(path, n.Clone())
if err != nil {
return fmt.Errorf("failed to restore MPT node with hash %s and path %s: %w", n.Hash().StringBE(), hex.EncodeToString(path), err)
}