From 59c186f2620a66e3de79853e2a6913fbee3ce5ce Mon Sep 17 00:00:00 2001 From: ququzone Date: Thu, 28 Jul 2022 09:54:50 +0800 Subject: [PATCH] refactor: add chain id to rpc method --- README.md | 2 +- pom.xml | 2 +- .../antenna/action/method/AbstractMethod.java | 2 +- .../antenna/contract/Contract.java | 8 ++------ .../antenna/protocol/ActionRequest.java | 1 - .../iotexproject/antenna/protocol/IOTX.java | 9 +++++---- .../iotexproject/antenna/rpc/RPCMethod.java | 11 +++++++--- .../iotexproject/antenna/token/XRC20.java | 20 ++++++++----------- .../antenna/action/EnvelopTest.java | 4 ++-- .../antenna/action/OfflineTransferDemo.java | 3 +-- .../antenna/action/SealedEnvelopTest.java | 4 ++-- .../antenna/contract/ContractTest.java | 12 +++++------ .../antenna/protocol/IOTXTest.java | 4 ++-- .../antenna/rpc/RPCMethodTest.java | 2 +- .../antenna/rpc/RawBlockTest.java | 2 +- .../iotexproject/antenna/token/XRC20Test.java | 6 +++--- 16 files changed, 44 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index a7b2d7a..c176153 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ network for IoT powered by scalability- and privacy-centric blockchains. Please com.github.iotexproject iotex-antenna-java - 0.6.0 + 0.6.1 ``` diff --git a/pom.xml b/pom.xml index ca9ccbb..f5fbd78 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.iotexproject iotex-antenna-java - 0.6.0 + 0.6.1 jar IoTeX Antenna Java IoTex java sdk for develop android and java apps. diff --git a/src/main/java/com/github/iotexproject/antenna/action/method/AbstractMethod.java b/src/main/java/com/github/iotexproject/antenna/action/method/AbstractMethod.java index 39489a4..11703e8 100644 --- a/src/main/java/com/github/iotexproject/antenna/action/method/AbstractMethod.java +++ b/src/main/java/com/github/iotexproject/antenna/action/method/AbstractMethod.java @@ -38,7 +38,7 @@ protected Envelop baseEnvelop(ActionRequest request) { if (request.getGasLimit() == null) { request.setGasLimit(0l); } - return Envelop.builder().version(1).nonce(request.getNonce()).gasLimit(request.getGasLimit()).gasPrice(request.getGasPrice()).chainID(request.getChainID()).build(); + return Envelop.builder().version(1).nonce(request.getNonce()).gasLimit(request.getGasLimit()).gasPrice(request.getGasPrice()).chainID(client.getChainID()).build(); } protected String sendAction(Envelop envelop) { diff --git a/src/main/java/com/github/iotexproject/antenna/contract/Contract.java b/src/main/java/com/github/iotexproject/antenna/contract/Contract.java index e17d4be..698fd2c 100644 --- a/src/main/java/com/github/iotexproject/antenna/contract/Contract.java +++ b/src/main/java/com/github/iotexproject/antenna/contract/Contract.java @@ -66,12 +66,11 @@ public RPCMethod currentProvider() { * @param nonce nonce, optional * @param gasLimit gasLimit, optional * @param gasPrice gasPrice, optional - * @param chainId chainID * @param account account * @param args constructor params * @return */ - public String deploy(Long nonce, Long gasLimit, String gasPrice, Integer chainID, Account account, String amount, Object... args) { + public String deploy(Long nonce, Long gasLimit, String gasPrice, Account account, String amount, Object... args) { if (this.bin == null) { throw new RuntimeException("deploy contract must set bin"); } @@ -84,7 +83,6 @@ public String deploy(Long nonce, Long gasLimit, String gasPrice, Integer chainID request.setNonce(nonce); request.setGasLimit(gasLimit); request.setGasPrice(gasPrice); - request.setChainID(chainID); request.setAccount(account); request.setContract(""); request.setAmount(amount); @@ -98,14 +96,13 @@ public String deploy(Long nonce, Long gasLimit, String gasPrice, Integer chainID * @param nonce nonce, optional * @param gasLimit gasLimit, optional * @param gasPrice gasPrice, optional - * @param chaindID chainID * @param account account * @param method contract method name * @param method execute amount * @param args contract method params * @return */ - public String execute(Long nonce, Long gasLimit, String gasPrice, Integer chaindID, Account account, String method, String amount, Object... args) { + public String execute(Long nonce, Long gasLimit, String gasPrice, Account account, String method, String amount, Object... args) { Abi.Function function = this.abi.findFunction(method); if (function == null) { throw new RuntimeException("contract method " + method + " not exists."); @@ -114,7 +111,6 @@ public String execute(Long nonce, Long gasLimit, String gasPrice, Integer chaind request.setNonce(nonce); request.setGasLimit(gasLimit); request.setGasPrice(gasPrice); - request.setChainID(chaindID); request.setAccount(account); request.setContract(address); request.setAmount(amount); diff --git a/src/main/java/com/github/iotexproject/antenna/protocol/ActionRequest.java b/src/main/java/com/github/iotexproject/antenna/protocol/ActionRequest.java index effe5a1..a559d01 100644 --- a/src/main/java/com/github/iotexproject/antenna/protocol/ActionRequest.java +++ b/src/main/java/com/github/iotexproject/antenna/protocol/ActionRequest.java @@ -13,6 +13,5 @@ public class ActionRequest { private Long nonce; private Long gasLimit; private String gasPrice; - private Integer chainID; private Account account; } diff --git a/src/main/java/com/github/iotexproject/antenna/protocol/IOTX.java b/src/main/java/com/github/iotexproject/antenna/protocol/IOTX.java index 0914447..56d898c 100644 --- a/src/main/java/com/github/iotexproject/antenna/protocol/IOTX.java +++ b/src/main/java/com/github/iotexproject/antenna/protocol/IOTX.java @@ -4,6 +4,7 @@ import com.github.iotexproject.antenna.action.method.StakeCreateMethod; import com.github.iotexproject.antenna.action.method.TransferMethod; import com.github.iotexproject.antenna.rpc.RPCMethod; +import com.sun.org.apache.xpath.internal.operations.Bool; /** * iotex encapsulate. @@ -13,12 +14,12 @@ public class IOTX { private RPCMethod provider; - public IOTX(String provider) { - this(provider, false); + public IOTX(String provider, Integer chainID) { + this(provider, false, chainID); } - public IOTX(String provider, boolean secure) { - this.provider = new RPCMethod(provider, secure); + public IOTX(String provider, Boolean secure, Integer chainID) { + this.provider = new RPCMethod(provider, secure, chainID); } public RPCMethod currentProvider() { diff --git a/src/main/java/com/github/iotexproject/antenna/rpc/RPCMethod.java b/src/main/java/com/github/iotexproject/antenna/rpc/RPCMethod.java index 97f0817..7062f7c 100644 --- a/src/main/java/com/github/iotexproject/antenna/rpc/RPCMethod.java +++ b/src/main/java/com/github/iotexproject/antenna/rpc/RPCMethod.java @@ -3,6 +3,7 @@ import com.github.iotexproject.grpc.api.*; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; +import lombok.Getter; /** * rpc method. @@ -13,17 +14,21 @@ public class RPCMethod { private ManagedChannel channel; private APIServiceGrpc.APIServiceBlockingStub stub; - public RPCMethod(String hostname) { - this(hostname, false); + @Getter + private Integer chainID; + + public RPCMethod(String hostname, Integer chainID) { + this(hostname, false, chainID); } - public RPCMethod(String hostname, boolean secure) { + public RPCMethod(String hostname, Boolean secure, Integer chainID) { if (secure) { channel = ManagedChannelBuilder.forTarget(hostname).useTransportSecurity().build(); } else { channel = ManagedChannelBuilder.forTarget(hostname).usePlaintext().build(); } stub = APIServiceGrpc.newBlockingStub(channel); + this.chainID = chainID; } public void setProvider(String hostname) { diff --git a/src/main/java/com/github/iotexproject/antenna/token/XRC20.java b/src/main/java/com/github/iotexproject/antenna/token/XRC20.java index 2d3b3ca..ff49c06 100644 --- a/src/main/java/com/github/iotexproject/antenna/token/XRC20.java +++ b/src/main/java/com/github/iotexproject/antenna/token/XRC20.java @@ -77,10 +77,9 @@ public String transfer( BigInteger value, Account account, String gasPrice, - Long gasLimit, - Integer chainID + Long gasLimit ) { - return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "transfer", "0", to, value); + return this.contract.execute(null, gasLimit, gasPrice, account, "transfer", "0", to, value); } public String allowance( @@ -88,10 +87,9 @@ public String allowance( String spender, Account account, String gasPrice, - Long gasLimit, - Integer chainID + Long gasLimit ) { - return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "allowance", "0", owner, spender); + return this.contract.execute(null, gasLimit, gasPrice, account, "allowance", "0", owner, spender); } public String approve( @@ -99,10 +97,9 @@ public String approve( BigInteger value, Account account, String gasPrice, - Long gasLimit, - Integer chainID + Long gasLimit ) { - return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "approve", "0", spender, value); + return this.contract.execute(null, gasLimit, gasPrice, account, "approve", "0", spender, value); } public String transferFrom( @@ -111,9 +108,8 @@ public String transferFrom( BigInteger value, Account account, String gasPrice, - Long gasLimit, - Integer chainID + Long gasLimit ) { - return this.contract.execute(null, gasLimit, gasPrice, chainID, account, "transferFrom", "0", from, to, value); + return this.contract.execute(null, gasLimit, gasPrice, account, "transferFrom", "0", from, to, value); } } diff --git a/src/test/java/com/github/iotexproject/antenna/action/EnvelopTest.java b/src/test/java/com/github/iotexproject/antenna/action/EnvelopTest.java index f1786ef..c5744a6 100644 --- a/src/test/java/com/github/iotexproject/antenna/action/EnvelopTest.java +++ b/src/test/java/com/github/iotexproject/antenna/action/EnvelopTest.java @@ -15,7 +15,7 @@ public class EnvelopTest { @Test public void testTransferDeserialize() { - Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300") + Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300").chainID(2) .transfer(Transfer.newBuilder() .setRecipient("io14jyvf4stclr80nmgx9hrkdr0c4hptfwl7ljxdz") .setAmount("10000") @@ -46,7 +46,7 @@ public void testTransferDeserialize() { @Test public void testExecutionDeserialize() { - Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300") + Envelop envelop = Envelop.builder().version(1).nonce(2l).gasLimit(100l).gasPrice("300").chainID(2) .execution(Execution.newBuilder() .setContract("io24jyvf4stclr80nmgx9hrkdr0c4hptfwl7ljxdz") .setAmount("20000") diff --git a/src/test/java/com/github/iotexproject/antenna/action/OfflineTransferDemo.java b/src/test/java/com/github/iotexproject/antenna/action/OfflineTransferDemo.java index cbd1d69..de0e448 100644 --- a/src/test/java/com/github/iotexproject/antenna/action/OfflineTransferDemo.java +++ b/src/test/java/com/github/iotexproject/antenna/action/OfflineTransferDemo.java @@ -17,14 +17,13 @@ public class OfflineTransferDemo { public static void main(String... args) { final String IOTEX_CORE = "api.testnet.iotex.one:80"; - RPCMethod rpcMethod = new RPCMethod(IOTEX_CORE); + RPCMethod rpcMethod = new RPCMethod(IOTEX_CORE, 2); // 构造转账交易 TransferRequest request = new TransferRequest(); request.setNonce(101l); request.setGasLimit(10000l); request.setGasPrice("1000000000000"); - request.setChainID(2); request.setAmount("1000000000000000000"); request.setRecipient("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml"); OfflineTransferMethod method = new OfflineTransferMethod(null, request); diff --git a/src/test/java/com/github/iotexproject/antenna/action/SealedEnvelopTest.java b/src/test/java/com/github/iotexproject/antenna/action/SealedEnvelopTest.java index fed37f7..7b97283 100644 --- a/src/test/java/com/github/iotexproject/antenna/action/SealedEnvelopTest.java +++ b/src/test/java/com/github/iotexproject/antenna/action/SealedEnvelopTest.java @@ -16,7 +16,7 @@ public class SealedEnvelopTest { @Test public void testSign() { Envelop envelop = Envelop.builder() - .version(1).nonce(1l).gasLimit(100000l).gasPrice("10000000000000") + .version(1).nonce(1l).gasLimit(100000l).gasPrice("10000000000000").chainID(2) .transfer(Transfer.newBuilder() .setAmount("8500000000000000") .setRecipient("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml") @@ -29,7 +29,7 @@ public void testSign() { envelop ); Assert.assertEquals( - "8d7174a896671f5c4565487cb267b7d4ef2a0e70f22082df0bd7e71282bcea7a7c38e2dcd29bc80410571d84b8f788c448f8fb0391160bf2d5eaeee7389c003b00", + "c6c296ea7e6d7c8422c5e70be716e47d91d1ea530c6b6c450d61e648a490f62c10448ebe87e473fda3de80ec52ddafbdcd2c4cf4126227f0f2d436db66879ef401", Numeric.toHexString(sealedEnvelop.getSignature()) ); } diff --git a/src/test/java/com/github/iotexproject/antenna/contract/ContractTest.java b/src/test/java/com/github/iotexproject/antenna/contract/ContractTest.java index ef04dd8..e4222db 100644 --- a/src/test/java/com/github/iotexproject/antenna/contract/ContractTest.java +++ b/src/test/java/com/github/iotexproject/antenna/contract/ContractTest.java @@ -26,26 +26,26 @@ public class ContractTest { @Test public void testDeploy() { - RPCMethod provider = new RPCMethod(IOTEX_CORE); + RPCMethod provider = new RPCMethod(IOTEX_CORE, 2); Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE); Contract contract = new Contract(provider, AbiParseTest.CONTRACT_WITH_CONSTRUCTOR_ABI, Numeric.hexStringToByteArray(AbiParseTest.CONTRACT_WITH_CONSTRUCTOR_BIN)); - String hash = contract.deploy(null, 100000l, "1000000000000", 2, account, "0", 5); + String hash = contract.deploy(null, 100000l, "1000000000000", account, "0", 5); assertNotNull(hash); } @Test public void testExecute() { - RPCMethod provider = new RPCMethod(IOTEX_CORE); + RPCMethod provider = new RPCMethod(IOTEX_CORE, 2); Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE); Contract contract = new Contract(provider, "io1tzevj29d4fsxet3a8gthhz9v4rmqc730myt0q4", AbiParseTest.CONTRACT_WITH_CONSTRUCTOR_ABI); - String hash = contract.execute(null, 100000l, "1000000000000", 2, account, "set", "0", 10); + String hash = contract.execute(null, 100000l, "1000000000000", account, "set", "0", 10); assertNotNull(hash); } public void testRead() { - RPCMethod provider = new RPCMethod(IOTEX_CORE); + RPCMethod provider = new RPCMethod(IOTEX_CORE, 2); Contract contract = new Contract(provider, "io1l4xyuc9t858qgv3xy946avyedf4pryg7hpfzmy", ERC20_ABI); List result = contract.read("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml", "balanceOf", "io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml"); assertNotNull(result); @@ -53,7 +53,7 @@ public void testRead() { @Test public void testDecode() { - RPCMethod provider = new RPCMethod(IOTEX_CORE); + RPCMethod provider = new RPCMethod(IOTEX_CORE, 2); Contract contract = new Contract(provider, "io1l4xyuc9t858qgv3xy946avyedf4pryg7hpfzmy", ERC20_ABI); Abi.Function function = contract.getFunctionBySignature("a9059cbb"); diff --git a/src/test/java/com/github/iotexproject/antenna/protocol/IOTXTest.java b/src/test/java/com/github/iotexproject/antenna/protocol/IOTXTest.java index 85049a7..8309f38 100644 --- a/src/test/java/com/github/iotexproject/antenna/protocol/IOTXTest.java +++ b/src/test/java/com/github/iotexproject/antenna/protocol/IOTXTest.java @@ -16,7 +16,7 @@ public class IOTXTest { public void testTransfer() { //Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE); Account account = IotexAccount.create("41a0dd7befae299109a2e64fd4f275d8c6d0148c2637fea605b8716d64d8236d"); - IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE); + IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE, 2); TransferRequest request = new TransferRequest(); //request.setNonce(1l); // optional, can be null @@ -34,7 +34,7 @@ public void testTransfer() { @Test public void testStakeCreate() { Account account = IotexAccount.create("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1"); - IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE); + IOTX iotx = new IOTX(RPCMethodTest.IOTEX_CORE, 2); StakeCreateRequest request = new StakeCreateRequest(); request.setGasLimit(1000000l); // optional, can be null diff --git a/src/test/java/com/github/iotexproject/antenna/rpc/RPCMethodTest.java b/src/test/java/com/github/iotexproject/antenna/rpc/RPCMethodTest.java index 03a88a4..c379d74 100644 --- a/src/test/java/com/github/iotexproject/antenna/rpc/RPCMethodTest.java +++ b/src/test/java/com/github/iotexproject/antenna/rpc/RPCMethodTest.java @@ -19,7 +19,7 @@ public class RPCMethodTest { @Before public void init() { - this.rpcMethod = new RPCMethod(IOTEX_CORE); + this.rpcMethod = new RPCMethod(IOTEX_CORE, 2); } @After diff --git a/src/test/java/com/github/iotexproject/antenna/rpc/RawBlockTest.java b/src/test/java/com/github/iotexproject/antenna/rpc/RawBlockTest.java index 721e13c..77d9813 100644 --- a/src/test/java/com/github/iotexproject/antenna/rpc/RawBlockTest.java +++ b/src/test/java/com/github/iotexproject/antenna/rpc/RawBlockTest.java @@ -23,7 +23,7 @@ public class RawBlockTest { @Before public void init() { - this.rpcMethod = new RPCMethod(IOTEX_CORE); + this.rpcMethod = new RPCMethod(IOTEX_CORE, 2); } @After diff --git a/src/test/java/com/github/iotexproject/antenna/token/XRC20Test.java b/src/test/java/com/github/iotexproject/antenna/token/XRC20Test.java index 7894090..f8f73c8 100644 --- a/src/test/java/com/github/iotexproject/antenna/token/XRC20Test.java +++ b/src/test/java/com/github/iotexproject/antenna/token/XRC20Test.java @@ -17,7 +17,7 @@ */ public class XRC20Test { public void testConstant() { - RPCMethod provider = new RPCMethod(IOTEX_CORE); + RPCMethod provider = new RPCMethod(IOTEX_CORE, 2); XRC20 vita = new XRC20(provider, "io14j96vg9pkx28htpgt2jx0tf3v9etpg4j9h384m"); Assert.assertEquals("Vitality", vita.name()); @@ -28,10 +28,10 @@ public void testConstant() { public void testTransfer() { - RPCMethod provider = new RPCMethod(IOTEX_CORE); + RPCMethod provider = new RPCMethod(IOTEX_CORE, 2); Account account = IotexAccount.create(IotexAccountTest.TEST_PRIVATE); XRC20 vita = new XRC20(provider, "io14j96vg9pkx28htpgt2jx0tf3v9etpg4j9h384m"); - Assert.assertNotNull(vita.transfer("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml", BigInteger.valueOf(1l), account, "1000000000000", 50000l, 2)); + Assert.assertNotNull(vita.transfer("io13zt8sznez2pf0q0hqdz2hyl938wak2fsjgdeml", BigInteger.valueOf(1l), account, "1000000000000", 50000l)); } }