Internship Lab 1 RD
Internship Lab 1 RD
Overview: Bitcoin has been gaining a lot of interest as of late. For this lab, we will be
creating a "Bitcoin." Specifically, we will be creating the ledger. The bitcoin
will be given to you in the transaction class. As was covered in the course, a
bitcoin needs to be able to have an input, output, and a signature. The ledger
needs to be able to verify a signature and check that the bitcoin is not being
double spent (there is enough bitcoin to pay the amount).
Material: Go to the following link to download the other classes that will be needed to
complete this lab. You will also need java in order to complete this lab. I
recommend using a program like Eclipse, but feel free to use any program that
you like.
Procedure:
Step 1:
Upload the precoded programs. This includes: UTXO.java, UTXOPool.java,
Tansaction.java, and Crypto.java.
Step 4:
Here is the constructor. You will need to refer to the other java classes.
public TxHandler (UTXOPool utxoPool){
/** Creates a public ledger whose current UTXOPool (collection of unspent
* transaction outputs) is utxoPool. This should make a defensive copy of
* utxoPool by using the UTXOPool(UTXOPool uPool) constructor.
*/
}
Step 5:
Let's work on the TxHandler method:
What are the 5 things that the ledger will need to know from each Transaction?
1. All ______ claimed by tx are in the current UTXO pool
2. All ______ on each _____ of tx are valid.
3. No UTXO is claimed _____ times by tx
4. ALl tx's _____ values are _____ negative.
5. The sum of tx's _____ values are greater than or equal to the sum of its output values;
and _____ otherwise.
/** Returns true if
//1. All ______ claimed by tx are in the current UTXO pool
//2. All ______ on each _____ of tx are valid.
//3. No UTXO is claimed _____ times by tx
//4. ALl tx's _____ values are _____ negative.
//5. The sum of tx's _____ values is greater than or equal to the sum of its output
//values; and _____ otherwise.
*/
public boolean isValidTx (Transaction tx) {
//code
}
This method will call on other methods to check if these rules are followed. If these rules
are followed, then it will return ____ and if they are not followed, it will return _____.
Step 6:
Let’s write a method to check for the first 3 rules.
//checks rule 2
//code
//checks rule 3
//code
usedUTXO.put(utxo, true);
//saving this value for rule 5
this.totalInputSum += previousTxOutput.value;
}
Step 7:
Now, we should validate rules 4 and 5.
private boolean validateRuleNumber45(Transaction tx) {
double outputSum = 0;
for (int i = 0; i < tx.numOutputs(); i++) {
Transaction.Output output = tx.getOutput(i);
if (output == null) {
//return what value?
}
if (output.value < 0) {
//return what value?
}
outputSum += output.value;
}
return this.totalInputSum >= outputSum;
}
Step 8:
Finally, let’s complete the last method.
/** 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){
//code
}