-
Notifications
You must be signed in to change notification settings - Fork 130
Added eea_getTransactionCount Json Rpc #1861
Changes from 7 commits
0a82f39
34c17c7
295359e
763cccc
9bcc695
3469e50
9a30277
4d2e300
88aebdf
4330f57
21f567a
84ed985
56960d6
11d0da6
dc2a972
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
||
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like the wrong error type There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this have the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a more narrow exception to throw instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, is any JsonRpcErrorResponse acceptable? or do you expecting a specific message? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.