-
Notifications
You must be signed in to change notification settings - Fork 130
Added eea_getTransactionCount Json Rpc #1861
Changes from 12 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,66 @@ | ||
/* | ||
* 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 { | ||
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. Think this class should be name PrivGetLegacyTransactionCount 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. 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 commentThe 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 EeaPrivateNonceProvider nonceProvider; | ||
|
||
public EeaGetTransactionCount( | ||
final JsonRpcParameter parameters, final EeaPrivateNonceProvider 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(e.getMessage(), 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 EeaPrivateNonceProvider { | ||
|
||
private static final Logger LOG = getLogger(); | ||
|
||
private final Enclave enclave; | ||
private final PrivateTransactionHandler privateTransactionHandler; | ||
|
||
public EeaPrivateNonceProvider( | ||
final Enclave enclave, final PrivateTransactionHandler privateTransactionHandler) { | ||
this.enclave = enclave; | ||
this.privateTransactionHandler = privateTransactionHandler; | ||
} | ||
|
||
public long determineNonce( | ||
final String privateFrom, final String[] privateFor, final Address address) { | ||
|
||
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); | ||
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. no need to log and throw, perhaps just throw with the error message. Will be logged in rpc layer |
||
throw new RuntimeException(errorMessage); | ||
} | ||
|
||
final String privacyGroupId = legacyGroups.get(0).getPrivacyGroupId(); | ||
|
||
return privateTransactionHandler.getSenderNonce(address, privacyGroupId); | ||
} | ||
} |
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.
throws Exception shouldn't be needed now it's throwing the runtime EnclaveException
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.
done.