Search in sources :

Example 11 with BlockchainKeypair

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

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

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

the class ContractDeployMojo method execute.

@Override
public void execute() throws MojoFailureException {
    Properties prop = new Properties();
    InputStream input = null;
    try {
        input = new FileInputStream(config);
        prop.load(input);
    } catch (IOException ex) {
        logger.error(ex.getMessage());
        throw new MojoFailureException("io error");
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
    }
    int port;
    try {
        port = Integer.parseInt(prop.getProperty("port"));
    } catch (NumberFormatException e) {
        logger.error(e.getMessage());
        throw new MojoFailureException("invalid port");
    }
    String host = prop.getProperty("host");
    String ledger = prop.getProperty("ledger");
    String pubKey = prop.getProperty("pubKey");
    String prvKey = prop.getProperty("prvKey");
    String password = prop.getProperty("password");
    String contractPath = prop.getProperty("contractPath");
    if (StringUtils.isEmpty(host)) {
        logger.info("host不能为空");
        return;
    }
    if (StringUtils.isEmpty(ledger)) {
        logger.info("ledger不能为空.");
        return;
    }
    if (StringUtils.isEmpty(pubKey)) {
        logger.info("pubKey不能为空.");
        return;
    }
    if (StringUtils.isEmpty(prvKey)) {
        logger.info("prvKey不能为空.");
        return;
    }
    if (StringUtils.isEmpty(contractPath)) {
        logger.info("contractPath不能为空.");
        return;
    }
    File contract = new File(contractPath);
    if (!contract.isFile()) {
        logger.info("文件" + contractPath + "不存在");
        return;
    }
    byte[] contractBytes = FileUtils.readBytes(contractPath);
    PrivKey prv = KeyGenUtils.decodePrivKeyWithRawPassword(prvKey, password);
    PubKey pub = KeyGenUtils.decodePubKey(pubKey);
    BlockchainKeypair blockchainKeyPair = new BlockchainKeypair(pub, prv);
    HashDigest ledgerHash = new HashDigest(Base58Utils.decode(ledger));
    StringBuffer sb = new StringBuffer();
    sb.append("host:" + host).append(",port:" + port).append(",ledgerHash:" + ledgerHash.toBase58()).append(",pubKey:" + pubKey).append(",prvKey:" + prv).append(",contractPath:" + contractPath);
    logger.info(sb.toString());
    ContractDeployExeUtil.instance.deploy(host, port, ledgerHash, blockchainKeyPair, contractBytes);
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) PrivKey(com.jd.blockchain.crypto.PrivKey) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) HashDigest(com.jd.blockchain.crypto.HashDigest) File(java.io.File)

Example 14 with BlockchainKeypair

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

the class TransactionBatchProcessorTest method testSingleTxProcess.

@Test
public void testSingleTxProcess() {
    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);
    // 验证交易总数;创始区块的交易数预期为 1 ——只有一笔账本初始化的交易;
    long totalCount = ledgerRepo.getTransactionSet().getTotalCount();
    assertEquals(1, totalCount);
    // 验证参与方账户的存在;
    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 userKeypair = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest = LedgerTestUtils.createTxRequest_UserReg(userKeypair, ledgerHash, parti0, parti0);
    TransactionResponse txResp = txbatchProcessor.schedule(transactionRequest);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    // 验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(newBlock.getHash(), latestBlock.getHash());
    assertEquals(1, newBlock.getHeight());
    // 再次验证交易数;在新增一笔交易之后,交易数预期为 2 ;
    totalCount = ledgerRepo.getTransactionSet().getTotalCount();
    assertEquals(2, totalCount);
    assertEquals(TransactionState.SUCCESS, txResp.getExecutionState());
}
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 15 with BlockchainKeypair

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

the class TransactionBatchProcessorTest method testTxRollbackByVersionsConflict.

