Search in sources :

Example 1 with TransactionRequestBuilder

use of com.jd.blockchain.ledger.TransactionRequestBuilder in project jdchain-core by blockchain-jd-com.

the class TransactionSetTest method buildTransactionRequest_RandomOperation.

/**
 * 创建交易请求;以随机生成的数据作为交易的操作参数;
 *
 * @param ledgerHash
 * @param defCryptoSetting
 * @return
 */
private TransactionRequest buildTransactionRequest_RandomOperation(HashDigest ledgerHash, CryptoSetting defCryptoSetting) {
    // Build transaction request;
    TxBuilder txBuilder = new TxBuilder(ledgerHash, defCryptoSetting.getHashAlgorithm());
    Operation[] operations = new Operation[5];
    // register user;
    BlockchainKeypair userKey = BlockchainKeyGenerator.getInstance().generate();
    operations[0] = txBuilder.users().register(userKey.getIdentity());
    // register data account;
    BlockchainKeypair dataKey = BlockchainKeyGenerator.getInstance().generate();
    operations[1] = txBuilder.dataAccounts().register(dataKey.getIdentity());
    // set data after registering data account immediately;
    operations[2] = txBuilder.dataAccount(dataKey.getAddress()).setText("A", "Value_A_0", -1).setText("B", "Value_B_0", -1).getOperation();
    // generate random bytes as the bytes of ChainCode to deploy as a smart
    // contract;
    byte[] chainCode = new byte[128];
    rand.nextBytes(chainCode);
    BlockchainKeypair contractKey = BlockchainKeyGenerator.getInstance().generate();
    operations[3] = txBuilder.contracts().deploy(contractKey.getIdentity(), chainCode);
    // invoke smart contract;
    operations[4] = txBuilder.contract(contractKey.getAddress()).invoke("test", BytesDataList.singleText("TestContractArgs"));
    // build transaction request;
    TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
    BlockchainKeypair sponsorKey = BlockchainKeyGenerator.getInstance().generate();
    txReqBuilder.signAsEndpoint(sponsorKey);
    BlockchainKeypair gatewayKey = BlockchainKeyGenerator.getInstance().generate();
    txReqBuilder.signAsNode(gatewayKey);
    TransactionRequest txReq = txReqBuilder.buildRequest();
    return txReq;
}
Also used : TransactionRequestBuilder(com.jd.blockchain.ledger.TransactionRequestBuilder) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) TxBuilder(com.jd.blockchain.transaction.TxBuilder) ContractEventSendOperation(com.jd.blockchain.ledger.ContractEventSendOperation) HashAlgorithmUpdateOperation(com.jd.blockchain.ledger.HashAlgorithmUpdateOperation) ConsensusReconfigOperation(com.jd.blockchain.ledger.ConsensusReconfigOperation) ParticipantRegisterOperation(com.jd.blockchain.ledger.ParticipantRegisterOperation) DataAccountKVSetOperation(com.jd.blockchain.ledger.DataAccountKVSetOperation) ParticipantStateUpdateOperation(com.jd.blockchain.ledger.ParticipantStateUpdateOperation) UserRegisterOperation(com.jd.blockchain.ledger.UserRegisterOperation) ContractCodeDeployOperation(com.jd.blockchain.ledger.ContractCodeDeployOperation) Operation(com.jd.blockchain.ledger.Operation) DataAccountRegisterOperation(com.jd.blockchain.ledger.DataAccountRegisterOperation) ConsensusSettingsUpdateOperation(com.jd.blockchain.ledger.ConsensusSettingsUpdateOperation) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Aggregations

BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 ConsensusReconfigOperation (com.jd.blockchain.ledger.ConsensusReconfigOperation)1 ConsensusSettingsUpdateOperation (com.jd.blockchain.ledger.ConsensusSettingsUpdateOperation)1 ContractCodeDeployOperation (com.jd.blockchain.ledger.ContractCodeDeployOperation)1 ContractEventSendOperation (com.jd.blockchain.ledger.ContractEventSendOperation)1 DataAccountKVSetOperation (com.jd.blockchain.ledger.DataAccountKVSetOperation)1 DataAccountRegisterOperation (com.jd.blockchain.ledger.DataAccountRegisterOperation)1 HashAlgorithmUpdateOperation (com.jd.blockchain.ledger.HashAlgorithmUpdateOperation)1 Operation (com.jd.blockchain.ledger.Operation)1 ParticipantRegisterOperation (com.jd.blockchain.ledger.ParticipantRegisterOperation)1 ParticipantStateUpdateOperation (com.jd.blockchain.ledger.ParticipantStateUpdateOperation)1 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)1 TransactionRequestBuilder (com.jd.blockchain.ledger.TransactionRequestBuilder)1 UserRegisterOperation (com.jd.blockchain.ledger.UserRegisterOperation)1 TxBuilder (com.jd.blockchain.transaction.TxBuilder)1