use of com.jd.blockchain.ledger.core.TransactionSetEditor 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);
}
use of com.jd.blockchain.ledger.core.TransactionSetEditor 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.core.TransactionSetEditor in project jdchain-core by blockchain-jd-com.
the class TransactionSetTest method testTransactionSequence.
@Test
public void testTransactionSequence() {
CryptoSetting cryptoSetting = LedgerTestUtils.createDefaultCryptoSetting();
MemoryKVStorage testStorage = new MemoryKVStorage();
// Create a new TransactionSet;
TransactionSetEditor txset = new TransactionSetEditor(cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE);
HashDigest ledgerHash = LedgerTestUtils.generateRandomHash();
long blockHeight = 8922L;
// 生成指定数量的测试数据:交易请求和交易执行结果;
int txCount0 = 10;
TransactionRequest[] txRequests_0 = new TransactionRequest[txCount0];
TransactionResult[] txResults_0 = new TransactionResult[txCount0];
buildRequestAndResult(ledgerHash, blockHeight, cryptoSetting, txCount0, txRequests_0, txResults_0);
// add tx to trasaction set;
for (int i = 0; i < txCount0; i++) {
txset.addTransaction(txRequests_0[i], txResults_0[i]);
}
txset.commit();
// 验证交易集合中记录的交易顺序;
assertEquals(txCount0, txset.getTotalCount());
TransactionResult[] actualTxResults = txset.getTransactionResults(0, txCount0);
assertEquals(txCount0, actualTxResults.length);
for (int i = 0; i < txCount0; i++) {
assertTransactionEquals(txResults_0[i], actualTxResults[i]);
}
// 重新加载交易集合;
HashDigest txsetRootHash = txset.getRootHash();
TransactionSetEditor reloadTxset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, true);
// 验证重新加载之后的交易集合中记录的交易顺序;
assertEquals(txCount0, reloadTxset.getTotalCount());
TransactionResult[] actualTxResults_reload = reloadTxset.getTransactionResults(0, txCount0);
assertEquals(txCount0, actualTxResults_reload.length);
for (int i = 0; i < txCount0; i++) {
assertTransactionEquals(txResults_0[i], actualTxResults_reload[i]);
}
// 生成指定数量的测试数据:交易请求和交易执行结果;
int txCount1 = new Random().nextInt(200) + 1;
TransactionRequest[] txRequests_1 = new TransactionRequest[txCount1];
TransactionResult[] txResults_1 = new TransactionResult[txCount1];
buildRequestAndResult(ledgerHash, blockHeight, cryptoSetting, txCount1, txRequests_1, txResults_1);
// add tx to trasaction set;
TransactionSetEditor newTxset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, false);
for (int i = 0; i < txCount1; i++) {
newTxset.addTransaction(txRequests_1[i], txResults_1[i]);
}
newTxset.commit();
// 验证交易集合中记录的交易顺序;
int totalCount = txCount0 + txCount1;
assertEquals(totalCount, newTxset.getTotalCount());
TransactionResult[] actualTxResults_reload_2 = newTxset.getTransactionResults(0, totalCount);
assertEquals(totalCount, actualTxResults_reload_2.length);
TransactionRequest[] txRequests = ArrayUtils.concat(txRequests_0, txRequests_1, TransactionRequest.class);
TransactionResult[] txResults = ArrayUtils.concat(txResults_0, txResults_1, TransactionResult.class);
for (int i = 0; i < totalCount; i++) {
assertTransactionEquals(txResults[i], actualTxResults_reload_2[i]);
}
}
use of com.jd.blockchain.ledger.core.TransactionSetEditor in project jdchain-core by blockchain-jd-com.
the class TransactionSetTest method testSpecialCase_1.
/**
* 根据实际运行中一个随机出现的错误中提取到的数据来建立的测试用例,可以更简化地验证正确性;
*
* <p>
*
* 注:重构了 {@link LedgerTransaction} 和 {@link TransactionContent}
* 等交易结构相关的类型之后,此用例已经失效; by huanghaiquan on 2020-09-16;
*/
// @Test
public void testSpecialCase_1() {
CryptoSetting defCryptoSetting = LedgerTestUtils.createDefaultCryptoSetting();
MemoryKVStorage testStorage = new MemoryKVStorage();
BufferedKVStorage bufferStorage = new BufferedKVStorage(null, testStorage, testStorage, false);
// Create a new TransactionSet, it's empty;
TransactionSetEditor txset = new TransactionSetEditor(defCryptoSetting, keyPrefix, bufferStorage, bufferStorage, LedgerDataStructure.MERKLE_TREE);
assertTrue(txset.isUpdated());
assertFalse(txset.isReadonly());
assertNull(txset.getRootHash());
HashDigest ledgerHash = Crypto.resolveAsHashDigest(Base58Utils.decode("j5iF5xJ7KN4kjRrhD3EUKVSPmHz2bExxp3h9avqxcnnzch"));
assertEquals("j5iF5xJ7KN4kjRrhD3EUKVSPmHz2bExxp3h9avqxcnnzch", ledgerHash.toBase58());
BlockchainKeypair parti0 = LedgerTestUtils.createKeyPair("7VeRLBwqTAz8oRazEazeaEfqei46sk2FzvBgyHMUBJvrUEGT", "7VeRUm27GbrsX9HbQSZguChLp24HZYub6s5FJ7FjBht8BmbA");
BlockchainKeypair userKeypair1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair1, ledgerHash, 1580315317127L, parti0, parti0);
// TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg(userKeypair1, ledgerHash, 202001202020L,
// parti0, parti0);
System.out.printf("\r\n ===||=== transactionRequest1.getTransactionHash()=[%s]\r\n", transactionRequest1.getTransactionHash().toBase58());
// assertEquals("j5sXmpcomtM2QMUNWeQWsF8bNFFnyeXoCjVAekEeLSscgY", transactionRequest1.getTransactionHash().toBase58());
assertEquals("j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG", transactionRequest1.getTransactionHash().toBase58());
TransactionStagedSnapshot txSnapshot = new TransactionStagedSnapshot();
txSnapshot.setAdminAccountHash(Crypto.resolveAsHashDigest(Base58Utils.decode("j5taeK6cpmJGcn8QbEYCqadna6s7NDSheDTK6NJdU4mFhh")));
txSnapshot.setUserAccountSetHash(Crypto.resolveAsHashDigest(Base58Utils.decode("j5oQDSob92mCoGSHtrXa9soqgAtMyjwfRMt2kj7igXXJrP")));
TransactionResult tx = new TransactionResultData(transactionRequest1.getTransactionHash(), 1, TransactionState.SUCCESS, txSnapshot);
txset.addTransaction(transactionRequest1, tx);
LedgerTransaction tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
assertNotNull(tx_query);
txset.commit();
bufferStorage.commit();
tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
TransactionState tx_state = txset.getState(transactionRequest1.getTransactionHash());
assertNotNull(tx_query);
assertEquals(0, tx_state.CODE);
HashDigest txsetRootHash = txset.getRootHash();
txset = new TransactionSetEditor(-1, txsetRootHash, defCryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, false);
tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
tx_state = txset.getState(transactionRequest1.getTransactionHash());
assertNotNull(tx_query);
assertEquals(0, tx_state.CODE);
}
Aggregations