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

Commit

Permalink
[PAN-3010] Enforce privateFrom to be present. (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
iikirilov authored and MadelineMurray committed Aug 12, 2019
1 parent 7deecfc commit 5c2bc03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,8 @@ public static Builder builder() {
return new Builder();
}

public static PrivateTransaction readFrom(final RLPInput input) throws RLPException {
return readFrom(input, null);
}

@SuppressWarnings({"unchecked"})
public static PrivateTransaction readFrom(final RLPInput input, final BytesValue enclavePublicKey)
throws RLPException {
public static PrivateTransaction readFrom(final RLPInput input) throws RLPException {
input.enterList();

final Builder builder =
Expand Down Expand Up @@ -124,22 +119,9 @@ public static PrivateTransaction readFrom(final RLPInput input, final BytesValue
final BigInteger s = BytesValues.asUnsignedBigInteger(input.readUInt256Scalar().getBytes());
final SECP256K1.Signature signature = SECP256K1.Signature.create(r, s, recId);

final RLPInput item1 = input.readAsRlp(); // privateFrom or privateFor/PrivacyGroupId
final RLPInput item2 = input.readAsRlp(); // privateFor/PrivacyGroupId or restriction

final BytesValue privateFrom;
final Object privateForOrPrivacyGroupId;
final Restriction restriction;

if (input.isEndOfCurrentList()) {
privateFrom = enclavePublicKey;
privateForOrPrivacyGroupId = resolvePrivateForOrPrivacyGroupId(item1);
restriction = convertToEnum(item2.readBytesValue());
} else {
privateFrom = item1.readBytesValue();
privateForOrPrivacyGroupId = resolvePrivateForOrPrivacyGroupId(item2);
restriction = convertToEnum(input.readBytesValue());
}
final BytesValue privateFrom = input.readBytesValue();
final Object privateForOrPrivacyGroupId = resolvePrivateForOrPrivacyGroupId(input.readAsRlp());
final Restriction restriction = convertToEnum(input.readBytesValue());

input.leaveList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import tech.pegasys.pantheon.ethereum.rlp.RLP;
import tech.pegasys.pantheon.ethereum.rlp.RLPException;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import tech.pegasys.pantheon.util.bytes.BytesValues;

import java.util.OptionalLong;

Expand Down Expand Up @@ -134,9 +133,7 @@ public JsonRpcResponse response(final JsonRpcRequest request) {
private PrivateTransaction decodeRawTransaction(final String hash)
throws InvalidJsonRpcRequestException {
try {
return PrivateTransaction.readFrom(
RLP.input(BytesValue.fromHexString(hash)),
BytesValues.fromBase64(privateTransactionHandler.getEnclaveKey()));
return PrivateTransaction.readFrom(RLP.input(BytesValue.fromHexString(hash)));
} catch (final IllegalArgumentException | RLPException e) {
LOG.debug(e);
throw new InvalidJsonRpcRequestException("Invalid raw private transaction hex", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.crypto.SECP256K1;
Expand Down Expand Up @@ -80,7 +81,7 @@ public class EeaSendRawTransactionTest {
+ "200e885ff29e973e2576b6600181d1b0a2b5294e30d9be4a1981"
+ "ffb33a0b8c8a72657374726963746564";

private static final String VALID_PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP_NO_PRIVATE_FROM =
private static final String PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP_NO_PRIVATE_FROM =
"0xf88b800182520894095e7baea6a6c7c4c2dfeb977efac326af55"
+ "2d8780801ba048b55bfa915ac795c431978d8a6a992b628d55"
+ "7da5ff759b307d495a36649353a01fffd310ac743f371de3b9"
Expand Down Expand Up @@ -124,9 +125,6 @@ public class EeaSendRawTransactionTest {
@Before
public void before() {
when(transactionPool.getPendingTransactions()).thenReturn(pendingTransactions);
when(privateTxHandler.getEnclaveKey())
.thenReturn("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=");

method =
new EeaSendRawTransaction(blockchainQueries, privateTxHandler, transactionPool, parameter);
}
Expand Down Expand Up @@ -278,42 +276,23 @@ public void validTransactionPrivacyGroupIsSentToTransactionPool() throws Excepti
}

@Test
public void validTransactionPrivacyGroupNoPrivateFromIsSentToTransactionPool() throws Exception {
public void transactionPrivacyGroupNoPrivateFromReturnsError() throws Exception {
when(parameter.required(any(Object[].class), anyInt(), any()))
.thenReturn(VALID_PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP_NO_PRIVATE_FROM);
when(privateTxHandler.sendToOrion(any(PrivateTransaction.class))).thenReturn(MOCK_ORION_KEY);
when(privateTxHandler.getPrivacyGroup(any(String.class), any(PrivateTransaction.class)))
.thenReturn(MOCK_PRIVACY_GROUP);
when(privateTxHandler.validatePrivateTransaction(
any(PrivateTransaction.class), any(String.class)))
.thenReturn(ValidationResult.valid());
when(privateTxHandler.createPrivacyMarkerTransaction(
any(String.class), any(PrivateTransaction.class), any(Long.class)))
.thenReturn(PUBLIC_TRANSACTION);
when(transactionPool.addLocalTransaction(any(Transaction.class)))
.thenReturn(ValidationResult.valid());
.thenReturn(PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP_NO_PRIVATE_FROM);

final JsonRpcRequest request =
new JsonRpcRequest(
"2.0",
"eea_sendRawTransaction",
new String[] {VALID_PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP_NO_PRIVATE_FROM});
new String[] {PRIVATE_TRANSACTION_RLP_PRIVACY_GROUP_NO_PRIVATE_FROM});

final JsonRpcResponse expectedResponse =
new JsonRpcSuccessResponse(
request.getId(), "0x221e930a2c18d91fca4d509eaa3512f3e01fef266f660e32473de67474b36c15");
new JsonRpcErrorResponse(request.getId(), JsonRpcError.DECODE_ERROR);

final JsonRpcResponse actualResponse = method.response(request);

assertThat(actualResponse).isEqualToComparingFieldByField(expectedResponse);
verify(privateTxHandler).sendToOrion(any(PrivateTransaction.class));
verify(privateTxHandler).getPrivacyGroup(any(String.class), any(PrivateTransaction.class));
verify(privateTxHandler)
.validatePrivateTransaction(any(PrivateTransaction.class), any(String.class));
verify(privateTxHandler)
.createPrivacyMarkerTransaction(
any(String.class), any(PrivateTransaction.class), any(Long.class));
verify(transactionPool).addLocalTransaction(any(Transaction.class));
verifyZeroInteractions(privateTxHandler);
}

@Test
Expand Down

0 comments on commit 5c2bc03

Please sign in to comment.