forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into develop
# Conflicts: # framework/src/main/java/org/tron/program/Version.java # framework/src/test/java/org/tron/core/db/ManagerTest.java
- Loading branch information
Showing
103 changed files
with
2,545 additions
and
1,633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
common/src/main/java/org/tron/common/utils/MerkleRoot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.tron.common.utils; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import lombok.Getter; | ||
|
||
public class MerkleRoot { | ||
|
||
private MerkleRoot() { | ||
|
||
} | ||
|
||
public static Sha256Hash root(List<Sha256Hash> hashList) { | ||
List<Leaf> leaves = createLeaves(hashList); | ||
while (leaves.size() > 1) { | ||
leaves = createParentLeaves(leaves); | ||
} | ||
return leaves.isEmpty() ? Sha256Hash.ZERO_HASH : leaves.get(0).hash; | ||
} | ||
|
||
private static List<Leaf> createParentLeaves(List<Leaf> leaves) { | ||
int step = 2; | ||
int len = leaves.size(); | ||
return IntStream.iterate(0, i -> i + step) | ||
.limit(len) | ||
.filter(i -> i < len) | ||
.mapToObj(i -> { | ||
Leaf right = i + 1 < len ? leaves.get(i + 1) : null; | ||
return createLeaf(leaves.get(i), right); | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
private static List<Leaf> createLeaves(List<Sha256Hash> hashList) { | ||
int step = 2; | ||
int len = hashList.size(); | ||
return IntStream.iterate(0, i -> i + step) | ||
.limit(len) | ||
.filter(i -> i < len) | ||
.mapToObj(i -> { | ||
Leaf right = i + 1 < len ? createLeaf(hashList.get(i + 1)) : null; | ||
return createLeaf(createLeaf(hashList.get(i)), right); | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
private static Leaf createLeaf(Leaf left, Leaf right) { | ||
Leaf leaf = new Leaf(); | ||
leaf.hash = right == null ? left.hash : computeHash(left.hash, right.hash); | ||
return leaf; | ||
} | ||
|
||
private static Leaf createLeaf(Sha256Hash hash) { | ||
Leaf leaf = new Leaf(); | ||
leaf.hash = hash; | ||
return leaf; | ||
} | ||
|
||
private static Sha256Hash computeHash(Sha256Hash leftHash, Sha256Hash rightHash) { | ||
return Sha256Hash.of(true, | ||
leftHash.getByteString().concat(rightHash.getByteString()).toByteArray()); | ||
} | ||
|
||
@Getter | ||
private static class Leaf { | ||
|
||
private Sha256Hash hash; | ||
} | ||
} |
Oops, something went wrong.