use of com.jd.blockchain.ledger.merkletree.KVEntry in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testDataIteratorSkipping.
private void testDataIteratorSkipping(String[] expectedKeys, SkippingIterator<KVEntry> iterator, int skip) {
int count = expectedKeys.length;
int index = skip;
iterator.skip(index);
if (skip < count) {
assertTrue(iterator.hasNext());
} else {
assertFalse(iterator.hasNext());
}
while (iterator.hasNext()) {
KVEntry data = iterator.next();
assertNotNull(data);
String key = data.getKey().toUTF8String();
assertEquals(expectedKeys[index], key);
index++;
}
assertEquals(count, index);
}
use of com.jd.blockchain.ledger.merkletree.KVEntry in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testDataIterator.
/**
* 测试浏览默克尔树的所有数据节点;
*/
@Test
public void testDataIterator() {
int count = 1024;
List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas);
HashDigest rootHash = merkleTree.getRootHash();
assertNotNull(rootHash);
assertEquals(count, merkleTree.getTotalKeys());
assertEquals(count, merkleTree.getTotalRecords());
Map<String, VersioningKVData<String, byte[]>> dataMap = new HashMap<String, VersioningKVData<String, byte[]>>();
for (VersioningKVData<String, byte[]> data : datas) {
dataMap.put(data.getKey(), data);
}
Iterator<KVEntry> dataIterator = merkleTree.iterator();
String[] dataKeys = new String[count];
int index = 0;
while (dataIterator.hasNext()) {
KVEntry data = dataIterator.next();
assertNotNull(data);
String key = data.getKey().toUTF8String();
assertTrue(dataMap.containsKey(key));
dataMap.remove(key);
dataKeys[index] = key;
index++;
}
assertEquals(0, dataMap.size());
assertEquals(count, index);
SkippingIterator<KVEntry> skippingIterator = merkleTree.iterator();
testDataIteratorSkipping(dataKeys, skippingIterator, 0);
skippingIterator = merkleTree.iterator();
testDataIteratorSkipping(dataKeys, skippingIterator, 1);
skippingIterator = merkleTree.iterator();
testDataIteratorSkipping(dataKeys, skippingIterator, 2);
skippingIterator = merkleTree.iterator();
testDataIteratorSkipping(dataKeys, skippingIterator, 16);
skippingIterator = merkleTree.iterator();
testDataIteratorSkipping(dataKeys, skippingIterator, 128);
skippingIterator = merkleTree.iterator();
testDataIteratorSkipping(dataKeys, skippingIterator, 1023);
skippingIterator = merkleTree.iterator();
testDataIteratorSkipping(dataKeys, skippingIterator, 1024);
}
use of com.jd.blockchain.ledger.merkletree.KVEntry in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testSpecialUseCase_1.
@Test
public void testSpecialUseCase_1() {
CryptoSetting cryptoSetting = createCryptoSetting();
MemoryKVStorage storage = new MemoryKVStorage();
MerkleHashTrie merkleTree = new MerkleHashTrie(cryptoSetting, KEY_PREFIX, storage);
byte[] key = Base58Utils.decode("j5sXmpcomtM2QMUNWeQWsF8bNFFnyeXoCjVAekEeLSscgY");
byte[] value = BytesUtils.toBytes("Special Use-Case VALUE");
long version = 0;
merkleTree.setData(key, version, value);
KVEntry mkdata = merkleTree.getData(key);
assertNotNull(mkdata);
merkleTree.commit();
mkdata = merkleTree.getData(key);
assertNotNull(mkdata);
MerkleTree merkleTreeReload = new MerkleHashTrie(merkleTree.getRootHash(), cryptoSetting, KEY_PREFIX, storage, false);
mkdata = merkleTreeReload.getData(key);
assertNotNull(mkdata);
}
use of com.jd.blockchain.ledger.merkletree.KVEntry in project jdchain-core by blockchain-jd-com.
the class MerkleHashSortTreeTest method testReloadTreeAddRandomNewDataNode.
/**
* 对已存在的树进行重载,增加新的数据节点,通过重载树验证新节点是否添加成功,total keys 与total records
* 是否符合预期,新添加的数据节点Key随机产生
*/
@Test
public void testReloadTreeAddRandomNewDataNode() {
Random random = new Random();
byte[] bytes = new byte[200];
random.nextBytes(bytes);
String newDataKey = bytes.toString();
TreeOptions treeOptions = createTreeOptions();
MemoryKVStorage storage = new MemoryKVStorage();
int count = 1024;
List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
MerkleHashSortTree merkleTree = newMerkleTree_with_committed(datas, treeOptions, storage);
HashDigest rootHash0 = merkleTree.getRootHash();
assertNotNull(rootHash0);
assertEquals(count, merkleTree.getTotalKeys());
// reload and add one data item;
MerkleHashSortTree merkleTree_reload = new MerkleHashSortTree(rootHash0, treeOptions, KEY_PREFIX, storage);
assertEquals(count, merkleTree_reload.getTotalKeys());
assertEquals(rootHash0, merkleTree_reload.getRootHash());
VersioningKVData<String, byte[]> data1025 = new VersioningKVData<String, byte[]>(newDataKey, 0, BytesUtils.toBytes("NEW-VALUE-1025-VERSION-0"));
merkleTree_reload.setData(data1025.getKey(), data1025.getVersion(), data1025.getValue());
merkleTree_reload.commit();
HashDigest rootHash1 = merkleTree_reload.getRootHash();
assertNotNull(rootHash1);
assertNotEquals(rootHash0, rootHash1);
KVEntry data1025_reload_0 = merkleTree_reload.getData(data1025.getKey(), 0);
assertNotNull(data1025_reload_0);
KVEntry data0_reload_0 = merkleTree_reload.getData("KEY-0", 0);
assertNotNull(data0_reload_0);
System.out.println("mkl reload total keys = " + merkleTree_reload.getTotalKeys());
assertEquals(count + 1, merkleTree_reload.getTotalKeys());
MerkleHashSortTree merkleTree_reload_1 = new MerkleHashSortTree(rootHash1, treeOptions, KEY_PREFIX, storage);
assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
assertEquals(rootHash1, merkleTree_reload_1.getRootHash());
HashDigest rootHash2 = merkleTree_reload_1.getRootHash();
assertNotNull(rootHash2);
assertNotEquals(rootHash0, rootHash2);
KVEntry data1025_reload_1 = merkleTree_reload_1.getData(data1025.getKey(), 0);
assertNotNull(data1025_reload_1);
KVEntry data0_reload_1 = merkleTree_reload_1.getData("KEY-0", 0);
assertNotNull(data0_reload_1);
System.out.println("mkl reload total keys = " + merkleTree_reload_1.getTotalKeys());
assertEquals(count + 1, merkleTree_reload_1.getTotalKeys());
}
use of com.jd.blockchain.ledger.merkletree.KVEntry in project jdchain-core by blockchain-jd-com.
the class MerkleHashSortTreeTest method testExtendPersistedPathNodes.
/**
* 测试在已经持久化的默克尔树上追加新节点时,扩展已有路径节点的正确性;
*/
@Test
public void testExtendPersistedPathNodes() {
int count = 10240;
List<VersioningKVData<String, byte[]>> dataList = generateDatas(count);
VersioningKVData<String, byte[]>[] datas = toArray(dataList);
TreeOptions treeOption = createTreeOptions();
MemoryKVStorage storage = new MemoryKVStorage();
Bytes prefix = Bytes.fromString(LedgerTestUtils.LEDGER_KEY_PREFIX);
MerkleHashSortTree merkleTree = new MerkleHashSortTree(treeOption, prefix, storage);
int firstBatch = 500;
for (int i = 0; i < firstBatch; i++) {
merkleTree.setData(datas[i].getKey(), datas[i].getVersion(), datas[i].getValue());
}
// 先提交;
merkleTree.commit();
// 加载默克尔树到新实例;
HashDigest rootHash = merkleTree.getRootHash();
MerkleHashSortTree merkleTree1 = new MerkleHashSortTree(rootHash, treeOption, prefix, storage);
for (int i = firstBatch; i < datas.length; i++) {
merkleTree1.setData(datas[i].getKey(), datas[i].getVersion(), datas[i].getValue());
}
merkleTree1.commit();
// 重新加载;未正确扩展路径节点时,部分已持久化的叶子节点有可能丢失,在重新加载默克尔树并进行检索时将发现此错误;
rootHash = merkleTree1.getRootHash();
MerkleHashSortTree merkleTree2 = new MerkleHashSortTree(rootHash, treeOption, prefix, storage);
for (int i = 0; i < datas.length; i++) {
KVEntry data = merkleTree2.getData(datas[i].getKey());
assertNotNull(data);
}
}
Aggregations