use of com.jd.blockchain.ledger.CryptoSetting 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.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class TransactionSetTest method testSingleTransactionGetAndSet.
@Test
public void testSingleTransactionGetAndSet() {
CryptoSetting cryptoSetting = LedgerTestUtils.createDefaultCryptoSetting();
MemoryKVStorage testStorage = new MemoryKVStorage();
// Create a new TransactionSet, it's empty;
TransactionSetEditor txset = new TransactionSetEditor(cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE);
assertTrue(txset.isUpdated());
assertFalse(txset.isReadonly());
assertNull(txset.getRootHash());
HashDigest ledgerHash = LedgerTestUtils.generateRandomHash();
TransactionRequest txReq = buildTransactionRequest_RandomOperation(ledgerHash, cryptoSetting);
long blockHeight = 8922L;
TransactionState txState = TransactionState.SUCCESS;
TransactionResult tx = buildTransactionResult(txReq, blockHeight, txState);
txset.addTransaction(txReq, tx);
assertTrue(txset.isUpdated());
txset.commit();
HashDigest txsetRootHash = txset.getRootHash();
assertNotNull(txsetRootHash);
assertEquals(1, txset.getTotalCount());
assertEquals(blockHeight, tx.getBlockHeight());
assertEquals(ledgerHash, txReq.getTransactionContent().getLedgerHash());
// Reload ;
TransactionSetEditor reloadTxset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, true);
assertEquals(1, reloadTxset.getTotalCount());
TransactionResult reloadTx = reloadTxset.getTransactionResult(txReq.getTransactionHash());
assertNotNull(reloadTx);
assertEquals(txState, reloadTx.getExecutionState());
TransactionState state = reloadTxset.getState(txReq.getTransactionHash());
assertEquals(txState, state);
assertTransactionEquals(tx, reloadTx);
}
use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method testMerkleTreeDiffNewLeafType.
@Test
public void testMerkleTreeDiffNewLeafType() {
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 = 1;
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);
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 = 0;
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.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class LedgerRepositoryImpl method innerGetLedgerEventSet.
private LedgerEventSetEditor innerGetLedgerEventSet(LedgerBlock block) {
LedgerAdminDataSetEditor adminDataset = createAdminDataset(block);
CryptoSetting cryptoSetting = adminDataset.getSettings().getCryptoSetting();
EventGroupPublisher systemEventSet = createSystemEventSet(block, cryptoSetting);
EventAccountSetEditor userEventSet = createUserEventSet(block, cryptoSetting);
return new LedgerEventSetEditor(systemEventSet, userEventSet, true);
}
use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.
the class LedgerManager method register.
@Override
public LedgerRepository register(HashDigest ledgerHash, KVStorageService storageService, LedgerDataStructure dataStructure) {
if (ledgers.containsKey(ledgerHash)) {
LedgerRepositoryContext ledgerCtx = ledgers.get(ledgerHash);
return ledgerCtx.ledgerRepo;
}
// 加载账本数据库;
VersioningKVStorage ledgerVersioningStorage = storageService.getVersioningKVStorage();
ExPolicyKVStorage ledgerExPolicyStorage = storageService.getExPolicyKVStorage();
// 数据锚定merkle tree的方式
LedgerRepository ledgerRepo = new LedgerRepositoryImpl(ledgerHash, LEDGER_PREFIX, ledgerExPolicyStorage, ledgerVersioningStorage, dataStructure);
// 校验 crypto service provider ;
CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
checkCryptoSetting(cryptoSetting, ledgerHash);
// 创建账本上下文;
LedgerRepositoryContext ledgerCtx = new LedgerRepositoryContext(ledgerRepo, storageService);
ledgers.put(ledgerHash, ledgerCtx);
return ledgerRepo;
}
Aggregations