Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Added eea_getTransactionCount Json Rpc #1861

Merged
merged 15 commits into from
Aug 26, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning.PermReloadPermissionsFromFile;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning.PermRemoveAccountsFromWhitelist;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.permissioning.PermRemoveNodesFromWhitelist;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea.EeaGetTransactionCount;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea.EeaGetTransactionReceipt;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea.EeaSendRawTransaction;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea.LegacyNonceProvider;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.priv.PrivCreatePrivacyGroup;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.priv.PrivDeletePrivacyGroup;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.priv.PrivFindPrivacyGroup;
Expand Down Expand Up @@ -345,7 +347,9 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter),
addMethods(
enabledMethods,
new EeaGetTransactionReceipt(blockchainQueries, enclave, parameter, privacyParameters),
new EeaSendRawTransaction(privateTransactionHandler, transactionPool, parameter));
new EeaSendRawTransaction(privateTransactionHandler, transactionPool, parameter),
new EeaGetTransactionCount(
parameter, new LegacyNonceProvider(enclave, privateTransactionHandler)));
}
if (priv) {
addMethods(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum RpcMethod {
PRIV_DELETE_PRIVACY_GROUP("priv_deletePrivacyGroup"),
PRIV_FIND_PRIVACY_GROUP("priv_findPrivacyGroup"),
EEA_SEND_RAW_TRANSACTION("eea_sendRawTransaction"),
EEA_GET_TRANSACTION_COUNT("eea_getTransactionCount"),
ETH_ACCOUNTS("eth_accounts"),
ETH_BLOCK_NUMBER("eth_blockNumber"),
ETH_CALL("eth_call"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea;

import static org.apache.logging.log4j.LogManager.getLogger;

import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.Quantity;

import org.apache.logging.log4j.Logger;

public class EeaGetTransactionCount implements JsonRpcMethod {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think this class should be name PrivGetLegacyTransactionCount

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked to Madeline last night - given this capability is really required of Legacy Privacy - it belongs in the Eea namespace, and thus, the spec will need updating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More to the point - priv_ namespace is geared toward the new Privacy Group - so having "priv" and "legacy" in a name is not a valid combination.


private static final Logger LOG = getLogger();

private final JsonRpcParameter parameters;
private final LegacyNonceProvider nonceProvider;

public EeaGetTransactionCount(
final JsonRpcParameter parameters, final LegacyNonceProvider nonceProvider) {
this.parameters = parameters;
this.nonceProvider = nonceProvider;
}

@Override
public String getName() {
return RpcMethod.EEA_GET_TRANSACTION_COUNT.getMethodName();
}

@Override
public JsonRpcResponse response(final JsonRpcRequest request) {
if (request.getParamLength() != 3) {
return new JsonRpcErrorResponse(request.getId(), JsonRpcError.INVALID_PARAMS);
}

final Address address = parameters.required(request.getParams(), 0, Address.class);
final String privateFrom = parameters.required(request.getParams(), 1, String.class);
final String[] privateFor = parameters.required(request.getParams(), 2, String[].class);

try {
final long nonce = nonceProvider.determineNonce(privateFrom, privateFor, address);
return new JsonRpcSuccessResponse(request.getId(), Quantity.create(nonce));
} catch (final Exception e) {
LOG.error("Failed to fetch group from Enclave with error " + e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think there is any need for two log statements here. Could just do LOG.error("Failed to fetch group from Enclave", e) and let log4j log the exception nicely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

LOG.error(e);
return new JsonRpcErrorResponse(request.getId(), JsonRpcError.FIND_PRIVACY_GROUP_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like the wrong error type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea;

import static org.apache.logging.log4j.LogManager.getLogger;

import tech.pegasys.pantheon.enclave.Enclave;
import tech.pegasys.pantheon.enclave.types.FindPrivacyGroupRequest;
import tech.pegasys.pantheon.enclave.types.PrivacyGroup;
import tech.pegasys.pantheon.enclave.types.PrivacyGroup.Type;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransactionHandler;

import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.util.Arrays;

public class LegacyNonceProvider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have the Legacy prefix, rather than Eea?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because "Legacy" is the default terminology for things which work with EEA privacy ... (i.e. internally the Privacy is deemed LEGACY or PANTHEON.


private static final Logger LOG = getLogger();

private final Enclave enclave;
private final PrivateTransactionHandler privateTransactionHandler;

public LegacyNonceProvider(
final Enclave enclave, final PrivateTransactionHandler privateTransactionHandler) {
this.enclave = enclave;
this.privateTransactionHandler = privateTransactionHandler;
}

public long determineNonce(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more narrow exception to throw instead of Exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alas - this is an effect of Encalve throwing Exception ... this behaviour is mimic'd from other Priv Json RPC ...

final String privateFrom, final String[] privateFor, final Address address) throws Exception {

final String[] groupMembers = Arrays.append(privateFor, privateFrom);

final FindPrivacyGroupRequest request = new FindPrivacyGroupRequest(groupMembers);
final List<PrivacyGroup> matchingGroups = Lists.newArrayList(enclave.findPrivacyGroup(request));

final List<PrivacyGroup> legacyGroups =
matchingGroups.stream()
.filter(group -> group.getType() == Type.LEGACY)
.collect(Collectors.toList());

if (legacyGroups.size() != 1) {
final String errorMessage =
String.format(
"Found invalid number of privacy groups (%d), expected 1.", legacyGroups.size());
LOG.error(errorMessage);
throw new RuntimeException(errorMessage);
}

final String privacyGroupId = legacyGroups.get(0).getPrivacyGroupId();

return privateTransactionHandler.getSenderNonce(address, privacyGroupId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;

import org.junit.Before;
import org.junit.Test;

public class EeaGetTransactionCountTest {

private LegacyNonceProvider nonceProvider = mock(LegacyNonceProvider.class);
private JsonRpcRequest request;

private final String privateFrom = "thePrivateFromKey";
private final String[] privateFor = new String[] {"first", "second", "third"};
private final Address address = Address.fromHexString("55");

@Before
public void setup() {
final Object[] jsonBody = new Object[] {address.toString(), privateFrom, privateFor};
request = new JsonRpcRequest("2.0", "eea_getTransactionCount", jsonBody);
}

@Test
public void validRequestProducesExpectedNonce() throws Exception {
final long reportedNonce = 8L;
final EeaGetTransactionCount method =
new EeaGetTransactionCount(new JsonRpcParameter(), nonceProvider);

when(nonceProvider.determineNonce(privateFrom, privateFor, address)).thenReturn(reportedNonce);

final JsonRpcResponse response = method.response(request);
assertThat(response).isInstanceOf(JsonRpcSuccessResponse.class);

final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
int returnedValue = Integer.decode((String) successResponse.getResult());
assertThat(returnedValue).isEqualTo(reportedNonce);
}

@Test
public void nonceProviderThrowsRuntimeExceptionProducesErrorResponse() throws Exception {
final EeaGetTransactionCount method =
new EeaGetTransactionCount(new JsonRpcParameter(), nonceProvider);

when(nonceProvider.determineNonce(privateFrom, privateFor, address))
.thenThrow(RuntimeException.class);

final JsonRpcResponse response = method.response(request);
assertThat(response).isInstanceOf(JsonRpcErrorResponse.class);

final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
assertThat(errorResponse.getError()).isEqualTo(JsonRpcError.FIND_PRIVACY_GROUP_ERROR);
}

@Test
public void nonceProviderThrowsAnExceptionProducesErrorResponse() throws Exception {
final EeaGetTransactionCount method =
new EeaGetTransactionCount(new JsonRpcParameter(), nonceProvider);

when(nonceProvider.determineNonce(privateFrom, privateFor, address))
.thenThrow(RuntimeException.class);

final JsonRpcResponse response = method.response(request);
assertThat(response).isInstanceOf(JsonRpcErrorResponse.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, is any JsonRpcErrorResponse acceptable? or do you expecting a specific message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


final JsonRpcErrorResponse errorResponse = (JsonRpcErrorResponse) response;
assertThat(errorResponse.getError()).isEqualTo(JsonRpcError.FIND_PRIVACY_GROUP_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy.eea;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.enclave.Enclave;
import tech.pegasys.pantheon.enclave.types.FindPrivacyGroupRequest;
import tech.pegasys.pantheon.enclave.types.PrivacyGroup;
import tech.pegasys.pantheon.enclave.types.PrivacyGroup.Type;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransactionHandler;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

public class LegacyNonceProviderTest {

private final Address address = Address.fromHexString("55");
private Enclave enclave = mock(Enclave.class);
private PrivateTransactionHandler privateTransactionHandler =
mock(PrivateTransactionHandler.class);

private final LegacyNonceProvider nonceProvider =
new LegacyNonceProvider(enclave, privateTransactionHandler);

@Test
public void validRequestProducesExpectedNonce() throws Exception {
final long reportedNonce = 8L;
PrivacyGroup[] returnedGroups =
new PrivacyGroup[] {
new PrivacyGroup("Group1", Type.LEGACY, "Group1_Name", "Group1_Desc", new String[0]),
};

final ArgumentCaptor<FindPrivacyGroupRequest> groupMembersCaptor =
ArgumentCaptor.forClass(FindPrivacyGroupRequest.class);

when(enclave.findPrivacyGroup(groupMembersCaptor.capture())).thenReturn(returnedGroups);
when(privateTransactionHandler.getSenderNonce(address, "Group1")).thenReturn(reportedNonce);

final long nonce =
nonceProvider.determineNonce("privateFrom", new String[] {"first", "second"}, address);

assertThat(nonce).isEqualTo(reportedNonce);
assertThat(groupMembersCaptor.getValue().addresses())
.containsAll(Lists.newArrayList("privateFrom", "first", "second"));
}

@Test
public void moreThanOneMatchingLegacyGroupThrowsException() throws Exception {
PrivacyGroup[] returnedGroups =
new PrivacyGroup[] {
new PrivacyGroup("Group1", Type.LEGACY, "Group1_Name", "Group1_Desc", new String[0]),
new PrivacyGroup("Group2", Type.LEGACY, "Group2_Name", "Group2_Desc", new String[0]),
};

when(enclave.findPrivacyGroup(any())).thenReturn(returnedGroups);

assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(
() ->
nonceProvider.determineNonce(
"privateFrom", new String[] {"first", "second"}, address));
}

@Test
public void noMatchingLegacyGroupsReturnsError() throws Exception {
PrivacyGroup[] returnedGroups = new PrivacyGroup[] {};

when(enclave.findPrivacyGroup(any())).thenReturn(returnedGroups);

assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(
() ->
nonceProvider.determineNonce(
"privateFrom", new String[] {"first", "second"}, address));
}

@Test
public void enclaveThrowingAnExceptionResultsInErrorResponse() throws Exception {
when(enclave.findPrivacyGroup(any())).thenThrow(new Exception("Enclave Failed"));
assertThatExceptionOfType(Exception.class)
.isThrownBy(
() ->
nonceProvider.determineNonce(
"privateFrom", new String[] {"first", "second"}, address));
}
}
4 changes: 1 addition & 3 deletions metrics/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ jar {

publishing {
publications {
mavenJava(MavenPublication) {
artifactId 'metrics-core'
}
mavenJava(MavenPublication) { artifactId 'metrics-core' }
}
}

Expand Down
4 changes: 1 addition & 3 deletions services/util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ jar {

publishing {
publications {
mavenJava(MavenPublication) {
artifactId 'services-util'
}
mavenJava(MavenPublication) { artifactId 'services-util' }
}
}

Expand Down