Skip to content

Commit

Permalink
<fix>(codec,contract): fix big int decode error, fix contract subscri…
Browse files Browse the repository at this point in the history
…be 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.
  • Loading branch information
kyonRay authored Aug 13, 2024
1 parent 9a2a3bb commit 0a865a4
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 21 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
types: [ published, created, edited ]
env:
CCACHE_DIR: ${{ github.workspace }}/ccache
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true

jobs:
build:
Expand Down Expand Up @@ -86,7 +87,14 @@ jobs:
distribution: 'zulu'
java-version: '8.0.345'
- name: install CentOS dependencies
run: yum install -y epel-release centos-release-scl wget which git openssl-devel openssl tree
run: |
sed -i s/mirror.centos.org/mirrors.aliyun.com/g /etc/yum.repos.d/*.repo
sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo
sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo
yum clean all
yum makecache
yum update -y
yum install -y epel-release centos-release-scl wget which git openssl-devel openssl tree
- name: Set up JDK 1.8.0.345
uses: actions/setup-java@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ext {
commonsIOVersion = '2.11.0'
commonsLang3Version = '3.12.0'
toml4jVersion = "0.7.2"
bcprovJDK18onVersion = '1.75'
bcprovJDK18onVersion = '1.78'
webankJavaCryptoVersion = "1.0.3"
junitVersion = '4.13.2'
commonsCollections4Version = "4.4"
Expand Down
32 changes: 28 additions & 4 deletions src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ public List<Object> decodeEvent(String abi, String eventName, EventLog log)
params =
ContractCodecTools.decodeJavaObject(inputObject, log.getData(), isWasm);
}
List<String> topics = log.getTopics();
List<String> topics = decodeIndexedEvent(log, abiDefinition);
return this.mergeEventParamsAndTopics(abiDefinition, params, topics);
} catch (Exception e) {
logger.error(" exception in decodeEventToObject : {}", e.getMessage());
Expand All @@ -1029,6 +1029,30 @@ public List<Object> decodeEvent(String abi, String eventName, EventLog log)
throw new ContractCodecException(errorMsg);
}

public List<String> decodeIndexedEvent(EventLog log, ABIDefinition abiDefinition)
throws ClassNotFoundException {
List<ABIObject> eventIndexedObject =
ABIObjectFactory.createEventIndexedObject(abiDefinition);
List<String> topics = new ArrayList<>();
if (!log.getTopics().isEmpty()) {
topics.add(log.getTopics().get(0));
for (int i = 1; i < log.getTopics().size(); i++) {
ABIObject indexedObject = eventIndexedObject.get(i - 1);
if (indexedObject.isDynamic()) {
topics.add(log.getTopics().get(i));
} else {
List<String> objects =
contractCodecJsonWrapper.decode(
indexedObject, Hex.decode(log.getTopics().get(i)), isWasm);
if (!objects.isEmpty()) {
topics.add(objects.get(0));
}
}
}
}
return topics;
}

public List<Object> decodeEventByTopic(String abi, String eventTopic, EventLog log)
throws ContractCodecException {
ContractABIDefinition contractABIDefinition = this.abiDefinitionFactory.loadABI(abi);
Expand All @@ -1040,7 +1064,7 @@ public List<Object> decodeEventByTopic(String abi, String eventTopic, EventLog l
if (!log.getData().equals("0x")) {
params = ContractCodecTools.decodeJavaObject(inputObject, log.getData(), isWasm);
}
List<String> topics = log.getTopics();
List<String> topics = decodeIndexedEvent(log, abiDefinition);
return this.mergeEventParamsAndTopics(abiDefinition, params, topics);
} catch (Exception e) {
logger.error(" exception in decodeEventByTopicToObject : {}", e.getMessage());
Expand Down Expand Up @@ -1078,7 +1102,7 @@ public List<String> decodeEventToString(String abi, String eventName, EventLog l
contractCodecJsonWrapper.decode(
inputObject, Hex.decode(log.getData()), isWasm);
}
List<String> topics = log.getTopics();
List<String> topics = decodeIndexedEvent(log, abiDefinition);
return this.mergeEventParamsAndTopicsToString(abiDefinition, params, topics);
} catch (Exception e) {
logger.error(" exception in decodeEventToString : {}", e.getMessage());
Expand All @@ -1103,7 +1127,7 @@ public List<String> decodeEventByTopicToString(String abi, String eventTopic, Ev
contractCodecJsonWrapper.decode(
inputObject, Hex.decode(log.getData()), isWasm);
}
List<String> topics = log.getTopics();
List<String> topics = decodeIndexedEvent(log, abiDefinition);
return this.mergeEventParamsAndTopicsToString(abiDefinition, params, topics);
} catch (Exception e) {
logger.error(" exception in decodeEventByTopicToString : {}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public static <T extends NumericType> T decodeNumeric(byte[] inputByteArray, Cla
byte[] resultByteArray = new byte[typeLengthAsBytes + 1];

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

int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.List;
import java.util.stream.Collectors;
import org.fisco.bcos.sdk.v3.codec.EventEncoder;
import org.fisco.bcos.sdk.v3.codec.Utils;
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;

/** Event wrapper type. */
public class Event {
Expand All @@ -29,4 +31,9 @@ public List<TypeReference<Type>> getIndexedParameters() {
public List<TypeReference<Type>> getNonIndexedParameters() {
return parameters.stream().filter(p -> !p.isIndexed()).collect(Collectors.toList());
}

public String encodeToTopic(Hash hashImpl) {
EventEncoder encoder = new EventEncoder(hashImpl);
return encoder.encode(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fisco.bcos.sdk.v3.codec.wrapper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.fisco.bcos.sdk.v3.utils.Numeric;
import org.slf4j.Logger;
Expand Down Expand Up @@ -37,6 +38,16 @@ private static ABIObject createObject(String name, List<ABIDefinition.NamedType>
return null;
}

public static List<ABIObject> createEventIndexedObject(ABIDefinition abiDefinition) {
ArrayList<ABIObject> abiObjects = new ArrayList<>();
for (ABIDefinition.NamedType namedType : abiDefinition.getInputs()) {
if (namedType.isIndexed()) {
abiObjects.add(buildTypeObject(namedType));
}
}
return abiObjects;
}

public static ABIObject createEventInputObject(ABIDefinition abiDefinition) {
return creatEventObjectWithOutIndexed(abiDefinition.getInputs());
}
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public class Contract {
protected TransactionManager transactionManager = null;
protected final Client client;
public static final String FUNC_DEPLOY = "deploy";
protected final FunctionEncoderInterface functionEncoder;
protected final FunctionReturnDecoderInterface functionReturnDecoder;
public final FunctionEncoderInterface functionEncoder;
public final FunctionReturnDecoderInterface functionReturnDecoder;
protected final CryptoKeyPair credential;
protected final CryptoSuite cryptoSuite;
protected final EventEncoder eventEncoder;
private final EventSubscribe eventSubscribe;
public final EventEncoder eventEncoder;
public final EventSubscribe eventSubscribe;
private boolean enableDAG = false;

/**
Expand Down Expand Up @@ -735,24 +735,24 @@ protected String createSignedTransaction(Function function) {
return txPair.getSignedTx();
}

public void subscribeEvent(EventSubParams params, EventSubCallback callback) {
this.eventSubscribe.subscribeEvent(params, callback);
public String subscribeEvent(EventSubParams params, EventSubCallback callback) {
return this.eventSubscribe.subscribeEvent(params, callback);
}

public void subscribeEvent(String topic0, EventSubCallback callback) {
subscribeEvent(topic0, BigInteger.valueOf(-1), BigInteger.valueOf(-1), callback);
public String subscribeEvent(String topic0, EventSubCallback callback) {
return subscribeEvent(topic0, BigInteger.valueOf(-1), BigInteger.valueOf(-1), callback);
}

public void subscribeEvent(
public String subscribeEvent(
String topic0, BigInteger fromBlock, BigInteger toBlock, EventSubCallback callback) {
subscribeEvent(
return subscribeEvent(
fromBlock,
toBlock,
Collections.singletonList(Collections.singletonList(topic0)),
callback);
}

public void subscribeEvent(
public String subscribeEvent(
String topic0,
List<String> otherTopics,
BigInteger fromBlock,
Expand All @@ -763,10 +763,10 @@ public void subscribeEvent(
for (String otherTopic : otherTopics) {
topics.add(Collections.singletonList(otherTopic));
}
subscribeEvent(fromBlock, toBlock, topics, callback);
return subscribeEvent(fromBlock, toBlock, topics, callback);
}

public void subscribeEvent(
public String subscribeEvent(
BigInteger fromBlock,
BigInteger toBlock,
List<List<String>> topics,
Expand All @@ -781,7 +781,11 @@ public void subscribeEvent(
eventSubParams.addTopic(i, topic);
}
}
subscribeEvent(eventSubParams, callback);
return subscribeEvent(eventSubParams, callback);
}

public void unsubscribeEvent(String eventId) {
this.eventSubscribe.unsubscribeEvent(eventId);
}

public static EventValues staticExtractEventParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public String decodeRevertMessage(String output) {
if (output.length() <= 8) {
return null;
} else {
// only decode evm Error(string) message now
if (!RevertMessageParser.isOutputStartWithRevertMethod(output)) {
throw new RuntimeException(
"Output is not start with revert method, maybe not a standard evm error.");
}
// This revert msg encoder/decoder only use ABI
FunctionReturnDecoderInterface functionReturnDecoderInterface =
new org.fisco.bcos.sdk.v3.codec.abi.FunctionReturnDecoder();
Expand Down

0 comments on commit 0a865a4

Please sign in to comment.