celestiaorg / smt

A Go library that implements a Sparse Merkle tree for a key-value map.
https://godoc.org/github.com/celestiaorg/smt
MIT License
138 stars 53 forks source link

Delete incorrectly tries to read side nodes #25

Closed adlerjohn closed 3 years ago

adlerjohn commented 3 years ago

22 uncovered a bug whereby deleteWithSideNodes tries to Get side nodes, but those nodes may not exist in the tree. The case where these nodes don't exist in the tree is when a branch is added with AddBranch (which only includes the side nodes hashes, but not their values).

This is mostly an issue with the DSMST, where it is constructed by adding branches rather than updating.

Specifically, here. This Get only happens during the first iteration of the loop, i.e. it only applies to the sibling of the leaf node to delete. This node can either be another leaf node or an internal node, but cannot be a placeholder.

adlerjohn commented 3 years ago

This can be resolved by adding an optional (i.e. nullable) field to SparseMerkleProof for the sibling nodes value. If this is non-nil on an Add, it is added to the tree's hashmap.