Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions src/main/java/io/zold/api/Copies.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,63 @@ private static Iterable<Copy> copies(final long id,
* @param second Second wallet
* @return Boolean Boolean
* @throws IOException If fails
* @todo #56:30min Compare the entire content of two wallets. In addition
* to id, compare RSA key and all transactions one by one. Entire content
* of each transaction should be compared.
*/
private static boolean equalWallets(final Wallet first,
final Wallet second) throws IOException {
return first.id() == second.id() && new ListOf<>(
first.ledger()
).size() == new ListOf<>(second.ledger()).size();
final List<Transaction> head = new ListOf<>(first.ledger());
final List<Transaction> tail = new ListOf<>(second.ledger());
boolean equal = first.id() == second.id()
&& first.key().equals(second.key())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

first.key().equals(second.key()) reaches Wallet.File.key(), which still throws UnsupportedOperationException (Wallet.java near line 280, @todo #54). Once two File wallets land here with the same id the short-circuit lets execution reach this expression and it throws instead of letting equalWallets return cleanly. Either gate this branch behind Wallet.File.key actually being implemented (#54 lands first), or have equalWallets treat an unsupported key() as equal-when-ids-match so the comparison degrades gracefully until #54.

&& head.size() == tail.size();
int idx = 0;
while (equal && idx < head.size()) {
equal = Copies.equalTransactions(head.get(idx), tail.get(idx));
idx += 1;
}
return equal;
}

/**
* Checks if two transactions carry the same content across every field
* exposed by {@link Transaction}.
* @param first First transaction
* @param second Second transaction
* @return True when every field matches
* @throws IOException If fails
*/
private static boolean equalTransactions(final Transaction first,
final Transaction second) throws IOException {
return Copies.equalNumbers(first, second)
&& Copies.equalText(first, second);
}

/**
* Checks the numeric fields of two transactions.
* @param first First transaction
* @param second Second transaction
* @return True when id, time, and amount match
* @throws IOException If fails
*/
private static boolean equalNumbers(final Transaction first,
final Transaction second) throws IOException {
return first.id() == second.id()
&& first.amount() == second.amount()
&& first.time().equals(second.time());
}

/**
* Checks the textual fields of two transactions.
* @param first First transaction
* @param second Second transaction
* @return True when prefix, bnf, details, and signature match
* @throws IOException If fails
*/
private static boolean equalText(final Transaction first,
final Transaction second) throws IOException {
return first.prefix().equals(second.prefix())
&& first.bnf().equals(second.bnf())
&& first.details().equals(second.details())
&& first.signature().equals(second.signature());
}

/**
Expand Down
Loading