[PAN-2343] made transactionpool expect a filter interface (#1030)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Chris Mckay 6 years ago committed by GitHub
parent fc2b1331d5
commit 72e870cd5c
  1. 1
      ethereum/core/build.gradle
  2. 18
      ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/AccountFilter.java
  3. 13
      ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/TransactionPool.java
  4. 13
      ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/core/TransactionPoolTest.java
  5. 2
      pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java

@ -31,7 +31,6 @@ dependencies {
implementation project(':enclave')
implementation project(':ethereum:rlp')
implementation project(':ethereum:trie')
implementation project(':ethereum:permissioning')
implementation project(':metrics')
implementation project(':services:kvstore')

@ -0,0 +1,18 @@
/*
* 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.core;
@FunctionalInterface
public interface AccountFilter {
boolean permitted(String account);
}

@ -25,7 +25,6 @@ import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator;
import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator.TransactionInvalidReason;
import tech.pegasys.pantheon.ethereum.mainnet.ValidationResult;
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController;
import java.util.ArrayList;
import java.util.Collection;
@ -50,7 +49,7 @@ public class TransactionPool implements BlockAddedObserver {
private final ProtocolSchedule<?> protocolSchedule;
private final ProtocolContext<?> protocolContext;
private final TransactionBatchAddedListener transactionBatchAddedListener;
private Optional<AccountWhitelistController> accountWhitelistController = Optional.empty();
private Optional<AccountFilter> accountFilter = Optional.empty();
public TransactionPool(
final PendingTransactions pendingTransactions,
@ -136,7 +135,7 @@ public class TransactionPool implements BlockAddedObserver {
}
final String sender = transaction.getSender().toString();
if (accountIsNotWhitelisted(sender)) {
if (accountIsNotPermitted(sender)) {
return ValidationResult.invalid(
TransactionInvalidReason.TX_SENDER_NOT_AUTHORIZED,
String.format("Sender %s is not on the Account Whitelist", sender));
@ -166,8 +165,8 @@ public class TransactionPool implements BlockAddedObserver {
.orElseGet(() -> ValidationResult.invalid(CHAIN_HEAD_WORLD_STATE_NOT_AVAILABLE));
}
private boolean accountIsNotWhitelisted(final String account) {
return accountWhitelistController.map(c -> !c.contains(account)).orElse(false);
private boolean accountIsNotPermitted(final String account) {
return accountFilter.map(c -> !c.permitted(account)).orElse(false);
}
private BlockHeader getChainHeadBlockHeader() {
@ -180,7 +179,7 @@ public class TransactionPool implements BlockAddedObserver {
void onTransactionsAdded(Iterable<Transaction> transactions);
}
public void setAccountWhitelist(AccountWhitelistController accountWhitelist) {
accountWhitelistController = Optional.of(accountWhitelist);
public void setAccountFilter(final AccountFilter accountFilter) {
this.accountFilter = Optional.of(accountFilter);
}
}

@ -41,7 +41,6 @@ import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSchedule;
import tech.pegasys.pantheon.ethereum.mainnet.ProtocolSpec;
import tech.pegasys.pantheon.ethereum.mainnet.TransactionValidator;
import tech.pegasys.pantheon.ethereum.mainnet.ValidationResult;
import tech.pegasys.pantheon.ethereum.permissioning.AccountWhitelistController;
import tech.pegasys.pantheon.util.uint.UInt256;
import java.util.List;
@ -72,8 +71,7 @@ public class TransactionPoolTest {
private final Transaction transaction2 = createTransaction(2);
private TransactionPool transactionPool;
private long genesisBlockGasLimit;
private final AccountWhitelistController accountWhitelistController =
mock(AccountWhitelistController.class);
private final AccountFilter accountFilter = mock(AccountFilter.class);
@Before
public void setUp() {
@ -361,10 +359,10 @@ public class TransactionPoolTest {
@Test
public void shouldAllowWhitelistedTransactionWhenWhitelistEnabled() {
transactionPool.setAccountWhitelist(accountWhitelistController);
transactionPool.setAccountFilter(accountFilter);
givenTransactionIsValid(transaction1);
when(accountWhitelistController.contains(transaction1.getSender().toString())).thenReturn(true);
when(accountFilter.permitted(transaction1.getSender().toString())).thenReturn(true);
assertThat(transactionPool.addLocalTransaction(transaction1)).isEqualTo(valid());
@ -373,11 +371,10 @@ public class TransactionPoolTest {
@Test
public void shouldRejectNonWhitelistedTransactionWhenWhitelistEnabled() {
transactionPool.setAccountWhitelist(accountWhitelistController);
transactionPool.setAccountFilter(accountFilter);
givenTransactionIsValid(transaction1);
when(accountWhitelistController.contains(transaction1.getSender().toString()))
.thenReturn(false);
when(accountFilter.permitted(transaction1.getSender().toString())).thenReturn(false);
assertThat(transactionPool.addLocalTransaction(transaction1))
.isEqualTo(ValidationResult.invalid(TX_SENDER_NOT_AUTHORIZED));

@ -255,7 +255,7 @@ public class RunnerBuilder {
configuration -> {
final AccountWhitelistController whitelistController =
new AccountWhitelistController(configuration);
transactionPool.setAccountWhitelist(whitelistController);
transactionPool.setAccountFilter(whitelistController::contains);
return whitelistController;
});

Loading…
Cancel
Save