Search in sources :

Example 1 with TxBuilder

use of com.jd.blockchain.transaction.TxBuilder 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());
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) TxBuilder(com.jd.blockchain.transaction.TxBuilder) Matchers.anyString(org.mockito.Matchers.anyString) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TxTestContract(test.com.jd.blockchain.ledger.TxTestContract) Bytes(utils.Bytes) HashDigest(com.jd.blockchain.crypto.HashDigest) TxTestContractImpl(test.com.jd.blockchain.ledger.TxTestContractImpl) BooleanValueHolder(com.jd.blockchain.transaction.BooleanValueHolder) TransactionBatchResultHandle(com.jd.blockchain.service.TransactionBatchResultHandle)

Example 2 with TxBuilder

use of com.jd.blockchain.transaction.TxBuilder 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();
}
Also used : LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) TxBuilder(com.jd.blockchain.transaction.TxBuilder) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionBatchResultHandle(com.jd.blockchain.service.TransactionBatchResultHandle)

Example 3 with TxBuilder

use of com.jd.blockchain.transaction.TxBuilder in project jdchain-core by blockchain-jd-com.

the class ContractInvokingTest method registerDataAccount.

private void registerDataAccount(LedgerRepository ledgerRepo, LedgerManager ledgerManager, DefaultOperationHandleRegisteration opReg, HashDigest ledgerHash, BlockchainKeypair kpDataAccount) {
    LedgerBlock preBlock = ledgerRepo.getLatestBlock();
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(preBlock);
    // 加载合约
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(getSecurityManager(), newBlockEditor, ledgerRepo, opReg);
    // 注册数据账户;
    CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
    TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    txBuilder.dataAccounts().register(kpDataAccount.getIdentity());
    TransactionRequestBuilder txReqBuilder1 = txBuilder.prepareRequest();
    txReqBuilder1.signAsEndpoint(parti0);
    txReqBuilder1.signAsNode(parti0);
    TransactionRequest txReq = txReqBuilder1.buildRequest();
    TransactionResponse resp = txbatchProcessor.schedule(txReq);
    TransactionBatchResultHandle txResultHandle = txbatchProcessor.prepare();
    txResultHandle.commit();
    assertNotNull(resp.getBlockHash());
    assertEquals(TransactionState.SUCCESS, resp.getExecutionState());
    assertEquals(preBlock.getHeight() + 1, resp.getBlockHeight());
}
Also used : TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) TransactionBatchResultHandle(com.jd.blockchain.service.TransactionBatchResultHandle) TxBuilder(com.jd.blockchain.transaction.TxBuilder) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet)

Example 4 with TxBuilder

use of com.jd.blockchain.transaction.TxBuilder 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);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) TxBuilder(com.jd.blockchain.transaction.TxBuilder) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccountSetEditor(com.jd.blockchain.ledger.core.DataAccountSetEditor) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet) ContractAccountSet(com.jd.blockchain.ledger.core.ContractAccountSet) UserAccount(com.jd.blockchain.ledger.core.UserAccount) UserAccountSet(com.jd.blockchain.ledger.core.UserAccountSet) Test(org.junit.Test)

Example 5 with TxBuilder

use of com.jd.blockchain.transaction.TxBuilder in project jdchain-core by blockchain-jd-com.

the class ManagementController method prepareActiveTx.

// 在指定的账本上准备一笔激活参与方状态及系统配置参数的操作
private TransactionRequest prepareActiveTx(HashDigest ledgerHash, ParticipantNode node, NetworkAddress addConsensusNodeAddress, Properties customProperties) {
    int activeID = node.getId();
    // organize system config properties
    Property[] properties = ParticipantContext.context().participantService().createActiveProperties(addConsensusNodeAddress, node.getPubKey(), activeID, customProperties);
    TxBuilder txbuilder = new TxBuilder(ledgerHash, ledgerCryptoSettings.get(ledgerHash).getHashAlgorithm());
    // This transaction contains participant state update and settings update two
    // ops
    txbuilder.states().update(new BlockchainIdentityData(node.getPubKey()), ParticipantNodeState.CONSENSUS);
    txbuilder.consensus().update(properties);
    TransactionRequestBuilder reqBuilder = txbuilder.prepareRequest();
    reqBuilder.signAsEndpoint(new AsymmetricKeypair(ledgerKeypairs.get(ledgerHash).getPubKey(), ledgerKeypairs.get(ledgerHash).getPrivKey()));
    return reqBuilder.buildRequest();
}
Also used : TxBuilder(com.jd.blockchain.transaction.TxBuilder) Property(utils.Property) ServiceEndpoint(com.jd.httpservice.agent.ServiceEndpoint)

Aggregations

TxBuilder (com.jd.blockchain.transaction.TxBuilder)18 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)6 HashDigest (com.jd.blockchain.crypto.HashDigest)5 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)5 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)5 TransactionBatchResultHandle (com.jd.blockchain.service.TransactionBatchResultHandle)5 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)4 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)4 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)3 ServiceEndpoint (com.jd.httpservice.agent.ServiceEndpoint)3 Test (org.junit.Test)3 Matchers.anyString (org.mockito.Matchers.anyString)3 TxTestContract (test.com.jd.blockchain.ledger.TxTestContract)3 Bytes (utils.Bytes)3 Property (utils.Property)3 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)2 Ignore (org.junit.Ignore)2 TxTestContractImpl (test.com.jd.blockchain.ledger.TxTestContractImpl)2 BlockchainIdentityData (com.jd.blockchain.ledger.BlockchainIdentityData)1 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1