package utxo import ( "crypto/sha256" "errors" "fmt" "git.smesh.lol/btcd-lite/internal/wire" ) // Lookup returns the unspent entry of an outpoint. found is false when the // outpoint is not in the set the lookup was built over. type Lookup func(op wire.OutPoint) (Entry, bool) // ScriptHashOf returns the script hash an entry carries. func ScriptHashOf(script []byte) [8]byte { sum := sha256.Sum256(script) var out [8]byte copy(out[:], sum[:8]) return out } // BlockTouches returns what one block did to the set: a spend for every input // of every non-coinbase transaction it does not create, and a creation for every // output of every transaction. // // A transaction in a block may spend an output an earlier transaction in the // same block created (BIP30's situation, and ordinary in a block with a chain of // spends). Those outpoints are never in the set the block is applied to, so they // are reported as neither: the creation and the spend cancel, and the outpoint // is only in the set between the two transactions of the same block, which is // not a height the set is ever asked about. // // lookup is asked only about inputs the block does not create, and only when it // is non-nil. The plot replays a range from its start set and passes nil, // because a lookup against a live set would refuse the spends the replay is // there to reconstruct. func BlockTouches(block *wire.Block, height uint32, lookup Lookup) ([]Touch, error) { created := make(map[wire.OutPoint]bool, len(block.Txs)) spentInBlock := make(map[wire.OutPoint]bool, len(block.Txs)) touches := make([]Touch, 0, len(block.Txs)*2) for i := range block.Txs { tx := &block.Txs[i] txid := tx.TxID() for j := range tx.TxOut { created[wire.OutPoint{Hash: txid, Index: uint32(j)}] = true } } for i := range block.Txs { tx := &block.Txs[i] if !(i == 0 && tx.IsCoinBase()) { for j := range tx.TxIn { op := tx.TxIn[j].PreviousOutPoint if created[op] { spentInBlock[op] = true continue } if lookup != nil { if _, ok := lookup(op); !ok { return nil, fmt.Errorf("utxo: %s spends %s, which is not in the set", tx.TxID(), op) } } touches = append(touches, Touch{OutPoint: op, Height: height, Spent: true}) } } } for i := range block.Txs { tx := &block.Txs[i] txid := tx.TxID() for j := range tx.TxOut { op := wire.OutPoint{Hash: txid, Index: uint32(j)} if spentInBlock[op] { // Created and spent inside this block: it was never in the set // this block is applied to. continue } out := tx.TxOut[j] touches = append(touches, Touch{ OutPoint: op, Height: height, New: Entry{ OutPoint: op, Value: out.Value, ScriptHash: ScriptHashOf(out.PkScript), Height: height, Coinbase: i == 0 && tx.IsCoinBase(), }, }) } } return touches, nil } // ErrOrder is a set that is not sorted. var ErrOrder = errors.New("utxo: set is not sorted by outpoint") // Validate reports whether a set is sorted and has no duplicate outpoints. func Validate(set []Entry) error { for i := 1; i < len(set); i++ { if compareOutPoints(set[i-1].OutPoint, set[i].OutPoint) >= 0 { return ErrOrder } } return nil } // Find returns the entry of an outpoint in a sorted set. func Find(set []Entry, op wire.OutPoint) (Entry, bool) { lo, hi := 0, len(set) for lo < hi { mid := (lo + hi) / 2 c := compareOutPoints(set[mid].OutPoint, op) switch { case c < 0: lo = mid + 1 case c > 0: hi = mid default: return set[mid], true } } return Entry{}, false } // LookupFor returns a Lookup over a sorted set. func LookupFor(set []Entry) Lookup { return func(op wire.OutPoint) (Entry, bool) { return Find(set, op) } }