-
Notifications
You must be signed in to change notification settings - Fork 185
/
verification_test.go
363 lines (302 loc) · 9.08 KB
/
verification_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package neutrino
import (
"crypto/sha256"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/gcs"
"github.com/btcsuite/btcd/btcutil/gcs/builder"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/stretchr/testify/require"
)
var (
chainParams = &chaincfg.RegressionNetParams
dummySignaturePush = []byte{
txscript.OP_DATA_72,
0x30, 0x45, 0x02, 0x21, 0x00, 0xad, 0x08, 0x51,
0xc6, 0x9d, 0xd7, 0x56, 0xb4, 0x51, 0x90, 0xb5,
0xa8, 0xe9, 0x7c, 0xb4, 0xac, 0x3c, 0x2b, 0x0f,
0xa2, 0xf2, 0xaa, 0xe2, 0x3a, 0xed, 0x6c, 0xa9,
0x7a, 0xb3, 0x3b, 0xf8, 0x83, 0x02, 0x20, 0x0b,
0x24, 0x85, 0x93, 0xab, 0xc1, 0x25, 0x95, 0x12,
0x79, 0x3e, 0x7d, 0xea, 0x61, 0x03, 0x6c, 0x60,
0x17, 0x75, 0xeb, 0xb2, 0x36, 0x40, 0xa0, 0x12,
0x0b, 0x0d, 0xba, 0x2c, 0x34, 0xb7, 0x90, 0x01,
}
)
// TestVerifyBlockFilter tests that a filter is correctly inspected for validity
// against a downloaded block.
func TestVerifyBlockFilter(t *testing.T) {
privKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
pubKey := privKey.PubKey()
pubKey2 := doubleKey(pubKey)
// We'll create an initial block with just one TX that spends to all
// currently known standard output types (P2PKH, P2SH, NP2WKH, P2WKH and
// P2WSH). All those outputs are then spent in the next block in a
// single TX that references all of them.
prevTx := &wire.MsgTx{
Version: 2,
TxIn: []*wire.TxIn{},
TxOut: []*wire.TxOut{{
Value: 999,
PkScript: makeP2PK(t, pubKey),
}, {
Value: 999,
PkScript: makeP2PKH(t, pubKey),
}, {
Value: 999,
PkScript: makeP2SH(t, pubKey),
}, {
Value: 999,
PkScript: makeNP2WKH(t, pubKey),
}, {
Value: 999,
PkScript: makeP2WKH(t, pubKey),
}, {
Value: 999,
PkScript: makeP2WSH(t, pubKey),
}},
}
prevBlock := &wire.MsgBlock{
Header: wire.BlockHeader{
PrevBlock: [32]byte{1, 2, 3},
MerkleRoot: [32]byte{3, 2, 1},
},
Transactions: []*wire.MsgTx{
{}, // Fake coinbase TX.
prevTx,
},
}
// The spend TX is the transaction that has an input to spend each of
// the different output types we created in the previous block/TX.
spendTx := &wire.MsgTx{
Version: 2,
TxIn: []*wire.TxIn{
spendP2PK(t, pubKey, prevTx, 0),
spendP2PKH(t, pubKey, prevTx, 1),
spendP2SH(t, pubKey, prevTx, 2),
spendNP2WKH(t, pubKey, prevTx, 3),
spendP2WKH(t, pubKey, prevTx, 4),
spendP2WSH(t, pubKey, prevTx, 5),
},
TxOut: []*wire.TxOut{{
Value: 999,
PkScript: makeP2WKH(t, pubKey2),
}, {
Value: 999,
PkScript: makeP2WSH(t, pubKey2),
}, {
Value: 999,
PkScript: []byte{txscript.OP_RETURN},
}},
}
spendBlock := &wire.MsgBlock{
Header: wire.BlockHeader{
PrevBlock: prevBlock.BlockHash(),
MerkleRoot: [32]byte{3, 2, 1},
},
Transactions: []*wire.MsgTx{
{}, // Fake coinbase TX.
spendTx,
},
}
// We now create a filter from our block that is fully valid and
// contains all the entries we require according to BIP-158.
utxoSet := []*wire.MsgTx{prevTx}
validFilter := filterFromBlock(t, utxoSet, spendBlock, true)
b := btcutil.NewBlock(spendBlock)
opReturnValid, err := VerifyBasicBlockFilter(validFilter, b)
require.NoError(t, err)
require.Equal(t, 1, opReturnValid)
}
func filterFromBlock(t *testing.T, utxoSet []*wire.MsgTx,
block *wire.MsgBlock, withInputPrevOut bool) *gcs.Filter {
var filterContent [][]byte
for idx, tx := range block.Transactions {
// Skip coinbase transaction.
if idx == 0 {
continue
}
// Add all output pk scripts. Normally we'd need to filter out
// any OP_RETURNs but for the test we want to make sure they're
// counted correctly so we leave them in.
for _, out := range tx.TxOut {
filterContent = append(filterContent, out.PkScript)
}
// To create an invalid filter we just skip the pk scripts of
// the spent outputs.
if !withInputPrevOut {
continue
}
// Add all previous output scripts of all transactions.
for _, in := range tx.TxIn {
utxo := locateUtxo(t, utxoSet, in)
filterContent = append(filterContent, utxo.PkScript)
}
}
blockHash := block.BlockHash()
key := builder.DeriveKey(&blockHash)
filter, err := gcs.BuildGCSFilter(
builder.DefaultP, builder.DefaultM, key, filterContent,
)
require.NoError(t, err)
return filter
}
func locateUtxo(t *testing.T, utxoSet []*wire.MsgTx, in *wire.TxIn) *wire.TxOut {
for _, utxo := range utxoSet {
if utxo.TxHash() == in.PreviousOutPoint.Hash {
return utxo.TxOut[in.PreviousOutPoint.Index]
}
}
require.Fail(t, "utxo for outpoint %v not found", in.PreviousOutPoint)
return nil
}
func spendP2PK(_ *testing.T, _ *btcec.PublicKey, prevTx *wire.MsgTx,
idx uint32) *wire.TxIn {
return &wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: prevTx.TxHash(),
Index: idx,
},
SignatureScript: dummySignaturePush,
}
}
func spendP2PKH(_ *testing.T, pubKey *btcec.PublicKey, prevTx *wire.MsgTx,
idx uint32) *wire.TxIn {
return &wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: prevTx.TxHash(),
Index: idx,
},
SignatureScript: append(
dummySignaturePush, pubKey.SerializeCompressed()...,
),
}
}
func spendP2SH(t *testing.T, pubKey *btcec.PublicKey, prevTx *wire.MsgTx,
idx uint32) *wire.TxIn {
return &wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: prevTx.TxHash(),
Index: idx,
},
SignatureScript: append(
dummySignaturePush, scriptP2PKH(t, pubKey)...,
),
}
}
func spendNP2WKH(t *testing.T, pubKey *btcec.PublicKey, prevTx *wire.MsgTx,
idx uint32) *wire.TxIn {
pkHash := btcutil.Hash160(pubKey.SerializeCompressed())
witAddr, err := btcutil.NewAddressWitnessPubKeyHash(pkHash, chainParams)
require.NoError(t, err)
witnessProgram, err := txscript.PayToAddrScript(witAddr)
require.NoError(t, err)
return &wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: prevTx.TxHash(),
Index: idx,
},
SignatureScript: witAddr.ScriptAddress(),
Witness: [][]byte{dummySignaturePush, witnessProgram},
}
}
func spendP2WKH(_ *testing.T, pubKey *btcec.PublicKey, prevTx *wire.MsgTx,
idx uint32) *wire.TxIn {
return &wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: prevTx.TxHash(),
Index: idx,
},
Witness: [][]byte{
dummySignaturePush, pubKey.SerializeCompressed(),
},
}
}
func spendP2WSH(t *testing.T, pubKey *btcec.PublicKey, prevTx *wire.MsgTx,
idx uint32) *wire.TxIn {
script := scriptP2PKH(t, pubKey)
return &wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: prevTx.TxHash(),
Index: idx,
},
Witness: [][]byte{dummySignaturePush, script},
}
}
func makeP2PK(t *testing.T, pubKey *btcec.PublicKey) []byte {
addr, err := btcutil.NewAddressPubKey(
pubKey.SerializeCompressed(), chainParams,
)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
return pkScript
}
func makeP2PKH(t *testing.T, pubKey *btcec.PublicKey) []byte {
pkHash := btcutil.Hash160(pubKey.SerializeCompressed())
addr, err := btcutil.NewAddressPubKeyHash(pkHash, chainParams)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
return pkScript
}
func makeP2SH(t *testing.T, pubKey *btcec.PublicKey) []byte {
script := scriptP2PKH(t, pubKey)
addr, err := btcutil.NewAddressScriptHash(script, chainParams)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
return pkScript
}
func makeNP2WKH(t *testing.T, pubKey *btcec.PublicKey) []byte {
pkHash := btcutil.Hash160(pubKey.SerializeCompressed())
witAddr, err := btcutil.NewAddressWitnessPubKeyHash(pkHash, chainParams)
require.NoError(t, err)
witnessProgram, err := txscript.PayToAddrScript(witAddr)
require.NoError(t, err)
addr, err := btcutil.NewAddressScriptHash(witnessProgram, chainParams)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
return pkScript
}
func makeP2WKH(t *testing.T, pubKey *btcec.PublicKey) []byte {
pkHash := btcutil.Hash160(pubKey.SerializeCompressed())
addr, err := btcutil.NewAddressWitnessPubKeyHash(pkHash, chainParams)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
return pkScript
}
func makeP2WSH(t *testing.T, pubKey *btcec.PublicKey) []byte {
witnessScript := scriptP2PKH(t, pubKey)
scriptHash := sha256.Sum256(witnessScript)
addr, err := btcutil.NewAddressWitnessScriptHash(
scriptHash[:], chainParams,
)
require.NoError(t, err)
pkScript, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
return pkScript
}
func scriptP2PKH(t *testing.T, pubKey *btcec.PublicKey) []byte {
// A simple P2PKH script but wrapped as P2SH.
// <pubKey> OP_CHECKSIG
b := txscript.NewScriptBuilder()
b.AddData(pubKey.SerializeCompressed())
b.AddOp(txscript.OP_CHECKSIG)
script, err := b.Script()
require.NoError(t, err)
return script
}
func doubleKey(key *btcec.PublicKey) *btcec.PublicKey {
var keyJacobian btcec.JacobianPoint
key.AsJacobian(&keyJacobian)
btcec.DoubleNonConst(&keyJacobian, &keyJacobian)
keyJacobian.ToAffine()
return btcec.NewPublicKey(&keyJacobian.X, &keyJacobian.Y)
}