From 3ab979d41dfd11f682da5d31da5862c337afbf2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flesch=20Tam=C3=A1s?= Date: Mon, 6 May 2024 16:20:57 +0200 Subject: [PATCH] some refactoring based on the recent code changes --- .../transaction/TransactionExample.java | 32 +++++-------------- .../blockchain/support/UtilityMethods.java | 2 +- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/main/java/hu/saborsoft/blockchain/example/transaction/TransactionExample.java b/src/main/java/hu/saborsoft/blockchain/example/transaction/TransactionExample.java index 3fdc735..90e3d45 100644 --- a/src/main/java/hu/saborsoft/blockchain/example/transaction/TransactionExample.java +++ b/src/main/java/hu/saborsoft/blockchain/example/transaction/TransactionExample.java @@ -27,31 +27,15 @@ public static void main(String[] args) { List 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); } } diff --git a/src/main/java/hu/saborsoft/blockchain/support/UtilityMethods.java b/src/main/java/hu/saborsoft/blockchain/support/UtilityMethods.java index 54aab66..0dac4a5 100644 --- a/src/main/java/hu/saborsoft/blockchain/support/UtilityMethods.java +++ b/src/main/java/hu/saborsoft/blockchain/support/UtilityMethods.java @@ -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, "}"); }