Skip to content

Commit 2e2ea3c

Browse files
committed
BlockCapsule.java, MerkleTree.java, MerkleTreeTest.java
- feat: add MerkleTree class & add test
1 parent dc5240f commit 2e2ea3c

3 files changed

Lines changed: 228 additions & 19 deletions

File tree

‎src/main/java/org/tron/core/capsule/BlockCapsule.java‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.tron.common.crypto.ECKey;
2929
import org.tron.common.crypto.ECKey.ECDSASignature;
3030
import org.tron.common.utils.Sha256Hash;
31+
import org.tron.core.capsule.utils.MerkleTree;
3132
import org.tron.core.exception.ValidateSignatureException;
3233
import org.tron.protos.Protocol.Block;
3334
import org.tron.protos.Protocol.BlockHeader;
@@ -229,27 +230,9 @@ public Sha256Hash calcMerkleRoot() {
229230
.map(TransactionCapsule::getHash)
230231
.collect(Collectors.toCollection(Vector::new));
231232

232-
int hashNum = ids.size();
233-
234-
while (hashNum > 1) {
235-
int max = hashNum - (hashNum & 1);
236-
int k = 0;
237-
for (int i = 0; i < max; i += 2) {
238-
ids.set(k++, Sha256Hash.of((ids.get(i).getByteString()
239-
.concat(ids.get(i + 1).getByteString()))
240-
.toByteArray()));
241-
}
242-
243-
if (hashNum % 2 == 1) {
244-
ids.set(k++, ids.get(max));
245-
}
246-
hashNum = k;
247-
}
248-
249-
return ids.firstElement();
233+
return MerkleTree.getInstance().createTree(ids).getRoot().getHash();
250234
}
251235

