From 72e870cd5c70be65decf59e34d9901bf958e5ca5 Mon Sep 17 00:00:00 2001 From: Chris Mckay Date: Mon, 4 Mar 2019 13:05:59 +1000 Subject: [PATCH] [PAN-2343] made transactionpool expect a filter interface (#1030) Signed-off-by: Adrian Sutton --- ethereum/core/build.gradle | 1 - .../pantheon/ethereum/core/AccountFilter.java | 18 ++++++++++++++++++ .../ethereum/core/TransactionPool.java | 13 ++++++------- .../ethereum/core/TransactionPoolTest.java | 13 +++++-------- .../tech/pegasys/pantheon/RunnerBuilder.java | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/AccountFilter.java diff --git a/ethereum/core/build.gradle b/ethereum/core/build.gradle index 4f524c29ff..de6349b11e 100644 --- a/ethereum/core/build.gradle +++ b/ethereum/core/build.gradle @@ -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') diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/AccountFilter.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/AccountFilter.java new file mode 100644 index 0000000000..7a6d93758d --- /dev/null +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/AccountFilter.java @@ -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); +} diff --git a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/TransactionPool.java b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/TransactionPool.java index e0882c2940..ec8d994564 100644 --- a/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/TransactionPool.java +++ b/ethereum/core/src/main/java/tech/pegasys/pantheon/ethereum/core/TransactionPool.java @@ -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 = Optional.empty(); + private Optional 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 transactions); } - public void setAccountWhitelist(AccountWhitelistController accountWhitelist) { - accountWhitelistController = Optional.of(accountWhitelist); + public void setAccountFilter(final AccountFilter accountFilter) { + this.accountFilter = Optional.of(accountFilter); } } diff --git a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/core/TransactionPoolTest.java b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/core/TransactionPoolTest.java index 7467e67242..094ac1e395 100644 --- a/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/core/TransactionPoolTest.java +++ b/ethereum/core/src/test/java/tech/pegasys/pantheon/ethereum/core/TransactionPoolTest.java @@ -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)); diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java b/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java index 09225eeac1..e2710b1c44 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java @@ -255,7 +255,7 @@ public class RunnerBuilder { configuration -> { final AccountWhitelistController whitelistController = new AccountWhitelistController(configuration); - transactionPool.setAccountWhitelist(whitelistController); + transactionPool.setAccountFilter(whitelistController::contains); return whitelistController; });