Building_UTXO_Set
Building UTXO Set
What is an UTXO
UTXO stands for unspent transaction output. In a bitcoin transaction there are inputs and outputs. An input to a transaction has to be an output of another transaction unless the transaction is a coinbase transaction (block reward) which has no inputs at all. In bitcoin a UTXO is essentially a transaction out which has not been included in another transactions input. So inorder to validate a transaction one must make sure that the input is a UTXO i.e., it has not been used as an input in anyother transaction and the provided scriptsig should unlock the scriptpubkey.
How it is represented in rust-bitcoin crate
Transaction output has the information about the amount being locked onto a script pubkey. In rust-bitcoin crate this is how Transaction Output is represented.
Every transaction has a sequence of TxOut
. So any TxOut
can be uniquely identified by transaction id and the index of that output in that transaction. That's exactly how OutPoint
is defined in rust-bitcoin,
How to build the UTXO set?
How big is the UTXO set?
Problem Statement
Given a block_height
, consider it as the genesis block and build the utxo set for the 10 blocks starting from block_height
.
Initialize the HashSet for maintaining the active utxos.
Starting from the block_height
to block_height + 10
fetch each block.
A block has a sequence of transactions and for each of them we have to process utxo_set
based on it's input and output. We have to remove the previous out of the input from the utxo_set
and add the outputs to the set, except when block refers to the block at block_height
because that's how we want to do.
Note Rust specific syntax
Why is that? Think about the consequences of the design and why they have been done this way.
Last updated