Search in sources :

Example 6 with TransactionRequest

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

the class BftsmartNodeServer method createAppResponse.

// Block full rollback responses, generated in pre compute phase, due to tx
// exception
private byte[] createAppResponse(byte[] command, TransactionState transactionState) {
    TransactionRequest txRequest = BinaryProtocol.decode(command);
    TxResponseMessage resp = new TxResponseMessage(txRequest.getTransactionHash());
    resp.setExecutionState(transactionState);
    return BinaryProtocol.encode(resp, TransactionResponse.class);
}
Also used : TxResponseMessage(com.jd.blockchain.transaction.TxResponseMessage) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Example 7 with TransactionRequest

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

the class TransactionBatchProcessorTest method testMultiTxsProcess.

@Test
public void testMultiTxsProcess() {
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    // 验证参与方账户的存在;
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    UserAccount user0 = previousBlockDataset.getUserAccountSet().getAccount(parti0.getAddress());
    assertNotNull(user0);
    boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress());
    assertTrue(partiRegistered);
    // 生成新区块;
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    // 注册新用户;
    BlockchainKeypair userKeypair1 = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg(userKeypair1, ledgerHash, parti0, parti0);
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    BlockchainKeypair userKeypair2 = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest2 = LedgerTestUtils.createTxRequest_UserReg(userKeypair2, ledgerHash, parti0, parti0);
    TransactionResponse txResp2 = txbatchProcessor.schedule(transactionRequest2);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    assertEquals(TransactionState.SUCCESS, txResp2.getExecutionState());
    // 验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(newBlock.getHash(), latestBlock.getHash());
    assertEquals(1, newBlock.getHeight());
    LedgerDataSet ledgerDS = ledgerRepo.getLedgerDataSet(latestBlock);
    boolean existUser1 = ledgerDS.getUserAccountSet().contains(userKeypair1.getAddress());
    boolean existUser2 = ledgerDS.getUserAccountSet().contains(userKeypair2.getAddress());
    assertTrue(existUser1);
    assertTrue(existUser2);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) UserAccount(com.jd.blockchain.ledger.core.UserAccount) OperationHandleRegisteration(com.jd.blockchain.ledger.core.OperationHandleRegisteration) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) Test(org.junit.Test)

Example 8 with TransactionRequest

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

the class TransactionBatchProcessorTest method testTxRollback.

@Test
public void testTxRollback() {
    System.out.println("------------ keys -----------");
    System.out.printf("Parti0 -- PUB:[%s]; PRIV:[%s]\r\n", parti0.getPubKey().toBase58(), parti0.getPrivKey().toBase58());
    System.out.printf("Parti1 -- PUB:[%s]; PRIV:[%s]\r\n", parti1.getPubKey().toBase58(), parti1.getPrivKey().toBase58());
    System.out.printf("Parti2 -- PUB:[%s]; PRIV:[%s]\r\n", parti2.getPubKey().toBase58(), parti2.getPrivKey().toBase58());
    System.out.printf("Parti3 -- PUB:[%s]; PRIV:[%s]\r\n", parti3.getPubKey().toBase58(), parti3.getPrivKey().toBase58());
    System.out.println("------------ end-keys -----------");
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    System.out.printf("\r\n------------ LEDGER [%s] -----------\r\n", ledgerHash.toBase58());
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    CryptoSetting cryptoSetting = ledgerRepo.getAdminSettings().getSettings().getCryptoSetting();
    // 验证参与方账户的存在;
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    UserAccount user0 = previousBlockDataset.getUserAccountSet().getAccount(parti0.getAddress());
    assertNotNull(user0);
    boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress());
    assertTrue(partiRegistered);
    // 生成新区块;
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    // 注册新用户;
    // BlockchainKeypair userKeypair1 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair1, ledgerHash, 1580315317127L, parti0, parti0);
    // 错误参数:ts=1580315317127; txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
    System.out.printf("\r\n ===||=== transactionRequest1.getTransactionHash()=[%s]\r\n", transactionRequest1.getTransactionHash().toBase58());
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    // BlockchainKeypair userKeypair2 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair2 = LedgerTestUtils.createKeyPair("7VeRKSnDFveTfLLMsLZDmmhGmgf7i142XHgBFjnrKuS95tY3", "7VeRTiJ2TpQD9aBi29ajnqdntgoVBANmC3oCbHThKb5tzfTJ");
    TransactionRequest transactionRequest2 = LedgerTestUtils.createTxRequest_MultiOPs_WithNotExistedDataAccount_SHA256(userKeypair2, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest2.getTransactionHash()=[%s]\r\n", transactionRequest2.getTransactionHash().toBase58());
    TransactionResponse txResp2 = txbatchProcessor.schedule(transactionRequest2);
    // BlockchainKeypair userKeypair3 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair3 = LedgerTestUtils.createKeyPair("7VeRDoaSexqLWKkaZyrQwdwSuE9n5nszduMrYBfYRfEkREQV", "7VeRdFtTuLfrzCYJzQ6enQUkGTc83ATgjr8WbmfjBQuTFpHt");
    TransactionRequest transactionRequest3 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair3, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest3.getTransactionHash()=[%s]\r\n", transactionRequest3.getTransactionHash().toBase58());
    TransactionResponse txResp3 = txbatchProcessor.schedule(transactionRequest3);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    // 在重新加载之前验证一次; 
    long blockHeight = newBlock.getHeight();
    assertEquals(1, blockHeight);
    HashDigest blockHash = newBlock.getHash();
    assertNotNull(blockHash);
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, txResp2.getExecutionState());
    assertEquals(TransactionState.SUCCESS, txResp3.getExecutionState());
    LedgerTransaction tx1 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest1.getTransactionHash());
    LedgerTransaction tx2 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest2.getTransactionHash());
    LedgerTransaction tx3 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx3);
    assertEquals(TransactionState.SUCCESS, tx3.getResult().getExecutionState());
    assertNotNull(tx2);
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, tx2.getResult().getExecutionState());
    assertNotNull(tx1);
    assertEquals(TransactionState.SUCCESS, tx1.getResult().getExecutionState());
    HashDigest txsetRootHash = ledgerRepo.getTransactionSet().getRootHash();
    // 单独加载交易集合;
    TransactionSetEditor txset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, "LDG://3A3dP4", STORAGE, STORAGE, LedgerDataStructure.MERKLE_TREE, false);
    tx1 = txset.getTransaction(transactionRequest1.getTransactionHash());
    // tx2 = txset.get(transactionRequest2.getTransactionHash());
    tx3 = txset.getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx3);
    // assertNotNull(tx2);
    assertNotNull(tx1);
    // 重新加载之后验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(blockHash, latestBlock.getHash());
    assertEquals(blockHeight, latestBlock.getHeight());
    assertEquals(txsetRootHash, ledgerRepo.getTransactionSet().getRootHash());
    tx1 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest1.getTransactionHash());
    tx2 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest2.getTransactionHash());
    tx3 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx1);
    assertEquals(TransactionState.SUCCESS, tx1.getResult().getExecutionState());
    assertNotNull(tx2);
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, tx2.getResult().getExecutionState());
    assertNotNull(tx3);
    assertEquals(TransactionState.SUCCESS, tx3.getResult().getExecutionState());
    LedgerDataSet ledgerDS = ledgerRepo.getLedgerDataSet(latestBlock);
    boolean existUser1 = ledgerDS.getUserAccountSet().contains(userKeypair1.getAddress());
    boolean existUser2 = ledgerDS.getUserAccountSet().contains(userKeypair2.getAddress());
    boolean existUser3 = ledgerDS.getUserAccountSet().contains(userKeypair3.getAddress());
    assertTrue(existUser1);
    assertFalse(existUser2);
    assertTrue(existUser3);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) TransactionSetEditor(com.jd.blockchain.ledger.core.TransactionSetEditor) UserAccount(com.jd.blockchain.ledger.core.UserAccount) OperationHandleRegisteration(com.jd.blockchain.ledger.core.OperationHandleRegisteration) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) Test(org.junit.Test)

