Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<feat>(rpc): adapt ethereum event related interfaces #920

Merged
merged 1 commit into from
Jun 5, 2024
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
33 changes: 33 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/BcosSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.fisco.bcos.sdk.v3.config.ConfigOption;
import org.fisco.bcos.sdk.v3.config.exceptions.ConfigException;
import org.fisco.bcos.sdk.v3.eventsub.EventSubscribe;
import org.fisco.bcos.sdk.v3.filter.FilterSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -150,6 +151,38 @@ public EventSubscribe getEventSubscribe(String groupId) throws BcosSDKException
}
}

/**
* Get an event subscribe instance of a specific group
*
* @param client
* @param poolSize the size of scheduledExecutorService
* @return FilterSystem
*/
public FilterSystem getFilterSystem(Client client, int poolSize) throws BcosSDKException {
try {
return new FilterSystem(client, poolSize);
} catch (Exception e) {
throw new BcosSDKException("get filter system failed, e: " + e.getMessage());
}
}

/**
* Get an event subscribe instance of a specific group
*
* @param client
* @param poolSize the size of scheduledExecutorService
* @param pollingInterval The time interval for polling getFilterChange
* @return FilterSystem
*/
public FilterSystem getFilterSystem(Client client, int poolSize, long pollingInterval)
throws BcosSDKException {
try {
return new FilterSystem(client, poolSize, pollingInterval);
} catch (Exception e) {
throw new BcosSDKException("get filter system failed, e: " + e.getMessage());
}
}

/** Stop all module of BcosSDK */
public void stopAll() {}
}
35 changes: 35 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.fisco.bcos.sdk.v3.client.protocol.response.Call;
import org.fisco.bcos.sdk.v3.client.protocol.response.Code;
import org.fisco.bcos.sdk.v3.client.protocol.response.ConsensusStatus;
import org.fisco.bcos.sdk.v3.client.protocol.response.EthFilter;
import org.fisco.bcos.sdk.v3.client.protocol.response.EthLog;
import org.fisco.bcos.sdk.v3.client.protocol.response.EthUninstallFilter;
import org.fisco.bcos.sdk.v3.client.protocol.response.GroupPeers;
import org.fisco.bcos.sdk.v3.client.protocol.response.ObserverList;
import org.fisco.bcos.sdk.v3.client.protocol.response.PbftView;
Expand Down Expand Up @@ -1016,6 +1019,38 @@ void getChainCompatibilityVersionAsync(
*/
int getNegotiatedProtocol();

EthFilter newFilter(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter);

void newFilterAsync(
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter,
RespCallback<EthFilter> callback);

EthFilter newBlockFilter();

void newBlockFilterAsync(RespCallback<EthFilter> callback);

EthFilter newPendingTransactionFilter();

void newPendingTransactionFilterAsync(RespCallback<EthFilter> callback);

EthLog getFilterChanges(EthFilter filter);

void getFilterChangesAsync(EthFilter filter, RespCallback<EthLog> callback);

EthUninstallFilter uninstallFilter(EthFilter filter);

void uninstallFilterAsync(EthFilter filter, RespCallback<EthUninstallFilter> callback);

EthLog getLogs(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter);

void getLogsAsync(
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter filter,
RespCallback<EthLog> callback);

EthLog getFilterLogs(EthFilter filter);

void getFilterLogsAsync(EthFilter filter, RespCallback<EthLog> callback);

void start();

void stop();
Expand Down
166 changes: 161 additions & 5 deletions src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.fisco.bcos.sdk.v3.client;

import static org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory.getObjectMapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.math.BigInteger;
Expand Down Expand Up @@ -43,6 +45,9 @@
import org.fisco.bcos.sdk.v3.client.protocol.response.Call;
import org.fisco.bcos.sdk.v3.client.protocol.response.Code;
import org.fisco.bcos.sdk.v3.client.protocol.response.ConsensusStatus;
import org.fisco.bcos.sdk.v3.client.protocol.response.EthFilter;
import org.fisco.bcos.sdk.v3.client.protocol.response.EthLog;
import org.fisco.bcos.sdk.v3.client.protocol.response.EthUninstallFilter;
import org.fisco.bcos.sdk.v3.client.protocol.response.GroupPeers;
import org.fisco.bcos.sdk.v3.client.protocol.response.ObserverList;
import org.fisco.bcos.sdk.v3.client.protocol.response.PbftView;
Expand All @@ -63,7 +68,6 @@
import org.fisco.bcos.sdk.v3.model.callback.ResponseCallback;
import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback;
import org.fisco.bcos.sdk.v3.utils.Hex;
import org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -94,7 +98,7 @@ public class ClientImpl implements Client {
private CryptoSuite cryptoSuite;
private RpcJniObj rpcJniObj;

protected final ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
protected final ObjectMapper objectMapper = getObjectMapper();

protected void initGroupInfo() {
this.groupInfo = getGroupInfo().getResult();
Expand Down Expand Up @@ -1306,13 +1310,166 @@ public String getNodeToSendRequest() {
* with max and min version bits combined, which is (max|min). Max protocol version is in first
* 16 bit, and min protocol version in the second 16 bit.
*
* @return (max|min) bits combined.
* @return (max | min) bits combined.
*/
@Override
public int getNegotiatedProtocol() {
return negotiatedProtocol;
}

@Override
public EthFilter newFilter(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params) {
return this.callRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.NEW_FILTER, Arrays.asList(this.groupID, params)),
EthFilter.class);
}

@Override
public void newFilterAsync(
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params,
RespCallback<EthFilter> callback) {
this.asyncCallRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(JsonRpcMethods.NEW_FILTER, Arrays.asList(groupID, params)),
EthFilter.class,
callback);
}

@Override
public EthFilter newBlockFilter() {
return this.callRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(JsonRpcMethods.NEW_BLOCK_FILTER, Arrays.asList(this.groupID)),
EthFilter.class);
}

