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

Commit

Permalink
[PAN-2343] made transactionpool expect a filter interface (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
Errorific authored Mar 4, 2019
1 parent b3eee20 commit b4d2e77
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
1 change: 0 additions & 1 deletion ethereum/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;
Expand All @@ -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,
Expand Down Expand Up @@ -136,7 +135,7 @@ private ValidationResult<TransactionInvalidReason> validateTransaction(
}

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));
Expand Down Expand Up @@ -166,8 +165,8 @@ private ValidationResult<TransactionInvalidReason> validateTransaction(
.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() {
Expand All @@ -180,7 +179,7 @@ public interface TransactionBatchAddedListener {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -361,10 +359,10 @@ public void shouldNotNotifyBatchListenerIfNoTransactionsAreAdded() {

@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());

Expand All @@ -373,11 +371,10 @@ public void shouldAllowWhitelistedTransactionWhenWhitelistEnabled() {

@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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public Runner build() {
configuration -> {
final AccountWhitelistController whitelistController =
new AccountWhitelistController(configuration);
transactionPool.setAccountWhitelist(whitelistController);
transactionPool.setAccountFilter(whitelistController::contains);
return whitelistController;
});

Expand Down

0 comments on commit b4d2e77

Please sign in to comment.