use of com.jd.blockchain.storage.service.utils.VersioningKVData in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testMerkleTreeDiffPathsType.
@Test
public void testMerkleTreeDiffPathsType() {
CryptoSetting cryptoSetting = createCryptoSetting();
MemoryKVStorage storage = new MemoryKVStorage();
List<String> newdataListString = new ArrayList<String>();
List<String> dataListString = new ArrayList<String>();
List<VersioningKVData<String, byte[]>> dataList = new ArrayList<VersioningKVData<String, byte[]>>();
List<VersioningKVData<String, byte[]>> newdataList = new ArrayList<VersioningKVData<String, byte[]>>();
int count = 2;
int newAddCount = 5;
VersioningKVData<String, byte[]> orginData0 = new VersioningKVData<String, byte[]>("KEY-19745261600024463", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
dataList.add(orginData0);
dataListString.add(orginData0.getKey());
VersioningKVData<String, byte[]> orginData1 = new VersioningKVData<String, byte[]>("KEY-155749221633494971", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
dataList.add(orginData1);
dataListString.add(orginData1.getKey());
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
HashDigest rootHash0 = merkleTree.getRootHash();
assertNotNull(rootHash0);
assertEquals(count, merkleTree.getTotalKeys());
assertEquals(count, merkleTree.getTotalRecords());
// reload and add random key data item;
MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
assertEquals(count, merkleTree_reload.getTotalKeys());
assertEquals(count, merkleTree_reload.getTotalRecords());
assertEquals(rootHash0, merkleTree_reload.getRootHash());
VersioningKVData<String, byte[]> data0 = new VersioningKVData<String, byte[]>("KEY-1741789838495252", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data0);
VersioningKVData<String, byte[]> data1 = new VersioningKVData<String, byte[]>("KEY-2741789838505562", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data1);
VersioningKVData<String, byte[]> data2 = new VersioningKVData<String, byte[]>("KEY-0745023937104559", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data2);
VersioningKVData<String, byte[]> data3 = new VersioningKVData<String, byte[]>("KEY-7745261599950097", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data3);
VersioningKVData<String, byte[]> data4 = new VersioningKVData<String, byte[]>("KEY-9745261599963367", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data4);
for (int i = 0; i < newdataList.size(); i++) {
merkleTree_reload.setData(newdataList.get(i).getKey(), newdataList.get(i).getVersion(), newdataList.get(i).getValue());
newdataListString.add(newdataList.get(i).getKey());
}
merkleTree_reload.commit();
HashDigest rootHash1 = merkleTree_reload.getRootHash();
assertNotNull(rootHash1);
assertNotEquals(rootHash0, rootHash1);
assertEquals(count + newAddCount, merkleTree_reload.getTotalKeys());
assertEquals(count + newAddCount, merkleTree_reload.getTotalRecords());
SkippingIterator<KVEntry> diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// max boundary skip test
assertEquals(newAddCount, diffIterator.getTotalCount());
assertEquals(-1, diffIterator.getCursor());
assertTrue(diffIterator.hasNext());
long skipped = diffIterator.skip(newAddCount);
assertEquals(newAddCount, skipped);
assertFalse(diffIterator.hasNext());
// re-interator and random skip test
int skipNum = 4;
diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
assertEquals(newAddCount, diffIterator.getTotalCount());
assertEquals(-1, diffIterator.getCursor());
assertTrue(diffIterator.hasNext());
long skipped1 = diffIterator.skip(skipNum);
assertEquals(skipNum, skipped1);
int diffNum = 0;
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// diffNum++;
// }
// assertEquals(diffNum, diffIterator.getCount() - skipNum);
// re-interator and next test
diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
int diffNum1 = 0;
assertEquals(newAddCount, diffIterator.getTotalCount());
while (diffIterator.hasNext()) {
KVEntry data = diffIterator.next();
assertNotNull(data);
diffNum1++;
}
assertFalse(diffIterator.hasNext());
assertEquals(newAddCount - 1, diffIterator.getCursor());
assertEquals(newAddCount, diffIterator.getTotalCount());
assertEquals(diffNum1, diffIterator.getTotalCount());
// re-interator and test next key consistency
diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// }
}
use of com.jd.blockchain.storage.service.utils.VersioningKVData in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testMerkleTreeDiffMerkleDataIteratorType.
@Test
public void testMerkleTreeDiffMerkleDataIteratorType() {
CryptoSetting cryptoSetting = createCryptoSetting();
MemoryKVStorage storage = new MemoryKVStorage();
List<String> newdataListString = new ArrayList<String>();
List<String> dataListString = new ArrayList<String>();
List<VersioningKVData<String, byte[]>> dataList = new ArrayList<VersioningKVData<String, byte[]>>();
List<VersioningKVData<String, byte[]>> newdataList = new ArrayList<VersioningKVData<String, byte[]>>();
int count = 1;
int newAddCount = 5;
VersioningKVData<String, byte[]> orginData0 = new VersioningKVData<String, byte[]>("KEY-0", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
dataList.add(orginData0);
dataListString.add(orginData0.getKey());
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
HashDigest rootHash0 = merkleTree.getRootHash();
assertNotNull(rootHash0);
assertEquals(count, merkleTree.getTotalKeys());
assertEquals(count, merkleTree.getTotalRecords());
// reload and add random key data item;
MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
assertEquals(count, merkleTree_reload.getTotalKeys());
assertEquals(count, merkleTree_reload.getTotalRecords());
assertEquals(rootHash0, merkleTree_reload.getRootHash());
VersioningKVData<String, byte[]> data0 = new VersioningKVData<String, byte[]>("KEY-1741789838495252", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data0);
VersioningKVData<String, byte[]> data1 = new VersioningKVData<String, byte[]>("KEY-2741789838505562", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data1);
VersioningKVData<String, byte[]> data2 = new VersioningKVData<String, byte[]>("KEY-0745023937104559", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data2);
VersioningKVData<String, byte[]> data3 = new VersioningKVData<String, byte[]>("KEY-7745261599950097", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data3);
VersioningKVData<String, byte[]> data4 = new VersioningKVData<String, byte[]>("KEY-9745261599963367", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
newdataList.add(data4);
for (int i = 0; i < newdataList.size(); i++) {
merkleTree_reload.setData(newdataList.get(i).getKey(), newdataList.get(i).getVersion(), newdataList.get(i).getValue());
newdataListString.add(newdataList.get(i).getKey());
}
merkleTree_reload.commit();
HashDigest rootHash1 = merkleTree_reload.getRootHash();
assertNotNull(rootHash1);
assertNotEquals(rootHash0, rootHash1);
assertEquals(count + newAddCount, merkleTree_reload.getTotalKeys());
assertEquals(count + newAddCount, merkleTree_reload.getTotalRecords());
SkippingIterator<KVEntry> diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// max boundary skip test
assertEquals(newAddCount, diffIterator.getTotalCount());
assertEquals(-1, diffIterator.getCursor());
assertTrue(diffIterator.hasNext());
long skipped = diffIterator.skip(newAddCount);
assertEquals(newAddCount, skipped);
assertFalse(diffIterator.hasNext());
// re-interator and random skip test
int skipNum = 4;
diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
assertEquals(newAddCount, diffIterator.getTotalCount());
assertEquals(-1, diffIterator.getCursor());
assertTrue(diffIterator.hasNext());
long skipped1 = diffIterator.skip(skipNum);
assertEquals(skipNum, skipped1);
int diffNum = 0;
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// diffNum++;
// }
// assertEquals(diffNum, diffIterator.getCount() - skipNum);
// re-interator and next test
diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
int diffNum1 = 0;
assertEquals(newAddCount, diffIterator.getTotalCount());
while (diffIterator.hasNext()) {
KVEntry data = diffIterator.next();
assertNotNull(data);
diffNum1++;
}
assertFalse(diffIterator.hasNext());
assertEquals(newAddCount - 1, diffIterator.getCursor());
assertEquals(newAddCount, diffIterator.getTotalCount());
assertEquals(diffNum1, diffIterator.getTotalCount());
// re-interator and test next key consistency
diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// }
}
use of com.jd.blockchain.storage.service.utils.VersioningKVData in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testMerkleProofCorrectness.
/**
* 测试 Merkle 证明的正确性;
*/
@Test
public void testMerkleProofCorrectness() {
// 长度为 0 的情况;
int count = 0;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash0 = merkleTree.getRootHash();
assertNotNull(rootHash0);
// 预期空的默克尔树中查询任何数据的证明都获得 null 返回;
MerkleProof proof = merkleTree.getProof("KEY-0");
assertNull(proof);
// 长度为 1 的情况;
count = 1;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash1 = merkleTree.getRootHash();
assertNotNull(rootHash1);
// TODO: 暂时忽略默克尔证明的测试;
// // 预期在只有 1 条数据的情况下可以正常得到该数据的默克尔证明;
// MerkleProof proof1_0 = merkleTree.getProof("KEY-0");
// assertNotNull(proof1_0);
// // 依照设计,预期任何默克尔证明都至少有 4 条路径;
// assertMerkleProofPath(proof1_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
// 长度为 2 的情况;
count = 2;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash2 = merkleTree.getRootHash();
assertNotNull(rootHash2);
// TODO: 暂时忽略默克尔证明的测试;
// MerkleProof proof2_0 = merkleTree.getProof("KEY-0");
// assertNotNull(proof2_0);
// // 依照设计,预期任何默克尔证明都至少有 4 条路径;
// assertMerkleProofPath(proof2_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
// 长度为 16 的情况;
count = 16;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash16 = merkleTree.getRootHash();
assertNotNull(rootHash16);
// TODO: 暂时忽略默克尔证明的测试;
// MerkleProof proof16_0 = merkleTree.getProof("KEY-0");
// assertNotNull(proof16_0);
// // 依照设计,预期任何默克尔证明都至少有 4 条路径;
// assertMerkleProofPath(proof16_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
// 长度为 32 的情况;
count = 32;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash32 = merkleTree.getRootHash();
assertNotNull(rootHash32);
// TODO: 暂时忽略默克尔证明的测试;
// MerkleProof proof32_0 = merkleTree.getProof("KEY-0");
// assertNotNull(proof32_0);
// // 依照设计,预期任何默克尔证明都至少有 4 条路径;
// assertMerkleProofPath(proof32_0, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
// 长度为 1025 的情况;
count = 1025;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash1025 = merkleTree.getRootHash();
assertNotNull(rootHash1025);
// TODO: 暂时忽略默克尔证明的测试;
// MerkleProof proof1025 = merkleTree.getProof("KEY-0");
// assertNotNull(proof1025);
// // 依照设计,预期任何默克尔证明都至少有 4 条路径;
// assertMerkleProofPath(proof1025, merkleTree.getRootHash(), merkleTree.getData("KEY-0"));
}
use of com.jd.blockchain.storage.service.utils.VersioningKVData in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testImmutability.
/**
* 测试 Merkle 根哈希的不变性,即相同的数据集合得到相同的默克尔树根哈希,与数据加入的先后顺序无关;
*/
@Test
public void testImmutability() {
// 长度为 0 的情况;
int count = 0;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
HashDigest rootHash0_1 = buildMerkleRootHash(datas);
HashDigest rootHash0_2 = buildMerkleRootHash(datas);
assertNotNull(rootHash0_1);
assertNotNull(rootHash0_2);
assertEquals(rootHash0_1, rootHash0_2);
// 长度为 1 的情况;
count = 1;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
HashDigest rootHash1_1 = buildMerkleRootHash(datas);
HashDigest rootHash1_2 = buildMerkleRootHash(datas);
assertNotNull(rootHash1_1);
assertNotNull(rootHash1_2);
assertEquals(rootHash1_1, rootHash1_2);
// 长度为 2 的情况;
count = 2;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
VersioningKVData<String, byte[]>[] datas1 = toArray(dataList);
datas1[0] = datas[1];
datas1[1] = datas[0];
HashDigest rootHash2_1 = buildMerkleRootHash(datas);
HashDigest rootHash2_2 = buildMerkleRootHash(datas1);
assertNotNull(rootHash2_1);
assertNotNull(rootHash2_2);
assertEquals(rootHash2_1, rootHash2_2);
// 长度为 8 的情况;
count = 8;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
datas1 = getRandomSortingCopy(datas);
HashDigest rootHash_N1 = buildMerkleRootHash(datas);
HashDigest rootHash_N2 = buildMerkleRootHash(datas1);
assertNotNull(rootHash_N1);
assertNotNull(rootHash_N2);
assertEquals(rootHash_N1, rootHash_N2);
// 长度为 16 的情况;
count = 16;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
datas1 = getRandomSortingCopy(datas);
rootHash_N1 = buildMerkleRootHash(datas);
rootHash_N2 = buildMerkleRootHash(datas1);
assertNotNull(rootHash_N1);
assertNotNull(rootHash_N2);
assertEquals(rootHash_N1, rootHash_N2);
// 长度为 32 的情况;
count = 32;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
datas1 = getRandomSortingCopy(datas);
rootHash_N1 = buildMerkleRootHash(datas);
rootHash_N2 = buildMerkleRootHash(datas1);
assertNotNull(rootHash_N1);
assertNotNull(rootHash_N2);
assertEquals(rootHash_N1, rootHash_N2);
// 长度为 1025 的情况;
count = 1025;
// System.out.printf("\r\n\r\n================= %s 个节点 =================\r\n\r\n", count);
dataList = generateDatas(count);
datas = toArray(dataList);
datas1 = getRandomSortingCopy(datas);
MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas);
MerkleHashTrie merkleTree1 = newMerkleTree_with_committed(datas1);
assertEquals(rootHash_N1, rootHash_N2);
assertEqualsTrie(merkleTree, merkleTree1);
}
use of com.jd.blockchain.storage.service.utils.VersioningKVData in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testCancel.
/**
* 验证 HashSortingMerkleTree 在未提交之前,新增的数据记录可读和可回滚特性;
*/
@Test
public void testCancel() {
// 数据集合长度为 1024 时也能正常生成;
int count = 1024;
List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
MerkleHashTrie merkleTree = newMerkleTree(datas);
assertTrue(merkleTree.isUpdated());
// 未提交之前查不到信息;
assertNull(merkleTree.getRootHash());
assertEquals(0, merkleTree.getTotalKeys());
assertEquals(0, merkleTree.getTotalRecords());
MerkleTrieData dt = merkleTree.getData("KEY-69");
assertNotNull(dt);
assertEquals(0, dt.getVersion());
dt = merkleTree.getData("KEY-69", 0);
assertNotNull(dt);
assertEquals(0, dt.getVersion());
dt = merkleTree.getData("KEY-69", 1);
assertNull(dt);
VersioningKVData<String, byte[]> data69 = new VersioningKVData<String, byte[]>("KEY-69", 1, BytesUtils.toBytes("NEW-VALUE-VERSION-1"));
merkleTree.setData(data69.getKey(), data69.getVersion(), data69.getValue());
dt = merkleTree.getData("KEY-69", 1);
assertNotNull(dt);
assertEquals(1, dt.getVersion());
assertTrue(merkleTree.isUpdated());
// 回滚全部数据;
merkleTree.cancel();
HashDigest rootHash = merkleTree.getRootHash();
assertNull(rootHash);
assertEquals(0, merkleTree.getTotalKeys());
// 由于 KEY-69 写入了 2 个版本的记录;
assertEquals(0, merkleTree.getTotalRecords());
dt = merkleTree.getData("KEY-69");
assertNull(dt);
dt = merkleTree.getData("KEY-69", 0);
assertNull(dt);
dt = merkleTree.getData("KEY-69", 1);
assertNull(dt);
assertFalse(merkleTree.isUpdated());
}
Aggregations