Skip to content

Commit 0a865a4

Browse files
authored
<fix>(codec,contract): fix big int decode error, fix contract subscribe event not return event id bug, fix codec not decode event indexed topic bug. (#925)
* <fix>(codec,contract): fix big int decode error, fix contract subscribe event not return event id bug. * <fix>(event,codec): fix codec not decode event indexed topic bug. * <fix>(workflow): fix centos workflow.
1 parent 9a2a3bb commit 0a865a4

File tree

8 files changed

+81
-21
lines changed

8 files changed

+81
-21
lines changed

.github/workflows/workflow.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
types: [ published, created, edited ]
66
env:
77
CCACHE_DIR: ${{ github.workspace }}/ccache
8+
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
89

910
jobs:
1011
build:
@@ -86,7 +87,14 @@ jobs:
8687
distribution: 'zulu'
8788
java-version: '8.0.345'
8889
- name: install CentOS dependencies
89-
run: yum install -y epel-release centos-release-scl wget which git openssl-devel openssl tree
90+
run: |
91+
sed -i s/mirror.centos.org/mirrors.aliyun.com/g /etc/yum.repos.d/*.repo
92+
sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo
93+
sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo
94+
yum clean all
95+
yum makecache
96+
yum update -y
97+
yum install -y epel-release centos-release-scl wget which git openssl-devel openssl tree
9098
- name: Set up JDK 1.8.0.345
9199
uses: actions/setup-java@v3
92100
with:

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ext {
2020
commonsIOVersion = '2.11.0'
2121
commonsLang3Version = '3.12.0'
2222
toml4jVersion = "0.7.2"
23-
bcprovJDK18onVersion = '1.75'
23+
bcprovJDK18onVersion = '1.78'
2424
webankJavaCryptoVersion = "1.0.3"
2525
junitVersion = '4.13.2'
2626
commonsCollections4Version = "4.4"

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ public List<Object> decodeEvent(String abi, String eventName, EventLog log)
10171017
params =
10181018
ContractCodecTools.decodeJavaObject(inputObject, log.getData(), isWasm);
10191019
}
1020-
List<String> topics = log.getTopics();
1020+
List<String> topics = decodeIndexedEvent(log, abiDefinition);
10211021
return this.mergeEventParamsAndTopics(abiDefinition, params, topics);
10221022
} catch (Exception e) {
10231023
logger.error(" exception in decodeEventToObject : {}", e.getMessage());
@@ -1029,6 +1029,30 @@ public List<Object> decodeEvent(String abi, String eventName, EventLog log)
10291029
throw new ContractCodecException(errorMsg);
10301030
}
10311031

1032+
public List<String> decodeIndexedEvent(EventLog log, ABIDefinition abiDefinition)
1033+
throws ClassNotFoundException {
1034+
List<ABIObject> eventIndexedObject =
1035+
ABIObjectFactory.createEventIndexedObject(abiDefinition);
1036+
List<String> topics = new ArrayList<>();
1037+
if (!log.getTopics().isEmpty()) {
1038+
topics.add(log.getTopics().get(0));
1039+
for (int i = 1; i < log.getTopics().size(); i++) {
1040+
ABIObject indexedObject = eventIndexedObject.get(i - 1);
1041+
if (indexedObject.isDynamic()) {
1042+
topics.add(log.getTopics().get(i));
1043+
} else {
1044+
List<String> objects =
1045+
contractCodecJsonWrapper.decode(
1046+
indexedObject, Hex.decode(log.getTopics().get(i)), isWasm);
1047+
if (!objects.isEmpty()) {
1048+
topics.add(objects.get(0));
1049+
}
1050+
}
1051+
}
1052+
}
1053+
return topics;
1054+
}
1055+
10321056
public List<Object> decodeEventByTopic(String abi, String eventTopic, EventLog log)
10331057
throws ContractCodecException {
10341058
ContractABIDefinition contractABIDefinition = this.abiDefinitionFactory.loadABI(abi);
@@ -1040,7 +1064,7 @@ public List<Object> decodeEventByTopic(String abi, String eventTopic, EventLog l
10401064
if (!log.getData().equals("0x")) {
10411065
params = ContractCodecTools.decodeJavaObject(inputObject, log.getData(), isWasm);
10421066
}
1043-
List<String> topics = log.getTopics();
1067+
List<String> topics = decodeIndexedEvent(log, abiDefinition);
10441068
return this.mergeEventParamsAndTopics(abiDefinition, params, topics);
10451069
} catch (Exception e) {
10461070
logger.error(" exception in decodeEventByTopicToObject : {}", e.getMessage());
@@ -1078,7 +1102,7 @@ public List<String> decodeEventToString(String abi, String eventName, EventLog l
10781102
contractCodecJsonWrapper.decode(
10791103
inputObject, Hex.decode(log.getData()), isWasm);
10801104
}
1081-
List<String> topics = log.getTopics();
1105+
List<String> topics = decodeIndexedEvent(log, abiDefinition);
10821106
return this.mergeEventParamsAndTopicsToString(abiDefinition, params, topics);
10831107
} catch (Exception e) {
10841108
logger.error(" exception in decodeEventToString : {}", e.getMessage());
@@ -1103,7 +1127,7 @@ public List<String> decodeEventByTopicToString(String abi, String eventTopic, Ev
11031127
contractCodecJsonWrapper.decode(
11041128
inputObject, Hex.decode(log.getData()), isWasm);
11051129
}
1106-
List<String> topics = log.getTopics();
1130+
List<String> topics = decodeIndexedEvent(log, abiDefinition);
11071131
return this.mergeEventParamsAndTopicsToString(abiDefinition, params, topics);
11081132
} catch (Exception e) {
11091133
logger.error(" exception in decodeEventByTopicToString : {}", e.getMessage());

src/main/java/org/fisco/bcos/sdk/v3/codec/abi/TypeDecoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public static <T extends NumericType> T decodeNumeric(byte[] inputByteArray, Cla
8585
byte[] resultByteArray = new byte[typeLengthAsBytes + 1];
8686

8787
if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
88-
resultByteArray[0] = inputByteArray[0]; // take MSB as sign bit
88+
// NOTE (first byte & 0xffff) >> 7 means take the MSB as sign bit
89+
resultByteArray[0] = (byte) ((inputByteArray[0] & 0xffff) >> 7);
8990
}
9091

9192
int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;

src/main/java/org/fisco/bcos/sdk/v3/codec/datatypes/Event.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.List;
44
import java.util.stream.Collectors;
5+
import org.fisco.bcos.sdk.v3.codec.EventEncoder;
56
import org.fisco.bcos.sdk.v3.codec.Utils;
7+
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;
68

79
/** Event wrapper type. */
810
public class Event {
@@ -29,4 +31,9 @@ public List<TypeReference<Type>> getIndexedParameters() {
2931
public List<TypeReference<Type>> getNonIndexedParameters() {
3032
return parameters.stream().filter(p -> !p.isIndexed()).collect(Collectors.toList());
3133
}
34+
35+
public String encodeToTopic(Hash hashImpl) {
36+
EventEncoder encoder = new EventEncoder(hashImpl);
37+
return encoder.encode(this);
38+
}
3239
}

src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIObjectFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.fisco.bcos.sdk.v3.codec.wrapper;
22

33
import java.math.BigInteger;
4+
import java.util.ArrayList;
45
import java.util.List;
56
import org.fisco.bcos.sdk.v3.utils.Numeric;
67
import org.slf4j.Logger;
@@ -37,6 +38,16 @@ private static ABIObject createObject(String name, List<ABIDefinition.NamedType>
3738
return null;
3839
}
3940

41+
public static List<ABIObject> createEventIndexedObject(ABIDefinition abiDefinition) {
42+
ArrayList<ABIObject> abiObjects = new ArrayList<>();
43+
for (ABIDefinition.NamedType namedType : abiDefinition.getInputs()) {
44+
if (namedType.isIndexed()) {
45+
abiObjects.add(buildTypeObject(namedType));
46+
}
47+
}
48+
return abiObjects;
49+
}
50+
4051
public static ABIObject createEventInputObject(ABIDefinition abiDefinition) {
4152
return creatEventObjectWithOutIndexed(abiDefinition.getInputs());
4253
}

src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public class Contract {
8181
protected TransactionManager transactionManager = null;
8282
protected final Client client;
8383
public static final String FUNC_DEPLOY = "deploy";
84-
protected final FunctionEncoderInterface functionEncoder;
85-
protected final FunctionReturnDecoderInterface functionReturnDecoder;
84+
public final FunctionEncoderInterface functionEncoder;
85+
public final FunctionReturnDecoderInterface functionReturnDecoder;
8686
protected final CryptoKeyPair credential;
8787
protected final CryptoSuite cryptoSuite;
88-
protected final EventEncoder eventEncoder;
89-
private final EventSubscribe eventSubscribe;
88+
public final EventEncoder eventEncoder;
89+
public final EventSubscribe eventSubscribe;
9090
private boolean enableDAG = false;
9191

9292
/**
@@ -735,24 +735,24 @@ protected String createSignedTransaction(Function function) {
735735
return txPair.getSignedTx();
736736
}
737737

738-
public void subscribeEvent(EventSubParams params, EventSubCallback callback) {
739-
this.eventSubscribe.subscribeEvent(params, callback);
738+
public String subscribeEvent(EventSubParams params, EventSubCallback callback) {
739+
return this.eventSubscribe.subscribeEvent(params, callback);
740740
}
741741

742-
public void subscribeEvent(String topic0, EventSubCallback callback) {
743-
subscribeEvent(topic0, BigInteger.valueOf(-1), BigInteger.valueOf(-1), callback);
742+
public String subscribeEvent(String topic0, EventSubCallback callback) {
743+
return subscribeEvent(topic0, BigInteger.valueOf(-1), BigInteger.valueOf(-1), callback);
744744
}
745745

746-
public void subscribeEvent(
746+
public String subscribeEvent(
747747
String topic0, BigInteger fromBlock, BigInteger toBlock, EventSubCallback callback) {
748-
subscribeEvent(
748+
return subscribeEvent(
749749
fromBlock,
750750
toBlock,
751751
Collections.singletonList(Collections.singletonList(topic0)),
752752
callback);
753753
}
754754

755-
public void subscribeEvent(
755+
public String subscribeEvent(
756756
String topic0,
757757
List<String> otherTopics,
758758
BigInteger fromBlock,
@@ -763,10 +763,10 @@ public void subscribeEvent(
763763
for (String otherTopic : otherTopics) {
764764
topics.add(Collections.singletonList(otherTopic));
765765
}
766-
subscribeEvent(fromBlock, toBlock, topics, callback);
766+
return subscribeEvent(fromBlock, toBlock, topics, callback);
767767
}
768768

769-
public void subscribeEvent(
769+
public String subscribeEvent(
770770
BigInteger fromBlock,
771771
BigInteger toBlock,
772772
List<List<String>> topics,
@@ -781,7 +781,11 @@ public void subscribeEvent(
781781
eventSubParams.addTopic(i, topic);
782782
}
783783
}
784-
subscribeEvent(eventSubParams, callback);
784+
return subscribeEvent(eventSubParams, callback);
785+
}
786+
787+
public void unsubscribeEvent(String eventId) {
788+
this.eventSubscribe.unsubscribeEvent(eventId);
785789
}
786790

787791
public static EventValues staticExtractEventParameters(

src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public String decodeRevertMessage(String output) {
9292
if (output.length() <= 8) {
9393
return null;
9494
} else {
95+
// only decode evm Error(string) message now
96+
if (!RevertMessageParser.isOutputStartWithRevertMethod(output)) {
97+
throw new RuntimeException(
98+
"Output is not start with revert method, maybe not a standard evm error.");
99+
}
95100
// This revert msg encoder/decoder only use ABI
96101
FunctionReturnDecoderInterface functionReturnDecoderInterface =
97102
new org.fisco.bcos.sdk.v3.codec.abi.FunctionReturnDecoder();

0 commit comments

Comments
 (0)