diff --git a/libs/iavl/mutable_tree_oec.go b/libs/iavl/mutable_tree_oec.go index f7410ccf43..091b195f47 100644 --- a/libs/iavl/mutable_tree_oec.go +++ b/libs/iavl/mutable_tree_oec.go @@ -53,8 +53,10 @@ func (tree *MutableTree) SaveVersionAsync(version int64, useDeltas bool) ([]byte if tree.root != nil { if useDeltas { tree.updateBranchWithDelta(tree.root) - } else { + } else if produceDelta { tree.ndb.updateBranchConcurrency(tree.root, tree.savedNodes) + } else { + tree.ndb.updateBranchConcurrency(tree.root, nil) } // generate state delta @@ -82,7 +84,9 @@ func (tree *MutableTree) SaveVersionAsync(version int64, useDeltas bool) ([]byte tree.ImmutableTree = tree.ImmutableTree.clone() tree.lastSaved = tree.ImmutableTree.clone() tree.orphans = []*Node{} - tree.savedNodes = map[string]*Node{} + for k := range tree.savedNodes { + delete(tree.savedNodes, k) + } rootHash := tree.lastSaved.Hash() tree.setHeightOrphansItem(version, rootHash) @@ -278,8 +282,10 @@ func (tree *MutableTree) addOrphansOptimized(orphans []*Node) { if node.persisted && EnablePruningHistoryState { k := string(node.hash) tree.commitOrphans[k] = node.version - commitOrp := &CommitOrphansImp{Key: k, CommitValue: node.version} - tree.deltas.CommitOrphansDelta = append(tree.deltas.CommitOrphansDelta, commitOrp) + if produceDelta { + commitOrp := &CommitOrphansImp{Key: k, CommitValue: node.version} + tree.deltas.CommitOrphansDelta = append(tree.deltas.CommitOrphansDelta, commitOrp) + } } } diff --git a/libs/iavl/node.go b/libs/iavl/node.go index c71a5202f5..f213295acb 100644 --- a/libs/iavl/node.go +++ b/libs/iavl/node.go @@ -560,8 +560,10 @@ func (node *Node) getRightNode(t *ImmutableTree) *Node { // NOTE: mutates height and size func (node *Node) calcHeightAndSize(t *ImmutableTree) { - node.height = maxInt8(node.getLeftNode(t).height, node.getRightNode(t).height) + 1 - node.size = node.getLeftNode(t).size + node.getRightNode(t).size + left := node.getLeftNode(t) + right := node.getRightNode(t) + node.height = maxInt8(left.height, right.height) + 1 + node.size = left.size + right.size } func (node *Node) calcBalance(t *ImmutableTree) int { diff --git a/libs/iavl/nodedb_oec.go b/libs/iavl/nodedb_oec.go index 82596e5d96..4f78647e37 100644 --- a/libs/iavl/nodedb_oec.go +++ b/libs/iavl/nodedb_oec.go @@ -395,7 +395,9 @@ func (ndb *nodeDB) updateBranchConcurrency(node *Node, savedNodes map[string]*No ndb.saveNodeToPrePersistCache(n) n.leftNode = nil n.rightNode = nil - savedNodes[string(n.hash)] = n + if savedNodes != nil { + savedNodes[string(n.hash)] = n + } } } }(wg, needNilNodeNum, savedNodes, ndb, nodeCh) @@ -420,7 +422,9 @@ func (ndb *nodeDB) updateBranchConcurrency(node *Node, savedNodes map[string]*No node.rightNode = nil // TODO: handle magic number - savedNodes[string(node.hash)] = node + if savedNodes != nil { + savedNodes[string(node.hash)] = node + } return node.hash } @@ -488,7 +492,7 @@ func (ndb *nodeDB) saveCommitOrphans(batch dbm.Batch, version int64, orphans map toVersion := ndb.getPreviousVersion(version) for hash, fromVersion := range orphans { ndb.log(IavlDebug, "SAVEORPHAN", "from", fromVersion, "to", toVersion, "hash", amino.BytesHexStringer(amino.StrToBytes(hash))) - ndb.saveOrphan(batch, []byte(hash), fromVersion, toVersion) + ndb.saveOrphan(batch, amino.StrToBytes(hash), fromVersion, toVersion) } }