Skip to content

Commit 7268e2e

Browse files
committed
Added AddSubtreeNodeWithoutLock function for adding to subtree without locking
1 parent a789056 commit 7268e2e

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

subtree.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,24 @@ func (st *Subtree) AddSubtreeNode(node Node) error {
237237
return fmt.Errorf("[AddSubtreeNode] %w, tree length is %d", ErrCoinbasePlaceholderMisuse, len(st.Nodes))
238238
}
239239

240-
// AddNode is not concurrency safe, so we can reuse the same byte arrays
241-
// binary.LittleEndian.PutUint64(st.feeBytes, fee)
242-
// st.feeHashBytes = append(node[:], st.feeBytes[:]...)
243-
// if len(st.Nodes) == 0 {
244-
// st.FeeHash = chainhash.HashH(st.feeHashBytes)
245-
// } else {
246-
// st.FeeHash = chainhash.HashH(append(st.FeeHash[:], st.feeHashBytes...))
247-
// }
240+
st.Nodes = append(st.Nodes, node)
241+
st.rootHash = nil // reset rootHash
242+
st.Fees += node.Fee
243+
st.SizeInBytes += node.SizeInBytes
244+
245+
if st.nodeIndex != nil {
246+
// node index map exists, add the node to it
247+
st.nodeIndex[node.Hash] = len(st.Nodes) - 1
248+
}
249+
250+
return nil
251+
}
252+
253+
// AddSubtreeNodeWithoutLock adds a Node to the subtree without locking.
254+
func (st *Subtree) AddSubtreeNodeWithoutLock(node Node) error {
255+
if (len(st.Nodes) + 1) > st.treeSize {
256+
return ErrSubtreeFull
257+
}
248258

249259
st.Nodes = append(st.Nodes, node)
250260
st.rootHash = nil // reset rootHash

0 commit comments

Comments
 (0)