@Test
public void testTxRollbackByVersionsConflict() {
    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 dataAccountKeypair = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_DataAccountReg_SHA256(dataAccountKeypair, ledgerHash, parti0, parti0);
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    DataAccount dataAccount = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress());
    assertNotNull(dataAccount);
    // 正确写入 KV 数据;
    TransactionRequest txreq1 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-1", -1, ledgerHash, parti0, parti0);
    TransactionRequest txreq2 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K2", "V-2-1", -1, ledgerHash, parti0, parti0);
    TransactionRequest txreq3 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-1", -1, ledgerHash, parti0, parti0);
    // 连续写 K1,K1的版本将变为1;
    TransactionRequest txreq4 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-2", 0, ledgerHash, parti0, parti0);
    newBlockEditor = ledgerRepo.createNextBlock();
    previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    txbatchProcessor.schedule(txreq1);
    txbatchProcessor.schedule(txreq2);
    txbatchProcessor.schedule(txreq3);
    txbatchProcessor.schedule(txreq4);
    newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    BytesValue v1_0 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 0);
    BytesValue v1_1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 1);
    BytesValue v2 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K2", 0);
    BytesValue v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3", 0);
    assertNotNull(v1_0);
    assertNotNull(v1_1);
    assertNotNull(v2);
    assertNotNull(v3);
    assertEquals("V-1-1", v1_0.getBytes().toUTF8String());
    assertEquals("V-1-2", v1_1.getBytes().toUTF8String());
    assertEquals("V-2-1", v2.getBytes().toUTF8String());
    assertEquals("V-3-1", v3.getBytes().toUTF8String());
    // 提交多笔数据写入的交易,包含存在数据版本冲突的交易,验证交易是否正确回滚;
    // 先写一笔正确的交易; k3 的版本将变为 1 ;
    TransactionRequest txreq5 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-2", 0, ledgerHash, parti0, parti0);
    // 指定冲突的版本号,正确的应该是版本1;
    TransactionRequest txreq6 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-3", 0, ledgerHash, parti0, parti0);
    newBlockEditor = ledgerRepo.createNextBlock();
    previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    txbatchProcessor.schedule(txreq5);
    // 预期会产生版本冲突异常; DataVersionConflictionException;
    DataVersionConflictException versionConflictionException = null;
    try {
        txbatchProcessor.schedule(txreq6);
    } catch (DataVersionConflictException e) {
        versionConflictionException = e;
    }
    // assertNotNull(versionConflictionException);
    newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    BytesValue v1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1");
    v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3");
    // k1 的版本仍然为1,没有更新;
    long k1_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K1");
    assertEquals(1, k1_version);
    long k3_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K3");
    assertEquals(1, k3_version);
    assertNotNull(v1);
    assertNotNull(v3);
    assertEquals("V-1-2", v1.getBytes().toUTF8String());
    assertEquals("V-3-2", v3.getBytes().toUTF8String());
// // 验证正确性;
// ledgerManager = new LedgerManager();
// ledgerRepo = ledgerManager.register(ledgerHash, STORAGE);
// 
// LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
// assertEquals(newBlock.getHash(), latestBlock.getHash());
// assertEquals(1, newBlock.getHeight());
// 
// LedgerTransaction tx1 = ledgerRepo.getTransactionSet()
// .get(transactionRequest1.getTransactionHash());
// 
// assertNotNull(tx1);
// assertEquals(TransactionState.SUCCESS, tx1.getExecutionState());
}
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) DataVersionConflictException(com.jd.blockchain.ledger.DataVersionConflictException) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) DataAccount(com.jd.blockchain.ledger.core.DataAccount) 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)

Aggregations

BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)23 Test (org.junit.Test)16 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)14 HashDigest (com.jd.blockchain.crypto.HashDigest)11 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)9 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)8 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)8 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)7 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)7 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)6 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)5 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)5 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)5 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)5 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)5 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)5 UserAccount (com.jd.blockchain.ledger.core.UserAccount)5 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)4 TransactionResult (com.jd.blockchain.ledger.TransactionResult)4 NetworkAddress (utils.net.NetworkAddress)4