Search in sources :

Example 16 with CryptoSetting

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);
}
Also used : MerkleTree(com.jd.blockchain.ledger.merkletree.MerkleTree) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 17 with CryptoSetting

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);
}
Also used : TransactionState(com.jd.blockchain.ledger.TransactionState) TransactionResult(com.jd.blockchain.ledger.TransactionResult) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) TransactionSetEditor(com.jd.blockchain.ledger.core.TransactionSetEditor) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) Test(org.junit.Test)

Example 18 with CryptoSetting

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())));
// }
}
Also used : ArrayList(java.util.ArrayList) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 19 with CryptoSetting

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);
}
Also used : CryptoSetting(com.jd.blockchain.ledger.CryptoSetting)

Example 20 with CryptoSetting

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;
}
Also used : ExPolicyKVStorage(com.jd.blockchain.storage.service.ExPolicyKVStorage) VersioningKVStorage(com.jd.blockchain.storage.service.VersioningKVStorage) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting)

Aggregations

CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)35 Test (org.junit.Test)26 HashDigest (com.jd.blockchain.crypto.HashDigest)23 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)23 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)15 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)12 Random (java.util.Random)11 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)10 ArrayList (java.util.ArrayList)8 MerkleSequenceTree (com.jd.blockchain.ledger.proof.MerkleSequenceTree)7 ExistancePolicyKVStorageMap (com.jd.blockchain.storage.service.utils.ExistancePolicyKVStorageMap)7 MerkleNode (com.jd.blockchain.ledger.MerkleNode)4 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)4 TransactionSetEditor (com.jd.blockchain.ledger.core.TransactionSetEditor)4 MerkleTrieData (com.jd.blockchain.ledger.proof.MerkleTrieData)4 TreeMap (java.util.TreeMap)4 Bytes (utils.Bytes)4 TransactionResult (com.jd.blockchain.ledger.TransactionResult)3 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)2