Skip to content

Commit 994da2e

Browse files
committed
<fix>(transaction): fix assemble event decode error.
1 parent c28be9c commit 994da2e

File tree

13 files changed

+815
-12
lines changed

13 files changed

+815
-12
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ src/integration-wasm-test/resources/log**
2525
build_chain.sh
2626
account/
2727
conf/
28-
bin/
2928
gmcert.cnf
3029
gmsm2.param
3130
integrationTestEnv.sh

src/integration-test/java/org/fisco/bcos/sdk/v3/test/contract/solidity/EventSubDemo.java

Lines changed: 609 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.fisco.bcos.sdk.v3.test.transaction.codec;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.checkerframework.checker.units.qual.C;
5+
import org.fisco.bcos.sdk.v3.BcosSDK;
6+
import org.fisco.bcos.sdk.v3.client.Client;
7+
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
8+
import org.fisco.bcos.sdk.v3.model.ConstantConfig;
9+
import org.fisco.bcos.sdk.v3.model.CryptoType;
10+
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
11+
import org.fisco.bcos.sdk.v3.test.contract.solidity.EventSubDemo;
12+
import org.fisco.bcos.sdk.v3.utils.Hex;
13+
import org.junit.Assert;
14+
import org.junit.Test;
15+
16+
import java.math.BigInteger;
17+
import java.util.List;
18+
19+
public class EvenDemoTest {
20+
private static final String CONFIG_FILE =
21+
"src/integration-test/resources/" + ConstantConfig.CONFIG_FILE_NAME;
22+
private final Client client;
23+
24+
public EvenDemoTest() {
25+
// init the sdk, and set the config options.
26+
BcosSDK sdk = BcosSDK.build(CONFIG_FILE);
27+
client = sdk.getClient("group0");
28+
}
29+
30+
@Test
31+
public void testEvent() throws Exception {
32+
33+
EventSubDemo eventSubDemo = EventSubDemo.deploy(client, client.getCryptoSuite().getCryptoKeyPair());
34+
Assert.assertEquals(eventSubDemo.getDeployReceipt().getStatus(), 0);
35+
Assert.assertTrue(StringUtils.isNotBlank(eventSubDemo.getContractAddress()));
36+
37+
// transfer
38+
{
39+
TransactionReceipt transfer = eventSubDemo.transfer("test1", "test2", BigInteger.valueOf(100));
40+
Assert.assertEquals(transfer.getStatus(), 0);
41+
Assert.assertEquals(transfer.getLogEntries().size(), 4);
42+
List<EventSubDemo.TransferDataEventResponse> transferDataEvents = eventSubDemo.getTransferDataEvents(transfer);
43+
EventSubDemo.TransferDataEventResponse transferDataEventResponse = transferDataEvents.get(0);
44+
EventSubDemo.TransactionData transactionData = transferDataEventResponse.transaction_data.getValue().get(0);
45+
Assert.assertEquals(transactionData.from_account, "test1");
46+
Assert.assertEquals(transactionData.to_account, "test2");
47+
Assert.assertEquals(transactionData.amount, BigInteger.valueOf(100));
48+
}
49+
50+
// echo
51+
{
52+
TransactionReceipt echo = eventSubDemo.echo(BigInteger.valueOf(100), BigInteger.valueOf(-100), "test");
53+
Assert.assertEquals(echo.getStatus(), 0);
54+
Assert.assertEquals(echo.getLogEntries().size(), 4);
55+
EventSubDemo.EchoUint256EventResponse echoUint256EventResponse = eventSubDemo.getEchoUint256Events(echo).get(0);
56+
Assert.assertEquals(echoUint256EventResponse.u, BigInteger.valueOf(100));
57+
EventSubDemo.EchoInt256EventResponse echoInt256EventResponse = eventSubDemo.getEchoInt256Events(echo).get(0);
58+
Assert.assertEquals(echoInt256EventResponse.i, BigInteger.valueOf(-100));
59+
EventSubDemo.EchoStringEventResponse echoStringEventResponse = eventSubDemo.getEchoStringEvents(echo).get(0);
60+
Assert.assertEquals(Hex.toHexString(echoStringEventResponse.s), client.getCryptoSuite().hash("test"));
61+
62+
EventSubDemo.EchoUint256Int256StringEventResponse echoUint256Int256StringEventResponse = eventSubDemo.getEchoUint256Int256StringEvents(echo).get(0);
63+
Assert.assertEquals(echoUint256Int256StringEventResponse.u, BigInteger.valueOf(100));
64+
Assert.assertEquals(echoUint256Int256StringEventResponse.i, BigInteger.valueOf(-100));
65+
Assert.assertEquals(Hex.toHexString(echoUint256Int256StringEventResponse.s), client.getCryptoSuite().hash("test"));
66+
}
67+
}
68+
}

