use of com.jd.blockchain.ledger.MerkleProof in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testCreation.
/**
* 测试树的创建的正确性;
*/
@Test
public void testCreation() {
// 数据集合长度为 0 时也能正常生成;
List<VersioningKVData<String, byte[]>> dataList = generateDatas(0);
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash = merkleTree.getRootHash();
assertNotNull(rootHash);
assertEquals(0, merkleTree.getTotalKeys());
assertEquals(0, merkleTree.getTotalRecords());
MerkleProof proof = merkleTree.getProof("KEY_NOT_EXIST");
assertNull(proof);
// 数据集合长度为 1 时也能正常生成;
dataList = generateDatas(1);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
rootHash = merkleTree.getRootHash();
assertNotNull(rootHash);
assertEquals(1, merkleTree.getTotalKeys());
assertEquals(1, merkleTree.getTotalRecords());
// TODO: 暂时忽略默克尔证明的测试;
// 默克尔证明路径的长度至少为 4 ——包括:根节点/叶子节点/数据节点/值哈希;
// assertMerkleProofAndProofLength(datas[0], merkleTree, 4);
// 数据集合长度为 2 时也能正常生成;
dataList = generateDatas(2);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
rootHash = merkleTree.getRootHash();
assertNotNull(rootHash);
assertEquals(2, merkleTree.getTotalKeys());
assertEquals(2, merkleTree.getTotalRecords());
// TODO: 暂时忽略默克尔证明的测试;
// assertMerkleProofAndProofLength(datas[0], merkleTree, 4);
// assertMerkleProofAndProofLength(datas[1], merkleTree, 4);
// 数据集合长度为 100 时也能正常生成;
dataList = generateDatas(100);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
rootHash = merkleTree.getRootHash();
assertNotNull(rootHash);
assertEquals(100, merkleTree.getTotalKeys());
assertEquals(100, merkleTree.getTotalRecords());
// 数据集合长度为 1024 时也能正常生成;
int count = 1024;
dataList = generateDatas(count);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
rootHash = merkleTree.getRootHash();
assertNotNull(rootHash);
assertEquals(count, merkleTree.getTotalKeys());
assertEquals(count, merkleTree.getTotalRecords());
// merkleTree.print();
// TODO: 暂时忽略默克尔证明的测试;
// for (VersioningKVData<String, byte[]> data : datas) {
// assertMerkleProof(data, merkleTree);
// }
// testMerkleProof1024(datas, merkleTree);
// 数据集合长度为 20000 时也能正常生成;
count = 20000;
dataList = generateDatas(count);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
rootHash = merkleTree.getRootHash();
assertNotNull(rootHash);
assertEquals(count, merkleTree.getTotalKeys());
assertEquals(count, merkleTree.getTotalRecords());
// merkleTree.print();
// TODO: 暂时忽略默克尔证明的测试;
// for (VersioningKVData<String, byte[]> data : datas) {
// assertMerkleProof(data, merkleTree);
// }
}
use of com.jd.blockchain.ledger.MerkleProof in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method assertMerkleProofAndProofLength.
private void assertMerkleProofAndProofLength(VersioningKVData<String, byte[]> data, MerkleTree merkleTree, int expectProofPathCount) {
MerkleProof proof = assertMerkleProof(data, merkleTree);
assertEquals(expectProofPathCount, proof.getProofLevels().length);
}
use of com.jd.blockchain.ledger.MerkleProof in project jdchain-core by blockchain-jd-com.
the class MerkleHashSortTreeTest method assertMerkleProofAndProofLength.
private void assertMerkleProofAndProofLength(VersioningKVData<String, byte[]> data, MerkleTree merkleTree, int expectProofPathCount) {
MerkleProof proof = assertMerkleProof(data, merkleTree);
assertEquals(expectProofPathCount, proof.getProofLevels().length);
}
use of com.jd.blockchain.ledger.MerkleProof in project jdchain-core by blockchain-jd-com.
the class MerkleSequenceDataset method getDataProof.
public MerkleDataProof getDataProof(Bytes key) {
DataEntry<Bytes, byte[]> dataEntry = getDataEntry(key);
if (dataEntry == null) {
return null;
}
MerkleProof proof = getProof(key);
return new MerkleDataEntryWrapper(dataEntry, proof);
}
use of com.jd.blockchain.ledger.MerkleProof in project jdchain-core by blockchain-jd-com.
the class MerkleHashDataset method getDataProof.
public MerkleDataProof getDataProof(Bytes key, long version) {
DataEntry<Bytes, byte[]> dataEntry = getDataEntry(key, version);
if (dataEntry == null) {
return null;
}
MerkleProof proof = getProof(key);
return new MerkleDataEntryWrapper(dataEntry, proof);
}
Aggregations