Skip to content

Commit

Permalink
some refactoring based on the recent code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
saborsoft committed May 6, 2024
1 parent 67fe7d4 commit 3ab979d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,15 @@ public static void main(String[] args) {
List<UTXO> input = new ArrayList<>();
input.add(uin);
Transaction t = new Transaction(sender.getPublic(), receivers, fundsToTransfer, input);
// make sure that the sender has enough fund
double available = 0.0;
for (int i = 0; i < input.size(); i++) {
available += input.get(i).getFundTransferred();
boolean b = t.prepareOutputUTXOs();
if (!b) {
System.out.println("Transaction failed");
} else {
// sign the transaction
t.signTheTransaction(sender.getPrivate());
// display the transaction to take a look
UtilityMethods.displayTransaction(t, System.out, 0);
}
// compute the total cost and add the transaction fee
double totalCost = t.getTotalFundToTransfer() + Transaction.TRANSACTION_FEE;
// if fund is not enough, abort
if (available < totalCost) {
System.out.println("fund available=" + available
+ ", not enough for total cost of " + totalCost);
return;
}
// generate the output
for (int i = 0; i < receivers.length; i++) {
UTXO ut = new UTXO(t.getHashID(), sender.getPublic(), receivers[i], fundsToTransfer[i]);
t.addOutputUTXO(ut);
}
// generate the change as an UTXO to the sender
UTXO change = new UTXO(t.getHashID(), sender.getPublic(), sender.getPublic(), available - totalCost);
t.addOutputUTXO(change);
// sign the transaction
t.signTheTransaction(sender.getPrivate());
// display the transaction to take a look
UtilityMethods.displayTransaction(t, System.out, 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static void displayTransaction(Transaction t, PrintStream out, int level)
displayTab(out, level + 2, "change: " + change.getFundTransferred());
displayTab(out, level + 1, "transaction fee: " + Transaction.TRANSACTION_FEE);
boolean b = t.verifySignature();
displayTab(out, level + 1, "\tsignature verification: " + b);
displayTab(out, level + 1, "signature verification: " + b);
displayTab(out, level, "}");
}

Expand Down

0 comments on commit 3ab979d

Please sign in to comment.