Skip to content

Commit cd1578b

Browse files
committed
feat>(rpc): adapt ethereum event related interfaces
1 parent 1c03c93 commit cd1578b

21 files changed

+1365
-5
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ext {
2323
bcprovJDK18onVersion = '1.75'
2424
webankJavaCryptoVersion = "1.0.3"
2525
junitVersion = '4.13.2'
26+
rxjavaVersion = '2.2.2'
2627
commonsCollections4Version = "4.4"
2728
bcosSdkJniVersion = "3.7.0"
2829
slf4jApiVerison = '1.7.36'
@@ -140,6 +141,7 @@ dependencies {
140141
api("com.moandjiezana.toml:toml4j:${toml4jVersion}") {
141142
exclude group: "com.google.code.gson"
142143
}
144+
api("io.reactivex.rxjava2:rxjava:$rxjavaVersion")
143145

144146
integrationTestImplementation project
145147
integrationWasmTestImplementation project

src/main/java/org/fisco/bcos/sdk/v3/BcosSDK.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.fisco.bcos.sdk.v3.config.ConfigOption;
2323
import org.fisco.bcos.sdk.v3.config.exceptions.ConfigException;
2424
import org.fisco.bcos.sdk.v3.eventsub.EventSubscribe;
25+
import org.fisco.bcos.sdk.v3.filter.FilterSystem;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728

@@ -150,6 +151,38 @@ public EventSubscribe getEventSubscribe(String groupId) throws BcosSDKException
150151
}
151152
}
152153

154+
/**
155+
* Get an event subscribe instance of a specific group
156+
*
157+
* @param client
158+
* @param poolSize the size of scheduledExecutorService
159+
* @return FilterSystem
160+
*/
161+
public FilterSystem getFilterSystem(Client client, int poolSize) throws BcosSDKException {
162+
try {
163+
return new FilterSystem(client, poolSize);
164+
} catch (Exception e) {
165+
throw new BcosSDKException("get filter system failed, e: " + e.getMessage());
166+
}
167+
}
168+
169+
/**
170+
* Get an event subscribe instance of a specific group
171+
*
172+
* @param client
173+
* @param poolSize the size of scheduledExecutorService
174+
* @param pollingInterval The time interval for polling getFilterChange
175+
* @return FilterSystem
176+
*/
177+
public FilterSystem getFilterSystem(Client client, int poolSize, long pollingInterval)
178+
throws BcosSDKException {
179+
try {
180+
return new FilterSystem(client, poolSize, pollingInterval);
181+
} catch (Exception e) {
182+
throw new BcosSDKException("get filter system failed, e: " + e.getMessage());
183+
}
184+
}
185+
153186
/** Stop all module of BcosSDK */
154187
public void stopAll() {}
155188
}

