Skip to content

Instantly share code, notes, and snippets.

@mrtrkmn
Created January 8, 2019 19:18
Show Gist options
  • Save mrtrkmn/2702d5e4aefa4b83952e38592bc3047c to your computer and use it in GitHub Desktop.
Save mrtrkmn/2702d5e4aefa4b83952e38592bc3047c to your computer and use it in GitHub Desktop.
Blockchain Assignment 1 - Scrooge Coin , TxHandler
import java.util.ArrayList;
import java.util.List;
// Ahmet Turkmen 110510016
public class TxHandler {
private UTXOPool utxoPool;
/**
* Creates a public ledger whose current UTXOPool (collection of unspent transaction outputs) is
* {@code utxoPool}. This should make a copy of utxoPool by using the UTXOPool(UTXOPool uPool)
* constructor.
*/
public TxHandler(UTXOPool utxoPool) {
// IMPLEMENT THIS
this.utxoPool = new UTXOPool(utxoPool);
}
/**
* @return true if:
* (1) all outputs claimed by {@code tx} are in the current UTXO pool,
* (2) the signatures on each input of {@code tx} are valid,
* (3) no UTXO is claimed multiple times by {@code tx},
* (4) all of {@code tx}s output values are non-negative, and
* (5) the sum of {@code tx}s input values is greater than or equal to the sum of its output
* values; and false otherwise.
*/
public boolean isValidTx(Transaction tx) {
// IMPLEMENT THIS
UTXOPool transactionsInPool = new UTXOPool();
double sumOfconsumedCoins = 0, sumOfProcudedCoins=0;
// We are basically checking the transactions to prevent double spending attack. By validating transactions from transaction pool.
for (int i = 0; i < tx.numInputs(); i++) {
Transaction.Input inputTransaction = tx.getInput(i);
UTXO utxo = new UTXO(inputTransaction.prevTxHash,inputTransaction.outputIndex);
Transaction.Output outTransaction = utxoPool.getTxOutput(utxo);
if (!utxoPool.contains(utxo))
return false;
if (!Crypto.verifySignature(outTransaction.address, tx.getRawDataToSign(i), inputTransaction.signature))
return false;
if (transactionsInPool.contains(utxo))
return false;
transactionsInPool.addUTXO(utxo, outTransaction);
sumOfconsumedCoins += outTransaction.value;
}
for (Transaction.Output outTr : tx.getOutputs()) {
if (outTr.value < 0)
return false;
sumOfProcudedCoins += outTr.value;
}
return sumOfconsumedCoins >= sumOfProcudedCoins;
}
/**
* Handles each epoch by receiving an unordered array of proposed transactions, checking each
* transaction for correctness, returning a mutually valid array of accepted transactions, and
* updating the current UTXO pool as appropriate.
*/
public Transaction[] handleTxs(Transaction[] possibleTxs) {
// IMPLEMENT THIS
List<Transaction> validTransactionsArrayList = new ArrayList<>();
boolean done = false;
while(!done){
done = true;
for(int i = 0; i < possibleTxs.length; i++){
if(possibleTxs[i] == null){
continue;
}
if(isValidTx(possibleTxs[i])){
List<Transaction.Input> inputs = possibleTxs[i].getInputs();
for(int j = 0; j < inputs.size(); j++){
Transaction.Input tempInput = inputs.get(j);
UTXO tempUTXO = new UTXO(tempInput.prevTxHash, tempInput.outputIndex);
utxoPool.removeUTXO(tempUTXO);
}
ArrayList<Transaction.Output> outputs = possibleTxs[i].getOutputs();
for(int j = 0; j < outputs.size(); j++){
Transaction.Output tempOutput = outputs.get(j);
UTXO tempUTXO = new UTXO(possibleTxs[i].getHash(), j);
utxoPool.addUTXO(tempUTXO, tempOutput);
}
validTransactionsArrayList.add(possibleTxs[i]);
possibleTxs[i++] = null;
done = false;
}
}
}
Transaction[] validTransactions = new Transaction[validTransactionsArrayList.size()];
validTransactions = validTransactionsArrayList.toArray(validTransactions);
return validTransactions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment