Make restriction field in Private Transaction an enum (#1462)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Ivaylo Kirilov 6 years ago committed by Lucas Saldanha
parent 569013b2c4
commit e536e1b8fb
  1. 5
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/transaction/eea/PrivateTransactionBuilder.java
  2. 27
      ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/privacy/PrivateTransaction.java
  3. 33
      ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/privacy/Restriction.java
  4. 2
      ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/privacy/PrivateTransactionHandlerTest.java
  5. 6
      ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/privacy/PrivateTransactionTest.java
  6. 6
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaSendRawTransaction.java
  7. 3
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/privacy/EeaGetTransactionReceiptTest.java

@ -12,12 +12,11 @@
*/
package tech.pegasys.pantheon.tests.acceptance.dsl.transaction.eea;
import static java.nio.charset.StandardCharsets.UTF_8;
import tech.pegasys.pantheon.crypto.SECP256K1;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.ethereum.core.Wei;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction;
import tech.pegasys.pantheon.ethereum.privacy.Restriction;
import tech.pegasys.pantheon.ethereum.rlp.RLP;
import tech.pegasys.pantheon.util.bytes.BytesValue;
@ -128,7 +127,7 @@ public class PrivateTransactionBuilder {
.chainId(BigInteger.valueOf(2018))
.privateFrom(privateFrom)
.privateFor(privateFor)
.restriction(BytesValue.wrap("restricted".getBytes(UTF_8)))
.restriction(Restriction.RESTRICTED)
.signAndBuild(keyPair)
::writeTo)
.toString();

@ -70,7 +70,7 @@ public class PrivateTransaction {
private final List<BytesValue> privateFor;
private final BytesValue restriction;
private final Restriction restriction;
// Caches a "hash" of a portion of the transaction used for sender recovery.
// Note that this hash does not include the transaction signature so it does not
@ -117,7 +117,7 @@ public class PrivateTransaction {
final SECP256K1.Signature signature = SECP256K1.Signature.create(r, s, recId);
final BytesValue privateFrom = input.readBytesValue();
final List<BytesValue> privateFor = input.readList(RLPInput::readBytesValue);
final BytesValue restriction = input.readBytesValue();
final Restriction restriction = convertToEnum(input.readBytesValue());
input.leaveList();
@ -130,6 +130,15 @@ public class PrivateTransaction {
.build();
}
private static Restriction convertToEnum(final BytesValue readBytesValue) {
if (readBytesValue.equals(Restriction.RESTRICTED.getBytes())) {
return Restriction.RESTRICTED;
} else if (readBytesValue.equals(Restriction.UNRESTRICTED.getBytes())) {
return Restriction.UNRESTRICTED;
}
return Restriction.UNSUPPORTED;
}
/**
* Instantiates a transaction instance.
*
@ -163,7 +172,7 @@ public class PrivateTransaction {
final Optional<BigInteger> chainId,
final BytesValue privateFrom,
final List<BytesValue> privateFor,
final BytesValue restriction) {
final Restriction restriction) {
this.nonce = nonce;
this.gasPrice = gasPrice;
this.gasLimit = gasLimit;
@ -279,7 +288,7 @@ public class PrivateTransaction {
*
* @return the restriction
*/
public BytesValue getRestriction() {
public Restriction getRestriction() {
return restriction;
}
@ -314,7 +323,7 @@ public class PrivateTransaction {
chainId,
privateFrom,
privateFor,
restriction);
restriction.getBytes());
}
return hashNoSignature;
}
@ -336,7 +345,7 @@ public class PrivateTransaction {
writeSignature(out);
out.writeBytesValue(getPrivateFrom());
out.writeList(getPrivateFor(), (bv, rlpO) -> rlpO.writeBytesValue(bv));
out.writeBytesValue(getRestriction());
out.writeBytesValue(getRestriction().getBytes());
out.endList();
}
@ -527,7 +536,7 @@ public class PrivateTransaction {
protected List<BytesValue> privateFor;
protected BytesValue restriction;
protected Restriction restriction;
public Builder chainId(final BigInteger chainId) {
this.chainId = Optional.of(chainId);
@ -584,7 +593,7 @@ public class PrivateTransaction {
return this;
}
public Builder restriction(final BytesValue restriction) {
public Builder restriction(final Restriction restriction) {
this.restriction = restriction;
return this;
}
@ -625,7 +634,7 @@ public class PrivateTransaction {
chainId,
privateFrom,
privateFor,
restriction);
restriction.getBytes());
return SECP256K1.sign(hash, keys);
}
}

@ -0,0 +1,33 @@
/*
* 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.privacy;
import static java.nio.charset.StandardCharsets.UTF_8;
import tech.pegasys.pantheon.util.bytes.BytesValue;
public enum Restriction {
RESTRICTED(BytesValue.wrap("restricted".getBytes(UTF_8))),
UNRESTRICTED(BytesValue.wrap("unrestricted".getBytes(UTF_8))),
UNSUPPORTED(BytesValue.EMPTY);
private final BytesValue bytes;
Restriction(final BytesValue bytes) {
this.bytes = bytes;
}
public BytesValue getBytes() {
return bytes;
}
}

@ -67,7 +67,7 @@ public class PrivateTransactionHandlerTest {
Lists.newArrayList(
BytesValue.wrap("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=".getBytes(UTF_8)),
BytesValue.wrap("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=".getBytes(UTF_8))))
.restriction(BytesValue.wrap("restricted".getBytes(UTF_8)))
.restriction(Restriction.RESTRICTED)
.signAndBuild(KEY_PAIR);
private static final Transaction PUBLIC_TRANSACTION =

@ -107,7 +107,7 @@ public class PrivateTransactionTest {
Lists.newArrayList(
BytesValue.wrap("A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=".getBytes(UTF_8)),
BytesValue.wrap("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=".getBytes(UTF_8))),
BytesValue.wrap("restricted".getBytes(UTF_8)));
Restriction.RESTRICTED);
private static final PrivateTransaction VALID_SIGNED_PRIVATE_TRANSACTION =
PrivateTransaction.builder()
@ -136,7 +136,7 @@ public class PrivateTransactionTest {
.privateFor(
Lists.newArrayList(
BytesValue.wrap("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=".getBytes(UTF_8))))
.restriction(BytesValue.wrap("restricted".getBytes(UTF_8)))
.restriction(Restriction.RESTRICTED)
.signAndBuild(
SECP256K1.KeyPair.create(
SECP256K1.PrivateKey.create(
@ -171,7 +171,7 @@ public class PrivateTransactionTest {
.privateFor(
Lists.newArrayList(
BytesValue.wrap("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=".getBytes(UTF_8))))
.restriction(BytesValue.wrap("restricted".getBytes(UTF_8)))
.restriction(Restriction.RESTRICTED)
.signAndBuild(
SECP256K1.KeyPair.create(
SECP256K1.PrivateKey.create(

@ -12,7 +12,6 @@
*/
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.privacy;
import static java.nio.charset.StandardCharsets.UTF_8;
import static tech.pegasys.pantheon.ethereum.jsonrpc.JsonRpcErrorConverter.convertTransactionInvalidReason;
import tech.pegasys.pantheon.ethereum.core.Address;
@ -32,6 +31,7 @@ import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator.TransactionIn
import tech.pegasys.pantheon.ethereum.mainnet.ValidationResult;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransactionHandler;
import tech.pegasys.pantheon.ethereum.privacy.Restriction;
import tech.pegasys.pantheon.ethereum.rlp.RLP;
import tech.pegasys.pantheon.ethereum.rlp.RLPException;
import tech.pegasys.pantheon.util.bytes.BytesValue;
@ -84,9 +84,7 @@ public class EeaSendRawTransaction implements JsonRpcMethod {
return new JsonRpcErrorResponse(request.getId(), JsonRpcError.VALUE_NOT_ZERO);
}
if (!privateTransaction
.getRestriction()
.equals(BytesValue.wrap("restricted".getBytes(UTF_8)))) {
if (!privateTransaction.getRestriction().equals(Restriction.RESTRICTED)) {
return new JsonRpcErrorResponse(
request.getId(), JsonRpcError.UNIMPLEMENTED_PRIVATE_TRANSACTION_TYPE);
}

@ -43,6 +43,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessRe
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.privacy.PrivateTransactionReceiptResult;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransaction;
import tech.pegasys.pantheon.ethereum.privacy.PrivateTransactionStorage;
import tech.pegasys.pantheon.ethereum.privacy.Restriction;
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput;
import tech.pegasys.pantheon.util.bytes.Bytes32;
import tech.pegasys.pantheon.util.bytes.BytesValue;
@ -97,7 +98,7 @@ public class EeaGetTransactionReceiptTest {
.privateFor(
Lists.newArrayList(
BytesValue.wrap("Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=".getBytes(UTF_8))))
.restriction(BytesValue.wrap("restricted".getBytes(UTF_8)))
.restriction(Restriction.RESTRICTED)
.signAndBuild(KEY_PAIR);
private final Transaction transaction =

Loading…
Cancel
Save