src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/manager/AssembleTransactionProcessorTest.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
package org.fisco.bcos.sdk.v3.test.transaction.manager;
1616

1717
import com.google.common.collect.Lists;
18+
1819
import java.math.BigInteger;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.Base64;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.concurrent.CompletableFuture;
26+
2527
import org.apache.commons.collections4.ListUtils;
2628
import org.apache.commons.lang3.StringUtils;
2729
import org.fisco.bcos.sdk.jni.utilities.tx.TxPair;
@@ -93,8 +95,8 @@ public void test1HelloWorld() throws Exception {
9395
Assert.assertTrue(
9496
StringUtils.isNotBlank(response.getContractAddress())
9597
&& !StringUtils.equalsIgnoreCase(
96-
helloWorldAddress,
97-
"0x0000000000000000000000000000000000000000000000000000000000000000"));
98+
helloWorldAddress,
99+
"0x0000000000000000000000000000000000000000000000000000000000000000"));
98100
// test call, which would be queried off-chain.
99101
CallResponse callResponse1 =
100102
transactionProcessor.sendCallByContractLoader(
@@ -196,8 +198,8 @@ public void test2ComplexDeploy() throws Exception {
196198
Assert.assertTrue(
197199
StringUtils.isNotBlank(response.getContractAddress())
198200
&& !StringUtils.equalsIgnoreCase(
199-
contractAddress,
200-
"0x0000000000000000000000000000000000000000000000000000000000000000"));
201+
contractAddress,
202+
"0x0000000000000000000000000000000000000000000000000000000000000000"));
201203
Map<String, List<List<Object>>> map = response.getEventResultMap();
202204
Assert.assertEquals("test2", map.get("LogInit").get(0).get(1));
203205
}
@@ -481,4 +483,53 @@ public void test9ComplexSetStaticBytes() throws Exception {
481483
"12345678");
482484
}
483485
}
486+
487+
@Test
488+
public void test10EventDemo() throws Exception {
489+
AssembleTransactionProcessor transactionProcessor =
490+
TransactionProcessorFactory.createAssembleTransactionProcessor(
491+
this.client, this.cryptoKeyPair, ABI_FILE, BIN_FILE);
492+
String contractAddress = null;
493+
// deploy
494+
{
495+
List<Object> params = Lists.newArrayList();
496+
TransactionResponse response =
497+
transactionProcessor.deployByContractLoader("EventSubDemo", params);
498+
Assert.assertEquals(response.getTransactionReceipt().getStatus(), 0);
499+
contractAddress = response.getContractAddress();
500+
Assert.assertTrue(contractAddress != null && !contractAddress.isEmpty());
501+
}
502+
503+
// transfer
504+
{
505+
List<Object> params = new ArrayList<>();
506+
params.add("test1");
507+
params.add("test2");
508+
params.add(BigInteger.valueOf(10));
509+
TransactionResponse transactionResponse3 =
510+
transactionProcessor.sendTransactionAndGetResponseByContractLoader(
511+
"EventSubDemo", contractAddress, "transfer", params);
512+
Assert.assertEquals(transactionResponse3.getReturnCode(), 0);
513+
Assert.assertEquals(transactionResponse3.getEventResultMap().size(), 4);
514+
List<Object> transferData = transactionResponse3.getEventResultMap().get("TransferData").get(0);
515+
List<List<Object>> result = (List<List<Object>>) transferData.get(0);
516+
Assert.assertEquals(result.get(0).get(0), "test1");
517+
Assert.assertEquals(result.get(0).get(1), "test2");
518+
Assert.assertEquals(result.get(0).get(2), 10);
519+
}
520+
521+
// echo
522+
{
523+
List<Object> params = new ArrayList<>();
524+
params.add(BigInteger.valueOf(100));
525+
params.add(BigInteger.valueOf(-100));
526+
params.add("test");
527+
TransactionResponse transactionResponse3 =
528+
transactionProcessor.sendTransactionAndGetResponseByContractLoader(
529+
"EventSubDemo", contractAddress, "echo", params);
530+
Assert.assertEquals(transactionResponse3.getReturnCode(), 0);
531+
Assert.assertEquals(transactionResponse3.getEventResultMap().size(), 1);
532+
Assert.assertEquals(transactionResponse3.getEventResultMap().get("Echo").size(), 4);
533+
}
534+
}
484535
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"u","type":"uint256"}],"name":"Echo","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"int256","name":"i","type":"int256"}],"name":"Echo","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"string","name":"s","type":"string"}],"name":"Echo","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"u","type":"uint256"},{"indexed":true,"internalType":"int256","name":"i","type":"int256"},{"indexed":true,"internalType":"string","name":"s","type":"string"}],"name":"Echo","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"bsn","type":"bytes32"}],"name":"Echo","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes","name":"bs","type":"bytes"}],"name":"Echo","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"bsn","type":"bytes32"},{"indexed":true,"internalType":"bytes","name":"bs","type":"bytes"}],"name":"Echo","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"from_account","type":"string"},{"indexed":false,"internalType":"string","name":"to_account","type":"string"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"string","name":"from_account","type":"string"},{"indexed":true,"internalType":"string","name":"to_account","type":"string"}],"name":"TransferAccount","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"TransferAmount","type":"event"},{"anonymous":false,"inputs":[{"components":[{"internalType":"string","name":"from_account","type":"string"},{"internalType":"string","name":"to_account","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"}],"indexed":false,"internalType":"struct EventSubDemo.TransactionData[]","name":"transaction_data","type":"tuple[]"}],"name":"TransferData","type":"event"},{"inputs":[{"internalType":"bytes32","name":"bsn","type":"bytes32"},{"internalType":"bytes","name":"bs","type":"bytes"}],"name":"echo","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"},{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"u","type":"uint256"},{"internalType":"int256","name":"i","type":"int256"},{"internalType":"string","name":"s","type":"string"}],"name":"echo","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"int256","name":"","type":"int256"},{"internalType":"string","name":"","type":"string"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"from_account","type":"string"},{"internalType":"string","name":"to_account","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
608060405234801561001057600080fd5b50610766806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063274b9f101461004657806375a71ca0146100705780639b80b05014610092575b600080fd5b610059610054366004610453565b6100a7565b60405161006792919061050a565b60405180910390f35b61008361007e366004610552565b61015e565b604051610067939291906105a2565b6100a56100a03660046105ca565b610247565b005b60006060837f347ac5ea9ec0d19b3b6ad2651257cc684ecca0e41558fc1c578516e90bcf45f260405160405180910390a2826040516100e69190610637565b604051908190038120907f5f886b86d4364df6c5d7d9a65705aac01b180e2a136442a9921860ca0fdf49db90600090a2826040516101249190610637565b6040519081900381209085907f3752a357a949d58944fee07631779eb98f8c1ba788096770cc9cbc014e2375b890600090a3509192909150565b6000806060857f34d6d9becd7a327109612b0e636ca3bea6263a273c0256df42fbdf3d92e467f960405160405180910390a260405185907f335e9c894374ff443b4a42deadc7dce6dba3b921062aef988a13ed1fba42034390600090a2836040516101c99190610637565b604051908190038120907fdb84d7c006c4de68f9c0bd50b8b81ed31f29ebeec325c872d36445c6565d757c90600090a2836040516102079190610637565b60405190819003812090869088907f6a69e35d4db25f48425c10a32d6e2553fdc3af0e5096cb309b0049c2235e197990600090a450939492935090919050565b604080516001808252818301909252600091816020015b61028260405180606001604052806060815260200160608152602001600081525090565b81526020019060019003908161025e579050509050604051806060016040528085815260200184815260200183815250816000815181106102c5576102c5610653565b60200260200101819052507f6e8b295299c2e7d6f65fcccc3b54ca076974ed2db26b305fe48716ec468a4ee6816040516102ff9190610669565b60405180910390a17f5358be4df107be4d9b023fc323f41d7109610225c6ef211b9d375b9fbd7ccc4f84848460405161033a939291906106fa565b60405180910390a1826040516103509190610637565b6040518091039020846040516103669190610637565b604051908190038120907f8c21d0fee2cb98bb839d8a17a9fe8e11839e85be0675622169aca05cda58260890600090a360405182907f8f922f97953a95596e8c51012daa271d3dd61455382767329dec1c997ed1fbf190600090a250505050565b634e487b7160e01b600052604160045260246000fd5b600067ffffffffffffffff808411156103f8576103f86103c7565b604051601f8501601f19908116603f01168101908282118183101715610420576104206103c7565b8160405280935085815286868601111561043957600080fd5b858560208301376000602087830101525050509392505050565b6000806040838503121561046657600080fd5b82359150602083013567ffffffffffffffff81111561048457600080fd5b8301601f8101851361049557600080fd5b6104a4858235602084016103dd565b9150509250929050565b60005b838110156104c95781810151838201526020016104b1565b838111156104d8576000848401525b50505050565b600081518084526104f68160208601602086016104ae565b601f01601f19169290920160200192915050565b82815260406020820152600061052360408301846104de565b949350505050565b600082601f83011261053c57600080fd5b61054b838335602085016103dd565b9392505050565b60008060006060848603121561056757600080fd5b8335925060208401359150604084013567ffffffffffffffff81111561058c57600080fd5b6105988682870161052b565b9150509250925092565b8381528260208201526060604082015260006105c160608301846104de565b95945050505050565b6000806000606084860312156105df57600080fd5b833567ffffffffffffffff808211156105f757600080fd5b6106038783880161052b565b9450602086013591508082111561061957600080fd5b506106268682870161052b565b925050604084013590509250925092565b600082516106498184602087016104ae565b9190910192915050565b634e487b7160e01b600052603260045260246000fd5b60006020808301818452808551808352604092508286019150828160051b87010184880160005b838110156106ec57603f198984030185528151606081518186526106b6828701826104de565b915050888201518582038a8701526106ce82826104de565b92890151958901959095525094870194925090860190600101610690565b509098975050505050505050565b60608152600061070d60608301866104de565b828103602084015261071f81866104de565b91505082604083015294935050505056fea264697066735822122089d55dc51975d823956db3c79734aa364503e977f601f3788cbd34b23bb53d6364736f6c634300080b0033

src/main/java/org/fisco/bcos/sdk/v3/codec/EventEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public <T extends Type> String buildMethodSignature(
3131
result.append(methodName);
3232
result.append("(");
3333
String params =
34-
parameters.stream().map(Utils::getTypeName).collect(Collectors.joining(","));
34+
parameters.stream().map(Utils::getMethodSign).collect(Collectors.joining(","));
3535
result.append(params);
3636
result.append(")");
3737
return result.toString();

src/main/java/org/fisco/bcos/sdk/v3/codec/Utils.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,28 @@
2626
public class Utils {
2727
private Utils() {}
2828

29+
public static <T extends Type> String getMethodSign(TypeReference<T> typeReference) {
30+
return getMethodSign(typeReference.getType());
31+
}
32+
2933
public static <T extends Type> String getTypeName(TypeReference<T> typeReference) {
3034
return getTypeName(typeReference.getType());
3135
}
3236

37+
public static <T extends Type> String getMethodSign(java.lang.reflect.Type type) {
38+
try {
39+
Class<?> cls = Utils.getClassType(type);
40+
if (type instanceof ParameterizedType) { // array
41+
return getParameterizedTypeName(type);
42+
} else { // simple type
43+
return getSimpleMethodSign(cls);
44+
}
45+
46+
} catch (ClassNotFoundException e) {
47+
throw new UnsupportedOperationException("Invalid class reference provided", e);
48+
}
49+
}
50+
3351
public static <T extends Type> String getTypeName(java.lang.reflect.Type type) {
3452
try {
3553

@@ -73,6 +91,50 @@ private static <T extends Type, U extends Type> String getParameterizedTypeName(
7391
}
7492
}
7593

94+
public static String getSimpleMethodSign(Class<?> type) {
95+
String simpleName = type.getSimpleName().toLowerCase();
96+
97+
if (type.equals(Uint.class)
98+
|| type.equals(Int.class)
99+
|| type.equals(Ufixed.class)
100+
|| type.equals(Fixed.class)) {
101+
return simpleName + "256";
102+
} else if (type.equals(Utf8String.class)) {
103+
return "string";
104+
} else if (type.equals(DynamicBytes.class)) {
105+
return "bytes";
106+
} else if (StructType.class.isAssignableFrom(type)) {
107+
Constructor<?> constructor =
108+
Arrays.stream(type.getDeclaredConstructors())
109+
.filter(
110+
declaredConstructor ->
111+
Arrays.stream(declaredConstructor.getParameterTypes())
112+
.allMatch(Type.class::isAssignableFrom)
113+
&& declaredConstructor.getParameterTypes()
114+
.length
115+
> 0)
116+
.findAny()
117+
.orElseThrow(
118+
() ->
119+
new RuntimeException(
120+
"TypeReferenced struct must contain a constructor with types that extend Type"));
121+
int length = constructor.getParameterCount();
122+
StringBuilder stringBuilder = new StringBuilder();
123+
stringBuilder.append('(');
124+
for (int i = 0; i < length; i++) {
125+
TypeReference<Type> typeReferenceElement =
126+
TypeReference.create(constructor.getGenericParameterTypes()[i]);
127+
stringBuilder.append(getTypeName(typeReferenceElement));
128+
stringBuilder.append(',');
129+
}
130+
stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
131+
stringBuilder.append(')');
132+
return stringBuilder.toString();
133+
} else {
134+
return simpleName;
135+
}
136+
}
137+
76138
public static String getSimpleTypeName(Class<?> type) {
77139
String simpleName = type.getSimpleName().toLowerCase();
78140

0 commit comments

Comments
 (0)