use of com.jd.blockchain.ledger.core.LedgerRepository 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);
}
use of com.jd.blockchain.ledger.core.LedgerRepository in project jdchain-core by blockchain-jd-com.
the class ContractInvokingTest method testReadNewWritting.
// @Test
public void testReadNewWritting() {
// 初始化账本到指定的存储库;
HashDigest ledgerHash = initLedger(storage, parti0, parti1, parti2, parti3);
// 重新加载账本;
LedgerManager ledgerManager = new LedgerManager();
LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
// 创建合约处理器;
ContractInvokingHandle contractInvokingHandle = new ContractInvokingHandle();
// 创建和加载合约实例;
BlockchainKeypair contractKey = BlockchainKeyGenerator.getInstance().generate();
Bytes contractAddress = contractKey.getAddress();
TxTestContractImpl contractInstance = new TxTestContractImpl();
contractInvokingHandle.setup(contractAddress, TxTestContract.class, contractInstance);
// 注册合约处理器;
DefaultOperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
opReg.registerHandle(contractInvokingHandle);
// 发布指定地址合约
deploy(ledgerRepo, ledgerManager, opReg, ledgerHash, contractKey);
// 创建新区块的交易处理器;
LedgerBlock preBlock = ledgerRepo.getLatestBlock();
LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(preBlock);
// 加载合约
LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(getSecurityManager(), newBlockEditor, ledgerRepo, opReg);
String key = TxTestContractImpl.KEY;
String value = "VAL";
CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
BlockchainKeypair kpDataAccount = BlockchainKeyGenerator.getInstance().generate();
contractInstance.setDataAddress(kpDataAccount.getAddress());
txBuilder.dataAccounts().register(kpDataAccount.getIdentity());
TransactionRequestBuilder txReqBuilder1 = txBuilder.prepareRequest();
txReqBuilder1.signAsEndpoint(parti0);
txReqBuilder1.signAsNode(parti0);
TransactionRequest txReq1 = txReqBuilder1.buildRequest();
// 构建基于接口调用合约的交易请求,用于测试合约调用;
txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
TxTestContract contractProxy = txBuilder.contract(contractAddress, TxTestContract.class);
BooleanValueHolder readableHolder = decode(contractProxy.testReadable());
TransactionRequestBuilder txReqBuilder2 = txBuilder.prepareRequest();
txReqBuilder2.signAsEndpoint(parti0);
txReqBuilder2.signAsNode(parti0);
TransactionRequest txReq2 = txReqBuilder2.buildRequest();
TransactionResponse resp1 = txbatchProcessor.schedule(txReq1);
TransactionResponse resp2 = txbatchProcessor.schedule(txReq2);
// 提交区块;
TransactionBatchResultHandle txResultHandle = txbatchProcessor.prepare();
txResultHandle.commit();
BytesValue latestValue = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getValue(key, -1);
System.out.printf("latest value=[%s] %s \r\n", latestValue.getType(), latestValue.getBytes().toUTF8String());
boolean readable = readableHolder.get();
assertTrue(readable);
LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
assertEquals(preBlock.getHeight() + 1, latestBlock.getHeight());
assertEquals(resp1.getBlockHeight(), latestBlock.getHeight());
assertEquals(resp1.getBlockHash(), latestBlock.getHash());
}
use of com.jd.blockchain.ledger.core.LedgerRepository in project jdchain-core by blockchain-jd-com.
the class LedgerManagerTest method testLedgerInit.
@Test
public void testLedgerInit() {
// 创建账本初始化配置;
LedgerInitSetting initSetting = createLedgerInitSetting();
// 采用基于内存的 Storage;
MemoryKVStorage storage = new MemoryKVStorage();
// 新建账本;
LedgerEditor ldgEdt = LedgerInitializer.createLedgerEditor(initSetting, storage);
// 创建一个模拟的创世交易;
TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest_SHA256(participants);
// 记录交易,注册用户;
LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq);
LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) txCtx.getDataset();
BlockchainKeypair userKP = BlockchainKeyGenerator.getInstance().generate();
UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey());
userAccount.setProperty("Name", "孙悟空", -1);
userAccount.setProperty("Age", "10000", -1);
System.out.println("UserAddress=" + userAccount.getAddress());
// 提交交易结果;
TransactionResult tx = txCtx.commit(TransactionState.SUCCESS);
assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
assertEquals(0, tx.getBlockHeight());
// 生成区块;
LedgerBlock genesisBlock = ldgEdt.prepare();
HashDigest ledgerHash = genesisBlock.getHash();
assertEquals(0, genesisBlock.getHeight());
assertNotNull(genesisBlock.getHash());
assertNull(genesisBlock.getPreviousHash());
// 创世区块的账本hash 为null;创世区块本身的哈希就代表了账本的哈希;
assertNull(genesisBlock.getLedgerHash());
// 提交数据,写入存储;
ldgEdt.commit();
assertNull(genesisBlock.getLedgerHash());
assertNotNull(genesisBlock.getHash());
// 重新加载并校验结果;
LedgerManager reloadLedgerManager = new LedgerManager();
LedgerRepository reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
HashDigest genesisHash = reloadLedgerRepo.getBlockHash(0);
assertEquals(ledgerHash, genesisHash);
LedgerBlock latestBlock = reloadLedgerRepo.getLatestBlock();
assertEquals(0, latestBlock.getHeight());
assertEquals(ledgerHash, latestBlock.getHash());
// 创世区块的账本hash 为null;创世区块本身的哈希就代表了账本的哈希;
assertNull(latestBlock.getLedgerHash());
LedgerEditor editor1 = reloadLedgerRepo.createNextBlock();
CryptoSetting cryptoSetting = reloadLedgerRepo.getAdminInfo().getSettings().getCryptoSetting();
TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
BlockchainKeypair dataKey = BlockchainKeyGenerator.getInstance().generate();
txBuilder.dataAccounts().register(dataKey.getIdentity());
TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
DigitalSignature dgtsign = txReqBuilder.signAsEndpoint(userKP);
TransactionRequest txRequest = txReqBuilder.buildRequest();
LedgerTransactionContext txCtx1 = editor1.newTransaction(txRequest);
((DataAccountSetEditor) (txCtx1.getDataset().getDataAccountSet())).register(dataKey.getAddress(), dataKey.getPubKey(), null);
txCtx1.commit(TransactionState.SUCCESS);
LedgerBlock block1 = editor1.prepare();
editor1.commit();
assertEquals(1, block1.getHeight());
assertNotNull(block1.getHash());
assertEquals(genesisHash, block1.getPreviousHash());
assertEquals(ledgerHash, block1.getLedgerHash());
latestBlock = reloadLedgerRepo.getLatestBlock();
assertEquals(1, latestBlock.getHeight());
assertEquals(block1.getHash(), latestBlock.getHash());
showStorageKeys(storage);
reloadLedgerManager = new LedgerManager();
reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
latestBlock = reloadLedgerRepo.getLatestBlock();
assertEquals(1, latestBlock.getHeight());
assertEquals(block1.getHash(), latestBlock.getHash());
DataAccountSet dataAccountSet = reloadLedgerRepo.getDataAccountSet(latestBlock);
UserAccountSet userAccountSet = reloadLedgerRepo.getUserAccountSet(latestBlock);
ContractAccountSet contractAccountSet = reloadLedgerRepo.getContractAccountSet(latestBlock);
}
use of com.jd.blockchain.ledger.core.LedgerRepository 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);
}
use of com.jd.blockchain.ledger.core.LedgerRepository 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);
}
Aggregations