use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class LedgerEditorTest method testMerkleDataSet1.
@Test
public void testMerkleDataSet1() {
CryptoSetting setting = LedgerTestUtils.createDefaultCryptoSetting();
Bytes keyPrefix = Bytes.fromString(LedgerTestUtils.LEDGER_KEY_PREFIX).concat(MerkleHashDataset.MERKLE_TREE_PREFIX);
MemoryKVStorage storage = new MemoryKVStorage();
byte[] key = Base58Utils.decode("j5q7n8ShYqKVitaobZrERtBK7GowGGZ54RuaUeWjLsdPYY");
HashDigest valueHash = Crypto.resolveAsHashDigest(Base58Utils.decode("j5o6mMnMQqE5fJKJ93FzXPnu4vFCfpBKp7u4r8tUUaFRK8"));
long version = 0;
MerkleHashTrie merkleTree = new MerkleHashTrie(setting, keyPrefix, storage);
merkleTree.setData(key, version, valueHash.toBytes());
merkleTree.commit();
MerkleTrieData data = merkleTree.getData(key);
assertNotNull(data);
merkleTree = new MerkleHashTrie(merkleTree.getRootHash(), setting, keyPrefix, storage, false);
data = merkleTree.getData(key);
assertNotNull(data);
// MerkleDataSet1 mkds = new MerkleDataSet1(setting, keyPrefix, storage, storage);
// HashDigest ledgerHash = new HashDigest(Base58Utils.decode("j5mxiw6RiHP7fhrySjYji1ER5aRe6d2quYHArtwUfsyoHZ"));
//
// BlockchainKeypair user1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp",
// "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
// TransactionRequest req1 = LedgerTestUtils.createTxRequest_UserReg(user1, ledgerHash, 1580315317127L, parti0,
// parti0);
// // 引发错误的参数:ts=1580315317127;
// // txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
// System.out.printf("\r\n ===||=== transactionRequest1.getTransactionContent().getHash()=[%s]\r\n",
// req1.getTransactionContent().getHash().toBase58());
}
use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class MerkleHashTriePerformanceTest method testPerformace1.
private void testPerformace1(int round, int count) {
System.out.printf("------------- Performance test: MerkleHashTrie --------------\r\n", round, count);
CryptoSetting setting = LedgerTestUtils.createDefaultCryptoSetting();
Bytes prefix = Bytes.fromString(LedgerTestUtils.LEDGER_KEY_PREFIX);
MemoryKVStorage storage = new MemoryKVStorage();
Random rand = new Random();
byte[] value = new byte[128];
rand.nextBytes(value);
long startTs = System.currentTimeMillis();
MerkleHashTrie merkleTree = new MerkleHashTrie(setting, prefix, storage);
String key;
for (int r = 0; r < round; r++) {
for (int i = 0; i < count; i++) {
key = "KEY-" + r + "-" + i;
merkleTree.setData(key, 0, value);
}
merkleTree.commit();
}
long elapsedTs = System.currentTimeMillis() - startTs;
long totalCount = count * round;
double tps = round * 1000.0D / elapsedTs;
double kps = round * count * 1000.0D / elapsedTs;
System.out.printf("--[Performance]:: TotalKeys=%s; Round=%s; Count=%s; Times=%sms; TPS=%.2f; KPS=%.2f\r\n\r\n", totalCount, round, count, elapsedTs, tps, kps);
}
use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class MerkleHashDataSetTest method testInsertSameData.
@Test
public void testInsertSameData() {
String keyPrefix = "";
Random rand = new Random();
CryptoSetting cryptoSetting = createCryptoSetting();
MemoryKVStorage storage = new MemoryKVStorage();
MerkleHashDataset mds = new MerkleHashDataset(cryptoSetting, keyPrefix, storage, storage);
Dataset<String, byte[]> ds = DatasetHelper.map(mds);
// 初始的时候没有任何数据,总是返回 null;
DataEntry verKVEntry = ds.getDataEntry("NULL_KEY");
byte[] vbytes = ds.getValue("NULL_KEY");
assertNull(verKVEntry);
assertNull(vbytes);
Map<String, Long> dataVersions = new HashMap<>();
// Map<String, byte[]> dataValues = new HashMap<>();
Map<HashDigest, Map<String, KeySnapshot>> history = new LinkedHashMap<>();
HashDigest rootHash;
// generate base data sample;
// + rand.nextInt(1024);
int count = 1024;
String key;
byte[] data = new byte[64];
rand.nextBytes(data);
long v;
MerkleProof proof;
for (int i = 0; i < count; i++) {
key = "data" + i;
v = ds.setValue(key, data, -1);
dataVersions.put(key, v);
// dataValues.put(key + "_" + v, data);
assertEquals(v, 0);
}
mds.commit();
// Take snapshot;
{
rootHash = mds.getRootHash();
Map<String, KeySnapshot> snapshot = new HashMap<>();
for (int i = 0; i < count; i++) {
key = "data" + i;
// TODO: 暂时注释掉默克尔证明相关的内容;
// proof = mds.getProof(key);
// assertNotNull(proof);
// assertEquals(rootHash, proof.getRootHash());
KeySnapshot ks = new KeySnapshot();
ks.rootHash = mds.getRootHash();
ks.maxVersion = ds.getVersion(key);
snapshot.put(key, ks);
}
history.put(rootHash, snapshot);
}
// verify;
{
MerkleHashDataset mdsReload = new MerkleHashDataset(rootHash, cryptoSetting, keyPrefix, storage, storage, true);
Dataset<String, byte[]> dsReload = DatasetHelper.map(mdsReload);
// verify every keys;
Map<String, KeySnapshot> snapshot = history.get(rootHash);
MerkleProof expProof;
for (int i = 0; i < count; i++) {
key = "data" + i;
// TODO: 暂时注释掉默克尔证明相关的内容;
// proof = mdsReload.getProof(key);
// assertNotNull(proof);
// assertEquals(rootHash, proof.getRootHash());
// expProof = snapshot.get(key).rootHash;
// assertEquals(expProof.toString(), proof.toString());
byte[] value = dsReload.getValue(key);
assertTrue(BytesUtils.equals(data, value));
}
}
}
use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class MerkleHashDataSetTest method testSpecialCase_1.
@Test
public void testSpecialCase_1() {
CryptoSetting cryptoSetting = createCryptoSetting();
MemoryKVStorage storage = new MemoryKVStorage();
MerkleHashDataset mds = new MerkleHashDataset(cryptoSetting, KEY_PREFIX, storage, storage);
Bytes key = Bytes.fromBase58("j5sXmpcomtM2QMUNWeQWsF8bNFFnyeXoCjVAekEeLSscgY");
byte[] value = BytesUtils.toBytes("Special Use-Case VALUE");
mds.setValue(key, value, -1);
byte[] data = mds.getValue(key);
assertNotNull(data);
mds.commit();
data = mds.getValue(key);
assertNotNull(data);
MerkleHashDataset mds_reload = new MerkleHashDataset(mds.getRootHash(), cryptoSetting, KEY_PREFIX, storage, storage, false);
data = mds_reload.getValue(key);
assertNotNull(data);
}
use of com.jd.blockchain.ledger.CryptoSetting 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())));
// }
}
Aggregations