Skip to content

Commit 6a7b902

Browse files
jdkuangxxjdkuangx
andauthored
<feat>(rpc): adapt ethereum event related interfaces (#920)
Co-authored-by: jdkuang <[email protected]>
1 parent 1c03c93 commit 6a7b902

22 files changed

+1369
-5
lines changed

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: 35 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,38 @@ void getChainCompatibilityVersionAsync(
10161019
*/
10171020
int getNegotiatedProtocol();
10181021

1022+
EthFilter newFilter(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter);
1023+
1024+
void newFilterAsync(
1025+
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter,
1026+
RespCallback<EthFilter> callback);
1027+
1028+
EthFilter newBlockFilter();
1029+
1030+
void newBlockFilterAsync(RespCallback<EthFilter> callback);
1031+
1032+
EthFilter newPendingTransactionFilter();
1033+
1034+
void newPendingTransactionFilterAsync(RespCallback<EthFilter> callback);
1035+
1036+
EthLog getFilterChanges(EthFilter filter);
1037+
1038+
void getFilterChangesAsync(EthFilter filter, RespCallback<EthLog> callback);
1039+
1040+
EthUninstallFilter uninstallFilter(EthFilter filter);
1041+
1042+
void uninstallFilterAsync(EthFilter filter, RespCallback<EthUninstallFilter> callback);
1043+
1044+
EthLog getLogs(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter);
1045+
1046+
void getLogsAsync(
1047+
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter,
1048+
RespCallback<EthLog> callback);
1049+
1050+
EthLog getFilterLogs(EthFilter filter);
1051+
1052+
void getFilterLogsAsync(EthFilter filter, RespCallback<EthLog> callback);
1053+
10191054
void start();
10201055

10211056
void stop();

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

Lines changed: 161 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,166 @@ 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 void newFilterAsync(
1332+
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params,
1333+
RespCallback<EthFilter> callback) {
1334+
this.asyncCallRemoteMethod(
1335+
this.groupID,
1336+
"",
1337+
new JsonRpcRequest<>(JsonRpcMethods.NEW_FILTER, Arrays.asList(groupID, params)),
1338+
EthFilter.class,
1339+
callback);
1340+
}
1341+
1342+
@Override
1343+
public EthFilter newBlockFilter() {
1344+
return this.callRemoteMethod(
1345+
this.groupID,
1346+
"",
1347+
new JsonRpcRequest<>(JsonRpcMethods.NEW_BLOCK_FILTER, Arrays.asList(this.groupID)),
1348+
EthFilter.class);
1349+
}
1350+
1351+
@Override
1352+
public void newBlockFilterAsync(RespCallback<EthFilter> callback) {
1353+
this.asyncCallRemoteMethod(
1354+
this.groupID,
1355+
"",
1356+
new JsonRpcRequest<>(JsonRpcMethods.NEW_BLOCK_FILTER, Arrays.asList(this.groupID)),
1357+
EthFilter.class,
1358+
callback);
1359+
}
1360+
1361+
@Override
1362+
public EthFilter newPendingTransactionFilter() {
1363+
return this.callRemoteMethod(
1364+
this.groupID,
1365+
"",
1366+
new JsonRpcRequest<>(
1367+
JsonRpcMethods.NEW_PENDING_TX_FILTER, Arrays.asList(this.groupID)),
1368+
EthFilter.class);
1369+
}
1370+
1371+
@Override
1372+
public void newPendingTransactionFilterAsync(RespCallback<EthFilter> callback) {
1373+
this.asyncCallRemoteMethod(
1374+
this.groupID,
1375+
"",
1376+
new JsonRpcRequest<>(
1377+
JsonRpcMethods.NEW_PENDING_TX_FILTER, Arrays.asList(this.groupID)),
1378+
EthFilter.class,
1379+
callback);
1380+
}
1381+
1382+
@Override
1383+
public EthLog getFilterChanges(EthFilter filter) {
1384+
return this.callRemoteMethod(
1385+
this.groupID,
1386+
"",
1387+
new JsonRpcRequest<>(
1388+
JsonRpcMethods.GET_FILTER_CHANGES,
1389+
Arrays.asList(this.groupID, filter.getResult())),
1390+
EthLog.class);
1391+
}
1392+
1393+
@Override
1394+
public void getFilterChangesAsync(EthFilter filter, RespCallback<EthLog> callback) {
1395+
this.asyncCallRemoteMethod(
1396+
this.groupID,
1397+
"",
1398+
new JsonRpcRequest<>(
1399+
JsonRpcMethods.GET_FILTER_CHANGES,
1400+
Arrays.asList(this.groupID, filter.getResult())),
1401+
EthLog.class,
1402+
callback);
1403+
}
1404+
1405+
@Override
1406+
public EthUninstallFilter uninstallFilter(EthFilter filter) {
1407+
return this.callRemoteMethod(
1408+
this.groupID,
1409+
"",
1410+
new JsonRpcRequest<>(
1411+
JsonRpcMethods.UNINSTALL_FILTER,
1412+
Arrays.asList(this.groupID, filter.getResult())),
1413+
EthUninstallFilter.class);
1414+
}
1415+
1416+
@Override
1417+
public void uninstallFilterAsync(EthFilter filter, RespCallback<EthUninstallFilter> callback) {
1418+
this.asyncCallRemoteMethod(
1419+
this.groupID,
1420+
"",
1421+
new JsonRpcRequest<>(
1422+
JsonRpcMethods.UNINSTALL_FILTER,
1423+
Arrays.asList(this.groupID, filter.getResult())),
1424+
EthUninstallFilter.class,
1425+
callback);
1426+
}
1427+
1428+
@Override
1429+
public EthLog getLogs(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params) {
1430+
return this.callRemoteMethod(
1431+
this.groupID,
1432+
"",
1433+
new JsonRpcRequest<>(JsonRpcMethods.GET_LOGS, Arrays.asList(this.groupID, params)),
1434+
EthLog.class);
1435+
}
1436+
1437+
@Override
1438+
public void getLogsAsync(
1439+
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params,
1440+
RespCallback<EthLog> callback) {
1441+
this.asyncCallRemoteMethod(
1442+
this.groupID,
1443+
"",
1444+
new JsonRpcRequest<>(JsonRpcMethods.GET_LOGS, Arrays.asList(this.groupID, params)),
1445+
EthLog.class,
1446+
callback);
1447+
}
1448+
1449+
@Override
1450+
public EthLog getFilterLogs(EthFilter filter) {
1451+
return this.callRemoteMethod(
1452+
this.groupID,
1453+
"",
1454+
new JsonRpcRequest<>(
1455+
JsonRpcMethods.GET_FILTER_LOGS,
1456+
Arrays.asList(this.groupID, filter.getResult())),
1457+
EthLog.class);
1458+
}
1459+
1460+
@Override
1461+
public void getFilterLogsAsync(EthFilter filter, RespCallback<EthLog> callback) {
1462+
1463+
this.asyncCallRemoteMethod(
1464+
this.groupID,
1465+
"",
1466+
new JsonRpcRequest<>(
1467+
JsonRpcMethods.GET_FILTER_LOGS,
1468+
Arrays.asList(this.groupID, filter.getResult())),
1469+
EthLog.class,
1470+
callback);
1471+
}
1472+
13161473
@Override
13171474
public void start() {
13181475
if (rpcJniObj != null) {
@@ -1446,8 +1603,7 @@ public static <T extends JsonRpcResponse<?>> T parseResponseIntoJsonRpcResponse(
14461603
if (response.getErrorCode() == 0) {
14471604
// parse the response into JsonRPCResponse
14481605
T jsonRpcResponse =
1449-
ObjectMapperFactory.getObjectMapper()
1450-
.readValue(response.getContent(), responseType);
1606+
getObjectMapper().readValue(response.getContent(), responseType);
14511607
if (jsonRpcResponse.getError() != null) {
14521608
logger.error(
14531609
"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+
}

0 commit comments

Comments
 (0)