|
1 | 1 | package org.fisco.bcos.sdk.test.abi;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.math.BigInteger;
|
4 | 5 | import java.util.ArrayList;
|
5 | 6 | import java.util.List;
|
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
6 | 11 | import org.fisco.bcos.sdk.abi.ABICodec;
|
7 | 12 | import org.fisco.bcos.sdk.abi.ABICodecException;
|
8 | 13 | import org.fisco.bcos.sdk.abi.wrapper.ABICodecObject;
|
9 | 14 | import org.fisco.bcos.sdk.abi.wrapper.ABIDefinition;
|
| 15 | +import org.fisco.bcos.sdk.abi.wrapper.ABIDefinitionFactory; |
10 | 16 | import org.fisco.bcos.sdk.abi.wrapper.ABIObject;
|
11 | 17 | import org.fisco.bcos.sdk.abi.wrapper.ABIObjectFactory;
|
12 | 18 | import org.fisco.bcos.sdk.abi.wrapper.ContractABIDefinition;
|
| 19 | +import org.fisco.bcos.sdk.crypto.CryptoSuite; |
| 20 | +import org.fisco.bcos.sdk.model.CryptoType; |
| 21 | +import org.fisco.bcos.sdk.utils.ObjectMapperFactory; |
13 | 22 | import org.junit.Assert;
|
14 | 23 | import org.junit.Test;
|
15 | 24 |
|
@@ -227,9 +236,13 @@ public class ABICodecTest {
|
227 | 236 | + " \"payable\": false,\n"
|
228 | 237 | + " \"stateMutability\": \"nonpayable\",\n"
|
229 | 238 | + " \"type\": \"fallback\"\n"
|
| 239 | + + " },\n" |
| 240 | + + " {\n" |
| 241 | + + " \"payable\": false,\n" |
| 242 | + + " \"stateMutability\": \"payable\",\n" |
| 243 | + + " \"type\": \"receive\"\n" |
230 | 244 | + " }\n"
|
231 | 245 | + "]";
|
232 |
| - |
233 | 246 | // int a, Info[] memory b, string memory c
|
234 | 247 | /*
|
235 | 248 | * {
|
@@ -291,7 +304,40 @@ function test(int a, Info[] memory b, string memory c) public returns(int) {
|
291 | 304 | private String encodedWithMethodId = "0x00a3c75d" + encoded;
|
292 | 305 |
|
293 | 306 | @Test
|
294 |
| - public void testEncodeFromString() { |
| 307 | + public void testEncodeFromString() throws IOException { |
| 308 | + CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE); |
| 309 | + ABIDefinitionFactory abiDefinitionFactory = new ABIDefinitionFactory(cryptoSuite); |
| 310 | + ContractABIDefinition abiDefinition = abiDefinitionFactory.loadABI(abiDesc); |
| 311 | + // check the fallback function |
| 312 | + Assert.assertTrue(abiDefinition.getFallbackFunction() != null); |
| 313 | + // check the content of the fallback function |
| 314 | + Assert.assertTrue(abiDefinition.getFallbackFunction().getStateMutability().equals( "nonpayable")); |
| 315 | + Assert.assertTrue(abiDefinition.getFallbackFunction().isPayable() == false); |
| 316 | + Assert.assertTrue(abiDefinition.getFallbackFunction().getType().equals("fallback")); |
| 317 | + |
| 318 | + // check receive functions |
| 319 | + Assert.assertTrue(abiDefinition.getReceiveFunction() != null); |
| 320 | + Assert.assertTrue(abiDefinition.getReceiveFunction().getType().equals("receive")); |
| 321 | + |
| 322 | + // check serialization and deserialization |
| 323 | + ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(); |
| 324 | + byte[] encodedABIDefinitions = objectMapper.writeValueAsBytes(abiDefinition.getFallbackFunction()); |
| 325 | + // decode |
| 326 | + ABIDefinition decodedABIDefinition = objectMapper.readValue(encodedABIDefinitions, ABIDefinition.class); |
| 327 | + Assert.assertTrue(decodedABIDefinition.getStateMutability().equals( "nonpayable")); |
| 328 | + Assert.assertTrue(decodedABIDefinition.isPayable() == false); |
| 329 | + Assert.assertTrue(decodedABIDefinition.getType().equals("fallback")); |
| 330 | + // test encode/decode for all the functions |
| 331 | + for(String key : abiDefinition.getFunctions().keySet()) |
| 332 | + { |
| 333 | + List<ABIDefinition> abiDefinitions = abiDefinition.getFunctions().get(key); |
| 334 | + for(int i = 0; i < abiDefinitions.size(); i++) { |
| 335 | + ABIDefinition functionAbiDefinition = abiDefinitions.get(i); |
| 336 | + encodedABIDefinitions = objectMapper.writeValueAsBytes(functionAbiDefinition); |
| 337 | + decodedABIDefinition = objectMapper.readValue(encodedABIDefinitions, ABIDefinition.class); |
| 338 | + } |
| 339 | + } |
| 340 | + |
295 | 341 | List<String> args = new ArrayList<String>();
|
296 | 342 | args.add("100");
|
297 | 343 | // [{"name": "Hello world!", "count": 100, "items": [{"a": 1, "b": 2, "c": 3}]}, {"name":
|
@@ -401,9 +447,21 @@ public void testEncodeByInterface() {
|
401 | 447 | argsObjects.add(a);
|
402 | 448 | try {
|
403 | 449 | String s1 = abiCodec.encodeMethodByInterface("call(uint256[2],uint256[],bytes,address)", argsObjects);
|
404 |
| - String abi = "[{\"constant\":false,\"inputs\":[{\"name\":\"u1\",\"type\":\"uint256[2]\"},{\"name\":\"u2\",\"type\":\"uint256[]\"},{\"name\":\"b\",\"type\":\"bytes\"},{\"name\":\"a\",\"type\":\"address\"}],\"name\":\"call\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"u\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"a\",\"type\":\"uint256\"},{\"name\":\"s\",\"type\":\"string\"}],\"name\":\"add\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd1\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd2\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"u\",\"type\":\"uint256\"},{\"indexed\":true,\"name\":\"a\",\"type\":\"uint256\"},{\"indexed\":true,\"name\":\"s\",\"type\":\"string\"}],\"name\":\"LogAdd3\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd4\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd5\",\"type\":\"event\"}]"; |
| 450 | + String abi = "[{\"inputs\":[{\"name\":\"u1\",\"type\":\"uint256[2]\"},{\"name\":\"u2\",\"type\":\"uint256[]\"},{\"name\":\"b\",\"type\":\"bytes\"},{\"name\":\"a\",\"type\":\"address\"}],\"name\":\"call\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"u\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"a\",\"type\":\"uint256\"},{\"name\":\"s\",\"type\":\"string\"}],\"name\":\"add\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd1\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"u\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd2\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"u\",\"type\":\"uint256\"},{\"indexed\":true,\"name\":\"a\",\"type\":\"uint256\"},{\"indexed\":true,\"name\":\"s\",\"type\":\"string\"}],\"name\":\"LogAdd3\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd4\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"LogAdd5\",\"type\":\"event\"}]"; |
405 | 451 | String s2 = abiCodec.encodeMethod(abi, "call", argsObjects);
|
406 | 452 | Assert.assertEquals(s1, s2);
|
| 453 | + // test ABIDefinition |
| 454 | + // check constant |
| 455 | + CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE); |
| 456 | + ABIDefinitionFactory abiDefinitionFactory = new ABIDefinitionFactory(cryptoSuite); |
| 457 | + ContractABIDefinition abiDefinition = abiDefinitionFactory.loadABI(abi); |
| 458 | + Map<String, List<ABIDefinition>> functions = abiDefinition.getFunctions(); |
| 459 | + Assert.assertTrue(functions.containsKey("call")); |
| 460 | + List<ABIDefinition> callABIDefinition = functions.get("call"); |
| 461 | + Assert.assertEquals(1, callABIDefinition.size()); |
| 462 | + Assert.assertTrue(callABIDefinition.get(0).isConstant()); |
| 463 | + // check without |
| 464 | + |
407 | 465 | } catch (ABICodecException e) {
|
408 | 466 | Assert.fail(e.getMessage());
|
409 | 467 | }
|
|
0 commit comments