In this exercise, you will implement a UTXO set and update it by executing the transactions of each block that you receive. This task will not yet cover all the features of the UTXO set. We will revisit this part in future homeworks.
For each block in your database, store a UTXO set that will be computed by executing the transactions in that block. This set is not modified when you receive transactions, only when they get executed during handling of new block objects.
When you receive a new block, you will compute the UTXO set after that block in the following way. To begin with, initialize the UTXO set to the UTXO set after the parent block (the block corresponding to the previd). Note that the UTXO set after the genesis block is empty. For each transaction in the block:
(a) Validate the transaction as per your validation logic implemented in Task 2. Addi- tionally, check that each input of the transaction corresponds to an output that is present in the UTXO set. This means that the output exists and also that the output has not been spent yet.
(b) Apply the transaction by removing UTXOs that are spent and adding UTXOs that are created. Update the UTXO set accordingly.
(c) Repeat steps a-b for the next transaction using the updated UTXO set. This is because a transaction can spend from an output of a non-coinbase transaction in the same block.
For testing your node, you can use the genesis block which is a valid block. Below is another block mined on the genesis block that is valid. You can also mine more blocks to test your validation.
In this exercise, you will implement a UTXO set and update it by executing the transactions of each block that you receive. This task will not yet cover all the features of the UTXO set. We will revisit this part in future homeworks.