@Override
public void newBlockFilterAsync(RespCallback<EthFilter> callback) {
this.asyncCallRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(JsonRpcMethods.NEW_BLOCK_FILTER, Arrays.asList(this.groupID)),
EthFilter.class,
callback);
}

@Override
public EthFilter newPendingTransactionFilter() {
return this.callRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.NEW_PENDING_TX_FILTER, Arrays.asList(this.groupID)),
EthFilter.class);
}

@Override
public void newPendingTransactionFilterAsync(RespCallback<EthFilter> callback) {
this.asyncCallRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.NEW_PENDING_TX_FILTER, Arrays.asList(this.groupID)),
EthFilter.class,
callback);
}

@Override
public EthLog getFilterChanges(EthFilter filter) {
return this.callRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.GET_FILTER_CHANGES,
Arrays.asList(this.groupID, filter.getResult())),
EthLog.class);
}

@Override
public void getFilterChangesAsync(EthFilter filter, RespCallback<EthLog> callback) {
this.asyncCallRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.GET_FILTER_CHANGES,
Arrays.asList(this.groupID, filter.getResult())),
EthLog.class,
callback);
}

@Override
public EthUninstallFilter uninstallFilter(EthFilter filter) {
return this.callRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.UNINSTALL_FILTER,
Arrays.asList(this.groupID, filter.getResult())),
EthUninstallFilter.class);
}

@Override
public void uninstallFilterAsync(EthFilter filter, RespCallback<EthUninstallFilter> callback) {
this.asyncCallRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.UNINSTALL_FILTER,
Arrays.asList(this.groupID, filter.getResult())),
EthUninstallFilter.class,
callback);
}

@Override
public EthLog getLogs(org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params) {
return this.callRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(JsonRpcMethods.GET_LOGS, Arrays.asList(this.groupID, params)),
EthLog.class);
}

@Override
public void getLogsAsync(
org.fisco.bcos.sdk.v3.client.protocol.request.EthFilter params,
RespCallback<EthLog> callback) {
this.asyncCallRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(JsonRpcMethods.GET_LOGS, Arrays.asList(this.groupID, params)),
EthLog.class,
callback);
}

@Override
public EthLog getFilterLogs(EthFilter filter) {
return this.callRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.GET_FILTER_LOGS,
Arrays.asList(this.groupID, filter.getResult())),
EthLog.class);
}

@Override
public void getFilterLogsAsync(EthFilter filter, RespCallback<EthLog> callback) {

this.asyncCallRemoteMethod(
this.groupID,
"",
new JsonRpcRequest<>(
JsonRpcMethods.GET_FILTER_LOGS,
Arrays.asList(this.groupID, filter.getResult())),
EthLog.class,
callback);
}

@Override
public void start() {
if (rpcJniObj != null) {
Expand Down Expand Up @@ -1446,8 +1603,7 @@ public static <T extends JsonRpcResponse<?>> T parseResponseIntoJsonRpcResponse(
if (response.getErrorCode() == 0) {
// parse the response into JsonRPCResponse
T jsonRpcResponse =
ObjectMapperFactory.getObjectMapper()
.readValue(response.getContent(), responseType);
getObjectMapper().readValue(response.getContent(), responseType);
if (jsonRpcResponse.getError() != null) {
logger.error(
"parseResponseIntoJsonRpcResponse failed for non-empty error message, method: {}, retErrorMessage: {}, retErrorCode: {}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.fisco.bcos.sdk.v3.client.protocol.request;

import java.math.BigInteger;

/**
* Wrapper for parameter that takes either a block number or block name as input
*
* <p>See the <a href="https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter">
* specification</a> for further information.
*/
public interface DefaultBlockParameter {
static DefaultBlockParameter valueOf(BigInteger blockNumber) {
if (BigInteger.ZERO.compareTo(blockNumber) >= 0) {
blockNumber = BigInteger.ZERO;
}
return new DefaultBlockParameterNumber(blockNumber);
}

static DefaultBlockParameter valueOf(int blockNumber) {
return valueOf(BigInteger.valueOf(blockNumber));
}

static DefaultBlockParameter valueOf(String blockName) {
return DefaultBlockParameterName.fromString(blockName);
}

String getValue();

public boolean isLatest();

public boolean isEarliest();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.fisco.bcos.sdk.v3.client.protocol.request;

import com.fasterxml.jackson.annotation.JsonValue;

/** https://github.com/ethereum/wiki/wiki/JSON-RPC#the-default-block-parameter */
public enum DefaultBlockParameterName implements DefaultBlockParameter {
EARLIEST("earliest"),
LATEST("latest");
// PENDING("pending"),
// FINALIZED("finalized"),
// SAFE("safe"),
// ACCEPTED("accepted");

private String name;

DefaultBlockParameterName(String name) {
this.name = name;
}

@JsonValue
@Override
public String getValue() {
return name;
}

public static DefaultBlockParameterName fromString(String name) {
if (name != null) {
for (DefaultBlockParameterName defaultBlockParameterName :
DefaultBlockParameterName.values()) {
if (name.equalsIgnoreCase(defaultBlockParameterName.name)) {
return defaultBlockParameterName;
}
}
}
return valueOf(name);
}

@Override
public boolean isLatest() {
return this == LATEST;
}

@Override
public boolean isEarliest() {
return this == EARLIEST;
}
}
Loading
Loading