@Beta public abstract class AbstractMerkleTree extends Object implements MerkleTree
Modifier and Type | Field and Description |
---|---|
protected int |
depth
number of levels in the tree and can be calculated as (int) Math.ceil((Math.log(numberOfLeaves) / Math.log(2))) +
1
|
protected Map<Integer,String> |
leaves
leaves encoded in a mapping of the index to the value
|
protected MessageDigest |
messageDigest
messageDigest to perform hashing function
|
protected Map<String,String> |
nodes
Tree nodes as a LinkedHashMap
|
protected int |
numberOfLeaves
number of leaves in the tree
|
protected Map<Integer,String> |
salts
salts encoded in a mapping of the index to the salts with index i corresponds the the leaf with the same index i
in the leaves map
|
ROOT_HASH_KEY
Modifier | Constructor and Description |
---|---|
protected |
AbstractMerkleTree(MessageDigest messageDigest,
int numberOfLeaves)
Creates an abstract MerkleTree
|
protected |
AbstractMerkleTree(MessageDigest messageDigest,
Map<Integer,String> leaves,
Map<Integer,String> salts)
Creates an abstract MerkleTree
|
Modifier and Type | Method and Description |
---|---|
static String |
bytesToHexString(byte[] byteArray) |
protected static int |
calculateDepth(int numberOfLeaves)
Could be seen as the number of rows in a binary tree with given number of leaves
|
protected abstract void |
calculateHash(String rootHashKey)
calculates the hash of given rootHashKey
|
protected abstract String |
calculatLeafHash(String string)
calculates the hash of the leaf
|
protected void |
createEmptyTree()
Calculates keys in the tree and set their value as null
It also ignores redundant nodes. e.g. |
protected static String |
formatKey(int depth,
String binaryString)
formats the key with leading zeros "0" to reach the correct length of a key in the given depth
|
protected String |
generateLeftChildKey(String parentKey)
Left child key is always equal to parentKey + "0"
|
protected String |
generateRightChildKey(String parentKey)
Right child key is always equal to parentKey + "1"
|
String |
getHashAlgorithm() |
Map<Integer,String> |
getLeaves()
leaves encoded in a mapping of the index to the value
|
static List<String> |
getMissingHashIndexes(Set<Integer> knownLeavesIndexes,
int numberOfLeaves)
Finds missing hash indexes to be able to rebuild the Merkle tree with given number of leaves and known leaf
values.
One can rebuild a Merkle tree with only a subset of leaves, if s/he has enough extra information about the tree. These extra information include the total number of leaves and hashes of missing leaves along with indexes of those hashes. e.g. |
Map<String,String> |
getNodes()
Tree nodes as a LinkedHashMap
|
int |
getNumberOfLeaves()
number of leaves in the tree
|
String |
getRootHash() |
Map<Integer,String> |
getSalts()
salts encoded in a mapping of the index to the salts with index i corresponds the the leaf with the same index i
in the leaves map
|
protected String |
getValueByKey(String key)
returns the value of the leaf
|
protected boolean |
isALeafKey(String key)
checks if a key corresponds to a leaf.
|
protected void |
removeRedundantNodes(String key)
removes nodes, when they have no child and replaces them with child when they have only one child.
|
protected void |
setLeavesHashes()
calculates and sets the hashes of the leaves
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getProtocolVersion
protected final MessageDigest messageDigest
protected Map<Integer,String> salts
protected final int numberOfLeaves
protected final int depth
protected AbstractMerkleTree(MessageDigest messageDigest, Map<Integer,String> leaves, Map<Integer,String> salts)
messageDigest
- messageDigestleaves
- Map<Integer, String>
leaves encoded in a mapping of the index to the valuesalts
- Map<Integer, String>
salts encoded in a mapping of the index to the salts with index i
corresponds the the leaf with the same index i in the leaves mapprotected AbstractMerkleTree(MessageDigest messageDigest, int numberOfLeaves)
messageDigest
- messageDigestnumberOfLeaves
- number of leavesprotected abstract void calculateHash(String rootHashKey)
rootHashKey
- rootHashKeyprotected abstract String calculatLeafHash(String string)
string
- key of the leafprotected String getValueByKey(String key)
key
- key of the leafprotected void setLeavesHashes()
protected void createEmptyTree()
protected void removeRedundantNodes(String key)
key
- keyprotected boolean isALeafKey(String key)
key
- key to checkprotected static int calculateDepth(int numberOfLeaves)
numberOfLeaves
- total number of leaves in the treeprotected String generateLeftChildKey(String parentKey)
parentKey
- the key of the parent nodeprotected String generateRightChildKey(String parentKey)
parentKey
- key of the parent@Nonnull public String getRootHash()
getRootHash
in interface MerkleTree
@Nonnull public static String bytesToHexString(@Nonnull byte[] byteArray)
byteArray
- arbitrary byteArray@Nonnull public static List<String> getMissingHashIndexes(@Nonnull Set<Integer> knownLeavesIndexes, int numberOfLeaves)
knownLeavesIndexes
- List<Integer>
index of known leaves starting from zero (0)numberOfLeaves
- int
total number of leavesList<String>
minimum set of hash indexes to have a unique Merkle tree for given arguments.protected static String formatKey(int depth, String binaryString)
depth
- depth in the tree where the key belongs tobinaryString
- position in the tree@Nonnull public String getHashAlgorithm()
getHashAlgorithm
in interface MerkleTree
public Map<Integer,String> getLeaves()
getLeaves
in interface MerkleTree
Map<Integer, String>
the leaves mapping position to valuepublic Map<Integer,String> getSalts()
getSalts
in interface MerkleTree
public Map<String,String> getNodes()
getNodes
in interface MerkleTree
Map<Integer, String>
the nodes of the treepublic int getNumberOfLeaves()
getNumberOfLeaves
in interface MerkleTree
Copyright © 2020 SAP SE. All rights reserved.