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

[PAN-1339] Send local transactions to new peers #1253

Merged
merged 11 commits into from
Apr 23, 2019
Merged
Prev Previous commit
consolidate
  • Loading branch information
smatthewenglish committed Apr 23, 2019
commit 32374dce5ac05ba952a247333637bb8b6b01b1d0
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,22 @@ public class TransactionPoolTest {
private TransactionPool transactionPool;
private long genesisBlockGasLimit;
private final AccountFilter accountFilter = mock(AccountFilter.class);
private SyncState syncState;
private EthContext ethContext;
private PeerTransactionTracker peerTransactionTracker;

@Before
public void setUp() {
blockchain = executionContext.getBlockchain();
when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);
when(protocolSpec.getTransactionValidator()).thenReturn(transactionValidator);
genesisBlockGasLimit = executionContext.getGenesis().getHeader().getGasLimit();
SyncState syncState = mock(SyncState.class);
syncState = mock(SyncState.class);
when(syncState.isInSync(anyLong())).thenReturn(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests for the new functionality?


PeerTransactionTracker peerTransactionTracker = mock(PeerTransactionTracker.class);
EthContext ethContext = mock(EthContext.class);
ethContext = mock(EthContext.class);
EthPeers ethPeers = mock(EthPeers.class);
when(ethContext.getEthPeers()).thenReturn(ethPeers);

peerTransactionTracker = mock(PeerTransactionTracker.class);
transactionPool =
new TransactionPool(
transactions,
Expand Down Expand Up @@ -450,10 +451,6 @@ public void shouldAllowTransactionWhenAccountWhitelistControllerIsNotPresent() {
public void shouldRejectRemoteTransactionsWhenNotInSync() {
SyncState syncState = mock(SyncState.class);
when(syncState.isInSync(anyLong())).thenReturn(false);
EthContext ethContext = mock(EthContext.class);
EthPeers ethPeers = mock(EthPeers.class);
when(ethContext.getEthPeers()).thenReturn(ethPeers);
PeerTransactionTracker peerTransactionTracker = mock(PeerTransactionTracker.class);
TransactionPool transactionPool =
new TransactionPool(
transactions,
Expand Down Expand Up @@ -490,22 +487,6 @@ public void shouldRejectRemoteTransactionsWhenNotInSync() {

@Test
public void shouldAllowRemoteTransactionsWhenInSync() {
SyncState syncState = mock(SyncState.class);
when(syncState.isInSync(anyLong())).thenReturn(true);
EthContext ethContext = mock(EthContext.class);
EthPeers ethPeers = mock(EthPeers.class);
when(ethContext.getEthPeers()).thenReturn(ethPeers);
PeerTransactionTracker peerTransactionTracker = mock(PeerTransactionTracker.class);
TransactionPool transactionPool =
new TransactionPool(
transactions,
protocolSchedule,
protocolContext,
batchAddedListener,
syncState,
ethContext,
peerTransactionTracker);

final TransactionTestFixture builder = new TransactionTestFixture();
final Transaction transaction1 = builder.nonce(1).createTransaction(KEY_PAIR1);
final Transaction transaction2 = builder.nonce(2).createTransaction(KEY_PAIR1);
Expand All @@ -529,6 +510,40 @@ public void shouldAllowRemoteTransactionsWhenInSync() {
assertTransactionPending(transaction3);
}

@Test
public void shouldSendOnlyLocalTransactionToNewlyConnectedPeer() {
EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create();
EthContext ethContext = ethProtocolManager.ethContext();
PeerTransactionTracker peerTransactionTracker = new PeerTransactionTracker();
TransactionPool transactionPool =
new TransactionPool(
transactions,
protocolSchedule,
protocolContext,
batchAddedListener,
syncState,
ethContext,
peerTransactionTracker);

final TransactionTestFixture builder = new TransactionTestFixture();
final Transaction transactionLocal = builder.nonce(1).createTransaction(KEY_PAIR1);
final Transaction transactionRemote = builder.nonce(2).createTransaction(KEY_PAIR1);
when(transactionValidator.validate(any(Transaction.class))).thenReturn(valid());
when(transactionValidator.validateForSender(
any(Transaction.class), nullable(Account.class), eq(true)))
.thenReturn(valid());

transactionPool.addLocalTransaction(transactionLocal);
transactionPool.addRemoteTransactions(Collections.singletonList(transactionRemote));

RespondingEthPeer peer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager);

Set<Transaction> transactionsToSendToPeer =
peerTransactionTracker.claimTransactionsToSendToPeer(peer.getEthPeer());

assertThat(transactionsToSendToPeer).containsExactly(transactionLocal);
}

private void assertTransactionPending(final Transaction t) {
assertThat(transactions.getTransactionByHash(t.hash())).contains(t);
}
Expand Down Expand Up @@ -583,42 +598,4 @@ private void givenTransactionIsValid(final Transaction transaction) {
eq(transaction), nullable(Account.class), anyBoolean()))
.thenReturn(valid());
}

@Test
public void shouldSendOnlyLocalTransactionToNewlyConnectedPeer() {
SyncState syncState = mock(SyncState.class);
when(syncState.isInSync(anyLong())).thenReturn(true);
EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create();
EthContext ethContext = ethProtocolManager.ethContext();
PeerTransactionTracker peerTransactionTracker = new PeerTransactionTracker();
TransactionPool transactionPool =
new TransactionPool(
transactions,
protocolSchedule,
protocolContext,
batchAddedListener,
syncState,
ethContext,
peerTransactionTracker);

final TransactionTestFixture builder = new TransactionTestFixture();
final Transaction transactionLocal = builder.nonce(1).createTransaction(KEY_PAIR1);
final Transaction transactionRemote = builder.nonce(2).createTransaction(KEY_PAIR1);
when(transactionValidator.validate(any(Transaction.class))).thenReturn(valid());
when(transactionValidator.validateForSender(
eq(transactionLocal), nullable(Account.class), eq(true)))
.thenReturn(valid());
when(transactionValidator.validateForSender(
eq(transactionRemote), nullable(Account.class), eq(true)))
.thenReturn(valid());
transactionPool.addLocalTransaction(transactionLocal);
transactionPool.addRemoteTransactions(Collections.singletonList(transactionRemote));

RespondingEthPeer peer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager);

Set<Transaction> transactionsToSendToPeer =
peerTransactionTracker.claimTransactionsToSendToPeer(peer.getEthPeer());

assertThat(transactionsToSendToPeer).containsExactly(transactionLocal);
}
}