use of com.jd.blockchain.ledger.core.LedgerEditor in project jdchain-core by blockchain-jd-com.
the class LedgerEditorTest method testGennesisBlockCreation.
/**
* 测试创建账本;
*/
@Test
public void testGennesisBlockCreation() {
LedgerEditor ldgEdt = createLedgerInitEditor();
LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
BlockchainKeypair userKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey());
userAccount.setProperty("Name", "孙悟空", -1);
userAccount.setProperty("Age", "10000", -1);
TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
TransactionRequest genesisTxReq = genisisTxCtx.getTransactionRequest();
assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
assertEquals(0, tx.getBlockHeight());
LedgerBlock block = ldgEdt.prepare();
assertEquals(0, block.getHeight());
assertNotNull(block.getHash());
assertNull(block.getLedgerHash());
assertNull(block.getPreviousHash());
// 提交数据,写入存储;
ldgEdt.commit();
}
use of com.jd.blockchain.ledger.core.LedgerEditor 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.LedgerEditor 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.LedgerEditor in project jdchain-core by blockchain-jd-com.
the class ContractInvokingTest method initLedger.
private HashDigest initLedger(MemoryKVStorage storage, BlockchainKeypair... partiKeys) {
// 创建初始化配置;
LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(partiKeys);
// 创建账本;
LedgerEditor ldgEdt = LedgerTransactionalEditor.createEditor(initSetting, LEDGER_KEY_PREFIX, storage, storage, LedgerDataStructure.MERKLE_TREE);
TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest_SHA256(partiKeys);
LedgerTransactionContext genisisTxCtx = ldgEdt.newTransaction(genesisTxReq);
LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
for (int i = 0; i < partiKeys.length; i++) {
UserAccount userAccount = ldgDS.getUserAccountSet().register(partiKeys[i].getAddress(), partiKeys[i].getPubKey());
userAccount.setProperty("Name", "参与方-" + i, -1);
userAccount.setProperty("Share", "" + (10 + i), -1);
}
TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
assertEquals(0, tx.getBlockHeight());
LedgerBlock block = ldgEdt.prepare();
assertEquals(0, block.getHeight());
assertNotNull(block.getHash());
assertNull(block.getLedgerHash());
assertNull(block.getPreviousHash());
// 提交数据,写入存储;
ldgEdt.commit();
HashDigest ledgerHash = block.getHash();
return ledgerHash;
}
use of com.jd.blockchain.ledger.core.LedgerEditor in project jdchain-core by blockchain-jd-com.
the class ContractInvokingTest method deploy.
private void deploy(LedgerRepository ledgerRepo, LedgerManager ledgerManager, DefaultOperationHandleRegisteration opReg, HashDigest ledgerHash, BlockchainKeypair contractKey) {
// 创建新区块的交易处理器;
LedgerBlock preBlock = ledgerRepo.getLatestBlock();
LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(preBlock);
// 加载合约
LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
LedgerSecurityManager securityManager = getSecurityManager();
TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
// 构建基于接口调用合约的交易请求,用于测试合约调用;
CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
txBuilder.contracts().deploy(contractKey.getIdentity(), chainCode());
TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
txReqBuilder.signAsEndpoint(parti0);
txReqBuilder.signAsNode(parti0);
TransactionRequest txReq = txReqBuilder.buildRequest();
TransactionResponse resp = txbatchProcessor.schedule(txReq);
OperationResult[] opResults = resp.getOperationResults();
assertNull(opResults);
// 提交区块;
TransactionBatchResultHandle txResultHandle = txbatchProcessor.prepare();
txResultHandle.commit();
}
Aggregations