From 6edc1863eba471b9f8f3ac95bbef2bb821d2b272 Mon Sep 17 00:00:00 2001 From: yangfang2 Date: Tue, 10 Sep 2024 10:39:22 +0800 Subject: [PATCH] (test): integration test --- build.gradle | 1 - .../stub/web3/integration/AsyncToSync.java | 23 ++ .../ConnectionEventHandlerImplMock.java | 23 ++ .../Web3StubCallContractIntegTest.java | 316 ++++++++++++++++++ .../web3/integration/contract/HelloWorld.java | 161 +++++++++ .../webank/wecross/stub/web3/Web3Driver.java | 36 +- .../stub/web3/client/ClientBlockManager.java | 51 +++ .../stub/web3/client/ClientWrapperImpl.java | 4 + .../stub/web3/config/Web3StubConfig.java | 5 + .../resources/WeCrossHub.sol | 0 .../resources/WeCrossProxy.sol | 0 .../wecross/stub/web3/Web3ConnectionTest.java | 3 +- .../wecross/stub/web3/Web3DriverTest.java | 18 +- .../web3/client/ClientWrapperImplMock.java | 1 - .../wecross/stub/web3/uaproof/SignerTest.java | 11 +- 15 files changed, 625 insertions(+), 28 deletions(-) create mode 100644 src/integTest/java/com/webank/wecross/stub/web3/integration/AsyncToSync.java create mode 100644 src/integTest/java/com/webank/wecross/stub/web3/integration/ConnectionEventHandlerImplMock.java create mode 100644 src/integTest/java/com/webank/wecross/stub/web3/integration/Web3StubCallContractIntegTest.java create mode 100644 src/integTest/java/com/webank/wecross/stub/web3/integration/contract/HelloWorld.java create mode 100644 src/main/java/com/webank/wecross/stub/web3/client/ClientBlockManager.java rename src/{integTest => main}/resources/WeCrossHub.sol (100%) rename src/{integTest => main}/resources/WeCrossProxy.sol (100%) diff --git a/build.gradle b/build.gradle index c526b82..6eac4cb 100644 --- a/build.gradle +++ b/build.gradle @@ -142,7 +142,6 @@ task makeJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar, d exclude '**/*.toml' exclude '**/*.properties' exclude '**/*.yml' - exclude '**/performance/guomi/*' manifest { try { diff --git a/src/integTest/java/com/webank/wecross/stub/web3/integration/AsyncToSync.java b/src/integTest/java/com/webank/wecross/stub/web3/integration/AsyncToSync.java new file mode 100644 index 0000000..29010f9 --- /dev/null +++ b/src/integTest/java/com/webank/wecross/stub/web3/integration/AsyncToSync.java @@ -0,0 +1,23 @@ +package com.webank.wecross.stub.web3.integration; + +import java.util.concurrent.Semaphore; + +public class AsyncToSync { + public AsyncToSync() { + try { + semaphore.acquire(1); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + public Semaphore getSemaphore() { + return semaphore; + } + + public void setSemaphore(Semaphore semaphore) { + this.semaphore = semaphore; + } + + public Semaphore semaphore = new Semaphore(1, true); +} diff --git a/src/integTest/java/com/webank/wecross/stub/web3/integration/ConnectionEventHandlerImplMock.java b/src/integTest/java/com/webank/wecross/stub/web3/integration/ConnectionEventHandlerImplMock.java new file mode 100644 index 0000000..1924308 --- /dev/null +++ b/src/integTest/java/com/webank/wecross/stub/web3/integration/ConnectionEventHandlerImplMock.java @@ -0,0 +1,23 @@ +package com.webank.wecross.stub.web3.integration; + +import com.webank.wecross.stub.Connection; +import com.webank.wecross.stub.ResourceInfo; +import java.util.List; + +public class ConnectionEventHandlerImplMock implements Connection.ConnectionEventHandler { + + private List resourceInfos; + + @Override + public void onResourcesChange(List resourceInfos) { + this.resourceInfos = resourceInfos; + } + + public List getResourceInfos() { + return resourceInfos; + } + + public void setResourceInfos(List resourceInfos) { + this.resourceInfos = resourceInfos; + } +} diff --git a/src/integTest/java/com/webank/wecross/stub/web3/integration/Web3StubCallContractIntegTest.java b/src/integTest/java/com/webank/wecross/stub/web3/integration/Web3StubCallContractIntegTest.java new file mode 100644 index 0000000..2f9e021 --- /dev/null +++ b/src/integTest/java/com/webank/wecross/stub/web3/integration/Web3StubCallContractIntegTest.java @@ -0,0 +1,316 @@ +package com.webank.wecross.stub.web3.integration; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertNull; +import static junit.framework.TestCase.assertTrue; + +import com.webank.wecross.stub.BlockHeader; +import com.webank.wecross.stub.BlockManager; +import com.webank.wecross.stub.Driver; +import com.webank.wecross.stub.Path; +import com.webank.wecross.stub.ResourceInfo; +import com.webank.wecross.stub.TransactionContext; +import com.webank.wecross.stub.TransactionRequest; +import com.webank.wecross.stub.web3.Web3Connection; +import com.webank.wecross.stub.web3.Web3ConnectionFactory; +import com.webank.wecross.stub.web3.Web3StubFactory; +import com.webank.wecross.stub.web3.account.Web3Account; +import com.webank.wecross.stub.web3.account.Web3AccountFactory; +import com.webank.wecross.stub.web3.client.ClientBlockManager; +import com.webank.wecross.stub.web3.client.ClientWrapperImpl; +import com.webank.wecross.stub.web3.common.Web3Constant; +import com.webank.wecross.stub.web3.common.Web3StatusCode; +import com.webank.wecross.stub.web3.custom.RegisterResourceHandler; +import com.webank.wecross.stub.web3.integration.contract.HelloWorld; +import java.math.BigInteger; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.web3j.crypto.Credentials; +import org.web3j.protocol.Web3j; +import org.web3j.tx.gas.StaticGasProvider; + +public class Web3StubCallContractIntegTest { + private static final Logger logger = LoggerFactory.getLogger(Web3StubCallContractIntegTest.class); + + public static final String CHAINS_PATH = "./chains/web3/"; + public static final String WEB3_WALLET = "Web3Wallet"; + + public final StaticGasProvider staticGasProvider = + new StaticGasProvider(BigInteger.valueOf(22_000_000_000L), BigInteger.valueOf(4_300_000)); + private final RegisterResourceHandler registerResourceHandler = new RegisterResourceHandler(); + + private Driver driver = null; + private Web3Connection connection = null; + private Web3Account account = null; + private BlockManager blockManager = null; + private Map resourceInfoMap = null; + + @Before + public void initializer() throws Exception { + connection = Web3ConnectionFactory.build(CHAINS_PATH, Web3Constant.STUB_TOML_NAME); + connection.setConnectionEventHandler(new ConnectionEventHandlerImplMock()); + + account = Web3AccountFactory.build(WEB3_WALLET, CHAINS_PATH + WEB3_WALLET); + + Web3StubFactory web3StubFactory = new Web3StubFactory(); + driver = web3StubFactory.newDriver(); + + ClientWrapperImpl clientWrapper = (ClientWrapperImpl) connection.getClientWrapper(); + blockManager = new ClientBlockManager(clientWrapper); + + Web3j web3j = clientWrapper.getWeb3j(); + Credentials credentials = account.getCredentials(); + + // deploy HelloWorld + HelloWorld helloWorld = HelloWorld.deploy(web3j, credentials, staticGasProvider).send(); + registerResource(HelloWorld.NAME, helloWorld.getContractAddress(), HelloWorld.ABI); + + resourceInfoMap = + connection.getResourceInfoList().stream() + .collect(Collectors.toMap(ResourceInfo::getName, Function.identity())); + } + + @Test + public void getBlockNumberIntegIntegTest() throws InterruptedException { + AsyncToSync asyncToSync = new AsyncToSync(); + + driver.asyncGetBlockNumber( + connection, + (e, blockNumber) -> { + assertTrue(blockNumber > 0); + asyncToSync.getSemaphore().release(); + }); + + asyncToSync.semaphore.acquire(1); + } + + @Test + public void getBlockHeaderIntegTest() throws InterruptedException { + AsyncToSync asyncToSync = new AsyncToSync(); + + driver.asyncGetBlockNumber( + connection, + (e1, blockNumber) -> { + assertNull(e1); + assertTrue(blockNumber > 0); + + driver.asyncGetBlock( + blockNumber, + false, + connection, + (e2, block) -> { + assertNull(e2); + assertNotNull(block); + BlockHeader blockHeader = block.getBlockHeader(); + assertTrue(block.getRawBytes().length > 1); + assertNotNull(blockHeader); + assertNotNull(blockHeader.getHash()); + assertNotNull(blockHeader.getReceiptRoot()); + assertNotNull(blockHeader.getTransactionRoot()); + assertNotNull(blockHeader.getPrevHash()); + assertNotNull(blockHeader.getStateRoot()); + assertEquals(blockHeader.getNumber(), blockNumber); + asyncToSync.getSemaphore().release(); + }); + }); + asyncToSync.semaphore.acquire(1); + } + + @Test + public void getBlockHeaderFailedIntegTest() throws InterruptedException { + AsyncToSync asyncToSync = new AsyncToSync(); + + driver.asyncGetBlockNumber( + connection, + (e1, blockNumber) -> { + assertNull(e1); + assertTrue(blockNumber > 0); + + driver.asyncGetBlock( + blockNumber + 1000, + true, + connection, + (e2, bytesBlockHeader) -> { + assertNotNull(e2); + assertNull(bytesBlockHeader); + asyncToSync.getSemaphore().release(); + }); + }); + + asyncToSync.semaphore.acquire(1); + } + + @Test + public void getGenesisBlockIntegTest() throws InterruptedException { + AsyncToSync asyncToSync = new AsyncToSync(); + + driver.asyncGetBlock( + 0, + true, + connection, + (e1, block) -> { + assertNull(e1); + assertNotNull(block); + BlockHeader blockHeader = block.getBlockHeader(); + assertTrue(block.getRawBytes().length > 1); + assertNotNull(blockHeader); + assertNotNull(blockHeader.getHash()); + assertEquals(0, blockHeader.getNumber()); + asyncToSync.getSemaphore().release(); + }); + + asyncToSync.semaphore.acquire(1); + } + + @Test + public void callIntegTest() throws Exception { + AsyncToSync asyncToSync = new AsyncToSync(); + + Path path = Path.decode("a.b.HelloWorld"); + TransactionRequest transactionRequest = createTransactionRequest("get", null); + TransactionContext transactionContext = createTransactionContext(path); + driver.asyncCall( + transactionContext, + transactionRequest, + false, + connection, + (e1, transactionResponse) -> { + assertNull(e1); + assertNotNull(transactionResponse); + assertEquals((int) transactionResponse.getErrorCode(), Web3StatusCode.Success); + assertTrue(transactionResponse.getResult().length != 0); + + asyncToSync.getSemaphore().release(); + }); + + asyncToSync.semaphore.acquire(1); + } + + @Test + public void callNotExistMethodIntegTest() throws Exception { + AsyncToSync asyncToSync = new AsyncToSync(); + + Path path = Path.decode("a.b.HelloWorld"); + TransactionRequest transactionRequest = createTransactionRequest("getNotExist", null); + TransactionContext transactionContext = createTransactionContext(path); + driver.asyncCall( + transactionContext, + transactionRequest, + false, + connection, + (e1, transactionResponse) -> { + assertNotNull(e1); + assertNull(transactionResponse); + + asyncToSync.getSemaphore().release(); + }); + + asyncToSync.semaphore.acquire(1); + } + + @Test + public void sendTransactionIntegTest() throws Exception { + AsyncToSync asyncToSync = new AsyncToSync(); + Path path = Path.decode("a.b.HelloWorld"); + String[] params = new String[] {"Hello,ni hao !!!"}; + TransactionRequest transactionRequest = createTransactionRequest("set", params); + TransactionContext transactionContext = createTransactionContext(path); + final String[] hash = {""}; + final long[] blockNumber = {0}; + driver.asyncSendTransaction( + transactionContext, + transactionRequest, + false, + connection, + (e1, transactionResponse) -> { + assertNull(e1); + assertNotNull(transactionResponse); + assertEquals((int) transactionResponse.getErrorCode(), Web3StatusCode.Success); + assertTrue(transactionResponse.getBlockNumber() > 0); + hash[0] = transactionResponse.getHash(); + blockNumber[0] = transactionResponse.getBlockNumber(); + asyncToSync.getSemaphore().release(); + }); + asyncToSync.getSemaphore().acquire(); + + AsyncToSync asyncToSync1 = new AsyncToSync(); + driver.asyncGetTransaction( + hash[0], + blockNumber[0], + blockManager, + false, + connection, + (e2, transaction) -> { + assertNull(e2); + assertNotNull(transaction); + assertTrue(transaction.getReceiptBytes().length > 1); + assertTrue(transaction.getTxBytes().length > 1); + asyncToSync1.getSemaphore().release(); + }); + asyncToSync1.getSemaphore().acquire(); + + AsyncToSync asyncToSync2 = new AsyncToSync(); + TransactionRequest transactionRequest2 = createTransactionRequest("get", null); + driver.asyncCall( + transactionContext, + transactionRequest2, + false, + connection, + (e3, transactionResponse) -> { + assertNull(e3); + assertNotNull(transactionResponse); + assertEquals((int) transactionResponse.getErrorCode(), Web3StatusCode.Success); + assertEquals(transactionResponse.getResult()[0], params[0]); + asyncToSync2.getSemaphore().release(); + }); + asyncToSync2.getSemaphore().acquire(); + } + + @Test + public void sendTransactionNotExistIntegTest() throws Exception { + AsyncToSync asyncToSync = new AsyncToSync(); + Path path = Path.decode("a.b.HelloWorld"); + String[] params = new String[] {"Hello,ni hao !!!"}; + TransactionRequest transactionRequest = createTransactionRequest("setNotExist", params); + TransactionContext transactionContext = createTransactionContext(path); + driver.asyncSendTransaction( + transactionContext, + transactionRequest, + false, + connection, + (e1, transactionResponse) -> { + assertNotNull(e1); + assertTrue(StringUtils.contains(e1.getMessage(), "Invalid method setNotExist")); + asyncToSync.getSemaphore().release(); + }); + asyncToSync.getSemaphore().acquire(); + } + + public TransactionRequest createTransactionRequest(String method, String[] args) { + return new TransactionRequest(method, args); + } + + public TransactionContext createTransactionContext(Path path) { + return new TransactionContext( + account, path, resourceInfoMap.get(path.getResource()), blockManager); + } + + public void registerResource(String resourceName, String contractAddress, String contractABI) { + registerResourceHandler.handle( + null, + new Object[] {resourceName, contractAddress, contractABI}, + null, + null, + connection, + (e1, response) -> { + assertNull(e1); + }); + } +} diff --git a/src/integTest/java/com/webank/wecross/stub/web3/integration/contract/HelloWorld.java b/src/integTest/java/com/webank/wecross/stub/web3/integration/contract/HelloWorld.java new file mode 100644 index 0000000..7bd7c21 --- /dev/null +++ b/src/integTest/java/com/webank/wecross/stub/web3/integration/contract/HelloWorld.java @@ -0,0 +1,161 @@ +package com.webank.wecross.stub.web3.integration.contract; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Collections; +import org.web3j.abi.TypeReference; +import org.web3j.abi.datatypes.Function; +import org.web3j.abi.datatypes.Type; +import org.web3j.abi.datatypes.Utf8String; +import org.web3j.crypto.Credentials; +import org.web3j.protocol.Web3j; +import org.web3j.protocol.core.RemoteCall; +import org.web3j.protocol.core.RemoteFunctionCall; +import org.web3j.protocol.core.methods.response.TransactionReceipt; +import org.web3j.tx.Contract; +import org.web3j.tx.TransactionManager; +import org.web3j.tx.gas.ContractGasProvider; + +/** + * Auto generated code. + * + *

Do not modify! + * + *

Please use the web3j command line tools, + * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the codegen module to update. + * + *

Generated with web3j version 4.10.0. + */ +@SuppressWarnings("rawtypes") +public class HelloWorld extends Contract { + + public static final String NAME = "HelloWorld"; + + public static final String ABI = + "[{\"constant\":false,\"inputs\":[{\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"; + + public static final String BINARY = + "608060405234801561001057600080fd5b5060408051808201909152600d81526c48656c6c6f2c20576f726c642160981b602082015260009061004290826100e7565b506101a6565b634e487b7160e01b600052604160045260246000fd5b600181811c9082168061007257607f821691505b60208210810361009257634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156100e257600081815260208120601f850160051c810160208610156100bf5750805b601f850160051c820191505b818110156100de578281556001016100cb565b5050505b505050565b81516001600160401b0381111561010057610100610048565b6101148161010e845461005e565b84610098565b602080601f83116001811461014957600084156101315750858301515b600019600386901b1c1916600185901b1785556100de565b600085815260208120601f198616915b8281101561017857888601518255948401946001909101908401610159565b50858210156101965787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b6103a4806101b56000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c14610050575b600080fd5b61004e610049366004610126565b61006e565b005b61005861007e565b60405161006591906101d7565b60405180910390f35b600061007a82826102ae565b5050565b60606000805461008d90610225565b80601f01602080910402602001604051908101604052809291908181526020018280546100b990610225565b80156101065780601f106100db57610100808354040283529160200191610106565b820191906000526020600020905b8154815290600101906020018083116100e957829003601f168201915b5050505050905090565b634e487b7160e01b600052604160045260246000fd5b60006020828403121561013857600080fd5b813567ffffffffffffffff8082111561015057600080fd5b818401915084601f83011261016457600080fd5b81358181111561017657610176610110565b604051601f8201601f19908116603f0116810190838211818310171561019e5761019e610110565b816040528281528760208487010111156101b757600080fd5b826020860160208301376000928101602001929092525095945050505050565b600060208083528351808285015260005b81811015610204578581018301518582016040015282016101e8565b506000604082860101526040601f19601f8301168501019250505092915050565b600181811c9082168061023957607f821691505b60208210810361025957634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156102a957600081815260208120601f850160051c810160208610156102865750805b601f850160051c820191505b818110156102a557828155600101610292565b5050505b505050565b815167ffffffffffffffff8111156102c8576102c8610110565b6102dc816102d68454610225565b8461025f565b602080601f83116001811461031157600084156102f95750858301515b600019600386901b1c1916600185901b1785556102a5565b600085815260208120601f198616915b8281101561034057888601518255948401946001909101908401610321565b508582101561035e5787850151600019600388901b60f8161c191681555b5050505050600190811b0190555056fea2646970667358221220b4be7189472dbad3d7eeab9e7d5adb069ffe08198b5ecb42d7d1d8d152e5112f64736f6c63430008130033"; + + public static final String FUNC_GET = "get"; + + public static final String FUNC_SET = "set"; + + @Deprecated + protected HelloWorld( + String contractAddress, + Web3j web3j, + Credentials credentials, + BigInteger gasPrice, + BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + protected HelloWorld( + String contractAddress, + Web3j web3j, + Credentials credentials, + ContractGasProvider contractGasProvider) { + super(BINARY, contractAddress, web3j, credentials, contractGasProvider); + } + + @Deprecated + protected HelloWorld( + String contractAddress, + Web3j web3j, + TransactionManager transactionManager, + BigInteger gasPrice, + BigInteger gasLimit) { + super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } + + protected HelloWorld( + String contractAddress, + Web3j web3j, + TransactionManager transactionManager, + ContractGasProvider contractGasProvider) { + super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider); + } + + public RemoteFunctionCall get() { + final Function function = + new Function( + FUNC_GET, + Arrays.asList(), + Arrays.>asList(new TypeReference() {})); + return executeRemoteCallSingleValueReturn(function, String.class); + } + + public RemoteFunctionCall set(String n) { + final Function function = + new Function( + FUNC_SET, + Arrays.asList(new Utf8String(n)), + Collections.>emptyList()); + return executeRemoteCallTransaction(function); + } + + @Deprecated + public static HelloWorld load( + String contractAddress, + Web3j web3j, + Credentials credentials, + BigInteger gasPrice, + BigInteger gasLimit) { + return new HelloWorld(contractAddress, web3j, credentials, gasPrice, gasLimit); + } + + @Deprecated + public static HelloWorld load( + String contractAddress, + Web3j web3j, + TransactionManager transactionManager, + BigInteger gasPrice, + BigInteger gasLimit) { + return new HelloWorld(contractAddress, web3j, transactionManager, gasPrice, gasLimit); + } + + public static HelloWorld load( + String contractAddress, + Web3j web3j, + Credentials credentials, + ContractGasProvider contractGasProvider) { + return new HelloWorld(contractAddress, web3j, credentials, contractGasProvider); + } + + public static HelloWorld load( + String contractAddress, + Web3j web3j, + TransactionManager transactionManager, + ContractGasProvider contractGasProvider) { + return new HelloWorld(contractAddress, web3j, transactionManager, contractGasProvider); + } + + public static RemoteCall deploy( + Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) { + return deployRemoteCall(HelloWorld.class, web3j, credentials, contractGasProvider, BINARY, ""); + } + + public static RemoteCall deploy( + Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) { + return deployRemoteCall( + HelloWorld.class, web3j, transactionManager, contractGasProvider, BINARY, ""); + } + + @Deprecated + public static RemoteCall deploy( + Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { + return deployRemoteCall(HelloWorld.class, web3j, credentials, gasPrice, gasLimit, BINARY, ""); + } + + @Deprecated + public static RemoteCall deploy( + Web3j web3j, + TransactionManager transactionManager, + BigInteger gasPrice, + BigInteger gasLimit) { + return deployRemoteCall( + HelloWorld.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, ""); + } +} diff --git a/src/main/java/com/webank/wecross/stub/web3/Web3Driver.java b/src/main/java/com/webank/wecross/stub/web3/Web3Driver.java index 362610c..4a958e1 100644 --- a/src/main/java/com/webank/wecross/stub/web3/Web3Driver.java +++ b/src/main/java/com/webank/wecross/stub/web3/Web3Driver.java @@ -175,25 +175,29 @@ public void asyncCall( return; } TransactionResponse transactionResponse = new TransactionResponse(); - EthCall ethCall = objectMapper.readValue(response.getData(), EthCall.class); - ContractABIDefinition contractABIDefinition = - abiDefinitionFactory.loadABI(contractAbi); - ABIDefinition abiDefinition = - contractABIDefinition.getFunctions().get(method).stream() - .filter( - function -> - function.getInputs().size() == (args == null ? 0 : args.length)) - .findFirst() - .orElseThrow( - () -> - new Web3StubException( - Web3StatusCode.MethodNotExist, "method not exist: " + method)); - ABIObject outputObject = ABIObjectFactory.createOutputObject(abiDefinition); transactionResponse.setErrorCode(Web3StatusCode.Success); transactionResponse.setMessage( Web3StatusCode.getStatusMessage(Web3StatusCode.Success)); - transactionResponse.setResult( - codecJsonWrapper.decode(outputObject, ethCall.getValue()).toArray(new String[0])); + EthCall ethCall = objectMapper.readValue(response.getData(), EthCall.class); + String ethCallValue = ethCall.getValue(); + if (StringUtils.isNotBlank(ethCallValue)) { + ContractABIDefinition contractABIDefinition = + abiDefinitionFactory.loadABI(contractAbi); + ABIDefinition abiDefinition = + contractABIDefinition.getFunctions().get(method).stream() + .filter( + function -> + function.getInputs().size() == (args == null ? 0 : args.length)) + .findFirst() + .orElseThrow( + () -> + new Web3StubException( + Web3StatusCode.MethodNotExist, "method not exist: " + method)); + ABIObject outputObject = ABIObjectFactory.createOutputObject(abiDefinition); + + transactionResponse.setResult( + codecJsonWrapper.decode(outputObject, ethCallValue).toArray(new String[0])); + } callback.onTransactionResponse(null, transactionResponse); } catch (Exception e) { logger.warn(" e: ", e); diff --git a/src/main/java/com/webank/wecross/stub/web3/client/ClientBlockManager.java b/src/main/java/com/webank/wecross/stub/web3/client/ClientBlockManager.java new file mode 100644 index 0000000..9736439 --- /dev/null +++ b/src/main/java/com/webank/wecross/stub/web3/client/ClientBlockManager.java @@ -0,0 +1,51 @@ +package com.webank.wecross.stub.web3.client; + +import com.webank.wecross.stub.Block; +import com.webank.wecross.stub.BlockManager; +import com.webank.wecross.stub.web3.contract.BlockUtility; +import java.io.IOException; +import java.math.BigInteger; +import org.web3j.protocol.core.methods.response.EthBlock; + +public class ClientBlockManager implements BlockManager { + private final ClientWrapper clientWrapper; + + public ClientBlockManager(ClientWrapper clientWrapper) { + this.clientWrapper = clientWrapper; + } + + public long getBlockNumber() throws IOException { + BigInteger blockNumber = clientWrapper.ethBlockNumber(); + return blockNumber.longValue(); + } + + public Block getBlock(long blockNumber) throws IOException { + EthBlock.Block block = clientWrapper.ethGetBlockByNumber(BigInteger.valueOf(blockNumber)); + return BlockUtility.convertToBlock(block, false); + } + + @Override + public void start() {} + + @Override + public void stop() {} + + @Override + public void asyncGetBlockNumber(GetBlockNumberCallback callback) { + try { + callback.onResponse(null, getBlockNumber()); + } catch (Exception e) { + callback.onResponse(e, -1); + } + } + + @Override + public void asyncGetBlock(long blockNumber, GetBlockCallback callback) { + try { + Block block = getBlock(blockNumber); + callback.onResponse(null, block); + } catch (IOException e) { + callback.onResponse(e, null); + } + } +} diff --git a/src/main/java/com/webank/wecross/stub/web3/client/ClientWrapperImpl.java b/src/main/java/com/webank/wecross/stub/web3/client/ClientWrapperImpl.java index f993647..b1716ac 100644 --- a/src/main/java/com/webank/wecross/stub/web3/client/ClientWrapperImpl.java +++ b/src/main/java/com/webank/wecross/stub/web3/client/ClientWrapperImpl.java @@ -170,4 +170,8 @@ private Optional sendTransactionReceiptRequest(String transa web3j.ethGetTransactionReceipt(transactionHash).sendAsync().get(); return transactionReceipt.getTransactionReceipt(); } + + public Web3j getWeb3j() { + return web3j; + } } diff --git a/src/main/java/com/webank/wecross/stub/web3/config/Web3StubConfig.java b/src/main/java/com/webank/wecross/stub/web3/config/Web3StubConfig.java index f2b272b..dd8cdcc 100644 --- a/src/main/java/com/webank/wecross/stub/web3/config/Web3StubConfig.java +++ b/src/main/java/com/webank/wecross/stub/web3/config/Web3StubConfig.java @@ -67,6 +67,11 @@ public String getUrl() { public void setUrl(String url) { this.url = url; } + + @Override + public String toString() { + return "Service{" + "url='" + url + '\'' + '}'; + } } public static class Resource { diff --git a/src/integTest/resources/WeCrossHub.sol b/src/main/resources/WeCrossHub.sol similarity index 100% rename from src/integTest/resources/WeCrossHub.sol rename to src/main/resources/WeCrossHub.sol diff --git a/src/integTest/resources/WeCrossProxy.sol b/src/main/resources/WeCrossProxy.sol similarity index 100% rename from src/integTest/resources/WeCrossProxy.sol rename to src/main/resources/WeCrossProxy.sol diff --git a/src/test/java/com/webank/wecross/stub/web3/Web3ConnectionTest.java b/src/test/java/com/webank/wecross/stub/web3/Web3ConnectionTest.java index b67c7d8..52b19e3 100644 --- a/src/test/java/com/webank/wecross/stub/web3/Web3ConnectionTest.java +++ b/src/test/java/com/webank/wecross/stub/web3/Web3ConnectionTest.java @@ -2,6 +2,7 @@ import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNull; import static junit.framework.TestCase.assertTrue; import com.fasterxml.jackson.core.JsonProcessingException; @@ -216,7 +217,7 @@ public void handleCallTest() throws JsonProcessingException { } assertTrue(Objects.nonNull(ethCall)); assertFalse(ethCall.hasError()); - assertEquals(data, ethCall.getResult()); + assertNull(ethCall.getResult()); }); } diff --git a/src/test/java/com/webank/wecross/stub/web3/Web3DriverTest.java b/src/test/java/com/webank/wecross/stub/web3/Web3DriverTest.java index eb0bff4..87d72f3 100644 --- a/src/test/java/com/webank/wecross/stub/web3/Web3DriverTest.java +++ b/src/test/java/com/webank/wecross/stub/web3/Web3DriverTest.java @@ -22,6 +22,7 @@ import com.webank.wecross.stub.web3.client.ClientWrapperWithNullMock; import com.webank.wecross.stub.web3.common.ObjectMapperFactory; import com.webank.wecross.stub.web3.common.Web3RequestType; +import com.webank.wecross.stub.web3.common.Web3StatusCode; import com.webank.wecross.stub.web3.config.Web3StubConfig; import com.webank.wecross.stub.web3.config.Web3StubConfigParser; import com.webank.wecross.stub.web3.protocol.request.TransactionParams; @@ -51,9 +52,9 @@ public class Web3DriverTest { @Before public void initializer() throws Exception { - String web3StubConfigPath = "classpath:./"; + String web3StubConfigPath = "./"; abiCodec = new ABICodec(new CryptoSuite(CryptoType.ECDSA_TYPE), true); - account = Web3AccountFactory.build("web3", "classpath:/account"); + account = Web3AccountFactory.build("wallet", "./account"); Web3StubConfig web3StubConfig = new Web3StubConfigParser(web3StubConfigPath, "stub-sample.toml").loadConfig(); connection = new Web3Connection(new ClientWrapperImplMock(), web3StubConfigPath); @@ -105,7 +106,10 @@ public void getBlockNumberNullTest() { request.setType(Web3RequestType.GET_BLOCK_NUMBER); driver.asyncGetBlockNumber( nonExistConnection, - (e, blockNumber) -> assertEquals(e.getMessage(), "block number not exist")); + (e, blockNumber) -> + assertEquals( + e.getMessage(), + Web3StatusCode.getStatusMessage(Web3StatusCode.BlockNumberNotExist))); } @Test @@ -171,7 +175,7 @@ public void getBlockHeaderTest() { @Test public void callTest() { - TransactionRequest transactionRequest = new TransactionRequest("set", new String[] {"0"}); + TransactionRequest transactionRequest = new TransactionRequest("get", new String[] {}); driver.asyncCall( transactionContext, transactionRequest, @@ -186,7 +190,7 @@ public void callTest() { @Test public void callExceptionTest() { - TransactionRequest transactionRequest = new TransactionRequest("set", new String[] {"0"}); + TransactionRequest transactionRequest = new TransactionRequest("get", new String[] {}); driver.asyncCall( transactionContext, transactionRequest, @@ -200,7 +204,7 @@ public void callExceptionTest() { @Test public void callNullTest() { - TransactionRequest transactionRequest = new TransactionRequest("set", new String[] {"a"}); + TransactionRequest transactionRequest = new TransactionRequest("get", new String[] {}); driver.asyncCall( transactionContext, transactionRequest, @@ -214,7 +218,7 @@ public void callNullTest() { @Test public void callRevertTest() { - TransactionRequest transactionRequest = new TransactionRequest("set", new String[] {"a"}); + TransactionRequest transactionRequest = new TransactionRequest("get", new String[] {}); driver.asyncCall( transactionContext, transactionRequest, diff --git a/src/test/java/com/webank/wecross/stub/web3/client/ClientWrapperImplMock.java b/src/test/java/com/webank/wecross/stub/web3/client/ClientWrapperImplMock.java index 0777f85..6bccffe 100644 --- a/src/test/java/com/webank/wecross/stub/web3/client/ClientWrapperImplMock.java +++ b/src/test/java/com/webank/wecross/stub/web3/client/ClientWrapperImplMock.java @@ -63,7 +63,6 @@ public EthCall ethCall(org.web3j.protocol.core.methods.request.Transaction trans EthCall ethCall = new EthCall(); ethCall.setId(1); ethCall.setJsonrpc("2.0"); - ethCall.setResult(transaction.getData()); return ethCall; } diff --git a/src/test/java/com/webank/wecross/stub/web3/uaproof/SignerTest.java b/src/test/java/com/webank/wecross/stub/web3/uaproof/SignerTest.java index c6327f3..fc5b3ef 100644 --- a/src/test/java/com/webank/wecross/stub/web3/uaproof/SignerTest.java +++ b/src/test/java/com/webank/wecross/stub/web3/uaproof/SignerTest.java @@ -1,15 +1,22 @@ package com.webank.wecross.stub.web3.uaproof; +import static junit.framework.TestCase.assertNotNull; import static junit.framework.TestCase.assertTrue; +import com.webank.wecross.stub.web3.account.Web3Account; +import com.webank.wecross.stub.web3.account.Web3AccountFactory; +import java.io.IOException; import org.junit.Test; +import org.web3j.crypto.CipherException; import org.web3j.crypto.Credentials; public class SignerTest { @Test - public void signerTest() { - Credentials credentials = Credentials.create("0xafF0CA253b97e54440965855cec0A8a2E2399896"); + public void signerTest() throws CipherException, IOException { + Web3Account web3Account = Web3AccountFactory.build("wallet", "./account"); + assertNotNull(web3Account); + Credentials credentials = web3Account.getCredentials(); String message = "{\"proof\":null,\"uaPub\":\"5d614e4cc8dcb73b4326a933a8236bafc36c3219f1f22cac257b755e1d7ecff2ab5f2842b434cacee34d0a79a701b5f4c5c850c43e8f5c5e06af8dcdcfc53c8f\",\"caPub\":\"5d614e4cc8dcb73b4326a933a8236bafc36c3219f1f22cac257b755e1d7ecff2ab5f2842b434cacee34d0a79a701b5f4c5c850c43e8f5c5e06af8dcdcfc53c8f\",\"uaSig\":\"f4df220abbf913abd6228dd31888c1a14a716a64d42186d975f6ae23f23ce279526675411e8a1cb257f358eabf4ffc99e6dfc78c36f9feceaae3de5946e24b5e01\",\"caSig\":\"f4df220abbf913abd6228dd31888c1a14a716a64d42186d975f6ae23f23ce279526675411e8a1cb257f358eabf4ffc99e6dfc78c36f9feceaae3de5946e24b5e01\",\"timestamp\":1600866010281}"; byte[] signData = Signer.sign(credentials.getEcKeyPair(), message.getBytes());