Supporting list of addresses in logs subscription (#336)

Lucas Saldanha 6 years ago committed by GitHub
parent 081c2f92a6
commit 61af57f716
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/subscription/request/LogsSubscriptionParam.java
  2. 2
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/subscription/request/SubscriptionRequestMapper.java
  3. 53
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/websocket/subscription/request/SubscriptionRequestMapperTest.java

@ -15,22 +15,24 @@ package tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
class LogsSubscriptionParam { class LogsSubscriptionParam {
private final String address; private final List<String> address;
private final List<String> topics; private final List<String> topics;
@JsonCreator @JsonCreator
LogsSubscriptionParam( LogsSubscriptionParam(
@JsonProperty("address") final String address, @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY) @JsonProperty("address")
final List<String> address,
@JsonProperty("topics") final List<String> topics) { @JsonProperty("topics") final List<String> topics) {
this.address = address; this.address = address;
this.topics = topics; this.topics = topics;
} }
String address() { List<String> address() {
return address; return address;
} }

@ -86,7 +86,7 @@ public class SubscriptionRequestMapper {
private List<String> hasAddresses(final LogsSubscriptionParam logFilterParams) { private List<String> hasAddresses(final LogsSubscriptionParam logFilterParams) {
return logFilterParams.address() != null && !logFilterParams.address().isEmpty() return logFilterParams.address() != null && !logFilterParams.address().isEmpty()
? Arrays.asList(logFilterParams.address()) ? logFilterParams.address()
: Collections.emptyList(); : Collections.emptyList();
} }

@ -150,16 +150,40 @@ public class SubscriptionRequestMapperTest {
} }
@Test @Test
public void mapRequestToLogs() { public void mapRequestWithSingleAddress() {
final JsonRpcRequest jsonRpcRequest = final JsonRpcRequest jsonRpcRequest =
parseWebSocketRpcRequest( parseWebSocketRpcRequest(
"{\"id\": 1, \"method\": \"eth_subscribe\", \"params\": [\"logs\", {\"address\": \"0x8320fe7702b96808f7bbc0d4a888ed1468216cfd\", \"topics\": [\"0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902\"]}]}"); "{\"id\": 1, \"method\": \"eth_subscribe\", \"params\": [\"logs\", {\"address\": \"0x8320fe7702b96808f7bbc0d4a888ed1468216cfd\"}]}");
final FilterParameter expectedFilterParam = final FilterParameter expectedFilterParam =
new FilterParameter( new FilterParameter(
null, null,
null, null,
Arrays.asList("0x8320fe7702b96808f7bbc0d4a888ed1468216cfd"), Arrays.asList("0x8320fe7702b96808f7bbc0d4a888ed1468216cfd"),
Collections.emptyList(),
null);
final SubscribeRequest expectedSubscribeRequest =
new SubscribeRequest(SubscriptionType.LOGS, expectedFilterParam, null, null);
final SubscribeRequest subscribeRequest = mapper.mapSubscribeRequest(jsonRpcRequest);
assertThat(subscribeRequest)
.isEqualToComparingFieldByFieldRecursively(expectedSubscribeRequest);
}
@Test
public void mapRequestWithMultipleAddresses() {
final JsonRpcRequest jsonRpcRequest =
parseWebSocketRpcRequest(
"{\"id\": 1, \"method\": \"eth_subscribe\", \"params\": [\"logs\", {\"address\": [\"0x8320fe7702b96808f7bbc0d4a888ed1468216cfd\", \"0xf17f52151EbEF6C7334FAD080c5704D77216b732\"], \"topics\": [\"0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902\"]}]}");
final FilterParameter expectedFilterParam =
new FilterParameter(
null,
null,
Arrays.asList(
"0x8320fe7702b96808f7bbc0d4a888ed1468216cfd",
"0xf17f52151EbEF6C7334FAD080c5704D77216b732"),
Arrays.asList( Arrays.asList(
Arrays.asList( Arrays.asList(
"0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902")), "0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902")),
@ -173,6 +197,31 @@ public class SubscriptionRequestMapperTest {
.isEqualToComparingFieldByFieldRecursively(expectedSubscribeRequest); .isEqualToComparingFieldByFieldRecursively(expectedSubscribeRequest);
} }
@Test
public void mapRequestWithMultipleTopics() {
final JsonRpcRequest jsonRpcRequest =
parseWebSocketRpcRequest(
"{\"id\": 1, \"method\": \"eth_subscribe\", \"params\": [\"logs\", {\"address\": \"0x8320fe7702b96808f7bbc0d4a888ed1468216cfd\", \"topics\": [\"0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902\", \"0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab901\"]}]}");
final FilterParameter expectedFilterParam =
new FilterParameter(
null,
null,
Arrays.asList("0x8320fe7702b96808f7bbc0d4a888ed1468216cfd"),
Arrays.asList(
Arrays.asList(
"0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902",
"0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab901")),
null);
final SubscribeRequest expectedSubscribeRequest =
new SubscribeRequest(SubscriptionType.LOGS, expectedFilterParam, null, null);
final SubscribeRequest subscribeRequest = mapper.mapSubscribeRequest(jsonRpcRequest);
assertThat(subscribeRequest)
.isEqualToComparingFieldByFieldRecursively(expectedSubscribeRequest);
}
@Test @Test
public void mapRequestToLogsWithoutTopics() { public void mapRequestToLogsWithoutTopics() {
final JsonRpcRequest jsonRpcRequest = final JsonRpcRequest jsonRpcRequest =

Loading…
Cancel
Save