Search in sources :

Example 1 with TransactionResult

use of com.jd.blockchain.ledger.TransactionResult in project jdchain-core by blockchain-jd-com.

the class TransactionSetEditor method getTransaction.

@Override
public LedgerTransaction getTransaction(long txSeq) {
    TransactionRequest txRequest = loadRequestKv(txSeq);
    if (null == txRequest) {
        return null;
    }
    TransactionResult txResult = loadResultKv(txSeq);
    if (null == txResult) {
        return null;
    }
    return new LedgerTransactionData(txRequest, txResult);
}
Also used : TransactionResult(com.jd.blockchain.ledger.TransactionResult) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Example 2 with TransactionResult

use of com.jd.blockchain.ledger.TransactionResult in project jdchain-core by blockchain-jd-com.

the class LedgerEditorTest method testGennesisBlockCreation.

/**
 * 测试创建账本;
 */
@Test
public void testGennesisBlockCreation() {
    LedgerEditor ldgEdt = createLedgerInitEditor();
    LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
    BlockchainKeypair userKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey());
    userAccount.setProperty("Name", "孙悟空", -1);
    userAccount.setProperty("Age", "10000", -1);
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    TransactionRequest genesisTxReq = genisisTxCtx.getTransactionRequest();
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    LedgerBlock block = ldgEdt.prepare();
    assertEquals(0, block.getHeight());
    assertNotNull(block.getHash());
    assertNull(block.getLedgerHash());
    assertNull(block.getPreviousHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) UserAccount(com.jd.blockchain.ledger.core.UserAccount) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) Test(org.junit.Test)

Example 3 with TransactionResult

use of com.jd.blockchain.ledger.TransactionResult in project jdchain-core by blockchain-jd-com.

the class LedgerEditorTest method testWriteDataAccoutKvOp.

@SuppressWarnings("unused")
@Test
public void testWriteDataAccoutKvOp() {
    MemoryKVStorage storage = new MemoryKVStorage();
    LedgerEditor ldgEdt = createLedgerInitEditor(storage);
    LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
    BlockchainKeypair dataKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    DataAccount dataAccount = ldgDS.getDataAccountSet().register(dataKP.getAddress(), dataKP.getPubKey(), null);
    dataAccount.getDataset().setValue("A", TypedValue.fromText("abc"), -1);
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    LedgerBlock block = ldgEdt.prepare();
    // 提交数据,写入存储;
    ldgEdt.commit();
    // 预期这是第1个区块;
    assertNotNull(block);
    assertNotNull(block.getHash());
    assertEquals(0, block.getHeight());
    // 验证数据读写的一致性;
    BytesValue bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    String textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    // 验证重新加载的正确性;
    LedgerManager manager = new LedgerManager();
    HashDigest ledgerHash = block.getHash();
    LedgerRepository repo = manager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    dataAccount = repo.getDataAccountSet().getAccount(dataKP.getAddress());
    assertNotNull(dataAccount);
    bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    LedgerTransaction tx_init = repo.getTransactionSet().getTransaction(tx.getTransactionHash());
    assertNotNull(tx_init);
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccount(com.jd.blockchain.ledger.core.DataAccount) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 4 with TransactionResult

use of com.jd.blockchain.ledger.TransactionResult in project jdchain-core by blockchain-jd-com.

the class BlockFullRollBackTest method initLedger.

private HashDigest initLedger(MemoryKVStorage storage, BlockchainKeypair... partiKeys) {
    // 创建初始化配置;
    LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(partiKeys);
    // 创建账本;
    LedgerEditor ldgEdt = LedgerTransactionalEditor.createEditor(initSetting, LEDGER_KEY_PREFIX, storage, storage, LedgerDataStructure.MERKLE_TREE);
    TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest_SHA256(partiKeys);
    LedgerTransactionContext genisisTxCtx = ldgEdt.newTransaction(genesisTxReq);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    for (int i = 0; i < partiKeys.length; i++) {
        UserAccount userAccount = ldgDS.getUserAccountSet().register(partiKeys[i].getAddress(), partiKeys[i].getPubKey());
        userAccount.setProperty("Name", "参与方-" + i, -1);
        userAccount.setProperty("Share", "" + (10 + i), -1);
    }
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    LedgerBlock block = ldgEdt.prepare();
    assertEquals(0, block.getHeight());
    assertNotNull(block.getHash());
    assertNull(block.getPreviousHash());
    // 创世区块的账本哈希为 null;
    assertNull(block.getLedgerHash());
    assertNotNull(block.getHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
    HashDigest ledgerHash = block.getHash();
    return ledgerHash;
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerInitSetting(com.jd.blockchain.ledger.LedgerInitSetting) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) UserAccount(com.jd.blockchain.ledger.core.UserAccount) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Example 5 with TransactionResult

use of com.jd.blockchain.ledger.TransactionResult 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)

Aggregations

TransactionResult (com.jd.blockchain.ledger.TransactionResult)12 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)9 HashDigest (com.jd.blockchain.crypto.HashDigest)7 Test (org.junit.Test)6 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)5 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)4 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)4 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)4 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)4 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)3 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)3 TransactionState (com.jd.blockchain.ledger.TransactionState)3 LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)3 TransactionSetEditor (com.jd.blockchain.ledger.core.TransactionSetEditor)3 AsymmetricKeypair (com.jd.blockchain.crypto.AsymmetricKeypair)2 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)2 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)2 UserAccount (com.jd.blockchain.ledger.core.UserAccount)2 BytesValue (com.jd.blockchain.ledger.BytesValue)1 LedgerInitSetting (com.jd.blockchain.ledger.LedgerInitSetting)1