// Package filter builds the BIP158 basic compact block filter of a block. // // The element set is the one BIP158 defines: the scriptPubKey of every output // that is not an OP_RETURN, and the previous output script of every // non-coinbase input. Input scripts come from the block's own transactions // where the spent output is earlier in the same block, and from a lookup for // anything older. That lookup is the caller's, because this package holds no // chain state. // // The Golomb-coded set itself is not reimplemented: it is nostrino's // pkg/filter, which carries the BIP158 test vectors, so there is one // implementation of the wire format in the two repositories. package filter import ( "bytes" "fmt" "git.smesh.lol/nostrino/pkg/crypto/chainhash" gcs "git.smesh.lol/nostrino/pkg/filter" "git.smesh.lol/btcd-lite/internal/wire" ) // ScriptLookup returns the scriptPubKey of an outpoint that is not in the block // being filtered. found is false when the caller does not have it, which for a // correctly synced chain cannot happen above the creation point of the outpoint // and does happen for a block whose inputs are older than what the caller holds. type ScriptLookup func(outpoint wire.OutPoint) (script []byte, found bool) // IsUnspendable reports whether a script is an OP_RETURN output, which BIP158 // excludes from the element set. Only the first opcode is looked at; an empty // script is treated as unspendable because it can never be committed to as an // output anyone spends. func IsUnspendable(script []byte) bool { return len(script) == 0 || script[0] == 0x6a } // Elements returns the distinct, non-empty filter elements of one block. // // It returns an error when a non-coinbase input spends an outpoint the block // does not create and the lookup does not have it, because a filter built // without that script is wrong and wrong in a way that is silent. func Elements(block *wire.Block, lookup ScriptLookup) ([][]byte, error) { // Outputs of this block, by outpoint, so an input spending within the // block does not need the lookup. type outpointKey [36]byte spendable := make(map[outpointKey][]byte, len(block.Txs)) key := func(op wire.OutPoint) outpointKey { var k outpointKey copy(k[:32], op.Hash[:]) k[32] = byte(op.Index) k[33] = byte(op.Index >> 8) k[34] = byte(op.Index >> 16) k[35] = byte(op.Index >> 24) return k } spentInBlock := make(map[outpointKey]bool, len(block.Txs)) elems := make([][]byte, 0, len(block.Txs)*2) for i := range block.Txs { tx := &block.Txs[i] for j := range tx.TxOut { script := tx.TxOut[j].PkScript if IsUnspendable(script) { continue } elems = append(elems, script) spendable[key(wire.OutPoint{Hash: tx.TxID(), Index: uint32(j)})] = script } } for i := range block.Txs { tx := &block.Txs[i] if i == 0 && tx.IsCoinBase() { continue } for j := range tx.TxIn { op := tx.TxIn[j].PreviousOutPoint k := key(op) spentInBlock[k] = true if script, ok := spendable[k]; ok { elems = append(elems, script) continue } if lookup == nil { return nil, fmt.Errorf("filter: no script for %s and no lookup", op) } script, found := lookup(op) if !found { return nil, fmt.Errorf("filter: the input script for %s is not known", op) } elems = append(elems, script) } } return dedupe(elems), nil } // dedupe drops empty and duplicate elements while keeping the first order. func dedupe(elems [][]byte) [][]byte { seen := make(map[string]struct{}, len(elems)) out := elems[:0] for _, e := range elems { if len(e) == 0 { continue } if _, ok := seen[string(e)]; ok { continue } seen[string(e)] = struct{}{} out = append(out, e) } return out } // Build returns the BIP158 basic filter of one block. func Build(block *wire.Block, lookup ScriptLookup) ([]byte, error) { elems, err := Elements(block, lookup) if err != nil { return nil, err } hash := block.BlockHash() return gcs.Build(elems, hash), nil } // Hash is the value a filter header chain commits to: double SHA-256 of the // filter's bytes. func Hash(filter []byte) chainhash.Hash { return chainhash.DoubleHashH(filter) } // Fold returns the filter header at one height: double SHA-256 of the filter // hash and the previous filter header, which is BIP157's chain rule with the // genesis header as its first element. func Fold(filterHash, previousHeader chainhash.Hash) chainhash.Hash { buf := make([]byte, 0, 64) buf = append(buf, filterHash[:]...) buf = append(buf, previousHeader[:]...) return chainhash.DoubleHashH(buf) } // Match reports whether any of the query scripts is in the filter, keyed by the // block hash in internal byte order. func Match(filter []byte, blockHash chainhash.Hash, scripts [][]byte) (bool, error) { return gcs.Match(filter, blockHash, scripts) } // Equal reports whether two filters are byte-identical. func Equal(a, b []byte) bool { return bytes.Equal(a, b) }