UTXO Validation

medium · bitcoin, utxo, validation

UTXO Validation and Updates

Implement validation and application of transactions against a UTXO set. This problem focuses on value conservation and double-spend prevention.

Types

type Outpoint struct {
    TxID  [32]byte
    Index uint32
}

type TxIn struct {
    Prev Outpoint
}

type TxOut struct {
    Value uint64
}

type Transaction struct {
    ID       [32]byte
    Inputs   []TxIn
    Outputs  []TxOut
    Coinbase bool
}

type UTXOSet map[Outpoint]TxOut

func ValidateTx(tx Transaction, utxo UTXOSet) (fee uint64, ok bool)
func ApplyTx(tx Transaction, utxo UTXOSet) (fee uint64, ok bool)

Rules

  • Coinbase transactions must have zero inputs and at least one output.
  • Non-coinbase transactions must have at least one input.
  • Each input must exist in the UTXO set and may be spent at most once.
  • sum(inputs) >= sum(outputs); the fee is sum(inputs) - sum(outputs).
  • On ApplyTx, remove spent UTXOs and add new outputs with outpoints (tx.ID, output_index).

Notes

  • ApplyTx should not modify the UTXO set when ok=false.
  • Overflow in value sums should cause validation failure.
Run tests to see results
No issues detected