From f874ddd34f1314cca3ac56f2d8a1f0c80fdd4e0b Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 5 Feb 2024 08:35:55 +0800 Subject: [PATCH] (transaction): mv transaction v2 to v1 package, add decode hex to JsonTransactionResponse ut. (#881) --- build.gradle | 2 +- .../contract/solidity/HelloWorldPayable.java | 215 ---------------- .../test/contract/solidity/PayableTest.java | 238 ++++++++++++++++++ .../TransactionManagerPayableTest.java | 126 +++++++--- .../manager/TransactionManagerTest.java | 22 +- .../org/fisco/bcos/sdk/v3/client/Client.java | 14 ++ .../fisco/bcos/sdk/v3/client/ClientImpl.java | 13 + .../model/JsonTransactionResponse.java | 65 ++--- .../fisco/bcos/sdk/v3/contract/Contract.java | 2 +- .../AssembleEIP1559TransactionService.java | 14 +- .../AssembleTransactionService.java | 14 +- .../DefaultTransactionManager.java | 34 ++- .../ProxySignTransactionManager.java | 16 +- .../TransactionManager.java | 11 +- .../TransferTransactionService.java | 2 +- .../transactionv1/dto/AbiEncodedRequest.java | 55 ++++ .../dto/BasicDeployRequest.java | 2 +- .../dto/BasicRequest.java | 2 +- .../dto/DeployTransactionRequest.java | 2 +- ...loyTransactionRequestWithStringParams.java | 2 +- .../dto/TransactionRequest.java | 2 +- .../TransactionRequestWithStringParams.java | 2 +- .../utils/TransactionRequestBuilder.java | 49 +++- .../codec/TransactionDecodeTest.java | 72 ++++-- 24 files changed, 589 insertions(+), 387 deletions(-) delete mode 100644 src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/HelloWorldPayable.java create mode 100644 src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/PayableTest.java rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/AssembleEIP1559TransactionService.java (94%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/AssembleTransactionService.java (97%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/DefaultTransactionManager.java (93%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/ProxySignTransactionManager.java (98%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/TransactionManager.java (97%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/TransferTransactionService.java (98%) create mode 100644 src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/AbiEncodedRequest.java rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/dto/BasicDeployRequest.java (94%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/dto/BasicRequest.java (95%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/dto/DeployTransactionRequest.java (92%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/dto/DeployTransactionRequestWithStringParams.java (94%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/dto/TransactionRequest.java (95%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/dto/TransactionRequestWithStringParams.java (94%) rename src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/{transactionv2 => transactionv1}/utils/TransactionRequestBuilder.java (74%) diff --git a/build.gradle b/build.gradle index 4bb7b1fa8..4163f8b75 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ ext { webankJavaCryptoVersion = "1.0.3" junitVersion = '4.13.2' commonsCollections4Version = "4.4" - bcosSdkJniVersion = "3.6.0-SNAPSHOT" + bcosSdkJniVersion = "3.6.0-DEV-SNAPSHOT" slf4jApiVerison = '1.7.36' mockitoVersion = '4.8.0' gsonVersion = '2.10.1' diff --git a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/HelloWorldPayable.java b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/HelloWorldPayable.java deleted file mode 100644 index c772f4881..000000000 --- a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/HelloWorldPayable.java +++ /dev/null @@ -1,215 +0,0 @@ -package org.fisco.bcos.sdk.v3.test.contract.solidity; - -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.fisco.bcos.sdk.v3.client.Client; -import org.fisco.bcos.sdk.v3.codec.datatypes.DynamicArray; -import org.fisco.bcos.sdk.v3.codec.datatypes.Event; -import org.fisco.bcos.sdk.v3.codec.datatypes.Function; -import org.fisco.bcos.sdk.v3.codec.datatypes.Type; -import org.fisco.bcos.sdk.v3.codec.datatypes.TypeReference; -import org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String; -import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple1; -import org.fisco.bcos.sdk.v3.contract.Contract; -import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; -import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; -import org.fisco.bcos.sdk.v3.eventsub.EventSubCallback; -import org.fisco.bcos.sdk.v3.model.CryptoType; -import org.fisco.bcos.sdk.v3.model.TransactionReceipt; -import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.ProxySignTransactionManager; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.TransactionManager; -import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; - -@SuppressWarnings("unchecked") -public class HelloWorldPayable extends Contract { - public static final String[] BINARY_ARRAY = { - "608060405234801561001057600080fd5b5060408051808201909152600d8082526c48656c6c6f2c20576f726c642160981b60209092019182526100459160009161004b565b5061011f565b828054610057906100e4565b90600052602060002090601f01602090048101928261007957600085556100bf565b82601f1061009257805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100bf5782518255916020019190600101906100a4565b506100cb9291506100cf565b5090565b5b808211156100cb57600081556001016100d0565b600181811c908216806100f857607f821691505b6020821081141561011957634e487b7160e01b600052602260045260246000fd5b50919050565b61033a8061012e6000396000f3fe6080604052600436106100295760003560e01c80634ed3885e1461002e5780636d4ce63c14610057575b600080fd5b61004161003c3660046101c9565b61006c565b60405161004e919061027a565b60405180910390f35b34801561006357600080fd5b50610041610088565b805160609061008290600090602085019061011a565b50919050565b606060008054610097906102cf565b80601f01602080910402602001604051908101604052809291908181526020018280546100c3906102cf565b80156101105780601f106100e557610100808354040283529160200191610110565b820191906000526020600020905b8154815290600101906020018083116100f357829003601f168201915b5050505050905090565b828054610126906102cf565b90600052602060002090601f016020900481019282610148576000855561018e565b82601f1061016157805160ff191683800117855561018e565b8280016001018555821561018e579182015b8281111561018e578251825591602001919060010190610173565b5061019a92915061019e565b5090565b5b8082111561019a576000815560010161019f565b634e487b7160e01b600052604160045260246000fd5b6000602082840312156101db57600080fd5b813567ffffffffffffffff808211156101f357600080fd5b818401915084601f83011261020757600080fd5b813581811115610219576102196101b3565b604051601f8201601f19908116603f01168101908382118183101715610241576102416101b3565b8160405282815287602084870101111561025a57600080fd5b826020860160208301376000928101602001929092525095945050505050565b600060208083528351808285015260005b818110156102a75785810183015185820160400152820161028b565b818111156102b9576000604083870101525b50601f01601f1916929092016040019392505050565b600181811c908216806102e357607f821691505b6020821081141561008257634e487b7160e01b600052602260045260246000fdfea26469706673582212204e290b021c36a93b084c22da8430091a363fd52dbb28dce015c1d2b4eafd2c5164736f6c634300080b0033" - }; - - public static final String BINARY = - org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", BINARY_ARRAY); - - public static final String[] SM_BINARY_ARRAY = { - "608060405234801561001057600080fd5b5060408051808201909152600d8082526c48656c6c6f2c20576f726c642160981b60209092019182526100459160009161004b565b5061011f565b828054610057906100e4565b90600052602060002090601f01602090048101928261007957600085556100bf565b82601f1061009257805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100bf5782518255916020019190600101906100a4565b506100cb9291506100cf565b5090565b5b808211156100cb57600081556001016100d0565b600181811c908216806100f857607f821691505b602082108114156101195763b95aa35560e01b600052602260045260246000fd5b50919050565b61033a8061012e6000396000f3fe6080604052600436106100295760003560e01c8063299f7f9d1461002e5780633590b49f14610059575b600080fd5b34801561003a57600080fd5b5061004361006c565b60405161005091906101b3565b60405180910390f35b61004361006736600461021e565b6100fe565b60606000805461007b906102cf565b80601f01602080910402602001604051908101604052809291908181526020018280546100a7906102cf565b80156100f45780601f106100c9576101008083540402835291602001916100f4565b820191906000526020600020905b8154815290600101906020018083116100d757829003601f168201915b5050505050905090565b805160609061011490600090602085019061011a565b50919050565b828054610126906102cf565b90600052602060002090601f016020900481019282610148576000855561018e565b82601f1061016157805160ff191683800117855561018e565b8280016001018555821561018e579182015b8281111561018e578251825591602001919060010190610173565b5061019a92915061019e565b5090565b5b8082111561019a576000815560010161019f565b600060208083528351808285015260005b818110156101e0578581018301518582016040015282016101c4565b818111156101f2576000604083870101525b50601f01601f1916929092016040019392505050565b63b95aa35560e01b600052604160045260246000fd5b60006020828403121561023057600080fd5b813567ffffffffffffffff8082111561024857600080fd5b818401915084601f83011261025c57600080fd5b81358181111561026e5761026e610208565b604051601f8201601f19908116603f0116810190838211818310171561029657610296610208565b816040528281528760208487010111156102af57600080fd5b826020860160208301376000928101602001929092525095945050505050565b600181811c908216806102e357607f821691505b602082108114156101145763b95aa35560e01b600052602260045260246000fdfea264697066735822122034d8e1ccdfe70284c826efb1bdd0c87f1c7183f04756946c181ae76e57827b6c64736f6c634300080b0033" - }; - - public static final String SM_BINARY = - org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", SM_BINARY_ARRAY); - - public static final String[] ABI_ARRAY = { - "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"string[]\",\"name\":\"\",\"type\":\"string[]\"}],\"name\":\"log\",\"type\":\"event\"},{\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"payable\",\"type\":\"function\"}]" - }; - - public static final String ABI = org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", ABI_ARRAY); - - public static final String FUNC_GET = "get"; - - public static final String FUNC_SET = "set"; - - public static final Event LOG_EVENT = - new Event( - "log", - Arrays.>asList( - new TypeReference>() {})); - - protected HelloWorldPayable( - String contractAddress, Client client, TransactionManager transactionManager) { - super(getBinary(client.getCryptoSuite()), contractAddress, client, transactionManager); - } - - public static String getBinary(CryptoSuite cryptoSuite) { - return (cryptoSuite.getCryptoTypeConfig() == CryptoType.ECDSA_TYPE ? BINARY : SM_BINARY); - } - - public static String getABI() { - return ABI; - } - - public List getLogEvents(TransactionReceipt transactionReceipt) { - List valueList = - extractEventParametersWithLog(LOG_EVENT, transactionReceipt); - ArrayList responses = new ArrayList(valueList.size()); - for (Contract.EventValuesWithLog eventValues : valueList) { - LogEventResponse typedResponse = new LogEventResponse(); - typedResponse.log = eventValues.getLog(); - typedResponse.logParam0 = - (List) eventValues.getNonIndexedValues().get(0).getValue(); - responses.add(typedResponse); - } - return responses; - } - - public void subscribeLogEvent( - BigInteger fromBlock, - BigInteger toBlock, - List otherTopics, - EventSubCallback callback) { - String topic0 = eventEncoder.encode(LOG_EVENT); - subscribeEvent(topic0, otherTopics, fromBlock, toBlock, callback); - } - - public void subscribeLogEvent(EventSubCallback callback) { - String topic0 = eventEncoder.encode(LOG_EVENT); - subscribeEvent(topic0, callback); - } - - public String get() throws ContractException { - final Function function = - new Function( - FUNC_GET, - Arrays.asList(), - Arrays.>asList(new TypeReference() {})); - return executeCallWithSingleValueReturn(function, String.class); - } - - public Function getMethodGetRawFunction() throws ContractException { - final Function function = - new Function( - FUNC_GET, - Arrays.asList(), - Arrays.>asList(new TypeReference() {})); - return function; - } - - public TransactionReceipt set(String n) { - final Function function = - new Function( - FUNC_SET, - Arrays.asList( - new org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String(n)), - Collections.>emptyList(), - 0); - return executeTransaction(function); - } - - public Function getMethodSetRawFunction(String n) throws ContractException { - final Function function = - new Function( - FUNC_SET, - Arrays.asList( - new org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String(n)), - Arrays.>asList(new TypeReference() {})); - return function; - } - - public String getSignedTransactionForSet(String n) { - final Function function = - new Function( - FUNC_SET, - Arrays.asList( - new org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String(n)), - Collections.>emptyList(), - 0); - return createSignedTransaction(function); - } - - public String set(String n, TransactionCallback callback) { - final Function function = - new Function( - FUNC_SET, - Arrays.asList( - new org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String(n)), - Collections.>emptyList(), - 0); - return asyncExecuteTransaction(function, callback); - } - - public Tuple1 getSetInput(TransactionReceipt transactionReceipt) { - String data = transactionReceipt.getInput().substring(10); - final Function function = - new Function( - FUNC_SET, - Arrays.asList(), - Arrays.>asList(new TypeReference() {})); - List results = - this.functionReturnDecoder.decode(data, function.getOutputParameters()); - return new Tuple1((String) results.get(0).getValue()); - } - - public Tuple1 getSetOutput(TransactionReceipt transactionReceipt) { - String data = transactionReceipt.getOutput(); - final Function function = - new Function( - FUNC_SET, - Arrays.asList(), - Arrays.>asList(new TypeReference() {})); - List results = - this.functionReturnDecoder.decode(data, function.getOutputParameters()); - return new Tuple1((String) results.get(0).getValue()); - } - - public static HelloWorldPayable load( - String contractAddress, Client client, TransactionManager transactionManager) { - return new HelloWorldPayable(contractAddress, client, transactionManager); - } - - public static HelloWorldPayable load(String contractAddress, Client client) { - return new HelloWorldPayable( - contractAddress, client, new ProxySignTransactionManager(client)); - } - - public static HelloWorldPayable deploy(Client client, CryptoKeyPair credential) - throws ContractException { - HelloWorldPayable contract = - deploy( - HelloWorldPayable.class, - client, - credential, - getBinary(client.getCryptoSuite()), - getABI(), - null, - null); - contract.setTransactionManager(new ProxySignTransactionManager(client)); - return contract; - } - - public static class LogEventResponse { - public TransactionReceipt.Logs log; - - public List logParam0; - } -} diff --git a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/PayableTest.java b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/PayableTest.java new file mode 100644 index 000000000..cb5437936 --- /dev/null +++ b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/PayableTest.java @@ -0,0 +1,238 @@ +package org.fisco.bcos.sdk.v3.test.contract.solidity; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.codec.datatypes.Event; +import org.fisco.bcos.sdk.v3.codec.datatypes.Function; +import org.fisco.bcos.sdk.v3.codec.datatypes.Type; +import org.fisco.bcos.sdk.v3.codec.datatypes.TypeReference; +import org.fisco.bcos.sdk.v3.codec.datatypes.Utf8String; +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.Uint256; +import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple1; +import org.fisco.bcos.sdk.v3.contract.Contract; +import org.fisco.bcos.sdk.v3.crypto.CryptoSuite; +import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; +import org.fisco.bcos.sdk.v3.eventsub.EventSubCallback; +import org.fisco.bcos.sdk.v3.model.CryptoType; +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.ProxySignTransactionManager; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.TransactionManager; +import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; + +@SuppressWarnings("unchecked") +public class PayableTest extends Contract { + public static final String[] BINARY_ARRAY = {"608060405260008055610260806100176000396000f3fe6080604052600436106100345760003560e01c80633fa4f245146100395780634a0ac20f14610061578063bdcbbf5414610074575b600080fd5b34801561004557600080fd5b5061004f60005481565b60405190815260200160405180910390f35b61004f61006f3660046100fe565b61008f565b34801561008057600080fd5b5061004f61006f3660046100fe565b600060016000808282546100a391906101af565b90915550506040517f7ed53cbadbbc7d8800605823ac88c67b7d9c9fa9d54d45a68d221760e0a86552906100d89084906101d5565b60405180910390a1505060005490565b634e487b7160e01b600052604160045260246000fd5b60006020828403121561011057600080fd5b813567ffffffffffffffff8082111561012857600080fd5b818401915084601f83011261013c57600080fd5b81358181111561014e5761014e6100e8565b604051601f8201601f19908116603f01168101908382118183101715610176576101766100e8565b8160405282815287602084870101111561018f57600080fd5b826020860160208301376000928101602001929092525095945050505050565b600082198211156101d057634e487b7160e01b600052601160045260246000fd5b500190565b600060208083528351808285015260005b81811015610202578581018301518582016040015282016101e6565b81811115610214576000604083870101525b50601f01601f191692909201604001939250505056fea2646970667358221220fa8eb1a6900123689f8fc8f453a833ef65799ff1621c365eb755b27affb2a22864736f6c634300080b0033"}; + + public static final String BINARY = org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", BINARY_ARRAY); + + public static final String[] SM_BINARY_ARRAY = {"608060405260008055610260806100176000396000f3fe6080604052600436106100345760003560e01c80632d92c59d1461003957806386ff705814610061578063d14dc71014610081575b600080fd5b34801561004557600080fd5b5061004f60005481565b60405190815260200160405180910390f35b34801561006d57600080fd5b5061004f61007c3660046100fe565b61008f565b61004f61007c3660046100fe565b600060016000808282546100a391906101af565b90915550506040517f7d280234d3c48f94f1914a272412c3e0030dd7a5841f7933ab1e45658bfaabbe906100d89084906101d5565b60405180910390a1505060005490565b63b95aa35560e01b600052604160045260246000fd5b60006020828403121561011057600080fd5b813567ffffffffffffffff8082111561012857600080fd5b818401915084601f83011261013c57600080fd5b81358181111561014e5761014e6100e8565b604051601f8201601f19908116603f01168101908382118183101715610176576101766100e8565b8160405282815287602084870101111561018f57600080fd5b826020860160208301376000928101602001929092525095945050505050565b600082198211156101d05763b95aa35560e01b600052601160045260246000fd5b500190565b600060208083528351808285015260005b81811015610202578581018301518582016040015282016101e6565b81811115610214576000604083870101525b50601f01601f191692909201604001939250505056fea2646970667358221220e323cce0a161c2d557143e940bdbe0b3d5f519b9f82685f275c6b0d2b1786d9e64736f6c634300080b0033"}; + + public static final String SM_BINARY = org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", SM_BINARY_ARRAY); + + public static final String[] ABI_ARRAY = {"[{\"inputs\":[],\"stateMutability\":\"payable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"string\",\"name\":\"msg\",\"type\":\"string\"}],\"name\":\"incEvent\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"mesg\",\"type\":\"string\"}],\"name\":\"inc\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"mesg\",\"type\":\"string\"}],\"name\":\"incWithPayable\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]"}; + + public static final String ABI = org.fisco.bcos.sdk.v3.utils.StringUtils.joinAll("", ABI_ARRAY); + + public static final String FUNC_INC = "inc"; + + public static final String FUNC_INCWITHPAYABLE = "incWithPayable"; + + public static final String FUNC_VALUE = "value"; + + public static final Event INCEVENT_EVENT = new Event("incEvent", + Arrays.>asList(new TypeReference() {})); + ; + + protected PayableTest(String contractAddress, Client client, CryptoKeyPair credential) { + super(getBinary(client.getCryptoSuite()), contractAddress, client, credential); + this.transactionManager = new ProxySignTransactionManager(client); + } + + protected PayableTest(String contractAddress, Client client, + TransactionManager transactionManager) { + super(getBinary(client.getCryptoSuite()), contractAddress, client, transactionManager); + } + + public static String getBinary(CryptoSuite cryptoSuite) { + return (cryptoSuite.getCryptoTypeConfig() == CryptoType.ECDSA_TYPE ? BINARY : SM_BINARY); + } + + public static String getABI() { + return ABI; + } + + public List getIncEventEvents(TransactionReceipt transactionReceipt) { + List valueList = extractEventParametersWithLog(INCEVENT_EVENT, transactionReceipt); + ArrayList responses = new ArrayList(valueList.size()); + for (EventValuesWithLog eventValues : valueList) { + IncEventEventResponse typedResponse = new IncEventEventResponse(); + typedResponse.log = eventValues.getLog(); + typedResponse.msg = (String) eventValues.getNonIndexedValues().get(0).getValue(); + responses.add(typedResponse); + } + return responses; + } + + public void subscribeIncEventEvent(BigInteger fromBlock, BigInteger toBlock, + List otherTopics, EventSubCallback callback) { + String topic0 = eventEncoder.encode(INCEVENT_EVENT); + subscribeEvent(topic0,otherTopics,fromBlock,toBlock,callback); + } + + public void subscribeIncEventEvent(EventSubCallback callback) { + String topic0 = eventEncoder.encode(INCEVENT_EVENT); + subscribeEvent(topic0,callback); + } + + public TransactionReceipt inc(String mesg) { + final Function function = new Function( + FUNC_INC, + Arrays.asList(new Utf8String(mesg)), + Collections.>emptyList(), 0); + return executeTransaction(function); + } + + public Function getMethodIncRawFunction(String mesg) throws ContractException { + final Function function = new Function(FUNC_INC, + Arrays.asList(new Utf8String(mesg)), + Arrays.>asList(new TypeReference() {})); + return function; + } + + public String getSignedTransactionForInc(String mesg) { + final Function function = new Function( + FUNC_INC, + Arrays.asList(new Utf8String(mesg)), + Collections.>emptyList(), 0); + return createSignedTransaction(function); + } + + public String inc(String mesg, TransactionCallback callback) { + final Function function = new Function( + FUNC_INC, + Arrays.asList(new Utf8String(mesg)), + Collections.>emptyList(), 0); + return asyncExecuteTransaction(function, callback); + } + + public Tuple1 getIncInput(TransactionReceipt transactionReceipt) { + String data = transactionReceipt.getInput().substring(10); + final Function function = new Function(FUNC_INC, + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + List results = this.functionReturnDecoder.decode(data, function.getOutputParameters()); + return new Tuple1( + + (String) results.get(0).getValue() + ); + } + + public Tuple1 getIncOutput(TransactionReceipt transactionReceipt) { + String data = transactionReceipt.getOutput(); + final Function function = new Function(FUNC_INC, + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + List results = this.functionReturnDecoder.decode(data, function.getOutputParameters()); + return new Tuple1( + + (BigInteger) results.get(0).getValue() + ); + } + + public TransactionReceipt incWithPayable(String mesg, BigInteger value) { + final Function function = new Function( + FUNC_INCWITHPAYABLE, + Arrays.asList(new Utf8String(mesg)), + Collections.>emptyList(), 0, value); + return executeTransaction(function); + } + + public Function getMethodIncWithPayableRawFunction(String mesg) throws ContractException { + final Function function = new Function(FUNC_INCWITHPAYABLE, + Arrays.asList(new Utf8String(mesg)), + Arrays.>asList(new TypeReference() {})); + return function; + } + + public String getSignedTransactionForIncWithPayable(String mesg, BigInteger value) { + final Function function = new Function( + FUNC_INCWITHPAYABLE, + Arrays.asList(new Utf8String(mesg)), + Collections.>emptyList(), 0, value); + return createSignedTransaction(function); + } + + public String incWithPayable(String mesg, BigInteger value, TransactionCallback callback) { + final Function function = new Function( + FUNC_INCWITHPAYABLE, + Arrays.asList(new Utf8String(mesg)), + Collections.>emptyList(), 0, value); + return asyncExecuteTransaction(function, callback); + } + + public Tuple1 getIncWithPayableInput(TransactionReceipt transactionReceipt) { + String data = transactionReceipt.getInput().substring(10); + final Function function = new Function(FUNC_INCWITHPAYABLE, + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + List results = this.functionReturnDecoder.decode(data, function.getOutputParameters()); + return new Tuple1( + + (String) results.get(0).getValue() + ); + } + + public Tuple1 getIncWithPayableOutput(TransactionReceipt transactionReceipt) { + String data = transactionReceipt.getOutput(); + final Function function = new Function(FUNC_INCWITHPAYABLE, + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + List results = this.functionReturnDecoder.decode(data, function.getOutputParameters()); + return new Tuple1( + + (BigInteger) results.get(0).getValue() + ); + } + + public BigInteger value() throws ContractException { + final Function function = new Function(FUNC_VALUE, + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + return executeCallWithSingleValueReturn(function, BigInteger.class); + } + + public Function getMethodValueRawFunction() throws ContractException { + final Function function = new Function(FUNC_VALUE, + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + return function; + } + + public static PayableTest load(String contractAddress, Client client, + TransactionManager transactionManager) { + return new PayableTest(contractAddress, client, transactionManager); + } + + public static PayableTest load(String contractAddress, Client client) { + return new PayableTest(contractAddress, client, new ProxySignTransactionManager(client)); + } + + public static PayableTest deploy(Client client, CryptoKeyPair credential, BigInteger value) + throws ContractException { + PayableTest contract = deploy(PayableTest.class, client, credential, getBinary(client.getCryptoSuite()), getABI(), null, null, value); + contract.setTransactionManager(new ProxySignTransactionManager(client)); + return contract; + } + + public static class IncEventEventResponse { + public TransactionReceipt.Logs log; + + public String msg; + } +} diff --git a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerPayableTest.java b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerPayableTest.java index 34cb40299..1bc9c7f0b 100644 --- a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerPayableTest.java +++ b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerPayableTest.java @@ -8,13 +8,15 @@ import org.fisco.bcos.sdk.v3.contract.precompiled.sysconfig.SystemConfigFeature; import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult; import org.fisco.bcos.sdk.v3.model.ConstantConfig; -import org.fisco.bcos.sdk.v3.test.contract.solidity.HelloWorldPayable; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.AssembleTransactionService; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.ProxySignTransactionManager; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.utils.TransactionRequestBuilder; +import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.test.contract.solidity.PayableTest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.AssembleTransactionService; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.ProxySignTransactionManager; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.utils.TransactionRequestBuilder; import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; +import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; import org.fisco.bcos.sdk.v3.transaction.tools.JsonUtils; import org.junit.Assert; import org.junit.Test; @@ -30,16 +32,17 @@ public class TransactionManagerPayableTest { private final Client client; private final AssembleTransactionService transactionService; - private final String abi = HelloWorldPayable.getABI(); + private final String abi = PayableTest.getABI(); private final String binary; - public TransactionManagerPayableTest() { - + public TransactionManagerPayableTest() throws ContractException { // init the sdk, and set the config options. BcosSDK sdk = BcosSDK.build(CONFIG_FILE); // group client = sdk.getClient("group0"); transactionService = new AssembleTransactionService(client); + client.getCryptoSuite() + .loadKeyPair("5ab8223a7b6656e939a4ebf233e3bcf8e230163d4048b9cb6380e1d9ad555ba9"); ProxySignTransactionManager proxySignTransactionManager = new ProxySignTransactionManager( client, @@ -50,7 +53,7 @@ public TransactionManagerPayableTest() { transactionSignCallback.handleSignedTransaction(sign); }); transactionService.setTransactionManager(proxySignTransactionManager); - binary = HelloWorldPayable.getBinary(client.getCryptoSuite()); + binary = PayableTest.getBinary(client.getCryptoSuite()); } @Override @@ -65,37 +68,17 @@ protected void finalize() { } @Test - public void test1ComplexCodecWithType() throws Exception { - if ((client.getNegotiatedProtocol() >> 16) < 2) { - return; - } - BalanceService balanceService = - new BalanceService(client, client.getCryptoSuite().getCryptoKeyPair()); + public void testPayableTestWithSDKInterface() throws Exception { + if (checkSkip()) return; - try { - SystemConfig balanceFeature = - client.getSystemConfigByKey( - SystemConfigFeature.Features.FEATURE_BALANCE.toString()); - if (!"1".equals(balanceFeature.getSystemConfig().getValue())) { - return; - } - } catch (Exception e) { - System.out.println("getSystemConfigByKey error, skip."); - return; - } - - BigInteger balance = - balanceService.getBalance(client.getCryptoSuite().getCryptoKeyPair().getAddress()); - if (balance.compareTo(BigInteger.valueOf(200000)) < 0) { - System.out.println("balance is not enough"); - return; - } // test deploy payable contract List deployParams = new ArrayList<>(); + int deployBalance = 10; + int setBalance = 10; TransactionRequestBuilder builder = new TransactionRequestBuilder(abi, binary); DeployTransactionRequest request = - builder.setValue(BigInteger.valueOf(100000)).buildDeployRequest(deployParams); + builder.setValue(BigInteger.valueOf(deployBalance)).buildDeployRequest(deployParams); TransactionResponse response = transactionService.deployContract(request); @@ -105,14 +88,14 @@ public void test1ComplexCodecWithType() throws Exception { System.out.println(response); - // test call set payable contract + // test call payable contract { List params = new ArrayList<>(); params.add("test"); TransactionRequest transactionRequest = - builder.setValue(BigInteger.valueOf(100000)) - .setMethod("set") + builder.setValue(BigInteger.valueOf(setBalance)) + .setMethod(PayableTest.FUNC_INCWITHPAYABLE) .setTo(contractAddress) .buildRequest(params); TransactionResponse transactionResponse = @@ -120,7 +103,72 @@ public void test1ComplexCodecWithType() throws Exception { Assert.assertEquals(transactionResponse.getTransactionReceipt().getStatus(), 0); System.out.println(JsonUtils.toJson(transactionResponse)); } + // test call no payable but set value + { + List params = new ArrayList<>(); + params.add("test"); + + TransactionRequest transactionRequest = + builder.setValue(BigInteger.valueOf(setBalance)) + .setMethod(PayableTest.FUNC_INC) + .setTo(contractAddress) + .buildRequest(params); + TransactionResponse transactionResponse = + transactionService.sendTransaction(transactionRequest); + Assert.assertFalse(transactionResponse.getTransactionReceipt().isStatusOK()); + } + BalanceService balanceService = + new BalanceService(client, client.getCryptoSuite().getCryptoKeyPair()); + BigInteger balance = balanceService.getBalance(contractAddress); + Assert.assertTrue(balance.compareTo(BigInteger.valueOf(deployBalance + setBalance)) == 0); + } + + @Test + public void testPayableTestWithCodeWrapper() throws ContractException { + if (checkSkip()) return; + int deployBalance = 10; + int setBalance = 10; + PayableTest payableTest = + PayableTest.deploy( + client, + client.getCryptoSuite().getCryptoKeyPair(), + BigInteger.valueOf(deployBalance)); + + TransactionReceipt transactionReceipt = + payableTest.incWithPayable("test", BigInteger.valueOf(setBalance)); + + Assert.assertTrue(transactionReceipt.isStatusOK()); + + BalanceService balanceService = + new BalanceService(client, client.getCryptoSuite().getCryptoKeyPair()); + BigInteger balance = balanceService.getBalance(payableTest.getContractAddress()); + Assert.assertTrue(balance.compareTo(BigInteger.valueOf(deployBalance + setBalance)) == 0); + } + + private boolean checkSkip() throws ContractException { + if (!client.isSupportTransactionV1()) { + return true; + } + BalanceService balanceService = + new BalanceService(client, client.getCryptoSuite().getCryptoKeyPair()); - System.out.println(contractAddress); + try { + SystemConfig balanceFeature = + client.getSystemConfigByKey( + SystemConfigFeature.Features.FEATURE_BALANCE.toString()); + if (!"1".equals(balanceFeature.getSystemConfig().getValue())) { + return true; + } + } catch (Exception e) { + System.out.println("getSystemConfigByKey error, skip."); + return true; + } + + BigInteger balance = + balanceService.getBalance(client.getCryptoSuite().getCryptoKeyPair().getAddress()); + if (balance.compareTo(BigInteger.valueOf(100000)) < 0) { + System.out.println("balance is not enough"); + } + return false; } } diff --git a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java index c8a471212..16b362d70 100644 --- a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java +++ b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/TransactionManagerTest.java @@ -19,14 +19,13 @@ import org.fisco.bcos.sdk.v3.codec.wrapper.ABIObject; import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult; import org.fisco.bcos.sdk.v3.model.ConstantConfig; -import org.fisco.bcos.sdk.v3.model.EnumNodeVersion; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.AssembleTransactionService; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.ProxySignTransactionManager; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequestWithStringParams; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequestWithStringParams; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.utils.TransactionRequestBuilder; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.AssembleTransactionService; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.ProxySignTransactionManager; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.utils.TransactionRequestBuilder; import org.fisco.bcos.sdk.v3.transaction.model.dto.CallResponse; import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; import org.fisco.bcos.sdk.v3.transaction.tools.ContractLoader; @@ -40,7 +39,6 @@ import org.junit.runners.Parameterized; import java.io.IOException; -import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -103,7 +101,7 @@ protected void finalize() { @Test public void test1ComplexCodecWithType() throws Exception { - if ((client.getNegotiatedProtocol() >> 16) < 2) { + if (!client.isSupportTransactionV1()) { return; } // test deploy with struct @@ -311,7 +309,7 @@ public void test1ComplexCodecWithType() throws Exception { @Test public void test2ComplexCodecWithStringParams() throws Exception { - if ((client.getNegotiatedProtocol() >> 16) < 2) { + if (!client.isSupportTransactionV1()) { return; } // test deploy with struct @@ -475,7 +473,7 @@ public void test2ComplexCodecWithStringParams() throws Exception { @Test public void test1ComplexCodecWithJavaObject() throws Exception { - if ((client.getNegotiatedProtocol() >> 16) < 2) { + if (!client.isSupportTransactionV1()) { return; } // test deploy with struct diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java b/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java index ef0942515..e8972bc4b 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java @@ -986,6 +986,13 @@ void getChainCompatibilityVersionAsync( */ void setNodeToSendRequest(String nodeToSendRequest); + /** + * Check this client whether supporting sending transaction with v1 version or not. + * + * @return is this client support sending transaction with v1 version + */ + boolean isSupportTransactionV1(); + /** * get node name to send rpc request directly * @@ -993,6 +1000,13 @@ void getChainCompatibilityVersionAsync( */ String getNodeToSendRequest(); + /** + * Get the protocol version after SDK and Blockchain node negotiated. This method returns int + * with max and min version bits combined, which is (max|min). Max protocol version is in first + * 16 bit, and min protocol version in the second 16 bit. + * + * @return (max|min) bits combined. + */ int getNegotiatedProtocol(); void start(); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java b/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java index 3e0f92f67..79e94ff41 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java @@ -1273,6 +1273,19 @@ public void setNodeToSendRequest(String nodeToSendRequest) { } } + @Override + public boolean isSupportTransactionV1() { + int protocol = getNegotiatedProtocol(); + return (protocol >> 16) >= 2; + } + + /** + * Get the protocol version after SDK and Blockchain node negotiated. This method returns int + * with max and min version bits combined, which is (max|min). Max protocol version is in first + * 16 bit, and min protocol version in the second 16 bit. + * + * @return (max|min) bits combined. + */ @Override public String getNodeToSendRequest() { return this.nodeToSendRequest; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/model/JsonTransactionResponse.java b/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/model/JsonTransactionResponse.java index 2cf0282e6..40ffa3f93 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/model/JsonTransactionResponse.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/model/JsonTransactionResponse.java @@ -26,9 +26,9 @@ import org.fisco.bcos.sdk.jni.common.JniException; import org.fisco.bcos.sdk.jni.utilities.tx.Transaction; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderJniObj; -import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV2JniObj; +import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV1JniObj; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionData; -import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV2; +import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV1; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionStructBuilderJniObj; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion; import org.fisco.bcos.sdk.v3.client.exceptions.ClientException; @@ -280,7 +280,7 @@ public String calculateHash(CryptoSuite cryptoSuite) throws ClientException { try { ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(); String json = objectMapper.writeValueAsString(this); - return TransactionBuilderV2JniObj.calcTransactionDataHashWithJson( + return TransactionBuilderV1JniObj.calcTransactionDataHashWithJson( cryptoSuite.cryptoTypeConfig, json); } catch (Exception e) { logger.warn( @@ -304,7 +304,7 @@ public String calculateHash(CryptoSuite cryptoSuite) throws ClientException { */ public String calculateHash(int cryptoSuiteType) throws ClientException { try { - return TransactionBuilderV2JniObj.calcTransactionDataHashWithFullFields( + return TransactionBuilderV1JniObj.calcTransactionDataHashWithFullFields( cryptoSuiteType, (getVersion() == 0 ? TransactionVersion.V0 : TransactionVersion.V1), getGroupID(), @@ -381,67 +381,42 @@ public byte[] encodeTransactionData() throws IOException { return byteArrayOutputStream.toByteArray(); } - public static JsonTransactionResponse decodeTransactionV1(String hexString) - throws JniException { - Transaction transactionV2 = - TransactionStructBuilderJniObj.decodeTransactionStructV2(hexString); - TransactionData transactionData = transactionV2.getTransactionData(); + public static JsonTransactionResponse decodeTransaction(String hexString) throws JniException { + Transaction transactionV1 = + TransactionStructBuilderJniObj.decodeTransactionStructV1(hexString); + TransactionData transactionData = transactionV1.getTransactionData(); JsonTransactionResponse jsonTransactionResponse = new JsonTransactionResponse(); jsonTransactionResponse.setVersion(transactionData.getVersion()); jsonTransactionResponse.setHash( - Hex.toHexStringWithPrefixNullable(transactionV2.getDataHash(), "")); + Hex.toHexStringWithPrefixNullable(transactionV1.getDataHash(), "")); jsonTransactionResponse.setNonce(transactionData.getNonce()); jsonTransactionResponse.setBlockLimit(transactionData.getBlockLimit()); jsonTransactionResponse.setTo(transactionData.getTo()); jsonTransactionResponse.setFrom( - Hex.toHexStringWithPrefixNullable(transactionV2.getSender(), "")); + Hex.toHexStringWithPrefixNullable(transactionV1.getSender(), "")); jsonTransactionResponse.setAbi(transactionData.getAbi()); jsonTransactionResponse.setInput( Hex.toHexStringWithPrefixNullable(transactionData.getInput(), "")); jsonTransactionResponse.setChainID(transactionData.getChainId()); jsonTransactionResponse.setGroupID(transactionData.getGroupId()); - jsonTransactionResponse.setExtraData(transactionV2.getExtraData()); + jsonTransactionResponse.setExtraData(transactionV1.getExtraData()); jsonTransactionResponse.setSignature( - Hex.toHexStringWithPrefixNullable(transactionV2.getSignature(), "")); - jsonTransactionResponse.setImportTime(transactionV2.getImportTime()); + Hex.toHexStringWithPrefixNullable(transactionV1.getSignature(), "")); + jsonTransactionResponse.setImportTime(transactionV1.getImportTime()); - if (transactionData instanceof TransactionDataV2 + if (transactionData instanceof TransactionDataV1 && transactionData.getVersion() == TransactionVersion.V1.getValue()) { - TransactionDataV2 transactionDataV2 = (TransactionDataV2) transactionData; - jsonTransactionResponse.setValue(transactionDataV2.getValue()); - jsonTransactionResponse.setGasPrice(transactionDataV2.getGasPrice()); - jsonTransactionResponse.setGasLimit(transactionDataV2.getGasLimit()); - jsonTransactionResponse.setMaxFeePerGas(transactionDataV2.getMaxFeePerGas()); + TransactionDataV1 transactionDataV1 = (TransactionDataV1) transactionData; + jsonTransactionResponse.setValue(transactionDataV1.getValue()); + jsonTransactionResponse.setGasPrice(transactionDataV1.getGasPrice()); + jsonTransactionResponse.setGasLimit(transactionDataV1.getGasLimit()); + jsonTransactionResponse.setMaxFeePerGas(transactionDataV1.getMaxFeePerGas()); jsonTransactionResponse.setMaxPriorityFeePerGas( - transactionDataV2.getMaxPriorityFeePerGas()); + transactionDataV1.getMaxPriorityFeePerGas()); } return jsonTransactionResponse; } - public static JsonTransactionResponse decodeTransaction(String hexString) throws JniException { - Transaction transaction = TransactionStructBuilderJniObj.decodeTransactionStruct(hexString); - JsonTransactionResponse jsonTransactionResponse = new JsonTransactionResponse(); - jsonTransactionResponse.setVersion(transaction.getTransactionData().getVersion()); - jsonTransactionResponse.setHash( - Hex.toHexStringWithPrefixNullable(transaction.getDataHash(), "")); - jsonTransactionResponse.setNonce(transaction.getTransactionData().getNonce()); - jsonTransactionResponse.setBlockLimit(transaction.getTransactionData().getBlockLimit()); - jsonTransactionResponse.setTo(transaction.getTransactionData().getTo()); - jsonTransactionResponse.setFrom( - Hex.toHexStringWithPrefixNullable(transaction.getSender(), "")); - jsonTransactionResponse.setAbi(transaction.getTransactionData().getAbi()); - jsonTransactionResponse.setInput( - Hex.toHexStringWithPrefixNullable(transaction.getTransactionData().getInput(), "")); - jsonTransactionResponse.setChainID(transaction.getTransactionData().getChainId()); - jsonTransactionResponse.setGroupID(transaction.getTransactionData().getGroupId()); - jsonTransactionResponse.setExtraData(transaction.getExtraData()); - jsonTransactionResponse.setSignature( - Hex.toHexStringWithPrefixNullable(transaction.getSignature(), "")); - jsonTransactionResponse.setImportTime(transaction.getImportTime()); - - return jsonTransactionResponse; - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java b/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java index e25091560..4e8e0a438 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java @@ -56,7 +56,7 @@ import org.fisco.bcos.sdk.v3.transaction.codec.decode.ReceiptParser; import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessor; import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessorFactory; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.TransactionManager; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.TransactionManager; import org.fisco.bcos.sdk.v3.transaction.model.dto.CallRequest; import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; import org.slf4j.Logger; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleEIP1559TransactionService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/AssembleEIP1559TransactionService.java similarity index 94% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleEIP1559TransactionService.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/AssembleEIP1559TransactionService.java index ab6b77d3a..b6fc893d0 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleEIP1559TransactionService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/AssembleEIP1559TransactionService.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1; import java.util.Objects; import org.fisco.bcos.sdk.jni.common.JniException; @@ -6,12 +6,12 @@ import org.fisco.bcos.sdk.v3.codec.ContractCodecException; import org.fisco.bcos.sdk.v3.model.TransactionReceipt; import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicDeployRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequestWithStringParams; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.BasicDeployRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.BasicRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequestWithStringParams; import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; import org.fisco.bcos.sdk.v3.utils.Hex; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleTransactionService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/AssembleTransactionService.java similarity index 97% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleTransactionService.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/AssembleTransactionService.java index 150ad8014..798b38686 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/AssembleTransactionService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/AssembleTransactionService.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1; import java.util.List; import java.util.Objects; @@ -17,12 +17,12 @@ import org.fisco.bcos.sdk.v3.transaction.codec.decode.ReceiptParser; import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderInterface; import org.fisco.bcos.sdk.v3.transaction.codec.decode.TransactionDecoderService; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicDeployRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.BasicRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequestWithStringParams; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.BasicDeployRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.BasicRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequestWithStringParams; import org.fisco.bcos.sdk.v3.transaction.model.dto.CallResponse; import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; import org.fisco.bcos.sdk.v3.utils.Hex; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/DefaultTransactionManager.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/DefaultTransactionManager.java similarity index 93% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/DefaultTransactionManager.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/DefaultTransactionManager.java index c1f7f21d5..2b9d6df2f 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/DefaultTransactionManager.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/DefaultTransactionManager.java @@ -1,9 +1,9 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1; import java.io.ByteArrayOutputStream; import java.math.BigInteger; import org.fisco.bcos.sdk.jni.common.JniException; -import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV2JniObj; +import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV1JniObj; import org.fisco.bcos.sdk.jni.utilities.tx.TxPair; import org.fisco.bcos.sdk.v3.client.Client; import org.fisco.bcos.sdk.v3.client.protocol.model.TransactionAttribute; @@ -18,6 +18,7 @@ import org.fisco.bcos.sdk.v3.transaction.gasProvider.ContractGasProvider; import org.fisco.bcos.sdk.v3.transaction.gasProvider.DefaultGasProvider; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.AbiEncodedRequest; import org.fisco.bcos.sdk.v3.transaction.nonce.DefaultNonceAndBlockLimitProvider; import org.fisco.bcos.sdk.v3.transaction.nonce.NonceAndBlockLimitProvider; import org.fisco.bcos.sdk.v3.utils.Hex; @@ -85,7 +86,7 @@ public TransactionReceipt sendTransaction( data, value, getGasProvider().getGasPrice(methodId), - getGasProvider().getGasPrice(methodId), + getGasProvider().getGasLimit(methodId), client.getBlockLimit(), abi, constructor); @@ -151,6 +152,23 @@ public TransactionReceipt sendTransaction( return bcosTransactionReceipt.getTransactionReceipt(); } + // @Override + public TransactionReceipt sendTransaction(AbiEncodedRequest request) throws JniException { + String signedTransaction = + createSignedTransaction( + request.getTo(), + request.getEncodedData(), + request.getValue(), + request.getGasPrice(), + request.getGasLimit(), + request.getBlockLimit(), + request.getAbi(), + request.getAbi() != null); + BcosTransactionReceipt bcosTransactionReceipt = + client.sendTransaction(signedTransaction, false); + return bcosTransactionReceipt.getTransactionReceipt(); + } + /** * This method is used to create a signed transaction. * @@ -187,7 +205,7 @@ public String createSignedTransaction( transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; } TxPair txPair = - TransactionBuilderV2JniObj.createSignedTransactionWithFullFields( + TransactionBuilderV1JniObj.createSignedTransactionWithFullFields( client.getCryptoSuite().getCryptoKeyPair().getJniKeyPair(), client.getGroup(), client.getChainId(), @@ -234,7 +252,7 @@ public String asyncSendTransaction( data, value, getGasProvider().getGasPrice(methodId), - getGasProvider().getGasPrice(methodId), + getGasProvider().getGasLimit(methodId), client.getBlockLimit(), abi, constructor, @@ -315,7 +333,7 @@ public String asyncSendTransaction( transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; } TxPair txPair = - TransactionBuilderV2JniObj.createSignedTransactionWithFullFields( + TransactionBuilderV1JniObj.createSignedTransactionWithFullFields( client.getCryptoSuite().getCryptoKeyPair().getJniKeyPair(), client.getGroup(), client.getChainId(), @@ -390,7 +408,7 @@ public TransactionReceipt sendTransactionEIP1559( transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; } TxPair txPair = - TransactionBuilderV2JniObj.createSignedEIP1559TransactionWithFullFields( + TransactionBuilderV1JniObj.createSignedEIP1559TransactionWithFullFields( client.getCryptoSuite().getCryptoKeyPair().getJniKeyPair(), client.getGroup(), client.getChainId(), @@ -469,7 +487,7 @@ public String asyncSendTransactionEIP1559( transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; } TxPair txPair = - TransactionBuilderV2JniObj.createSignedEIP1559TransactionWithFullFields( + TransactionBuilderV1JniObj.createSignedEIP1559TransactionWithFullFields( client.getCryptoSuite().getCryptoKeyPair().getJniKeyPair(), client.getGroup(), client.getChainId(), diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/ProxySignTransactionManager.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/ProxySignTransactionManager.java similarity index 98% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/ProxySignTransactionManager.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/ProxySignTransactionManager.java index 78abb9632..20f591d73 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/ProxySignTransactionManager.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/ProxySignTransactionManager.java @@ -1,11 +1,11 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1; import java.io.ByteArrayOutputStream; import java.math.BigInteger; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import org.fisco.bcos.sdk.jni.common.JniException; -import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV2JniObj; +import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV1JniObj; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion; import org.fisco.bcos.sdk.v3.client.Client; import org.fisco.bcos.sdk.v3.client.protocol.model.TransactionAttribute; @@ -204,7 +204,7 @@ public String createSignedTransaction( } String nonce = nonceProvider.getNonce(); String dataHash = - TransactionBuilderV2JniObj.calcTransactionDataHashWithFullFields( + TransactionBuilderV1JniObj.calcTransactionDataHashWithFullFields( cryptoType, TransactionVersion.V1, client.getGroup(), @@ -245,7 +245,7 @@ public String createSignedTransaction( } else { transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; } - return TransactionBuilderV2JniObj.createSignedTransactionWithSignature( + return TransactionBuilderV1JniObj.createSignedTransactionWithSignature( signatureResult.encode(), dataHash, TransactionVersion.V1, @@ -397,7 +397,7 @@ public String asyncSendTransaction( throws JniException { String nonce = nonceProvider.getNonce(); String dataHash = - TransactionBuilderV2JniObj.calcTransactionDataHashWithFullFields( + TransactionBuilderV1JniObj.calcTransactionDataHashWithFullFields( cryptoType, TransactionVersion.V1, client.getGroup(), @@ -427,7 +427,7 @@ public String asyncSendTransaction( transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; } String signedTransaction = - TransactionBuilderV2JniObj.createSignedTransactionWithSignature( + TransactionBuilderV1JniObj.createSignedTransactionWithSignature( signature.encode(), dataHash, TransactionVersion.V1, @@ -601,7 +601,7 @@ public String asyncSendTransactionEIP1559( throws JniException { String nonce = nonceProvider.getNonce(); String dataHash = - TransactionBuilderV2JniObj.calcTransactionDataHashWithFullFields( + TransactionBuilderV1JniObj.calcTransactionDataHashWithFullFields( cryptoType, TransactionVersion.V1, client.getGroup(), @@ -630,7 +630,7 @@ public String asyncSendTransactionEIP1559( transactionAttribute = TransactionAttribute.EVM_ABI_CODEC; } String signedTransaction = - TransactionBuilderV2JniObj.createSignedTransactionWithSignature( + TransactionBuilderV1JniObj.createSignedTransactionWithSignature( signature.encode(), dataHash, TransactionVersion.V1, diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/TransactionManager.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/TransactionManager.java similarity index 97% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/TransactionManager.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/TransactionManager.java index 014d05f31..6ad9e9bcb 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/TransactionManager.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/TransactionManager.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1; import java.math.BigInteger; import org.fisco.bcos.sdk.jni.common.JniException; @@ -24,12 +24,10 @@ public Client getClient() { } protected TransactionManager(Client client) { - int negotiatedProtocol = client.getNegotiatedProtocol(); - int maxProtocol = negotiatedProtocol >> 16; - if (maxProtocol < 2) { + if (!client.isSupportTransactionV1()) { logger.error( "The current version of the node does not support the transaction manager, please upgrade the node to the latest version. Max protocol version is {}", - maxProtocol); + client.getNegotiatedProtocol() >> 16); } this.client = client; } @@ -118,6 +116,9 @@ public abstract TransactionReceipt sendTransaction( boolean constructor) throws JniException; + // public abstract TransactionReceipt sendTransaction(AbiEncodedRequest request) throws + // JniException; + /** * This method is used to create a signed transaction. * diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/TransferTransactionService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/TransferTransactionService.java similarity index 98% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/TransferTransactionService.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/TransferTransactionService.java index 7f4ef22bf..b1e46dd99 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/TransferTransactionService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/TransferTransactionService.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1; import java.math.BigDecimal; import java.math.BigInteger; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/AbiEncodedRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/AbiEncodedRequest.java new file mode 100644 index 000000000..a82198950 --- /dev/null +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/AbiEncodedRequest.java @@ -0,0 +1,55 @@ +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto; + +import java.math.BigInteger; +import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; + +public class AbiEncodedRequest extends BasicRequest { + // required + protected byte[] encodedData; + // optional + protected BigInteger blockLimit; + // optional + protected String nonce; + + public AbiEncodedRequest( + String to, + BigInteger value, + BigInteger gasPrice, + BigInteger gasLimit, + EIP1559Struct eip1559Struct) { + super("", "", to, value, gasPrice, gasLimit, eip1559Struct); + } + + @Override + public boolean isTransactionEssentialSatisfy() { + return encodedData != null && to != null; + } + + public byte[] getEncodedData() { + return encodedData; + } + + public void setEncodedData(byte[] encodedData) { + this.encodedData = encodedData; + } + + public BigInteger getBlockLimit() { + return blockLimit; + } + + public void setBlockLimit(BigInteger blockLimit) { + this.blockLimit = blockLimit; + } + + public String getNonce() { + return nonce; + } + + public void setNonce(String nonce) { + this.nonce = nonce; + } + + public void setAbi(String abi) { + this.abi = abi; + } +} diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicDeployRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/BasicDeployRequest.java similarity index 94% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicDeployRequest.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/BasicDeployRequest.java index 729af6761..ea1cad979 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicDeployRequest.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/BasicDeployRequest.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto; import java.math.BigInteger; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/BasicRequest.java similarity index 95% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicRequest.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/BasicRequest.java index 5f011635f..4237c4e90 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/BasicRequest.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/BasicRequest.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto; import java.math.BigInteger; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/DeployTransactionRequest.java similarity index 92% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequest.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/DeployTransactionRequest.java index 13a513813..6d4f895f3 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequest.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/DeployTransactionRequest.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto; import java.math.BigInteger; import java.util.List; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequestWithStringParams.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/DeployTransactionRequestWithStringParams.java similarity index 94% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequestWithStringParams.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/DeployTransactionRequestWithStringParams.java index 3c5444083..f6d0f6809 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/DeployTransactionRequestWithStringParams.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/DeployTransactionRequestWithStringParams.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto; import java.math.BigInteger; import java.util.List; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequest.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/TransactionRequest.java similarity index 95% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequest.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/TransactionRequest.java index 0e9b28cbb..7509477d9 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequest.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/TransactionRequest.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto; import java.math.BigInteger; import java.util.List; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequestWithStringParams.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/TransactionRequestWithStringParams.java similarity index 94% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequestWithStringParams.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/TransactionRequestWithStringParams.java index 8266fe0d9..d4373b422 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/dto/TransactionRequestWithStringParams.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/dto/TransactionRequestWithStringParams.java @@ -1,4 +1,4 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto; import java.math.BigInteger; import java.util.List; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/utils/TransactionRequestBuilder.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/utils/TransactionRequestBuilder.java similarity index 74% rename from src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/utils/TransactionRequestBuilder.java rename to src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/utils/TransactionRequestBuilder.java index b07027ccd..bd79638c9 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv2/utils/TransactionRequestBuilder.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/manager/transactionv1/utils/TransactionRequestBuilder.java @@ -1,21 +1,25 @@ -package org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.utils; +package org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.utils; import java.math.BigInteger; import java.util.List; import org.fisco.bcos.sdk.v3.transaction.gasProvider.EIP1559Struct; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.DeployTransactionRequestWithStringParams; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequest; -import org.fisco.bcos.sdk.v3.transaction.manager.transactionv2.dto.TransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.AbiEncodedRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.DeployTransactionRequestWithStringParams; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequest; +import org.fisco.bcos.sdk.v3.transaction.manager.transactionv1.dto.TransactionRequestWithStringParams; import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; public class TransactionRequestBuilder { private String abi; private String method; + private byte[] abiEncodedData; private String to; private BigInteger value; private BigInteger gasPrice; private BigInteger gasLimit; + private BigInteger blockLimit; + private String nonce; private String bin; private EIP1559Struct eip1559Struct; @@ -65,6 +69,21 @@ public TransactionRequestBuilder setBin(String bin) { return this; } + public TransactionRequestBuilder setAbiEncodedData(byte[] abiEncodedData) { + this.abiEncodedData = abiEncodedData; + return this; + } + + public TransactionRequestBuilder setBlockLimit(BigInteger blockLimit) { + this.blockLimit = blockLimit; + return this; + } + + public TransactionRequestBuilder setNonce(String nonce) { + this.nonce = nonce; + return this; + } + public TransactionRequest buildRequest(List params) throws ContractException { if (params == null) { throw new ContractException("SendTransaction params is null, please set it manually."); @@ -147,4 +166,24 @@ public DeployTransactionRequestWithStringParams buildDeployStringParamsRequest( request.setStringParams(stringParams); return request; } + + public AbiEncodedRequest buildAbiEncodedRequest(boolean isCreate) throws ContractException { + if (abiEncodedData == null) { + throw new ContractException( + "AbiEncodedRequest abiEncodedData is null, please set it manually."); + } + if (isCreate && abi == null) { + throw new ContractException("AbiEncodedRequest abi is null, please set it manually."); + } + AbiEncodedRequest request = + new AbiEncodedRequest( + this.to, this.value, this.gasPrice, this.gasLimit, this.eip1559Struct); + request.setEncodedData(abiEncodedData); + request.setBlockLimit(blockLimit); + request.setNonce(nonce); + if (isCreate) { + request.setAbi(abi); + } + return request; + } } diff --git a/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/codec/TransactionDecodeTest.java b/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/codec/TransactionDecodeTest.java index e44bec56d..654fabe80 100644 --- a/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/codec/TransactionDecodeTest.java +++ b/src/test/java/org/fisco/bcos/sdk/v3/test/transaction/codec/TransactionDecodeTest.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.fisco.bcos.sdk.jni.common.JniException; import org.fisco.bcos.sdk.jni.utilities.tx.Transaction; -import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV2; +import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV1; import org.fisco.bcos.sdk.jni.utilities.tx.TransactionStructBuilderJniObj; import org.fisco.bcos.sdk.v3.client.protocol.model.JsonTransactionResponse; import org.fisco.bcos.sdk.v3.client.protocol.response.BcosTransactionReceipt; @@ -175,26 +175,26 @@ public void parseTransactionReceiptTest() throws ContractException { @Test public void testDeocde() throws JniException { - TransactionDataV2 transactionDataV2 = new TransactionDataV2(); - transactionDataV2.setVersion(1); - transactionDataV2.setBlockLimit(999999999); - transactionDataV2.setChainId("chain0"); - transactionDataV2.setGroupId("group0"); - transactionDataV2.setNonce("1420055182625795013612786582903998422427"); - transactionDataV2.setTo("0x0102e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94"); - transactionDataV2.setAbi("abi"); - transactionDataV2.setInput( + TransactionDataV1 transactionDataV1 = new TransactionDataV1(); + transactionDataV1.setVersion(1); + transactionDataV1.setBlockLimit(999999999); + transactionDataV1.setChainId("chain0"); + transactionDataV1.setGroupId("group0"); + transactionDataV1.setNonce("1420055182625795013612786582903998422427"); + transactionDataV1.setTo("0x0102e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94"); + transactionDataV1.setAbi("abi"); + transactionDataV1.setInput( Hex.decode( "4ed3885e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047465737400000000000000000000000000000000000000000000000000000000")); - transactionDataV2.setValue("0x11"); - transactionDataV2.setGasLimit(1000000); - transactionDataV2.setGasPrice("0x12"); - transactionDataV2.setMaxFeePerGas("0x13"); - transactionDataV2.setMaxPriorityFeePerGas("0x14"); + transactionDataV1.setValue("0x11"); + transactionDataV1.setGasLimit(1000000); + transactionDataV1.setGasPrice("0x12"); + transactionDataV1.setMaxFeePerGas("0x13"); + transactionDataV1.setMaxPriorityFeePerGas("0x14"); String hash = TransactionStructBuilderJniObj.calcTransactionDataStructHash( - CryptoType.ECDSA_TYPE, transactionDataV2); + CryptoType.ECDSA_TYPE, transactionDataV1); System.out.println("hash: " + hash); CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE); SignatureResult sign = cryptoSuite.sign(Hex.decode(hash), cryptoSuite.getCryptoKeyPair()); @@ -202,7 +202,7 @@ public void testDeocde() throws JniException { Transaction transaction = new Transaction(); transaction.setDataHash(Hex.decode(hash)); transaction.setSignature(sign.encode()); - transaction.setTransactionData(transactionDataV2); + transaction.setTransactionData(transactionDataV1); transaction.setExtraData("testExtraData"); transaction.setAttribute(1); transaction.setSender(Hex.decode("0x1232e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94")); @@ -213,7 +213,7 @@ public void testDeocde() throws JniException { System.out.println("encodeTransactionStruct: " + encodeTransactionStruct); JsonTransactionResponse jsonTransactionResponse = - JsonTransactionResponse.decodeTransactionV1(encodeTransactionStruct); + JsonTransactionResponse.decodeTransaction(encodeTransactionStruct); Assert.assertEquals(1, jsonTransactionResponse.getVersion().intValue()); Assert.assertEquals(999999999, jsonTransactionResponse.getBlockLimit()); @@ -225,7 +225,8 @@ public void testDeocde() throws JniException { "0x0102e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94", jsonTransactionResponse.getTo()); Assert.assertEquals("abi", jsonTransactionResponse.getAbi()); Assert.assertEquals( - Hex.toHexStringWithPrefix(transactionDataV2.getInput()), jsonTransactionResponse.getInput()); + Hex.toHexStringWithPrefix(transactionDataV1.getInput()), + jsonTransactionResponse.getInput()); Assert.assertEquals("0x11", jsonTransactionResponse.getValue()); Assert.assertEquals("0x12", jsonTransactionResponse.getGasPrice()); Assert.assertEquals("0x13", jsonTransactionResponse.getMaxFeePerGas()); @@ -233,7 +234,8 @@ public void testDeocde() throws JniException { Assert.assertEquals(1000000, jsonTransactionResponse.getGasLimit()); Assert.assertEquals(jsonTransactionResponse.getHash(), hash); - Assert.assertEquals(Hex.toHexStringWithPrefix(sign.encode()), jsonTransactionResponse.getSignature()); + Assert.assertEquals( + Hex.toHexStringWithPrefix(sign.encode()), jsonTransactionResponse.getSignature()); Assert.assertEquals("testExtraData", jsonTransactionResponse.getExtraData()); Assert.assertEquals( "0x1232e8b6fc8cdf9626fddc1c3ea8c1e79b3fce94", jsonTransactionResponse.getFrom()); @@ -246,17 +248,33 @@ public void testDeocde() throws JniException { { JsonTransactionResponse transactionV0 = JsonTransactionResponse.decodeTransaction(txV0); - JsonTransactionResponse transactionV1 = - JsonTransactionResponse.decodeTransactionV1(txV1); + JsonTransactionResponse transactionV1 = JsonTransactionResponse.decodeTransaction(txV1); System.out.println(transactionV0); System.out.println(transactionV1); } - { -// JsonTransactionResponse transactionV0 = -// JsonTransactionResponse.decodeTransactionV1(txV0); -// System.out.println(transactionV0); - } + Assert.assertThrows( + JniException.class, () -> JsonTransactionResponse.decodeTransaction(null)); + + Assert.assertThrows( + JniException.class, () -> JsonTransactionResponse.decodeTransaction("")); + + Assert.assertThrows( + JniException.class, + () -> + JsonTransactionResponse.decodeTransaction( + txV0.substring(0, txV0.length() / 2))); + Assert.assertThrows( + JniException.class, + () -> + JsonTransactionResponse.decodeTransaction( + txV0.substring(0, txV1.length() / 3))); + + Assert.assertThrows( + JniException.class, + () -> + JsonTransactionResponse.decodeTransaction( + txV0.substring(txV1.length() / 3))); } }