diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 9817bda8b..cece5dc3c 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -5,6 +5,7 @@ on: types: [ published, created, edited ] env: CCACHE_DIR: ${{ github.workspace }}/ccache + ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true jobs: build: @@ -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: diff --git a/build.gradle b/build.gradle index fcdd0bf78..14fca8703 100644 --- a/build.gradle +++ b/build.gradle @@ -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" diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java index 03073a956..3e21e854c 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java @@ -1017,7 +1017,7 @@ public List decodeEvent(String abi, String eventName, EventLog log) params = ContractCodecTools.decodeJavaObject(inputObject, log.getData(), isWasm); } - List topics = log.getTopics(); + List topics = decodeIndexedEvent(log, abiDefinition); return this.mergeEventParamsAndTopics(abiDefinition, params, topics); } catch (Exception e) { logger.error(" exception in decodeEventToObject : {}", e.getMessage()); @@ -1029,6 +1029,30 @@ public List decodeEvent(String abi, String eventName, EventLog log) throw new ContractCodecException(errorMsg); } + public List decodeIndexedEvent(EventLog log, ABIDefinition abiDefinition) + throws ClassNotFoundException { + List eventIndexedObject = + ABIObjectFactory.createEventIndexedObject(abiDefinition); + List 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 objects = + contractCodecJsonWrapper.decode( + indexedObject, Hex.decode(log.getTopics().get(i)), isWasm); + if (!objects.isEmpty()) { + topics.add(objects.get(0)); + } + } + } + } + return topics; + } + public List decodeEventByTopic(String abi, String eventTopic, EventLog log) throws ContractCodecException { ContractABIDefinition contractABIDefinition = this.abiDefinitionFactory.loadABI(abi); @@ -1040,7 +1064,7 @@ public List decodeEventByTopic(String abi, String eventTopic, EventLog l if (!log.getData().equals("0x")) { params = ContractCodecTools.decodeJavaObject(inputObject, log.getData(), isWasm); } - List topics = log.getTopics(); + List topics = decodeIndexedEvent(log, abiDefinition); return this.mergeEventParamsAndTopics(abiDefinition, params, topics); } catch (Exception e) { logger.error(" exception in decodeEventByTopicToObject : {}", e.getMessage()); @@ -1078,7 +1102,7 @@ public List decodeEventToString(String abi, String eventName, EventLog l contractCodecJsonWrapper.decode( inputObject, Hex.decode(log.getData()), isWasm); } - List topics = log.getTopics(); + List topics = decodeIndexedEvent(log, abiDefinition); return this.mergeEventParamsAndTopicsToString(abiDefinition, params, topics); } catch (Exception e) { logger.error(" exception in decodeEventToString : {}", e.getMessage()); @@ -1103,7 +1127,7 @@ public List decodeEventByTopicToString(String abi, String eventTopic, Ev contractCodecJsonWrapper.decode( inputObject, Hex.decode(log.getData()), isWasm); } - List topics = log.getTopics(); + List topics = decodeIndexedEvent(log, abiDefinition); return this.mergeEventParamsAndTopicsToString(abiDefinition, params, topics); } catch (Exception e) { logger.error(" exception in decodeEventByTopicToString : {}", e.getMessage()); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/TypeDecoder.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/TypeDecoder.java index d393e83ae..8f731b19d 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/TypeDecoder.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/abi/TypeDecoder.java @@ -85,7 +85,8 @@ public static 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; diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/datatypes/Event.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/datatypes/Event.java index ba6e5b15a..ffe045343 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/datatypes/Event.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/datatypes/Event.java @@ -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 { @@ -29,4 +31,9 @@ public List> getIndexedParameters() { public List> 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); + } } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIObjectFactory.java b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIObjectFactory.java index 701f1c308..dd14f0950 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIObjectFactory.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/codec/wrapper/ABIObjectFactory.java @@ -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; @@ -37,6 +38,16 @@ private static ABIObject createObject(String name, List return null; } + public static List createEventIndexedObject(ABIDefinition abiDefinition) { + ArrayList 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()); } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java b/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java index 95e1bd8a7..8dc1856d5 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/contract/Contract.java @@ -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; /** @@ -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 otherTopics, BigInteger fromBlock, @@ -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> topics, @@ -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( diff --git a/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java b/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java index b2790eea6..83a099d5d 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/transaction/codec/decode/TransactionDecoderService.java @@ -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();