Skip to content

Commit

Permalink
Added tests to check tx should be replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
lsebrie committed Oct 12, 2018
1 parent f8f4705 commit 4e7fb39
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
Expand Up @@ -514,6 +514,30 @@ public void executeContractWithFakeBlock() {
Assert.assertEquals(DataWord.ONE, transactionPool.getPendingState().getStorageValue(tx.getContractAddress(), DataWord.ZERO));
}

@Test
public void checkTxWithSameNonceIsRejected() {
Coin balance = Coin.valueOf(1000000);
createTestAccounts(2, balance);
Transaction tx = createSampleTransaction(1, 0, 1000, 0);
Transaction tx2 = createSampleTransaction(1, 0, 2000, 0);

transactionPool.addTransaction(tx);
Assert.assertFalse(transactionPool.addTransaction(tx2));
}

@Test
public void checkTxWithSameNonceBumpedIsAccepted() {
Coin balance = Coin.valueOf(1000000);
createTestAccounts(2, balance);
Transaction tx1 = createSampleTransactionWithGasPrice(1, 0, 1000, 0, 1);
Transaction tx2 = createSampleTransactionWithGasPrice(1, 0, 2000, 0, 2);

transactionPool.addTransaction(tx1);
Assert.assertTrue(transactionPool.addTransaction(tx2));
Assert.assertTrue(transactionPool.getPendingTransactions().stream().anyMatch(tx -> tx.getHash().equals(tx2.getHash())));
Assert.assertFalse(transactionPool.getPendingTransactions().stream().anyMatch(tx -> tx.getHash().equals(tx1.getHash())));
}

private void createTestAccounts(int naccounts, Coin balance) {
Repository repository = blockChain.getRepository();

Expand Down
Expand Up @@ -23,26 +23,24 @@ public static Transaction createSampleTransaction(long nonce) {
Account sender = new AccountBuilder().name("sender").build();
Account receiver = new AccountBuilder().name("receiver").build();

Transaction tx = new TransactionBuilder()
.nonce(nonce)
.sender(sender)
.receiver(receiver)
.value(BigInteger.TEN)
.build();
Transaction tx = getBuilder(sender, receiver, nonce, 10).build();

return tx;
}

private static TransactionBuilder getBuilder(Account sender, Account receiver, long nonce, long value) {
return new TransactionBuilder()
.sender(sender)
.receiver(receiver)
.nonce(nonce)
.value(BigInteger.valueOf(value));
}

public static Transaction createSampleTransaction(int from, int to, long value, int nonce) {
Account sender = createAccount(from);
Account receiver = createAccount(to);

Transaction tx = new TransactionBuilder()
.sender(sender)
.receiver(receiver)
.nonce(nonce)
.value(BigInteger.valueOf(value))
.build();
Transaction tx = getBuilder(sender, receiver, nonce, value).build();

return tx;
}
Expand All @@ -51,17 +49,24 @@ public static Transaction createSampleTransaction(int from, int to, long value,
Account sender = createAccount(from);
Account receiver = createAccount(to);

Transaction tx = new TransactionBuilder()
.sender(sender)
.receiver(receiver)
.nonce(nonce)
.value(BigInteger.valueOf(value))
Transaction tx = getBuilder(sender, receiver, nonce, value)
.gasLimit(gasLimit)
.build();

return tx;
}

public static Transaction createSampleTransactionWithGasPrice(int from, int to, long value, int nonce, long gasPrice) {
Account sender = createAccount(from);
Account receiver = createAccount(to);

Transaction tx = getBuilder(sender, receiver, nonce, value)
.gasPrice(BigInteger.valueOf(gasPrice))
.build();

return tx;
}

public static Transaction createSampleTransactionWithData(int from, int nonce, String data) {
Account sender = createAccount(from);

Expand Down

0 comments on commit 4e7fb39

Please sign in to comment.