src/main/java/org/fisco/bcos/sdk/v3/client/Client.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import org.fisco.bcos.sdk.v3.client.protocol.response.Call;
3232
import org.fisco.bcos.sdk.v3.client.protocol.response.Code;
3333
import org.fisco.bcos.sdk.v3.client.protocol.response.ConsensusStatus;
34+
import org.fisco.bcos.sdk.v3.client.protocol.response.EthFilter;
35+
import org.fisco.bcos.sdk.v3.client.protocol.response.EthLog;
36+
import org.fisco.bcos.sdk.v3.client.protocol.response.EthUninstallFilter;
3437
import org.fisco.bcos.sdk.v3.client.protocol.response.GroupPeers;
3538
import org.fisco.bcos.sdk.v3.client.protocol.response.ObserverList;
3639
import org.fisco.bcos.sdk.v3.client.protocol.response.PbftView;
@@ -1016,6 +1019,20 @@ void getChainCompatibilityVersionAsync(
10161019
*/
10171020
int getNegotiatedProtocol();
10181021

1022+
EthFilter newFilter(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter);
1023+
1024+
EthFilter newBlockFilter();
1025+
1026+
EthFilter newPendingTransactionFilter();
1027+
1028+
EthLog getFilterChanges(EthFilter filter);
1029+
1030+
EthUninstallFilter uninstallFilter(EthFilter filter);
1031+
1032+
EthLog getLogs(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter);
1033+
1034+
EthLog getFilterLogs(EthFilter filter);
1035+
10191036
void start();
10201037

10211038
void stop();

src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package org.fisco.bcos.sdk.v3.client;
1515

16+
import static org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory.getObjectMapper;
17+
1618
import com.fasterxml.jackson.core.JsonProcessingException;
1719
import com.fasterxml.jackson.databind.ObjectMapper;
1820
import java.math.BigInteger;
@@ -43,6 +45,9 @@
4345
import org.fisco.bcos.sdk.v3.client.protocol.response.Call;
4446
import org.fisco.bcos.sdk.v3.client.protocol.response.Code;
4547
import org.fisco.bcos.sdk.v3.client.protocol.response.ConsensusStatus;
48+
import org.fisco.bcos.sdk.v3.client.protocol.response.EthFilter;
49+
import org.fisco.bcos.sdk.v3.client.protocol.response.EthLog;
50+
import org.fisco.bcos.sdk.v3.client.protocol.response.EthUninstallFilter;
4651
import org.fisco.bcos.sdk.v3.client.protocol.response.GroupPeers;
4752
import org.fisco.bcos.sdk.v3.client.protocol.response.ObserverList;
4853
import org.fisco.bcos.sdk.v3.client.protocol.response.PbftView;
@@ -63,7 +68,6 @@
6368
import org.fisco.bcos.sdk.v3.model.callback.ResponseCallback;
6469
import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback;
6570
import org.fisco.bcos.sdk.v3.utils.Hex;
66-
import org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory;
6771
import org.slf4j.Logger;
6872
import org.slf4j.LoggerFactory;
6973

@@ -94,7 +98,7 @@ public class ClientImpl implements Client {
9498
private CryptoSuite cryptoSuite;
9599
private RpcJniObj rpcJniObj;
96100

97-
protected final ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
101+
protected final ObjectMapper objectMapper = getObjectMapper();
98102

99103
protected void initGroupInfo() {
100104
this.groupInfo = getGroupInfo().getResult();
@@ -1306,13 +1310,84 @@ public String getNodeToSendRequest() {
13061310
* with max and min version bits combined, which is (max|min). Max protocol version is in first
13071311
* 16 bit, and min protocol version in the second 16 bit.
13081312
*
1309-
* @return (max|min) bits combined.
1313+
* @return (max | min) bits combined.
13101314
*/
13111315
@Override
13121316
public int getNegotiatedProtocol() {
13131317
return negotiatedProtocol;
13141318
}
13151319

1320+
@Override
1321+
public EthFilter newFilter(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params) {
1322+
return this.callRemoteMethod(
1323+
this.groupID,
1324+
"",
1325+
new JsonRpcRequest<>(
1326+
JsonRpcMethods.NEW_FILTER, Arrays.asList(this.groupID, params)),
1327+
EthFilter.class);
1328+
}
1329+
1330+
@Override
1331+
public EthFilter newBlockFilter() {
1332+
return this.callRemoteMethod(
1333+
this.groupID,
1334+
"",
1335+
new JsonRpcRequest<>(JsonRpcMethods.NEW_BLOCK_FILTER, Arrays.asList(this.groupID)),
1336+
EthFilter.class);
1337+
}
1338+
1339+
@Override
1340+
public EthFilter newPendingTransactionFilter() {
1341+
return this.callRemoteMethod(
1342+
this.groupID,
1343+
"",
1344+
new JsonRpcRequest<>(
1345+
JsonRpcMethods.NEW_PENDING_TX_FILTER, Arrays.asList(this.groupID)),
1346+
EthFilter.class);
1347+
}
1348+
1349+
@Override
1350+
public EthLog getFilterChanges(EthFilter filter) {
1351+
return this.callRemoteMethod(
1352+
this.groupID,
1353+
"",
1354+
new JsonRpcRequest<>(
1355+
JsonRpcMethods.GET_FILTER_CHANGES,
1356+
Arrays.asList(this.groupID, filter.getResult())),
1357+
EthLog.class);
1358+
}
1359+
1360+
@Override
1361+
public EthUninstallFilter uninstallFilter(EthFilter filter) {
1362+
return this.callRemoteMethod(
1363+
this.groupID,
1364+
"",
1365+
new JsonRpcRequest<>(
1366+
JsonRpcMethods.UNINSTALL_FILTER,
1367+
Arrays.asList(this.groupID, filter.getResult())),
1368+
EthUninstallFilter.class);
1369+
}
1370+
1371+
@Override
1372+
public EthLog getLogs(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params) {
1373+
return this.callRemoteMethod(
1374+
this.groupID,
1375+
"",
1376+
new JsonRpcRequest<>(JsonRpcMethods.GET_LOGS, Arrays.asList(this.groupID, params)),
1377+
EthLog.class);
1378+
}
1379+
1380+
@Override
1381+
public EthLog getFilterLogs(EthFilter filter) {
1382+
return this.callRemoteMethod(
1383+
this.groupID,
1384+
"",
1385+
new JsonRpcRequest<>(
1386+
JsonRpcMethods.GET_FILTER_LOGS,
1387+
Arrays.asList(this.groupID, filter.getResult())),
1388+
EthLog.class);
1389+
}
1390+
13161391
@Override
13171392
public void start() {
13181393
if (rpcJniObj != null) {
@@ -1446,8 +1521,7 @@ public static <T extends JsonRpcResponse<?>> T parseResponseIntoJsonRpcResponse(
14461521
if (response.getErrorCode() == 0) {
14471522
// parse the response into JsonRPCResponse
14481523
T jsonRpcResponse =
1449-
ObjectMapperFactory.getObjectMapper()
1450-
.readValue(response.getContent(), responseType);
1524+
getObjectMapper().readValue(response.getContent(), responseType);
14511525
if (jsonRpcResponse.getError() != null) {
14521526
logger.error(
14531527
"parseResponseIntoJsonRpcResponse failed for non-empty error message, method: {}, retErrorMessage: {}, retErrorCode: {}",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.fisco.bcos.sdk.v3.client.protocol.request;
2+
3+
import java.math.BigInteger;
4+
5+
/**
6+
* Wrapper for parameter that takes either a block number or block name as input
7+
*
8+
* <p>See the <a href="https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter">
9+
* specification</a> for further information.
10+
*/
11+
public interface DefaultBlockParameter {
12+
static DefaultBlockParameter valueOf(BigInteger blockNumber) {
13+
if (BigInteger.ZERO.compareTo(blockNumber) >= 0) {
14+
blockNumber = BigInteger.ZERO;
15+
}
16+
return new DefaultBlockParameterNumber(blockNumber);
17+
}
18+
19+
static DefaultBlockParameter valueOf(int blockNumber) {
20+
return valueOf(BigInteger.valueOf(blockNumber));
21+
}
22+
23+
static DefaultBlockParameter valueOf(String blockName) {
24+
return DefaultBlockParameterName.fromString(blockName);
25+
}
26+
27+
String getValue();
28+
29+
public boolean isLatest();
30+
31+
public boolean isEarliest();
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.fisco.bcos.sdk.v3.client.protocol.request;
2+
3+
import com.fasterxml.jackson.annotation.JsonValue;
4+
5+
/** https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter */
6+
public enum DefaultBlockParameterName implements DefaultBlockParameter {
7+
EARLIEST("earliest"),
8+
LATEST("latest");
9+
// PENDING("pending"),
10+
// FINALIZED("finalized"),
11+
// SAFE("safe"),
12+
// ACCEPTED("accepted");
13+
14+
private String name;
15+
16+
DefaultBlockParameterName(String name) {
17+
this.name = name;
18+
}
19+
20+
@JsonValue
21+
@Override
22+
public String getValue() {
23+
return name;
24+
}
25+
26+
public static DefaultBlockParameterName fromString(String name) {
27+
if (name != null) {
28+
for (DefaultBlockParameterName defaultBlockParameterName :
29+
DefaultBlockParameterName.values()) {
30+
if (name.equalsIgnoreCase(defaultBlockParameterName.name)) {
31+
return defaultBlockParameterName;
32+
}
33+
}
34+
}
35+
return valueOf(name);
36+
}
37+
38+
@Override
39+
public boolean isLatest() {
40+
return this == LATEST;
41+
}
42+
43+
@Override
44+
public boolean isEarliest() {
45+
return this == EARLIEST;
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.fisco.bcos.sdk.v3.client.protocol.request;
2+
3+
import com.fasterxml.jackson.annotation.JsonValue;
4+
import java.math.BigInteger;
5+
import org.fisco.bcos.sdk.v3.utils.Numeric;
6+
7+
/** DefaultBlockParameter implementation that takes a numeric value. */
8+
public class DefaultBlockParameterNumber implements DefaultBlockParameter {
9+
10+
private BigInteger blockNumber;
11+
12+
public DefaultBlockParameterNumber(BigInteger blockNumber) {
13+
this.blockNumber = blockNumber;
14+
}
15+
16+
public DefaultBlockParameterNumber(long blockNumber) {
17+
this(BigInteger.valueOf(blockNumber));
18+
}
19+
20+
@Override
21+
@JsonValue
22+
public String getValue() {
23+
return Numeric.encodeQuantity(blockNumber);
24+
}
25+
26+
public BigInteger getBlockNumber() {
27+
return blockNumber;
28+
}
29+
30+
@Override
31+
public boolean isLatest() {
32+
return false;
33+
}
34+
35+
@Override
36+
public boolean isEarliest() {
37+
return false;
38+
}
39+
}

0 commit comments

Comments
 (0)