252-
253236
public void setMerkleRoot() {
254237
BlockHeader.raw blockHeaderRaw =
255238
this.block.getBlockHeader().getRawData().toBuilder()
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.tron.core.capsule.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
import org.tron.common.utils.Sha256Hash;
8+
9+
public class MerkleTree {
10+
private static volatile MerkleTree instance;
11+
private List<Sha256Hash> hashList;
12+
private List<Leaf> leaves;
13+
private Leaf root;
14+
15+
public Leaf getRoot() {
16+
return root;
17+
}
18+
19+
public List<Sha256Hash> getHashList() {
20+
return hashList;
21+
}
22+
23+
public List<Leaf> getLeaves() {
24+
return leaves;
25+
}
26+
27+
public static MerkleTree getInstance() {
28+
if (instance == null) {
29+
synchronized (MerkleTree.class) {
30+
if (instance == null) {
31+
instance = new MerkleTree();
32+
}
33+
}
34+
}
35+
return instance;
36+
}
37+
38+
public MerkleTree createTree(List<Sha256Hash> hashList) {
39+
this.leaves = new ArrayList<>();
40+
this.hashList = hashList;
41+
List<Leaf> leaves = createLeaves(hashList);
42+
43+
while (leaves.size() > 1) {
44+
leaves = createParentLeaves(leaves);
45+
}
46+
47+
this.root = leaves.get(0);
48+
return this;
49+
}
50+
51+
List<Leaf> createParentLeaves(List<Leaf> leaves) {
52+
int step = 2;
53+
int len = leaves.size();
54+
return IntStream.iterate(0, i -> i + step)
55+
.limit(len)
56+
.filter(i -> i < len)
57+
.mapToObj(i -> {
58+
Leaf right = i + 1 < len ? leaves.get(i + 1) : null;
59+
return createLeaf(leaves.get(i), right);
60+
}).collect(Collectors.toList());
61+
}
62+
63+
List<Leaf> createLeaves(List<Sha256Hash> hashList) {
64+
int step = 2;
65+
int len = hashList.size();
66+
return IntStream.iterate(0, i -> i + step)
67+
.limit(len)
68+
.filter(i -> i < len)
69+
.mapToObj(i -> {
70+
Leaf right = i + 1 < len ? createLeaf(hashList.get(i + 1)) : null;
71+
return createLeaf(createLeaf(hashList.get(i)), right);
72+
}).collect(Collectors.toList());
73+
}
74+
75+
private Leaf createLeaf(Leaf left, Leaf right) {
76+
Leaf leaf = new Leaf();
77+
leaf.hash = right == null ? left.hash : computeHash(left.hash, right.hash);
78+
leaf.left = left;
79+
leaf.right = right;
80+
this.leaves.add(leaf);
81+
return leaf;
82+
}
83+
84+
private Leaf createLeaf(Sha256Hash hash) {
85+
Leaf leaf = new Leaf();
86+
leaf.hash = hash;
87+
this.leaves.add(leaf);
88+
return leaf;
89+
}
90+
91+
private Sha256Hash computeHash(Sha256Hash leftSig, Sha256Hash rightSig) {
92+
return Sha256Hash.of(leftSig.getByteString().concat(rightSig.getByteString()).toByteArray());
93+
}
94+
95+
public class Leaf {
96+
private Sha256Hash hash;
97+
private Leaf left, right;
98+
99+
public Sha256Hash getHash() {
100+
return hash;
101+
}
102+
103+
public Leaf getLeft() {
104+
return left;
105+
}
106+
107+
public Leaf getRight() {
108+
return right;
109+
}
110+
}
111+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package org.tron.core.capsule.utils;
2+
3+
import com.google.protobuf.ByteString;
4+
import org.junit.Test;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.tron.common.utils.ByteArray;
8+
import org.tron.common.utils.Sha256Hash;
9+
import org.tron.core.capsule.BlockCapsule;
10+
import org.tron.core.capsule.TransactionCapsule;
11+
import org.tron.protos.Protocol;
12+
13+
import java.util.List;
14+
import java.util.Vector;
15+
import java.util.stream.Collectors;
16+
17+
import static org.junit.Assert.*;
18+
19+
public class MerkleTreeTest {
20+
private static final Logger logger = LoggerFactory.getLogger("Test");
21+
private static BlockCapsule blockCapsule0 = new BlockCapsule(1, ByteString
22+
.copyFrom(ByteArray
23+
.fromHexString("9938a342238077182498b464ac0292229938a342238077182498b464ac029222")), 1234,
24+
ByteString.copyFrom("1234567".getBytes()));
25+
26+
private static BlockCapsule blockCapsule1 = new BlockCapsule(1, ByteString
27+
.copyFrom(ByteArray
28+
.fromHexString("9938a342238077182498b464ac0292229938a342238077182498b464ac029222")), 1234,
29+
ByteString.copyFrom("1234567".getBytes()));
30+
31+
private TransactionCapsule transactionCapsule1 = new TransactionCapsule("123", 1L);
32+
private TransactionCapsule transactionCapsule2 = new TransactionCapsule("124", 2L);
33+
private TransactionCapsule transactionCapsule3 = new TransactionCapsule("125", 2L);
34+
35+
@Test
36+
public void testMerkleTreeTest() {
37+
Sha256Hash hash1 = getBeforeZeroHash();
38+
MerkleTree tree = MerkleTree.getInstance().createTree(getZeroIds());
39+
40+
logger.info("Transaction[X] Compare :");
41+
logger.info("left: {}", hash1);
42+
logger.info("right: {}", tree.getRoot().getHash());
43+
44+
assertEquals(hash1, tree.getRoot().getHash());
45+
46+
Sha256Hash hash2 = getBeforeTxHash();
47+
tree.createTree(getTxIds2(blockCapsule1));
48+
49+
logger.info("Transaction[O] Compare :");
50+
logger.info("left: {}", hash2);
51+
logger.info("right: {}", tree.getRoot().getHash());
52+
53+
assertEquals(hash2, tree.getRoot().getHash());
54+
}
55+
56+
private Sha256Hash getBeforeHash(Vector<Sha256Hash> ids) {
57+
int hashNum = ids.size();
58+
59+
while (hashNum > 1) {
60+
int max = hashNum - (hashNum & 1);
61+
int k = 0;
62+
for (int i = 0; i < max; i += 2) {
63+
ids.set(k++, Sha256Hash.of((ids.get(i).getByteString()
64+
.concat(ids.get(i + 1).getByteString()))
65+
.toByteArray()));
66+
}
67+
68+
if (hashNum % 2 == 1) {
69+
ids.set(k++, ids.get(max));
70+
}
71+
hashNum = k;
72+
}
73+
74+
return ids.firstElement();
75+
}
76+
77+
private Sha256Hash getBeforeZeroHash() {
78+
return getBeforeHash(getZeroIds());
79+
}
80+
81+
private Sha256Hash getBeforeTxHash() {
82+
return getBeforeHash(getTxIds1(blockCapsule0));
83+
}
84+
85+
private Vector<Sha256Hash> getZeroIds() {
86+
Vector<Sha256Hash> ids = new Vector<>();
87+
ids.add(Sha256Hash.ZERO_HASH);
88+
return ids;
89+
}
90+
91+
private Vector<Sha256Hash> getTxIds1(BlockCapsule blockCapsule) {
92+
return getSha256Hashes(blockCapsule);
93+
}
94+
95+
private Vector<Sha256Hash> getTxIds2(BlockCapsule blockCapsule) {
96+
return getSha256Hashes(blockCapsule);
97+
}
98+
99+
private Vector<Sha256Hash> getSha256Hashes(BlockCapsule blockCapsule) {
100+
blockCapsule.addTransaction(transactionCapsule1);
101+
blockCapsule.addTransaction(transactionCapsule2);
102+
blockCapsule.addTransaction(transactionCapsule3);
103+
104+
List<Protocol.Transaction> transactionList = blockCapsule.getInstance().getTransactionsList();
105+
106+
return getSha256Hashes(transactionList);
107+
}
108+
109+
private Vector<Sha256Hash> getSha256Hashes(List<Protocol.Transaction> transactionList) {
110+
return transactionList.stream()
111+
.map(TransactionCapsule::new)
112+
.map(TransactionCapsule::getHash)
113+
.collect(Collectors.toCollection(Vector::new));
114+
}
115+
}

0 commit comments

Comments
 (0)