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

[PIE-1723] Cache TransactionValidationParams instead of creating new object for each call #1616

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -14,6 +14,16 @@

public class TransactionValidationParams {

private static final TransactionValidationParams processingBlockParams =
new TransactionValidationParams(false, true, false);
private static final TransactionValidationParams transactionPoolParams =
new TransactionValidationParams(true, false, true);
private static final TransactionValidationParams miningParams =
new TransactionValidationParams(false, true, true);
private static final TransactionValidationParams blockReplayParams =
new TransactionValidationParams(false, false, false);
private static final TransactionValidationParams transactionSimulatorParams =
new TransactionValidationParams(false, false, false);
private final boolean allowFutureNonce;
private final boolean checkOnchainPermissions;
private final boolean checkLocalPermissions;
Expand All @@ -40,43 +50,23 @@ public boolean checkLocalPermissions() {
}

public static TransactionValidationParams transactionSimulator() {
return new Builder()
.checkLocalPermissions(false)
.checkOnchainPermissions(false)
.allowFutureNonce(false)
.build();
return transactionSimulatorParams;
}

public static TransactionValidationParams processingBlock() {
return new Builder()
.checkLocalPermissions(false)
.checkOnchainPermissions(true)
.allowFutureNonce(false)
.build();
return processingBlockParams;
}

public static TransactionValidationParams transactionPool() {
return new Builder()
.checkLocalPermissions(true)
.checkOnchainPermissions(false)
.allowFutureNonce(true)
.build();
return transactionPoolParams;
}

public static TransactionValidationParams mining() {
return new Builder()
.checkLocalPermissions(true)
.checkOnchainPermissions(true)
.allowFutureNonce(false)
.build();
return miningParams;
}

public static TransactionValidationParams blockReplay() {
return new Builder()
.checkLocalPermissions(false)
.checkOnchainPermissions(false)
.allowFutureNonce(false)
.build();
return blockReplayParams;
}

static class Builder {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.mainnet;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class TransactionValidationParamsTest {

@Test
public void isAllowFutureNonce() {
assertThat(
new TransactionValidationParams.Builder()
.allowFutureNonce(true)
.build()
.isAllowFutureNonce())
.isTrue();
assertThat(
new TransactionValidationParams.Builder()
.allowFutureNonce(false)
.build()
.isAllowFutureNonce())
.isFalse();
}

@Test
public void checkOnchainPermissions() {
assertThat(
new TransactionValidationParams.Builder()
.checkOnchainPermissions(true)
.build()
.checkOnchainPermissions())
.isTrue();
assertThat(
new TransactionValidationParams.Builder()
.checkOnchainPermissions(false)
.build()
.checkOnchainPermissions())
.isFalse();
}

@Test
public void checkLocalPermissions() {
assertThat(
new TransactionValidationParams.Builder()
.checkLocalPermissions(true)
.build()
.checkLocalPermissions())
.isTrue();
assertThat(
new TransactionValidationParams.Builder()
.checkLocalPermissions(false)
.build()
.checkLocalPermissions())
.isFalse();
}

@Test
public void transactionSimulator() {
final TransactionValidationParams params = TransactionValidationParams.transactionSimulator();
assertThat(params.isAllowFutureNonce()).isFalse();
assertThat(params.checkOnchainPermissions()).isFalse();
assertThat(params.checkLocalPermissions()).isFalse();
}

@Test
public void processingBlock() {
final TransactionValidationParams params = TransactionValidationParams.processingBlock();
assertThat(params.isAllowFutureNonce()).isFalse();
assertThat(params.checkOnchainPermissions()).isTrue();
assertThat(params.checkLocalPermissions()).isFalse();
}

@Test
public void transactionPool() {
final TransactionValidationParams params = TransactionValidationParams.transactionPool();
assertThat(params.isAllowFutureNonce()).isTrue();
assertThat(params.checkOnchainPermissions()).isFalse();
assertThat(params.checkLocalPermissions()).isTrue();
}

@Test
public void mining() {
final TransactionValidationParams params = TransactionValidationParams.mining();
assertThat(params.isAllowFutureNonce()).isFalse();
assertThat(params.checkOnchainPermissions()).isTrue();
assertThat(params.checkLocalPermissions()).isTrue();
}

@Test
public void blockReplay() {
final TransactionValidationParams params = TransactionValidationParams.blockReplay();
assertThat(params.isAllowFutureNonce()).isFalse();
assertThat(params.checkOnchainPermissions()).isFalse();
assertThat(params.checkLocalPermissions()).isFalse();
}
}