Skip to content

Commit

Permalink
protect access to GoQuorum flag by getter and write once setter
Browse files Browse the repository at this point in the history
Signed-off-by: Taccat Isid <taccatisid@protonmail.com>
  • Loading branch information
taccatisid committed Oct 14, 2021
1 parent 2591f11 commit c40a6fc
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 23 deletions.
12 changes: 8 additions & 4 deletions besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ void setBannedNodeIds(final List<String> values) {
private Vertx vertx;
private EnodeDnsConfiguration enodeDnsConfiguration;
private KeyValueStorageProvider keyValueStorageProvider;
private Boolean isGoQuorumCompatibilityMode = false;
protected Boolean isGoQuorumCompatibilityMode = false;

public BesuCommand(
final Logger logger,
Expand Down Expand Up @@ -1206,9 +1206,7 @@ public void run() {
if (genesisFile != null) {
genesisConfigOptions = readGenesisConfigOptions();
if (genesisConfigOptions.isQuorum()) {
// this static flag is read by the RLP decoder
GoQuorumOptions.goQuorumCompatibilityMode = true;
isGoQuorumCompatibilityMode = true;
enableGoQuorumCompatibilityMode();
}
}

Expand Down Expand Up @@ -2736,4 +2734,10 @@ private Optional<String> getEcCurveFromGenesisFile() {
}
return genesisConfigOptions.getEcCurve();
}

protected void enableGoQuorumCompatibilityMode() {
// this static flag is read by the RLP decoder
GoQuorumOptions.setGoQuorumCompatibilityMode(true);
isGoQuorumCompatibilityMode = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ KeyPair loadKeyPair() {
return keyPair;
}

@Override
protected void enableGoQuorumCompatibilityMode() {
// We do *not* set the static GoQuorumOptions for test runs as
// these are only allowed to be set once during the program
// runtime.
isGoQuorumCompatibilityMode = true;
}

public CommandSpec getSpec() {
return spec;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,29 @@
* with MainNet.
*/
public class GoQuorumOptions {
// To make it easier for tests to reset the value to default
public static final boolean GOQUORUM_COMPATIBILITY_MODE_DEFAULT_VALUE = false;
private static final boolean GOQUORUM_COMPATIBILITY_MODE_DEFAULT_VALUE = false;

public static boolean goQuorumCompatibilityMode = GOQUORUM_COMPATIBILITY_MODE_DEFAULT_VALUE;
private static Boolean goQuorumCompatibilityMode;

public static void setGoQuorumCompatibilityMode(final boolean goQuorumCompatibilityMode) {
if (GoQuorumOptions.goQuorumCompatibilityMode == null) {
GoQuorumOptions.goQuorumCompatibilityMode = goQuorumCompatibilityMode;
} else {
throw new Error("goQuorumCompatibilityMode can not be changed after having been assigned");
}
}

public static boolean getGoQuorumCompatibilityMode() {
if (goQuorumCompatibilityMode == null) {
// If the quorum mode has never been set, we default it
// here. This allows running individual unit tests that
// query the quorum mode without having to include a
// setGoQuorumCompatibilityMode call in their setup
// procedure. For production use, this case is not
// triggered as we set the quorum mode very early during
// startup.
goQuorumCompatibilityMode = GOQUORUM_COMPATIBILITY_MODE_DEFAULT_VALUE;
}
return goQuorumCompatibilityMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private TransactionAdapter getTransactionAdapter(
final TransactionWithMetadata transactionWithMetadata) {
final Transaction transaction = transactionWithMetadata.getTransaction();
final boolean isGoQuorumPrivateTransaction =
transaction.isGoQuorumPrivateTransaction(GoQuorumOptions.goQuorumCompatibilityMode);
transaction.isGoQuorumPrivateTransaction(GoQuorumOptions.getGoQuorumCompatibilityMode());
return isGoQuorumPrivateTransaction && goQuorumPrivacyParameters.isPresent()
? updatePrivatePayload(transaction)
: new TransactionAdapter(transactionWithMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public List<LogAdapter> getLogs(final DataFetchingEnvironment environment) {
public boolean getIsPrivate() {
return transactionWithMetadata
.getTransaction()
.isGoQuorumPrivateTransaction(GoQuorumOptions.goQuorumCompatibilityMode);
.isGoQuorumPrivateTransaction(GoQuorumOptions.getGoQuorumCompatibilityMode());
}

public Optional<Bytes> getPrivateInputData() {
final Transaction transaction = transactionWithMetadata.getTransaction();
if (transaction.isGoQuorumPrivateTransaction(GoQuorumOptions.goQuorumCompatibilityMode)) {
if (transaction.isGoQuorumPrivateTransaction(GoQuorumOptions.getGoQuorumCompatibilityMode())) {
return Optional.ofNullable(transaction.getPayload());
}
return Optional.of(Bytes.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected BlockParameterOrBlockHash blockParameterOrBlockHash(
@Override
protected String resultByBlockHash(final JsonRpcRequestContext request, final Hash blockHash) {
final Address address = request.getRequiredParameter(0, Address.class);
final boolean isGoQuorumCompatibilityMode = GoQuorumOptions.goQuorumCompatibilityMode;
final boolean isGoQuorumCompatibilityMode = GoQuorumOptions.getGoQuorumCompatibilityMode();
if (isGoQuorumCompatibilityMode && privacyParameters.isPresent()) {
// get from private state if we can
final Optional<BlockHeader> blockHeader =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcErrorConverter.convertTransactionInvalidReason;
import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.DECODE_ERROR;

import org.hyperledger.besu.config.GoQuorumOptions;
import org.hyperledger.besu.enclave.GoQuorumEnclave;
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
Expand All @@ -30,6 +31,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.privacy.GoQuorumSendRawTxArgs;
import org.hyperledger.besu.ethereum.rlp.RLP;
Expand All @@ -47,14 +49,28 @@ public class GoQuorumSendRawPrivateTransaction implements JsonRpcMethod {
final TransactionPool transactionPool;
private final PrivacyIdProvider privacyIdProvider;
private final GoQuorumEnclave enclave;
private final boolean goQuorumCompatibilityMode;

public GoQuorumSendRawPrivateTransaction(
final GoQuorumEnclave enclave,
final TransactionPool transactionPool,
final PrivacyIdProvider privacyIdProvider) {
this(
enclave,
transactionPool,
privacyIdProvider,
GoQuorumOptions.getGoQuorumCompatibilityMode());
}

public GoQuorumSendRawPrivateTransaction(
final GoQuorumEnclave enclave,
final TransactionPool transactionPool,
final PrivacyIdProvider privacyIdProvider,
final boolean goQuorumCompatibilityMode) {
this.enclave = enclave;
this.transactionPool = transactionPool;
this.privacyIdProvider = privacyIdProvider;
this.goQuorumCompatibilityMode = goQuorumCompatibilityMode;
}

@Override
Expand All @@ -72,7 +88,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

try {
final Transaction transaction =
Transaction.readFrom(RLP.input(Bytes.fromHexString(rawPrivateTransaction)));
TransactionDecoder.decodeForWire(
RLP.input(Bytes.fromHexString(rawPrivateTransaction)), goQuorumCompatibilityMode);

checkAndHandlePrivateTransaction(transaction, rawTxArgs, requestContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static Transaction decodeRawTransaction(final String rawTransaction)
throws InvalidJsonRpcRequestException {
try {
Bytes txnBytes = Bytes.fromHexString(rawTransaction);
final boolean isGoQuorumCompatibilityMode = GoQuorumOptions.goQuorumCompatibilityMode;
final boolean isGoQuorumCompatibilityMode = GoQuorumOptions.getGoQuorumCompatibilityMode();
return TransactionDecoder.decodeOpaqueBytes(txnBytes, isGoQuorumCompatibilityMode);
} catch (final IllegalArgumentException | RLPException e) {
LOG.debug(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.config.GoQuorumOptions;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.crypto.SignatureAlgorithm;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
Expand Down Expand Up @@ -80,7 +79,10 @@ public class GoQuorumSendRawPrivateTransactionTest {

@Before
public void before() {
method = new GoQuorumSendRawPrivateTransaction(enclave, transactionPool, privacyIdProvider);
boolean goQuorumCompatibilityMode = true;
method =
new GoQuorumSendRawPrivateTransaction(
enclave, transactionPool, privacyIdProvider, goQuorumCompatibilityMode);
}

@Test
Expand Down Expand Up @@ -126,8 +128,6 @@ public void requestHasNullArrayParameter() {

@Test
public void validTransactionIsSentToTransactionPool() {
GoQuorumOptions.goQuorumCompatibilityMode = true;

when(enclave.sendSignedTransaction(any(), any())).thenReturn(null);

when(transactionPool.addLocalTransaction(any(Transaction.class)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ interface Decoder {
Suppliers.memoize(SignatureAlgorithmFactory::getInstance);

public static Transaction decodeForWire(final RLPInput rlpInput) {
return decodeForWire(rlpInput, GoQuorumOptions.goQuorumCompatibilityMode);
return decodeForWire(rlpInput, GoQuorumOptions.getGoQuorumCompatibilityMode());
}

public static Transaction decodeForWire(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.hyperledger.besu.config.GoQuorumOptions;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
Expand All @@ -41,16 +40,15 @@ public class TransactionDecoderTest {

@Test
public void decodeGoQuorumPrivateTransactionRlp() {
GoQuorumOptions.goQuorumCompatibilityMode = true;
boolean goQuorumCompatibilityMode = true;
RLPInput input = RLP.input(Bytes.fromHexString(GOQUORUM_PRIVATE_TX_RLP));

final Transaction transaction = TransactionDecoder.decodeForWire(input);
final Transaction transaction =
TransactionDecoder.decodeForWire(input, goQuorumCompatibilityMode);
assertThat(transaction).isNotNull();
assertThat(transaction.getV()).isEqualTo(38);
assertThat(transaction.getSender())
.isEqualByComparingTo(Address.fromHexString("0xed9d02e382b34818e88b88a309c7fe71e65f419d"));
GoQuorumOptions.goQuorumCompatibilityMode =
GoQuorumOptions.GOQUORUM_COMPATIBILITY_MODE_DEFAULT_VALUE;
}

@Test
Expand Down

0 comments on commit c40a6fc

Please sign in to comment.