use of com.jd.blockchain.ledger.merkletree.BytesKeyValue in project jdchain-core by blockchain-jd-com.
the class MerkleHashSortTreeTest method testHashBucket.
/**
*/
@Test
public void testHashBucket() {
byte[][] keys = new byte[4][];
byte[][] values = new byte[4][];
for (int i = 0; i < keys.length; i++) {
keys[i] = BytesUtils.toBytes("KEY-" + i);
values[i] = RandomUtils.generateRandomBytes(16);
}
TreeOptions treeOptions = TreeOptions.build().setDefaultHashAlgorithm(ClassicAlgorithm.SHA256.code());
Bytes bucketPrefix = Bytes.fromString("BUCKET");
MemoryKVStorage kvStorage = new MemoryKVStorage();
MerkleHashBucket hashBucket = new MerkleHashBucket(100, keys[0], values[0], TreeDegree.D3, treeOptions, bucketPrefix, kvStorage);
// 验证初始化之后的数据是否正确;
assertEquals(1, hashBucket.getKeysCount());
MerkleValue<byte[]> value = hashBucket.getValue(keys[0]);
assertNotNull(value);
assertEquals(0, value.getId());
assertArrayEquals(values[0], value.getValue());
assertEquals(0, hashBucket.getVersion(keys[0]));
MerkleValue<byte[]> value_v1 = hashBucket.getValue(keys[0], 1);
assertNull(value_v1);
MerkleValue<byte[]> value1_v1 = hashBucket.getValue(keys[1], 0);
assertNull(value1_v1);
// 提交数据;
hashBucket.commit();
// 模拟对默尔克哈希桶的存储;
byte[] bucketBytes = BinaryProtocol.encode(hashBucket, HashBucketEntry.class);
HashBucketEntry bucketEntry = BinaryProtocol.decode(bucketBytes);
// 重新加载;
hashBucket = new MerkleHashBucket(100, bucketEntry.getKeySet(), TreeDegree.D3, treeOptions, bucketPrefix, kvStorage);
// 验证重新加载之后的数据正确性;
value = hashBucket.getValue(keys[0]);
assertNotNull(value);
assertEquals(0, value.getId());
assertArrayEquals(values[0], value.getValue());
assertEquals(0, hashBucket.getVersion(keys[0]));
assertEquals(1, hashBucket.getKeysCount());
SkippingIterator<MerkleValue<HashEntry>> keysIterator = hashBucket.iterator();
assertEquals(1, keysIterator.getTotalCount());
assertTrue(keysIterator.hasNext());
MerkleValue<HashEntry> entry = keysIterator.next();
assertNotNull(entry);
assertTrue(entry.getValue() instanceof BytesKeyValue);
BytesKeyValue kv = (BytesKeyValue) entry.getValue();
assertArrayEquals(keys[0], kv.getKey().toBytes());
assertArrayEquals(values[0], kv.getValue().toBytes());
// 验证加入新的键;
for (int i = 1; i < keys.length; i++) {
hashBucket.setValue(keys[i], 0, values[i]);
}
assertEquals(keys.length, hashBucket.getKeysCount());
for (int i = 0; i < keys.length; i++) {
value = hashBucket.getValue(keys[i]);
assertNotNull(value);
// id 即版本;
assertEquals(0, value.getId());
assertArrayEquals(values[i], value.getValue());
assertEquals(0, hashBucket.getVersion(keys[i]));
}
hashBucket.commit();
// 重新加载并验证数据;
bucketBytes = BinaryProtocol.encode(hashBucket, HashBucketEntry.class);
bucketEntry = BinaryProtocol.decode(bucketBytes);
hashBucket = new MerkleHashBucket(100, bucketEntry.getKeySet(), TreeDegree.D3, treeOptions, bucketPrefix, kvStorage);
assertEquals(keys.length, hashBucket.getKeysCount());
for (int i = 0; i < keys.length; i++) {
value = hashBucket.getValue(keys[i]);
assertNotNull(value);
// id 即版本;
assertEquals(0, value.getId());
assertArrayEquals(values[i], value.getValue());
assertEquals(0, hashBucket.getVersion(keys[i]));
}
}
Aggregations