Skip to content

Commit

Permalink
<fix>(event,codec): fix codec not decode event indexed topic bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay committed Aug 13, 2024
1 parent a06de5f commit 43d8346
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
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
@@ -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

0 comments on commit 43d8346

Please sign in to comment.