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

Remove duplication between EeaTransactionCountRpc & PrivateTransactionHandler #1619

Merged
merged 4 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import tech.pegasys.pantheon.enclave.types.SendResponse;
import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.core.Transaction;
import tech.pegasys.pantheon.ethereum.core.Util;
import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator.TransactionInvalidReason;
import tech.pegasys.pantheon.ethereum.mainnet.ValidationResult;
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput;
Expand Down Expand Up @@ -70,7 +70,7 @@ public PrivateTransactionHandler(
this.enclave = enclave;
this.privacyPrecompileAddress = privacyPrecompileAddress;
this.nodeKeyPair = nodeKeyPair;
this.signerAddress = Address.extract(Hash.hash(nodeKeyPair.getPublicKey().getEncodedBytes()));
this.signerAddress = Util.publicKeyToAddress(nodeKeyPair.getPublicKey());
this.privateStateStorage = privateStateStorage;
this.privateWorldStateArchive = privateWorldStateArchive;
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public Transaction createPrivacyMarkerTransaction(
public ValidationResult<TransactionInvalidReason> validatePrivateTransaction(
final PrivateTransaction privateTransaction, final String privacyGroupId) {
final long actualNonce = privateTransaction.getNonce();
final long expectedNonce = getSenderNonce(privateTransaction, privacyGroupId);
final long expectedNonce = getSenderNonce(privateTransaction.getSender(), privacyGroupId);
LOG.debug("Validating actual nonce {} with expected nonce {}", actualNonce, expectedNonce);
if (expectedNonce > actualNonce) {
return ValidationResult.invalid(
Expand Down Expand Up @@ -163,8 +163,7 @@ private SendRequest createSendRequest(final PrivateTransaction privateTransactio
privateFor);
}

private long getSenderNonce(
final PrivateTransaction privateTransaction, final String privacyGroupId) {
public long getSenderNonce(final Address sender, final String privacyGroupId) {
return privateStateStorage
.getPrivateAccountState(BytesValue.fromHexString(privacyGroupId))
.map(
Expand All @@ -173,8 +172,7 @@ private long getSenderNonce(
.getMutable(lastRootHash)
.map(
worldState -> {
final Account maybePrivateSender =
worldState.get(privateTransaction.getSender());
final Account maybePrivateSender = worldState.get(sender);

if (maybePrivateSender != null) {
return maybePrivateSender.getNonce();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,15 @@ blockchainQueries, new TransactionTracer(blockReplay), parameter),
new AdminPeers(p2pNetwork));
}
if (rpcApis.contains(RpcApis.EEA)) {
final PrivateTransactionHandler privateTransactionHandler =
new PrivateTransactionHandler(privacyParameters);
final Enclave enclave = new Enclave(privacyParameters.getEnclaveUri());
addMethods(
enabledMethods,
new EeaGetTransactionReceipt(blockchainQueries, enclave, parameter, privacyParameters),
new EeaSendRawTransaction(
blockchainQueries,
new PrivateTransactionHandler(privacyParameters),
transactionPool,
parameter),
new EeaGetTransactionCount(parameter, privacyParameters),
blockchainQueries, privateTransactionHandler, transactionPool, parameter),
new EeaGetTransactionCount(parameter, privateTransactionHandler),
new EeaGetPrivateTransaction(enclave, parameter, privacyParameters),
new EeaCreatePrivacyGroup(new Enclave(privacyParameters.getEnclaveUri()), parameter),
new EeaDeletePrivacyGroup(new Enclave(privacyParameters.getEnclaveUri()), parameter),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy;

import tech.pegasys.pantheon.ethereum.core.Account;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.jsonrpc.RpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
Expand All @@ -25,21 +22,18 @@
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 tech.pegasys.pantheon.ethereum.privacy.PrivateStateStorage;
import tech.pegasys.pantheon.ethereum.worldstate.WorldStateArchive;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransactionHandler;

public class EeaGetTransactionCount implements JsonRpcMethod {

private final JsonRpcParameter parameters;
private final PrivateStateStorage privateStateStorage;
private final WorldStateArchive privateWorldStateArchive;
private final PrivateTransactionHandler privateTransactionHandler;

public EeaGetTransactionCount(
final JsonRpcParameter parameters, final PrivacyParameters privacyParameters) {
final JsonRpcParameter parameters,
final PrivateTransactionHandler privateTransactionHandler) {
this.parameters = parameters;
this.privateStateStorage = privacyParameters.getPrivateStateStorage();
this.privateWorldStateArchive = privacyParameters.getPrivateWorldStateArchive();
this.privateTransactionHandler = privateTransactionHandler;
}

@Override
Expand All @@ -56,21 +50,7 @@ public JsonRpcResponse response(final JsonRpcRequest request) {
final Address address = parameters.required(request.getParams(), 0, Address.class);
final String privacyGroupId = parameters.required(request.getParams(), 1, String.class);

return privateStateStorage
.getPrivateAccountState(BytesValue.fromHexString(privacyGroupId))
.map(
lastRootHash -> {
final MutableWorldState privateWorldState =
privateWorldStateArchive.getMutable(lastRootHash).get();

final Account maybePrivateSender = privateWorldState.get(address);

if (maybePrivateSender != null) {
return new JsonRpcSuccessResponse(
request.getId(), Quantity.create(maybePrivateSender.getNonce()));
}
return new JsonRpcSuccessResponse(request.getId(), Quantity.create(0));
})
.orElse(new JsonRpcSuccessResponse(request.getId(), Quantity.create(0)));
final long nonce = privateTransactionHandler.getSenderNonce(address, privacyGroupId);
return new JsonRpcSuccessResponse(request.getId(), Quantity.create(nonce));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,17 @@
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.ethereum.core.MutableAccount;
import tech.pegasys.pantheon.ethereum.core.MutableWorldState;
import tech.pegasys.pantheon.ethereum.core.PrivacyParameters;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.core.WorldUpdater;
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.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.privacy.PrivateStateStorage;
import tech.pegasys.pantheon.ethereum.worldstate.WorldStateArchive;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransactionHandler;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.util.Optional;

import org.junit.Test;

public class EeaGetTransactionCountTest {

private final JsonRpcParameter parameters = new JsonRpcParameter();
private final PrivateStateStorage privacyStateStorage = mock(PrivateStateStorage.class);
private final WorldStateArchive privateWorldStateArchive = mock(WorldStateArchive.class);
private final PrivacyParameters privacyParameters = mock(PrivacyParameters.class);
private final MutableWorldState mutableWorldState = mock(MutableWorldState.class);
private final WorldUpdater worldUpdater = mock(WorldUpdater.class);
private final MutableAccount mutableAccount = mock(MutableAccount.class);
private final Hash lastRootHash = mock(Hash.class);
private final BytesValue privacyGroupId = BytesValue.wrap("0x123".getBytes(UTF_8));

private final Address senderAddress =
Expand All @@ -53,30 +37,20 @@ public class EeaGetTransactionCountTest {

@Test
public void verifyTransactionCount() {
when(mutableWorldState.updater()).thenReturn(worldUpdater);
when(mutableAccount.getNonce()).thenReturn(NONCE);
// Create account in storage with given nonce
when(worldUpdater.createAccount(senderAddress, NONCE, Wei.ZERO)).thenReturn(mutableAccount);
when(privateWorldStateArchive.getMutable()).thenReturn(mutableWorldState);

when(privacyParameters.getPrivateStateStorage()).thenReturn(privacyStateStorage);
when(privacyParameters.getPrivateWorldStateArchive()).thenReturn(privateWorldStateArchive);

when(privacyStateStorage.getPrivateAccountState(privacyGroupId))
.thenReturn(Optional.of(lastRootHash));
when(privateWorldStateArchive.getMutable(lastRootHash))
.thenReturn(Optional.of(mutableWorldState));
when(mutableWorldState.get(senderAddress)).thenReturn(mutableAccount);
final PrivateTransactionHandler privateTransactionHandler =
mock(PrivateTransactionHandler.class);
when(privateTransactionHandler.getSenderNonce(senderAddress, privacyGroupId.toString()))
.thenReturn(NONCE);

final EeaGetTransactionCount eeaGetTransactionCount =
new EeaGetTransactionCount(parameters, privacyParameters);
new EeaGetTransactionCount(parameters, privateTransactionHandler);

final Object[] params = new Object[] {senderAddress, privacyGroupId.toString()};
final JsonRpcRequest request = new JsonRpcRequest("1", "eea_getTransactionCount", params);

final JsonRpcSuccessResponse response =
(JsonRpcSuccessResponse) eeaGetTransactionCount.response(request);

assertEquals("0x5", response.getResult());
assertEquals(String.format("0x%X", NONCE), response.getResult());
}
}