Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@

import java.math.BigInteger;
import java.util.Objects;

import org.fisco.bcos.sdk.codec.abi.TypeEncoder;
import org.fisco.bcos.sdk.codec.datatypes.Bytes;
import org.fisco.bcos.sdk.crypto.CryptoSuite;
import org.fisco.bcos.sdk.utils.AddressUtils;
import org.fisco.bcos.sdk.utils.Hex;
import org.fisco.bcos.sdk.utils.Numeric;

public class TopicTools {

public static final int MAX_NUM_TOPIC_EVENT_LOG = 4;
public static final int TOPIC_LENGTH = 64;
public static final int TOPIC_LENGTH_IN_HEX = 64;

private final CryptoSuite cryptoSuite;

public TopicTools(CryptoSuite cryptoSuite) {
this.cryptoSuite = cryptoSuite;
}

public String integerToTopic(BigInteger i) {
return Numeric.toHexStringWithPrefixZeroPadded(i, 64);
}

public static boolean validTopic(String topic) {
if (Objects.isNull(topic)) {
return false;
}

if (topic.startsWith("0x") || topic.startsWith("0X")) {
return topic.length() == (TOPIC_LENGTH + 2);
return topic.length() == (TOPIC_LENGTH_IN_HEX + 2);
}

return topic.length() == TOPIC_LENGTH;
return topic.length() == TOPIC_LENGTH_IN_HEX;
}

public String integerToTopic(BigInteger i) {
return Numeric.toHexStringWithPrefixZeroPadded(i, 64);
}

public String boolToTopic(boolean b) {
if (b) {
Expand All @@ -53,20 +52,20 @@ public String addressToTopic(String s) {
return "0x000000000000000000000000" + Numeric.cleanHexPrefix(s);
}

public byte[] stringToTopic(String s) {
return this.cryptoSuite.hash(s.getBytes());
public String stringToTopic(String s) {
return "0x" + Hex.toHexString(this.cryptoSuite.hash(s.getBytes()));
}

public byte[] bytesToTopic(byte[] b) {
return this.cryptoSuite.hash(b);
public String bytesToTopic(byte[] b) {
return "0x" + Hex.toHexString(this.cryptoSuite.hash(b));
}

public byte[] byteNToTopic(byte[] b) {
public String byteNToTopic(byte[] b) {
// byte[] can't be more than 32 byte
if (b.length > 32) {
throw new IllegalArgumentException("byteN can't be more than 32 byte");
}
Bytes bs = new Bytes(b.length, b);
return TypeEncoder.encode(bs);
return "0x" + Hex.toHexString(TypeEncoder.encode(bs));
}
}
35 changes: 24 additions & 11 deletions sdk-core/src/main/java/org/fisco/bcos/sdk/model/EventLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

import org.fisco.bcos.sdk.utils.Numeric;

public class EventLog {
Expand Down Expand Up @@ -177,19 +176,33 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(logIndex, transactionIndex, transactionHash, blockNumber, address, data, topics);
return Objects.hash(
logIndex, transactionIndex, transactionHash, blockNumber, address, data, topics);
}

@Override
public String toString() {
return "EventLog{" +
"logIndex='" + logIndex + '\'' +
", transactionIndex='" + transactionIndex + '\'' +
", transactionHash='" + transactionHash + '\'' +
", blockNumber='" + blockNumber + '\'' +
", address='" + address + '\'' +
", data='" + data + '\'' +
", topics=" + topics +
'}';
return "EventLog{"
+ "logIndex='"
+ logIndex
+ '\''
+ ", transactionIndex='"
+ transactionIndex
+ '\''
+ ", transactionHash='"
+ transactionHash
+ '\''
+ ", blockNumber='"
+ blockNumber
+ '\''
+ ", address='"
+ address
+ '\''
+ ", data='"
+ data
+ '\''
+ ", topics="
+ topics
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ public class EventSubParams {
private BigInteger fromBlock = BigInteger.valueOf(-1);
private BigInteger toBlock = BigInteger.valueOf(-1);
private List<String> addresses = new ArrayList<>();
private List<List<String>> topics = new ArrayList<List<String>>() {{ add(null); add(null); add(null); add(null);}};
private List<List<String>> topics =
new ArrayList<List<String>>() {
{
add(null);
add(null);
add(null);
add(null);
}
};

public BigInteger getFromBlock() {
return fromBlock;
Expand Down Expand Up @@ -75,9 +83,7 @@ public boolean addTopic(int index, String topic) {
return true;
}

/**
* @return check params
*/
/** @return check params */
@SuppressWarnings("unchecked")
public boolean checkParams() {
if (fromBlock.compareTo(BigInteger.ZERO) > 0 && toBlock.compareTo(BigInteger.ZERO) > 0) {
Expand All @@ -88,12 +94,15 @@ public boolean checkParams() {

@Override
public String toString() {
return "EventLogParams{" +
"fromBlock=" + fromBlock +
", toBlock=" + toBlock +
", addresses=" + addresses +
", topics=" + topics +
'}';
return "EventLogParams{"
+ "fromBlock="
+ fromBlock
+ ", toBlock="
+ toBlock
+ ", addresses="
+ addresses
+ ", topics="
+ topics
+ '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@

package org.fisco.bcos.sdk.eventsub;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import org.fisco.bcos.sdk.model.EventLog;

public class EventSubResponse {
Expand All @@ -29,13 +28,7 @@ public class EventSubResponse {

@Override
public String toString() {
return "EventLogResponse [result="
+ status
+ ", id="
+ id
+ ", logs="
+ logs
+ "]";
return "EventLogResponse [result=" + status + ", id=" + id + ", logs=" + logs + "]";
}

public int getStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static EventSubscribe build(Client client) throws JniException {

/**
* get all events subscribed by clients
*
* @return
*/
Set<String> getAllSubscribedEvents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

package org.fisco.bcos.sdk.eventsub;

import java.util.Set;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import java.util.Set;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.config.ConfigOption;
import org.fisco.bcos.sdk.crypto.CryptoSuite;
Expand All @@ -44,9 +42,13 @@ public EventSubscribeImp(Client client, ConfigOption configOption) throws JniExc
this.groupId = client.getGroup();
this.configOption = configOption;
this.cryptoSuite = client.getCryptoSuite();
this.eventSubscribe = org.fisco.bcos.sdk.jni.event.EventSubscribe.build(configOption.getJniConfig());
this.eventSubscribe =
org.fisco.bcos.sdk.jni.event.EventSubscribe.build(configOption.getJniConfig());

logger.info(" EventSub constructor, group: {}, config: {}", groupId, configOption.getJniConfig());
logger.info(
" EventSub constructor, group: {}, config: {}",
groupId,
configOption.getJniConfig());
}

public CryptoSuite getCryptoSuite() {
Expand Down Expand Up @@ -91,27 +93,44 @@ public void subscribeEvent(EventSubParams params, EventSubCallback callback) {

logger.info("EventSub subscribeEvent, params: {}", params);

eventSubscribe.subscribeEvent(groupId, strParams, new EventSubscribeCallback() {
@Override
public void onResponse(Response response) {
if (response.getErrorCode() != 0) {
logger.error("subscribeEvent response error, errorCode: {}, errorMessage: {}", response.getErrorCode(), response.getErrorMessage());
callback.onReceiveLog("", response.getErrorCode(), null);
return;
}

String strResp = new String(response.getData());
logger.debug("subscribeEvent response, errorCode: {}, errorMessage: {}, data: {}", response.getErrorCode(), response.getErrorMessage(), strResp);

ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
try {
EventSubResponse eventSubResponse = objectMapper.readValue(strResp, EventSubResponse.class);
callback.onReceiveLog(eventSubResponse.getId(), eventSubResponse.getStatus(), eventSubResponse.getLogs());
} catch (JsonProcessingException e) {
logger.error("subscribeEvent response parser json error, resp: {}, e: {}", strResp, e);
}
}
});
eventSubscribe.subscribeEvent(
groupId,
strParams,
new EventSubscribeCallback() {
@Override
public void onResponse(Response response) {
if (response.getErrorCode() != 0) {
logger.error(
"subscribeEvent response error, errorCode: {}, errorMessage: {}",
response.getErrorCode(),
response.getErrorMessage());
callback.onReceiveLog("", response.getErrorCode(), null);
return;
}

String strResp = new String(response.getData());
logger.debug(
"subscribeEvent response, errorCode: {}, errorMessage: {}, data: {}",
response.getErrorCode(),
response.getErrorMessage(),
strResp);

ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
try {
EventSubResponse eventSubResponse =
objectMapper.readValue(strResp, EventSubResponse.class);
callback.onReceiveLog(
eventSubResponse.getId(),
eventSubResponse.getStatus(),
eventSubResponse.getLogs());
} catch (JsonProcessingException e) {
logger.error(
"subscribeEvent response parser json error, resp: {}, e: {}",
strResp,
e);
}
}
});
}

@Override
Expand All @@ -126,8 +145,12 @@ public Set<String> getAllSubscribedEvents() {
}

@Override
public void start() { eventSubscribe.start(); }
public void start() {
eventSubscribe.start();
}

@Override
public void stop() { eventSubscribe.stop();}
public void stop() {
eventSubscribe.stop();
}
}