1 package utxo
2 3 import (
4 "crypto/sha256"
5 "errors"
6 "fmt"
7 8 "git.smesh.lol/btcd-lite/internal/wire"
9 )
10 11 // Lookup returns the unspent entry of an outpoint. found is false when the
12 // outpoint is not in the set the lookup was built over.
13 type Lookup func(op wire.OutPoint) (Entry, bool)
14 15 // ScriptHashOf returns the script hash an entry carries.
16 func ScriptHashOf(script []byte) [8]byte {
17 sum := sha256.Sum256(script)
18 var out [8]byte
19 copy(out[:], sum[:8])
20 return out
21 }
22 23 // BlockTouches returns what one block did to the set: a spend for every input
24 // of every non-coinbase transaction it does not create, and a creation for every
25 // output of every transaction.
26 //
27 // A transaction in a block may spend an output an earlier transaction in the
28 // same block created (BIP30's situation, and ordinary in a block with a chain of
29 // spends). Those outpoints are never in the set the block is applied to, so they
30 // are reported as neither: the creation and the spend cancel, and the outpoint
31 // is only in the set between the two transactions of the same block, which is
32 // not a height the set is ever asked about.
33 //
34 // lookup is asked only about inputs the block does not create, and only when it
35 // is non-nil. The plot replays a range from its start set and passes nil,
36 // because a lookup against a live set would refuse the spends the replay is
37 // there to reconstruct.
38 func BlockTouches(block *wire.Block, height uint32, lookup Lookup) ([]Touch, error) {
39 created := make(map[wire.OutPoint]bool, len(block.Txs))
40 spentInBlock := make(map[wire.OutPoint]bool, len(block.Txs))
41 touches := make([]Touch, 0, len(block.Txs)*2)
42 43 for i := range block.Txs {
44 tx := &block.Txs[i]
45 txid := tx.TxID()
46 for j := range tx.TxOut {
47 created[wire.OutPoint{Hash: txid, Index: uint32(j)}] = true
48 }
49 }
50 for i := range block.Txs {
51 tx := &block.Txs[i]
52 if !(i == 0 && tx.IsCoinBase()) {
53 for j := range tx.TxIn {
54 op := tx.TxIn[j].PreviousOutPoint
55 if created[op] {
56 spentInBlock[op] = true
57 continue
58 }
59 if lookup != nil {
60 if _, ok := lookup(op); !ok {
61 return nil, fmt.Errorf("utxo: %s spends %s, which is not in the set", tx.TxID(), op)
62 }
63 }
64 touches = append(touches, Touch{OutPoint: op, Height: height, Spent: true})
65 }
66 }
67 }
68 for i := range block.Txs {
69 tx := &block.Txs[i]
70 txid := tx.TxID()
71 for j := range tx.TxOut {
72 op := wire.OutPoint{Hash: txid, Index: uint32(j)}
73 if spentInBlock[op] {
74 // Created and spent inside this block: it was never in the set
75 // this block is applied to.
76 continue
77 }
78 out := tx.TxOut[j]
79 touches = append(touches, Touch{
80 OutPoint: op,
81 Height: height,
82 New: Entry{
83 OutPoint: op,
84 Value: out.Value,
85 ScriptHash: ScriptHashOf(out.PkScript),
86 Height: height,
87 Coinbase: i == 0 && tx.IsCoinBase(),
88 },
89 })
90 }
91 }
92 return touches, nil
93 }
94 95 // ErrOrder is a set that is not sorted.
96 var ErrOrder = errors.New("utxo: set is not sorted by outpoint")
97 98 // Validate reports whether a set is sorted and has no duplicate outpoints.
99 func Validate(set []Entry) error {
100 for i := 1; i < len(set); i++ {
101 if compareOutPoints(set[i-1].OutPoint, set[i].OutPoint) >= 0 {
102 return ErrOrder
103 }
104 }
105 return nil
106 }
107 108 // Find returns the entry of an outpoint in a sorted set.
109 func Find(set []Entry, op wire.OutPoint) (Entry, bool) {
110 lo, hi := 0, len(set)
111 for lo < hi {
112 mid := (lo + hi) / 2
113 c := compareOutPoints(set[mid].OutPoint, op)
114 switch {
115 case c < 0:
116 lo = mid + 1
117 case c > 0:
118 hi = mid
119 default:
120 return set[mid], true
121 }
122 }
123 return Entry{}, false
124 }
125 126 // LookupFor returns a Lookup over a sorted set.
127 func LookupFor(set []Entry) Lookup {
128 return func(op wire.OutPoint) (Entry, bool) { return Find(set, op) }
129 }
130