Skip to content

Commit

Permalink
Transaction equals, used in TransactionPool
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Aug 17, 2019
1 parent 60dff50 commit 82fb470
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/main/java/com/ajlopez/blockchain/core/Transaction.java
Expand Up @@ -70,4 +70,23 @@ public Hash getHash() {
private Hash calculateHash() {
return HashUtils.calculateHash(TransactionEncoder.encode(this));
}

@Override
public boolean equals(Object obj) {
if (obj == null)
return false;

if (this == obj)
return true;

if (!(obj instanceof Transaction))
return false;

return this.getHash().equals(((Transaction)obj).getHash());
}

@Override
public int hashCode() {
return this.getHash().hashCode();
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/ajlopez/blockchain/core/TransactionTest.java
Expand Up @@ -95,4 +95,29 @@ public void createTransactionWithNoReceiver() {

new Transaction(sender, null, value, 42, null, 6000000, Coin.ZERO);
}

@Test
public void equalTransaction() {
Address sender = FactoryHelper.createRandomAddress();
Address receiver = FactoryHelper.createRandomAddress();
Coin value = Coin.ONE;

byte[] data = FactoryHelper.createRandomBytes(128);

Transaction tx = new Transaction(sender, receiver, value, 42, data, 6000000, Coin.ZERO);
Transaction tx2 = new Transaction(sender, receiver, value, 42, data, 6000000, Coin.ZERO);
Transaction tx3 = new Transaction(sender, receiver, value, 42, data, 6000000, Coin.ONE);

Assert.assertTrue(tx.equals(tx));
Assert.assertTrue(tx.equals(tx2));
Assert.assertTrue(tx2.equals(tx));
Assert.assertEquals(tx.hashCode(), tx2.hashCode());

Assert.assertFalse(tx.equals(tx3));
Assert.assertFalse(tx2.equals(tx3));
Assert.assertFalse(tx3.equals(tx));
Assert.assertFalse(tx3.equals(tx2));
Assert.assertFalse(tx.equals(null));
Assert.assertFalse(tx.equals("foo"));
}
}
Expand Up @@ -84,7 +84,7 @@ public void removeUnknownTransaction() {
}

@Test
public void addTransactionTwice() {
public void addSameTransactionTwice() {
TransactionPool pool = new TransactionPool();
Transaction transaction = FactoryHelper.createTransaction(100);

Expand All @@ -102,6 +102,26 @@ public void addTransactionTwice() {
Assert.assertEquals(transaction, result.get(0));
}

@Test
public void addTransactionTwice() {
TransactionPool pool = new TransactionPool();
Transaction transaction = FactoryHelper.createTransaction(100);
Transaction transaction2 = new Transaction(transaction.getSender(), transaction.getReceiver(), transaction.getValue(), transaction.getNonce(), transaction.getData(), transaction.getGas(), transaction.getGasPrice());

pool.addTransaction(transaction);
List<Transaction> added = pool.addTransaction(transaction2);

Assert.assertNotNull(added);
Assert.assertTrue(added.isEmpty());

List<Transaction> result = pool.getTransactions();

Assert.assertNotNull(result);
Assert.assertFalse(result.isEmpty());
Assert.assertEquals(1, result.size());
Assert.assertEquals(transaction, result.get(0));
}

@Test
public void addTransactionGetListAddTransaction() {
TransactionPool pool = new TransactionPool();
Expand Down

0 comments on commit 82fb470

Please sign in to comment.