Package com.horizen.merkletreenative
Class InMemorySparseMerkleTree
- java.lang.Object
-
- com.horizen.merkletreenative.InMemorySparseMerkleTree
-
- All Implemented Interfaces:
AutoCloseable
public class InMemorySparseMerkleTree extends Object implements AutoCloseable
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddLeaves(Map<Long,FieldElement> leaves)Add the specified leaves at the specified positions inside the tree.voidclose()voidfinalizeInPlace()Update the tree and the root with the leaves added/removed until this moment.voidfreeInMemorySparseMerkleTree()Free memory Rust sideMerklePathgetMerklePath(long leafPosition)Return the MerklePath corresponding to the leaf at index leafPosition, only if the tree is in "finalized" statestatic InMemorySparseMerkleTreeinit(int height)Initialize an instance of InMemorySparseMerkleTree with specified heightbooleanisPositionEmpty(long position)Check if specified position is emptyvoidremoveLeaves(Set<Long> positions)Remove the specified leaves at the specified positions inside the tree.FieldElementroot()Return the root of the tree, only if the tree is in "finalized" state.
-
-
-
Method Detail
-
init
public static InMemorySparseMerkleTree init(int height)
Initialize an instance of InMemorySparseMerkleTree with specified height- Parameters:
height- the height of the tree- Returns:
- an instance of InMemorySparseMerkleTree, allocated Rust side
-
isPositionEmpty
public boolean isPositionEmpty(long position) throws ExceptionCheck if specified position is empty- Parameters:
position- the index of the leaf to check- Returns:
- True if no leaf is allocated at that position, False otherwise
- Throws:
Exception- If position > 2^height - 1
-
addLeaves
public void addLeaves(Map<Long,FieldElement> leaves) throws Exception
Add the specified leaves at the specified positions inside the tree. No internal updates in the tree will be triggered by this operation.- Parameters:
leaves- the leaves to be added to the tree and their corresponding index- Throws:
Exception- if one of the indices is > 2^height - 1
-
removeLeaves
public void removeLeaves(Set<Long> positions) throws Exception
Remove the specified leaves at the specified positions inside the tree. No internal updates in the tree will be triggered by this operation.- Parameters:
positions- a set of the indices of the leaves to be removed- Throws:
Exception- if one of the indices is > 2^height - 1 or if attempting to remove a non-existing leaf.
-
finalizeInPlace
public void finalizeInPlace() throws ExceptionUpdate the tree and the root with the leaves added/removed until this moment. The tree is modified in place and set to a "finalized" state. However, it is possibile to call this method as many times as possibile, with new leaves insertion/removal between each call, bringing again the tree to a "non-finalized" state.- Throws:
Exception- if it was not possible to update the tree correctly
-
root
public FieldElement root() throws Exception
Return the root of the tree, only if the tree is in "finalized" state.- Returns:
- The current root of the tree
- Throws:
Exception- if the tree was not in "finalized" state
-
getMerklePath
public MerklePath getMerklePath(long leafPosition) throws Exception
Return the MerklePath corresponding to the leaf at index leafPosition, only if the tree is in "finalized" state- Parameters:
leafPosition- the index of the leaf of which computing the Merkle Path- Returns:
- the Merkle Path of the leaf at index leafPosition
- Throws:
Exception- if the tree was not in "finalized" state
-
freeInMemorySparseMerkleTree
public void freeInMemorySparseMerkleTree()
Free memory Rust side
-
close
public void close() throws Exception- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
-