Skip to content

Commit a2d1242

Browse files
authored
update TopicTools (#425)
1 parent 62522ee commit a2d1242

File tree

6 files changed

+111
-73
lines changed

6 files changed

+111
-73
lines changed

sdk-codec/src/main/java/org/fisco/bcos/sdk/codec/abi/tools/TopicTools.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,39 @@
22

33
import java.math.BigInteger;
44
import java.util.Objects;
5-
65
import org.fisco.bcos.sdk.codec.abi.TypeEncoder;
76
import org.fisco.bcos.sdk.codec.datatypes.Bytes;
87
import org.fisco.bcos.sdk.crypto.CryptoSuite;
98
import org.fisco.bcos.sdk.utils.AddressUtils;
9+
import org.fisco.bcos.sdk.utils.Hex;
1010
import org.fisco.bcos.sdk.utils.Numeric;
1111

1212
public class TopicTools {
1313

1414
public static final int MAX_NUM_TOPIC_EVENT_LOG = 4;
15-
public static final int TOPIC_LENGTH = 64;
15+
public static final int TOPIC_LENGTH_IN_HEX = 64;
1616

1717
private final CryptoSuite cryptoSuite;
1818

1919
public TopicTools(CryptoSuite cryptoSuite) {
2020
this.cryptoSuite = cryptoSuite;
2121
}
2222

23-
public String integerToTopic(BigInteger i) {
24-
return Numeric.toHexStringWithPrefixZeroPadded(i, 64);
25-
}
26-
2723
public static boolean validTopic(String topic) {
2824
if (Objects.isNull(topic)) {
2925
return false;
3026
}
3127

3228
if (topic.startsWith("0x") || topic.startsWith("0X")) {
33-
return topic.length() == (TOPIC_LENGTH + 2);
29+
return topic.length() == (TOPIC_LENGTH_IN_HEX + 2);
3430
}
3531

36-
return topic.length() == TOPIC_LENGTH;
32+
return topic.length() == TOPIC_LENGTH_IN_HEX;
3733
}
3834

35+
public String integerToTopic(BigInteger i) {
36+
return Numeric.toHexStringWithPrefixZeroPadded(i, 64);
37+
}
3938

4039
public String boolToTopic(boolean b) {
4140
if (b) {
@@ -53,20 +52,20 @@ public String addressToTopic(String s) {
5352
return "0x000000000000000000000000" + Numeric.cleanHexPrefix(s);
5453
}
5554

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

60-
public byte[] bytesToTopic(byte[] b) {
61-
return this.cryptoSuite.hash(b);
59+
public String bytesToTopic(byte[] b) {
60+
return "0x" + Hex.toHexString(this.cryptoSuite.hash(b));
6261
}
6362

64-
public byte[] byteNToTopic(byte[] b) {
63+
public String byteNToTopic(byte[] b) {
6564
// byte[] can't be more than 32 byte
6665
if (b.length > 32) {
6766
throw new IllegalArgumentException("byteN can't be more than 32 byte");
6867
}
6968
Bytes bs = new Bytes(b.length, b);
70-
return TypeEncoder.encode(bs);
69+
return "0x" + Hex.toHexString(TypeEncoder.encode(bs));
7170
}
7271
}

sdk-core/src/main/java/org/fisco/bcos/sdk/model/EventLog.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.math.BigInteger;
1919
import java.util.List;
2020
import java.util.Objects;
21-
2221
import org.fisco.bcos.sdk.utils.Numeric;
2322

2423
public class EventLog {
@@ -177,19 +176,33 @@ public boolean equals(Object o) {
177176

178177
@Override
179178
public int hashCode() {
180-
return Objects.hash(logIndex, transactionIndex, transactionHash, blockNumber, address, data, topics);
179+
return Objects.hash(
180+
logIndex, transactionIndex, transactionHash, blockNumber, address, data, topics);
181181
}
182182

183183
@Override
184184
public String toString() {
185-
return "EventLog{" +
186-
"logIndex='" + logIndex + '\'' +
187-
", transactionIndex='" + transactionIndex + '\'' +
188-
", transactionHash='" + transactionHash + '\'' +
189-
", blockNumber='" + blockNumber + '\'' +
190-
", address='" + address + '\'' +
191-
", data='" + data + '\'' +
192-
", topics=" + topics +
193-
'}';
185+
return "EventLog{"
186+
+ "logIndex='"
187+
+ logIndex
188+
+ '\''
189+
+ ", transactionIndex='"
190+
+ transactionIndex
191+
+ '\''
192+
+ ", transactionHash='"
193+
+ transactionHash
194+
+ '\''
195+
+ ", blockNumber='"
196+
+ blockNumber
197+
+ '\''
198+
+ ", address='"
199+
+ address
200+
+ '\''
201+
+ ", data='"
202+
+ data
203+
+ '\''
204+
+ ", topics="
205+
+ topics
206+
+ '}';
194207
}
195208
}

sdk-service/src/main/java/org/fisco/bcos/sdk/eventsub/EventSubParams.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ public class EventSubParams {
2525
private BigInteger fromBlock = BigInteger.valueOf(-1);
2626
private BigInteger toBlock = BigInteger.valueOf(-1);
2727
private List<String> addresses = new ArrayList<>();
28-
private List<List<String>> topics = new ArrayList<List<String>>() {{ add(null); add(null); add(null); add(null);}};
28+
private List<List<String>> topics =
29+
new ArrayList<List<String>>() {
30+
{
31+
add(null);
32+
add(null);
33+
add(null);
34+
add(null);
35+
}
36+
};
2937

3038
public BigInteger getFromBlock() {
3139
return fromBlock;
@@ -75,9 +83,7 @@ public boolean addTopic(int index, String topic) {
7583
return true;
7684
}
7785

78-
/**
79-
* @return check params
80-
*/
86+
/** @return check params */
8187
@SuppressWarnings("unchecked")
8288
public boolean checkParams() {
8389
if (fromBlock.compareTo(BigInteger.ZERO) > 0 && toBlock.compareTo(BigInteger.ZERO) > 0) {
@@ -88,12 +94,15 @@ public boolean checkParams() {
8894

8995
@Override
9096
public String toString() {
91-
return "EventLogParams{" +
92-
"fromBlock=" + fromBlock +
93-
", toBlock=" + toBlock +
94-
", addresses=" + addresses +
95-
", topics=" + topics +
96-
'}';
97+
return "EventLogParams{"
98+
+ "fromBlock="
99+
+ fromBlock
100+
+ ", toBlock="
101+
+ toBlock
102+
+ ", addresses="
103+
+ addresses
104+
+ ", topics="
105+
+ topics
106+
+ '}';
97107
}
98-
99108
}

sdk-service/src/main/java/org/fisco/bcos/sdk/eventsub/EventSubResponse.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
package org.fisco.bcos.sdk.eventsub;
1717

18-
import java.util.List;
19-
2018
import com.fasterxml.jackson.annotation.JsonProperty;
19+
import java.util.List;
2120
import org.fisco.bcos.sdk.model.EventLog;
2221

2322
public class EventSubResponse {
@@ -29,13 +28,7 @@ public class EventSubResponse {
2928

3029
@Override
3130
public String toString() {
32-
return "EventLogResponse [result="
33-
+ status
34-
+ ", id="
35-
+ id
36-
+ ", logs="
37-
+ logs
38-
+ "]";
31+
return "EventLogResponse [result=" + status + ", id=" + id + ", logs=" + logs + "]";
3932
}
4033

4134
public int getStatus() {

sdk-service/src/main/java/org/fisco/bcos/sdk/eventsub/EventSubscribe.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static EventSubscribe build(Client client) throws JniException {
6666

6767
/**
6868
* get all events subscribed by clients
69+
*
6970
* @return
7071
*/
7172
Set<String> getAllSubscribedEvents();

sdk-service/src/main/java/org/fisco/bcos/sdk/eventsub/EventSubscribeImp.java

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515

1616
package org.fisco.bcos.sdk.eventsub;
1717

18-
import java.util.Set;
19-
2018
import com.fasterxml.jackson.core.JsonProcessingException;
2119
import com.fasterxml.jackson.databind.ObjectMapper;
22-
import lombok.SneakyThrows;
20+
import java.util.Set;
2321
import org.fisco.bcos.sdk.client.Client;
2422
import org.fisco.bcos.sdk.config.ConfigOption;
2523
import org.fisco.bcos.sdk.crypto.CryptoSuite;
@@ -44,9 +42,13 @@ public EventSubscribeImp(Client client, ConfigOption configOption) throws JniExc
4442
this.groupId = client.getGroup();
4543
this.configOption = configOption;
4644
this.cryptoSuite = client.getCryptoSuite();
47-
this.eventSubscribe = org.fisco.bcos.sdk.jni.event.EventSubscribe.build(configOption.getJniConfig());
45+
this.eventSubscribe =
46+
org.fisco.bcos.sdk.jni.event.EventSubscribe.build(configOption.getJniConfig());
4847

49-
logger.info(" EventSub constructor, group: {}, config: {}", groupId, configOption.getJniConfig());
48+
logger.info(
49+
" EventSub constructor, group: {}, config: {}",
50+
groupId,
51+
configOption.getJniConfig());
5052
}
5153

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

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

94-
eventSubscribe.subscribeEvent(groupId, strParams, new EventSubscribeCallback() {
95-
@Override
96-
public void onResponse(Response response) {
97-
if (response.getErrorCode() != 0) {
98-
logger.error("subscribeEvent response error, errorCode: {}, errorMessage: {}", response.getErrorCode(), response.getErrorMessage());
99-
callback.onReceiveLog("", response.getErrorCode(), null);
100-
return;
101-
}
102-
103-
String strResp = new String(response.getData());
104-
logger.debug("subscribeEvent response, errorCode: {}, errorMessage: {}, data: {}", response.getErrorCode(), response.getErrorMessage(), strResp);
105-
106-
ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
107-
try {
108-
EventSubResponse eventSubResponse = objectMapper.readValue(strResp, EventSubResponse.class);
109-
callback.onReceiveLog(eventSubResponse.getId(), eventSubResponse.getStatus(), eventSubResponse.getLogs());
110-
} catch (JsonProcessingException e) {
111-
logger.error("subscribeEvent response parser json error, resp: {}, e: {}", strResp, e);
112-
}
113-
}
114-
});
96+
eventSubscribe.subscribeEvent(
97+
groupId,
98+
strParams,
99+
new EventSubscribeCallback() {
100+
@Override
101+
public void onResponse(Response response) {
102+
if (response.getErrorCode() != 0) {
103+
logger.error(
104+
"subscribeEvent response error, errorCode: {}, errorMessage: {}",
105+
response.getErrorCode(),
106+
response.getErrorMessage());
107+
callback.onReceiveLog("", response.getErrorCode(), null);
108+
return;
109+
}
110+
111+
String strResp = new String(response.getData());
112+
logger.debug(
113+
"subscribeEvent response, errorCode: {}, errorMessage: {}, data: {}",
114+
response.getErrorCode(),
115+
response.getErrorMessage(),
116+
strResp);
117+
118+
ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
119+
try {
120+
EventSubResponse eventSubResponse =
121+
objectMapper.readValue(strResp, EventSubResponse.class);
122+
callback.onReceiveLog(
123+
eventSubResponse.getId(),
124+
eventSubResponse.getStatus(),
125+
eventSubResponse.getLogs());
126+
} catch (JsonProcessingException e) {
127+
logger.error(
128+
"subscribeEvent response parser json error, resp: {}, e: {}",
129+
strResp,
130+
e);
131+
}
132+
}
133+
});
115134
}
116135

117136
@Override
@@ -126,8 +145,12 @@ public Set<String> getAllSubscribedEvents() {
126145
}
127146

128147
@Override
129-
public void start() { eventSubscribe.start(); }
148+
public void start() {
149+
eventSubscribe.start();
150+
}
130151

131152
@Override
132-
public void stop() { eventSubscribe.stop();}
153+
public void stop() {
154+
eventSubscribe.stop();
155+
}
133156
}

0 commit comments

Comments
 (0)