Implementation of Scrooge Coin Transactions


Here, we will implement the theory of Scrooge Coin transactions, which we have explored in previous sections, using Java. This is one of the programming assignments from Princeton University’s course, “Bitcoin and Cryptocurrency Technologies.”

The table below summarizes the list of classes used and their descriptions.

FileDescription
Crypto.javaA Crypto class containing the verifySignature() method to verify the validity of digital signatures.
Transaction.javaA Transaction class representing a Scrooge Coin transaction. It includes two inner classes, Transaction.Output and Transaction.Input.
TxHandler.javaA TxHandler class containing the isValidTx() method to verify transaction validity and the handleTxs() method to update the current UTXO pool with accepted valid transactions.
UTXO.javaA UTXO class representing an Unspent Transaction Output. It includes fields such as the hash of the transaction that generated the UTXO and the corresponding index within the transaction.
UTXOPool.javaA UTXOPool class representing the current collection of UTXOs. It contains a map from each UTXO to the corresponding transaction output.
Table 1: Class Specifications (Overview)

In this implementation, we will create a TxHandler class with the following API. To make the flow of operations clear, the implementation is written in pseudocode.

public class TxHandler {

    Declares the field utxoPool;

    public TxHandler(UTXOPool utxoPool) {
        Initializes the field utxoPool with the argument value utxoPool;
    }

    public boolean isValidTx(Transaction tx) {
        Declare the HashSet with UTXO type;
        Initializes the input value is 0.0;
        Initializes the output value is 0.0;

        Loop i = 0, 1, ..., number of elements stored in the input of tx
            Creates a new UTXO corresponding to the output 
            with index outputIndex in the transaction whose hash is prevTxHash;

            if (!all outputs claimed by tx are in the current UTXO pool) {
                return false;
            }

            if (!the signatures on each input of tx are valid) {
                return false;
            }

            if (UTXO is claimed multiple times by tx) {
                return false;
            }
            Adds the UTXO to Set;
            Adds the previous output value to the input value;
        End i Loop

        Loop i = 0, 1, ..., number of elements stored in the output of tx
            if (!all of txs output values are non-negative) {
                return false;
            }
            Adds the value to the output value;
        End i Loop

        if (output value > input value) {
            return false;
        }

        return true;
    }

    public Transaction[] handleTxs(Transaction[] possibleTxs) {
        Declares the HashSet with Transaction type;

        Loop i = 0, 1, ..., number of elements stored in the possibleTxs
            if (transaction is valid) {
                Adds the transaction to Set;

                    Loop i = 0, 1, ..., number of elements stored in the input of transacion
                        Creates a new UTXO corresponding to the output with 
                        index outputIndex in the transaction whose hash is prevTxHash;
                        Removes the UTXO from the utxoPool;
                    End i Loop
                    Loop i = 0, 1, ..., number of elements stored in the output of transacion
                        Creates a new UTXO corresponding to the output with 
                        index i in the transaction;
                        Adds a mapping from UTXO to transaction output to the utxoPool;
                    End i Loop
            }
        End i Loop

        return a valid transaction array;
    }
}

This pseudocode outlines the main steps required to handle Scrooge Coin transactions, focusing on validation and UTXO pool updates.

,

Leave a Reply

Your email address will not be published. Required fields are marked *