From 40e397da05461d2c389b2f81943183bea2b98049 Mon Sep 17 00:00:00 2001 From: shaozhuguang Date: Fri, 21 Jun 2019 18:03:24 +0800 Subject: [PATCH] =?UTF-8?q?Modify=20contract=20usage=20in=20readme=20file?= =?UTF-8?q?=20=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 49 ++++++++++--------- .../jd/blockchain/intgr/IntegrationBase.java | 18 +++---- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index c498e807..6b98db84 100644 --- a/README.md +++ b/README.md @@ -596,28 +596,31 @@ JD区块链的核心对象包括: // 合约地址 String contractAddressBase58 = ""; - - // Event - String event = ""; - - // args(注意参数的格式) - byte[] args = "20##30##abc".getBytes(); - - - // 提交合约执行代码 - txTemp.contractEvents().send(contractAddressBase58, event, args); - - // TX 准备就绪; - PreparedTransaction prepTx = txTemp.prepare(); - - // 生成私钥并使用私钥进行签名; - CryptoKeyPair keyPair = getSponsorKey(); - - prepTx.sign(keyPair); - - // 提交交易; - TransactionResponse transactionResponse = prepTx.commit(); - - assertTrue(transactionResponse.isSuccess()); + + // 使用接口方式调用合约 + TransferContract transferContract = txTpl.contract(contractAddress, TransferContract.class); + + // 使用decode方式调用合约内部方法(create方法) + // 返回GenericValueHolder可通过get方法获取结果,但get方法需要在commit调用后执行 + GenericValueHolder result = ContractReturnValue.decode(transferContract.create(address, account, money)); + + PreparedTransaction ptx = txTpl.prepare(); + + ptx.sign(adminKey); + + TransactionResponse transactionResponse = ptx.commit(); + + String cotractExecResult = result.get(); + + // TransactionResponse也提供了可供查询结果的接口 + OperationResult[] operationResults = transactionResponse.getOperationResults(); + + // 通过OperationResult获取结果 + for (int i = 0; i < operationResults.length; i++) { + OperationResult opResult = operationResults[i]; + System.out.printf("Operation[%s].result = %s \r\n", + opResult.getIndex(), BytesValueEncoding.decode(opResult.getResult())); + } + ``` \ No newline at end of file diff --git a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java index 455940dd..84de078f 100644 --- a/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java +++ b/source/test/test-integration/src/test/java/test/com/jd/blockchain/intgr/IntegrationBase.java @@ -25,6 +25,7 @@ import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicLong; +import com.jd.blockchain.ledger.*; import org.apache.commons.io.FileUtils; import org.springframework.core.io.ClassPathResource; @@ -33,17 +34,6 @@ import com.jd.blockchain.contract.ReadContract; import com.jd.blockchain.crypto.AddressEncoding; import com.jd.blockchain.crypto.AsymmetricKeypair; import com.jd.blockchain.crypto.HashDigest; -import com.jd.blockchain.ledger.BlockchainKeyGenerator; -import com.jd.blockchain.ledger.BlockchainKeypair; -import com.jd.blockchain.ledger.KVDataEntry; -import com.jd.blockchain.ledger.LedgerBlock; -import com.jd.blockchain.ledger.LedgerInitOperation; -import com.jd.blockchain.ledger.OperationResult; -import com.jd.blockchain.ledger.PreparedTransaction; -import com.jd.blockchain.ledger.TransactionResponse; -import com.jd.blockchain.ledger.TransactionState; -import com.jd.blockchain.ledger.TransactionTemplate; -import com.jd.blockchain.ledger.UserRegisterOperation; import com.jd.blockchain.ledger.core.LedgerRepository; import com.jd.blockchain.ledger.core.impl.LedgerManager; import com.jd.blockchain.sdk.BlockchainService; @@ -609,6 +599,12 @@ public class IntegrationBase { System.out.printf("readContract1.result = %s \r\n", result1.get()); System.out.printf("readContract3.result = %s \r\n", result3.get()); + for (int i = 0; i < operationResults.length; i++) { + OperationResult opResult = operationResults[i]; + System.out.printf("Operation[%s].result = %s \r\n", opResult.getIndex(), BytesValueEncoding.decode(opResult.getResult())); + } + + // // 打印结果 // for (OperationResult or : operationResults) {