use of com.jd.blockchain.ledger.LedgerTransaction in project jdchain-core by blockchain-jd-com.
the class LedgerEditorTest method testRollback.
@Test
public void testRollback() {
BlockchainKeypair parti0 = LedgerTestUtils.createKeyPair("7VeRLBwqTAz8oRazEazeaEfqei46sk2FzvBgyHMUBJvrUEGT", "7VeRUm27GbrsX9HbQSZguChLp24HZYub6s5FJ7FjBht8BmbA");
BlockchainKeypair parti1 = LedgerTestUtils.createKeyPair("7VeRNJasZp76ThmUkoAajJEduotS4JC6T9wzhz9TDPvjLCRk", "7VeRcBcPkTZ4hFwfcKRgFJWdDesHyysQWkKYC6xfPApbfvwQ");
BlockchainKeypair parti2 = LedgerTestUtils.createKeyPair("7VeR7uSd7sqxkMp73936MoK7eUSmGPVrsmrwdekiR9fmvdYN", "7VeRUkgMXRegHHWhezv4LdJV6oQuSXo6Ezp2sjC2M5NTUWkz");
BlockchainKeypair parti3 = LedgerTestUtils.createKeyPair("7VeR8X8fa9th42XSXvnuBLfR4v3dxjXq6jPfvF7nDPB2MTo1", "7VeRdreAev1E8ySsLWX7rRMArh5wHBTmZXKwNUuoVo7cBn6o");
BlockchainKeypair[] participants = new BlockchainKeypair[] { parti0, parti1, parti2, parti3 };
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 manager = new LedgerManager();
LedgerRepository repo = manager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
LedgerBlock block = repo.getBlock(ledgerHash);
assertNotNull(block);
assertNotNull(block.getHash());
assertEquals(0, block.getHeight());
// 创建交易连续交易,验证中间的交易回滚是否影响前后的交易;
BlockchainKeypair user1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
TransactionRequest req1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(user1, ledgerHash, 1580315317127L, parti0, parti0);
// 引发错误的参数:ts=1580315317127;
// txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
System.out.printf("\r\n ===||=== transactionRequest1.getTransactionContent().getHash()=[%s]\r\n", req1.getTransactionHash().toBase58());
BlockchainKeypair user2 = LedgerTestUtils.createKeyPair("7VeRKSnDFveTfLLMsLZDmmhGmgf7i142XHgBFjnrKuS95tY3", "7VeRTiJ2TpQD9aBi29ajnqdntgoVBANmC3oCbHThKb5tzfTJ");
TransactionRequest req2 = LedgerTestUtils.createTxRequest_MultiOPs_WithNotExistedDataAccount_SHA256(user2, ledgerHash, 202001202020L, parti0, parti0);
System.out.printf("\r\n ===||=== transactionRequest2.getTransactionContent().getHash()=[%s]\r\n", req2.getTransactionHash().toBase58());
BlockchainKeypair user3 = LedgerTestUtils.createKeyPair("7VeRDoaSexqLWKkaZyrQwdwSuE9n5nszduMrYBfYRfEkREQV", "7VeRdFtTuLfrzCYJzQ6enQUkGTc83ATgjr8WbmfjBQuTFpHt");
TransactionRequest req3 = LedgerTestUtils.createTxRequest_UserReg_SHA256(user3, ledgerHash, 202001202020L, parti0, parti0);
System.out.printf("\r\n ===||=== transactionRequest3.getTransactionContent().getHash()=[%s]\r\n", req3.getTransactionHash().toBase58());
System.out.println("\r\n--------------- Start new Block 1 --------------\r\n");
// 创建交易;
LedgerEditor editor = repo.createNextBlock();
System.out.println("\r\n--------------- Start new tx1 --------------\r\n");
LedgerTransactionContext txctx1 = editor.newTransaction(req1);
((UserAccountSetEditor) (txctx1.getDataset().getUserAccountSet())).register(user1.getAddress(), user1.getPubKey());
TransactionResult tx1 = txctx1.commit(TransactionState.SUCCESS);
HashDigest txHash1 = tx1.getTransactionHash();
System.out.println("\r\n--------------- Start new tx2 --------------\r\n");
LedgerTransactionContext txctx2 = editor.newTransaction(req2);
((UserAccountSetEditor) (txctx2.getDataset().getUserAccountSet())).register(user2.getAddress(), user2.getPubKey());
TransactionResult tx2 = txctx2.discardAndCommit(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST);
HashDigest txHash2 = tx2.getTransactionHash();
System.out.println("\r\n--------------- Start new tx3 --------------\r\n");
LedgerTransactionContext txctx3 = editor.newTransaction(req3);
((UserAccountSetEditor) (txctx3.getDataset().getUserAccountSet())).register(user3.getAddress(), user3.getPubKey());
TransactionResult tx3 = txctx3.commit(TransactionState.SUCCESS);
HashDigest txHash3 = tx3.getTransactionHash();
System.out.println("\r\n--------------- Start preparing new block 1 --------------\r\n");
LedgerBlock block1 = editor.prepare();
System.out.println("\r\n--------------- Start commiting new block 1 --------------\r\n");
editor.commit();
System.out.printf("\r\n--------------- End commiting new block 1 [Storage.Count=%s]--------------\r\n\r\n", STORAGE.getStorageCount());
assertEquals(1, block1.getHeight());
// 重新加载和验证;
manager = new LedgerManager();
repo = manager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
LedgerTransaction act_tx1 = repo.getTransactionSet().getTransaction(txHash1);
LedgerTransaction act_tx2 = repo.getTransactionSet().getTransaction(txHash2);
LedgerTransaction act_tx3 = repo.getTransactionSet().getTransaction(txHash3);
assertNotNull(act_tx3);
assertNotNull(act_tx2);
assertNotNull(act_tx1);
}
use of com.jd.blockchain.ledger.LedgerTransaction in project jdchain-core by blockchain-jd-com.
the class LedgerTransactionDataTest method testSerialize_LedgerTransaction.
@Test
public void testSerialize_LedgerTransaction() throws Exception {
byte[] serialBytes = BinaryProtocol.encode(ledgerTransactionData, LedgerTransaction.class);
LedgerTransaction resolvedData = BinaryProtocol.decode(serialBytes);
System.out.println("------Assert start ------");
Assert_TransactionResult(resolvedData.getResult());
Assert_TransactionRequest(resolvedData.getRequest());
}
use of com.jd.blockchain.ledger.LedgerTransaction 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);
}
use of com.jd.blockchain.ledger.LedgerTransaction in project jdchain-core by blockchain-jd-com.
the class LedgerQueryController method getAdditionalTransactions.
@RequestMapping(method = RequestMethod.GET, path = GET_TRANSACTIONS_IN_BLOCK_HEIGHT)
@Override
public LedgerTransaction[] getAdditionalTransactions(@PathVariable(name = "ledgerHash") HashDigest ledgerHash, @PathVariable(name = "blockHeight") long blockHeight, @RequestParam(name = "fromIndex", required = false, defaultValue = "0") int fromIndex, @RequestParam(name = "count", required = false, defaultValue = "-1") int count) {
LedgerQuery ledger = ledgerService.getLedger(ledgerHash);
LedgerBlock ledgerBlock = ledger.getBlock(blockHeight);
if (null == ledgerBlock) {
return null;
}
TransactionSet currTransactionSet = ledger.getTransactionSet(ledgerBlock);
TransactionSet lastTransactionSet = null;
int lastHeightTxTotalNums = 0;
if (blockHeight > 0) {
lastTransactionSet = ledger.getTransactionSet(ledger.getBlock(blockHeight - 1));
lastHeightTxTotalNums = (int) lastTransactionSet.getTotalCount();
}
int currentHeightTxTotalNums = (int) ledger.getTransactionSet(ledger.getBlock(blockHeight)).getTotalCount();
// 取当前高度的增量交易数,在增量交易里进行查找
int currentHeightTxNums = currentHeightTxTotalNums - lastHeightTxTotalNums;
QueryArgs queryArgs = QueryUtils.calFromIndexAndCount(fromIndex, count, currentHeightTxNums);
LedgerTransaction[] txs = currTransactionSet.getTransactions(lastHeightTxTotalNums + queryArgs.getFrom(), queryArgs.getCount());
return txsDecorator(txs);
}
use of com.jd.blockchain.ledger.LedgerTransaction in project jdchain-core by blockchain-jd-com.
the class LedgerQueryController method getTransactions.
@RequestMapping(method = RequestMethod.GET, path = GET_TRANSACTIONS_ON_BLOCK_HEIGHT)
@Override
public LedgerTransaction[] getTransactions(@PathVariable(name = "ledgerHash") HashDigest ledgerHash, @PathVariable(name = "blockHeight") long blockHeight, @RequestParam(name = "fromIndex", required = false, defaultValue = "0") int fromIndex, @RequestParam(name = "count", required = false, defaultValue = "-1") int count) {
LedgerQuery ledger = ledgerService.getLedger(ledgerHash);
LedgerBlock ledgerBlock = ledger.getBlock(blockHeight);
if (null == ledgerBlock) {
return null;
}
QueryArgs queryArgs = QueryUtils.calFromIndexAndCount(fromIndex, count, (int) ledger.getTransactionSet(ledgerBlock).getTotalCount());
LedgerTransaction[] txs = ledger.getTransactionSet(ledgerBlock).getTransactions(queryArgs.getFrom(), queryArgs.getCount());
return txsDecorator(txs);
// LedgerQuery ledger = ledgerService.getLedger(ledgerHash);
// LedgerBlock ledgerBlock = ledger.getBlock(blockHeight);
// TransactionQuery transactionSet = ledger.getTransactionSet(ledgerBlock);
// TransactionQuery origTransactionSet = null;
//
// int lastHeightTxTotalNums = 0;
//
// if (blockHeight > 0) {
// origTransactionSet = ledger.getTransactionSet(ledger.getBlock(blockHeight - 1));
// lastHeightTxTotalNums = (int) origTransactionSet.getTotalCount();
// }
//
// int currentHeightTxTotalNums = (int) ledger.getTransactionSet(ledger.getBlock(blockHeight)).getTotalCount();
// // 取当前高度的增量交易数,在增量交易里进行查找
// int currentHeightTxNums = currentHeightTxTotalNums - lastHeightTxTotalNums;
//
// QueryArgs queryArgs = QueryUtils.calFromIndexAndCount(fromIndex, count, currentHeightTxNums);
// LedgerTransaction[] txs = transactionSet.getBlockTxs(queryArgs.getFrom(), queryArgs.getCount(), origTransactionSet);
// return txsDecorator(txs);
}
Aggregations