Example 9 with TransactionRequest

use of com.jd.blockchain.ledger.TransactionRequest 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 10 with TransactionRequest

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

the class LedgerInitializer method prepareLedger.

/**
 * 初始化账本数据,返回创始区块;
 *
 * @param ledgerEditor
 * @return
 */
private LedgerBlock prepareLedger(LedgerEditor ledgerEditor, DigitalSignature... nodeSignatures) {
    // 初始化时,自动将参与方注册为账本的用户;
    HashDigest txHash = TxBuilder.computeTxContentHash(initSetting.getCryptoSetting().getHashAlgorithm(), this.initTxContent);
    TxRequestBuilder txReqBuilder = new TxRequestBuilder(txHash, this.initTxContent);
    txReqBuilder.addNodeSignature(nodeSignatures);
    TransactionRequest txRequest = txReqBuilder.buildRequest();
    TransactionBatchProcessor txProcessor = null;
    if (initSetting.getLedgerDataStructure().equals(LedgerDataStructure.MERKLE_TREE)) {
        txProcessor = new TransactionBatchProcessor(FULL_PERMISSION_SECURITY_MANAGER, ledgerEditor, EMPTY_LEDGER_MERKLE, DEFAULT_OP_HANDLE_REG);
    } else {
        txProcessor = new TransactionBatchProcessor(FULL_PERMISSION_SECURITY_MANAGER, ledgerEditor, EMPTY_LEDGER_KV, DEFAULT_OP_HANDLE_REG);
    }
    LedgerEditor.TIMESTAMP_HOLDER.set(initSetting.getCreatedTime());
    TransactionResponse response = txProcessor.schedule(txRequest);
    if (!response.isSuccess()) {
        throw new LedgerInitException("Transaction execution failed in genesis block!");
    }
    txResultsHandle = txProcessor.prepare();
    LedgerEditor.TIMESTAMP_HOLDER.remove();
    return txResultsHandle.getBlock();
}
Also used : TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) TxRequestBuilder(com.jd.blockchain.transaction.TxRequestBuilder) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Aggregations

TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)20 HashDigest (com.jd.blockchain.crypto.HashDigest)10 Test (org.junit.Test)10 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)9 TransactionResult (com.jd.blockchain.ledger.TransactionResult)9 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)9 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)8 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)8 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)6 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)6 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)6 UserAccount (com.jd.blockchain.ledger.core.UserAccount)6 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)5 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)5 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)5 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)5 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)5 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)4 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)4 TransactionSetEditor (com.jd.blockchain.ledger.core.TransactionSetEditor)4