-
Notifications
You must be signed in to change notification settings - Fork 287
/
tx_test.go
391 lines (375 loc) · 15.2 KB
/
tx_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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright (c) 2019-2022 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package standalone
import (
"bytes"
"encoding/hex"
"errors"
"testing"
"github.com/decred/dcrd/wire"
)
// TestIsCoinbaseTx ensures the coinbase identification works as intended.
func TestIsCoinbaseTx(t *testing.T) {
tests := []struct {
name string // test description
tx string // transaction to test
wantPreTrsy bool // expected coinbase result before treasury active
wantPostTrsy bool // expected coinbase result after treasury active
}{{
name: "mainnet block 2 coinbase",
tx: "010000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff03fa1a981200000000000017a914f59161" +
"58e3e2c4551c1796708db8367207ed13bb8700000000000000000000266a240" +
"2000000000000000000000000000000000000000000000000000000ffa310d9" +
"a6a9588edea1906f0000000000001976a9148ffe7a49ecf0f4858e7a5215530" +
"2177398d2296988ac000000000000000001d8bc28820000000000000000ffff" +
"ffff0800002f646372642f",
wantPreTrsy: true,
wantPostTrsy: false,
}, {
name: "modified mainnet block 2 coinbase: tx version 3",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff02fa1a981200000000000017a914f59161" +
"58e3e2c4551c1796708db8367207ed13bb8700000000000000000000266a240" +
"2000000000000000000000000000000000000000000000000000000ffa310d9" +
"a6a9588e000000000000000001d8bc28820000000000000000ffffffff08000" +
"02f646372642f",
wantPreTrsy: true,
wantPostTrsy: true,
}, {
name: "modified mainnet block 2 coinbase: tx version 3, no miner payout",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff02fa1a981200000000000017a914f59161" +
"58e3e2c4551c1796708db8367207ed13bb8700000000000000000000266a240" +
"2000000000000000000000000000000000000000000000000000000ffa310d9" +
"a6a9588e000000000000000001d8bc28820000000000000000ffffffff08000" +
"02f646372642f",
wantPreTrsy: true,
wantPostTrsy: true,
}, {
name: "mainnet block 3, tx[1] (one input), not coinbase",
tx: "0100000001e68bcb9222c7f6336e865c81d7fd3e4b3244cd83998ac9767efcb" +
"355b3cd295efe02000000ffffffff01105ba3940600000000001976a914dcfd" +
"20801304752f618295dfe0a4c044afcfde3a88ac000000000000000001e04aa" +
"7940600000001000000000000006b48304502210089d763b0c28314b5eb0d4c" +
"97e0183a78bb4a656dcbcd293d29d91921a64c55af02203554e76f432f73862" +
"edd4f2ed80a4599141b13c6ac2406158b05a97c6867a1ba01210244709193c0" +
"5a649df0fb0a96180ec1a8e3cbcc478dc9c4a69a3ec5aba1e97a79",
wantPreTrsy: false,
wantPostTrsy: false,
}, {
name: "mainnet block 373, tx[5] (two inputs), not coinbase",
tx: "010000000201261057a5ecaf6edede86c5446c62f067f30d654117668325090" +
"9ac3e45bec00100000000ffffffff03c65ad19cb990cc916e38dc94f0255f34" +
"4c5e9b7af3b69bfa19931f6027e44c0100000000ffffffff02c1c5760000000" +
"00000001976a914e07c0b2a499312f5d95e3bd4f126e618087a15a588ac402c" +
"42060000000000001976a91411f2b3135e457259009bdd14cfcb942eec58bd7" +
"a88ac0000000000000000023851f6050000000073010000040000006a473044" +
"022009ff5aed5d2e5eeec89319d0a700b7abdf842e248641804c82dee17df44" +
"6c24202207c252cc36199ea8a6cc71d2252a3f7e61f9cce272dff82c5818e3b" +
"f08167e3a6012102773925f9ee53837aa0efba2212f71ee8ab20aeb603fa732" +
"4a8c2555efe5c482709ec0e010000000025010000050000006a473044022011" +
"65136a2b792cc6d7e75f576ed64e1919cbf954afb989f8590844a628e58def0" +
"2206ba7e60f5ae9810794297359cc883e7ff97ecd21bc7177fcc668a84f64a4" +
"b9120121026a4151513b4e6650e3d213451037cd6b78ed829d12ed1d43d5d34" +
"ce0834831e9",
wantPreTrsy: false,
wantPostTrsy: false,
}, {
name: "simnet block 32 coinbase (treasury active)",
tx: "0300000001000000000000000000000000000000000000000000000000000000" +
"0000000000ffffffff00ffffffff02000000000000000000000e6a0c20000000" +
"93c1b2181e4cd3b100ac23fc0600000000001976a91423d4150eb4332733b5bf" +
"88e5d9cea3897bc09dbc88ac00000000000000000100ac23fc06000000000000" +
"00ffffffff0800002f646372642f",
wantPreTrsy: true,
wantPostTrsy: true,
}, {
name: "modified simnet block 32 coinbase (treasury active): no miner payout",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff01000000000000000000000e6a0c200000" +
"0093c1b2181e4cd3b100000000000000000100ac23fc0600000000000000fff" +
"fffff0800002f646372642f",
wantPreTrsy: true,
wantPostTrsy: true,
}, {
name: "random simnet treasury spend",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0200000000000000000000226a20b63c13" +
"400000000045c279dd8870eff33bc219a4c9a39a9190960601d8fccdefc0321" +
"3400000000000001ac376a914c945ac8cdbf5e37ad4bfde5dc92f65e13ff5c6" +
"7488ac000000008201000001b63c13400000000000000000ffffffff6440650" +
"063174184d0438b26d05a2f6d3190d020994ef3c18ac110bf70df3d1ed7066c" +
"8c62c349b8ae86862d04ee94d2bddc42bd33d449c1ba53ed37a2c8d1e8f8f62" +
"102a36b785d584555696b69d1b2bbeff4010332b301e3edd316d79438554cac" +
"b3e7c2",
wantPreTrsy: true,
wantPostTrsy: false,
}, {
// This is neither a valid coinbase nor a valid treasury spend, but
// since the coinbase identification function is only a fast heuristic,
// this is crafted to test that.
name: "modified random simnet treasury spend: missing signature script",
tx: "0300000001000000000000000000000000000000000000000000000000000000" +
"0000000000ffffffff00ffffffff0200000000000000000000226a20b63c1340" +
"0000000045c279dd8870eff33bc219a4c9a39a9190960601d8fccdefc0321340" +
"0000000000001ac376a914c945ac8cdbf5e37ad4bfde5dc92f65e13ff5c67488" +
"ac000000008201000001b63c13400000000000000000ffffffff00",
wantPreTrsy: true,
wantPostTrsy: true,
}}
for _, test := range tests {
txBytes, err := hex.DecodeString(test.tx)
if err != nil {
t.Errorf("%q: unexpected err parsing tx hex %q: %v", test.name,
test.tx, err)
continue
}
var tx wire.MsgTx
if err := tx.FromBytes(txBytes); err != nil {
t.Errorf("%q: unexpected err parsing tx: %v", test.name, err)
continue
}
result := IsCoinBaseTx(&tx, noTreasury)
if result != test.wantPreTrsy {
t.Errorf("%s: unexpected result pre treasury -- got %v, want %v",
test.name, result, test.wantPreTrsy)
continue
}
result = IsCoinBaseTx(&tx, withTreasury)
if result != test.wantPostTrsy {
t.Errorf("%s: unexpected result post treasury -- got %v, want %v",
test.name, result, test.wantPostTrsy)
continue
}
}
}
// TestIsTreasurybaseTx ensures treasurybase identification works as intended.
func TestIsTreasurybaseTx(t *testing.T) {
tests := []struct {
name string // test description
tx string // transaction to test
want bool // expected coinbase test result
}{{
name: "mainnet block 2 coinbase",
tx: "010000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff03fa1a981200000000000017a914f59161" +
"58e3e2c4551c1796708db8367207ed13bb8700000000000000000000266a240" +
"2000000000000000000000000000000000000000000000000000000ffa310d9" +
"a6a9588edea1906f0000000000001976a9148ffe7a49ecf0f4858e7a5215530" +
"2177398d2296988ac000000000000000001d8bc28820000000000000000ffff" +
"ffff0800002f646372642f",
want: false,
}, {
name: "mainnet block 3, tx[1] (one input), not coinbase",
tx: "0100000001e68bcb9222c7f6336e865c81d7fd3e4b3244cd83998ac9767efcb" +
"355b3cd295efe02000000ffffffff01105ba3940600000000001976a914dcfd" +
"20801304752f618295dfe0a4c044afcfde3a88ac000000000000000001e04aa" +
"7940600000001000000000000006b48304502210089d763b0c28314b5eb0d4c" +
"97e0183a78bb4a656dcbcd293d29d91921a64c55af02203554e76f432f73862" +
"edd4f2ed80a4599141b13c6ac2406158b05a97c6867a1ba01210244709193c0" +
"5a649df0fb0a96180ec1a8e3cbcc478dc9c4a69a3ec5aba1e97a79",
want: false,
}, {
name: "mainnet block 373, tx[5] (two inputs), not coinbase",
tx: "010000000201261057a5ecaf6edede86c5446c62f067f30d654117668325090" +
"9ac3e45bec00100000000ffffffff03c65ad19cb990cc916e38dc94f0255f34" +
"4c5e9b7af3b69bfa19931f6027e44c0100000000ffffffff02c1c5760000000" +
"00000001976a914e07c0b2a499312f5d95e3bd4f126e618087a15a588ac402c" +
"42060000000000001976a91411f2b3135e457259009bdd14cfcb942eec58bd7" +
"a88ac0000000000000000023851f6050000000073010000040000006a473044" +
"022009ff5aed5d2e5eeec89319d0a700b7abdf842e248641804c82dee17df44" +
"6c24202207c252cc36199ea8a6cc71d2252a3f7e61f9cce272dff82c5818e3b" +
"f08167e3a6012102773925f9ee53837aa0efba2212f71ee8ab20aeb603fa732" +
"4a8c2555efe5c482709ec0e010000000025010000050000006a473044022011" +
"65136a2b792cc6d7e75f576ed64e1919cbf954afb989f8590844a628e58def0" +
"2206ba7e60f5ae9810794297359cc883e7ff97ecd21bc7177fcc668a84f64a4" +
"b9120121026a4151513b4e6650e3d213451037cd6b78ed829d12ed1d43d5d34" +
"ce0834831e9",
want: false,
}, {
name: "simnet generated treasury base",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0200f2052a01000000000001c100000000" +
"0000000000000e6a0c4600000020cb18b066b6523c00000000000000000100f" +
"2052a0100000000000000ffffffff00",
want: true,
}, {
name: "modified: no inputs",
tx: "03000000000200f2052a01000000000001c1000000000000000000000e6a0c4" +
"600000020cb18b066b6523c000000000000000000",
want: false,
}, {
name: "modified: only one output",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0100f2052a01000000000001c100000000" +
"000000000100f2052a0100000000000000ffffffff00",
want: false,
}, {
name: "modified: first input sig script not empty",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0200f2052a01000000000001c100000000" +
"0000000000000e6a0c4600000020cb18b066b6523c00000000000000000100f" +
"2052a0100000000000000ffffffff016a",
want: false,
}, {
name: "modified: first output pkscript missing",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0200f2052a010000000000000000000000" +
"00000000000e6a0c4600000020cb18b066b6523c00000000000000000100f20" +
"52a0100000000000000ffffffff00",
want: false,
}, {
name: "modified: first output pkscript not OP_TADD",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0200f2052a010000000000016a00000000" +
"0000000000000e6a0c4600000020cb18b066b6523c00000000000000000100f" +
"2052a0100000000000000ffffffff00",
want: false,
}, {
name: "modified: second output pkscript normal payout (not OP_RETURN)",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0200f2052a01000000000001c100000000" +
"0000000000001976a91423d4150eb4332733b5bf88e5d9cea3897bc09dbc88a" +
"c00000000000000000100f2052a0100000000000000ffffffff00",
want: false,
}, {
name: "modified: second output pkscript 13-byte OP_RETURN",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff00ffffffff0200f2052a01000000000001c100000000" +
"0000000000000f6a0d20000000e36029002399907a010000000000000000010" +
"0f2052a0100000000000000ffffffff00",
want: false,
}, {
name: "modified: input prevout tree not regular",
tx: "030000000100000000000000000000000000000000000000000000000000000" +
"00000000000ffffffff01ffffffff0200f2052a01000000000001c100000000" +
"0000000000000e6a0c4600000020cb18b066b6523c00000000000000000100f" +
"2052a0100000000000000ffffffff00",
want: false,
}}
for _, test := range tests {
txBytes, err := hex.DecodeString(test.tx)
if err != nil {
t.Errorf("%q: unexpected err parsing tx hex %q: %v", test.name,
test.tx, err)
continue
}
var tx wire.MsgTx
if err := tx.FromBytes(txBytes); err != nil {
t.Errorf("%q: unexpected err parsing tx: %v", test.name, err)
continue
}
result := IsTreasuryBase(&tx)
if result != test.want {
t.Errorf("%s: unexpected result -- got %v, want %v", test.name,
result, test.want)
continue
}
}
}
// TestCheckTransactionSanity ensures transaction sanity checking works as
// intended.
func TestCheckTransactionSanity(t *testing.T) {
// Create a base transaction that is further manipulated in the tests below
// to test error conditions.
//
// This is mainnet block 373, tx[5] (two inputs, two outputs).
txHex := "010000000201261057a5ecaf6edede86c5446c62f067f30d654117668325090" +
"9ac3e45bec00100000000ffffffff03c65ad19cb990cc916e38dc94f0255f344c5e9" +
"b7af3b69bfa19931f6027e44c0100000000ffffffff02c1c57600000000000000197" +
"6a914e07c0b2a499312f5d95e3bd4f126e618087a15a588ac402c420600000000000" +
"01976a91411f2b3135e457259009bdd14cfcb942eec58bd7a88ac000000000000000" +
"0023851f6050000000073010000040000006a473044022009ff5aed5d2e5eeec8931" +
"9d0a700b7abdf842e248641804c82dee17df446c24202207c252cc36199ea8a6cc71" +
"d2252a3f7e61f9cce272dff82c5818e3bf08167e3a6012102773925f9ee53837aa0e" +
"fba2212f71ee8ab20aeb603fa7324a8c2555efe5c482709ec0e01000000002501000" +
"0050000006a47304402201165136a2b792cc6d7e75f576ed64e1919cbf954afb989f" +
"8590844a628e58def02206ba7e60f5ae9810794297359cc883e7ff97ecd21bc7177f" +
"cc668a84f64a4b9120121026a4151513b4e6650e3d213451037cd6b78ed829d12ed1" +
"d43d5d34ce0834831e9"
txBytes, err := hex.DecodeString(txHex)
if err != nil {
t.Fatalf("unexpected err parsing base tx hex: %v", err)
}
var baseTx wire.MsgTx
if err := baseTx.FromBytes(txBytes); err != nil {
t.Fatalf("nexpected err parsing base tx: %v", err)
}
const maxTxSize = 393216
tests := []struct {
name string // test description
tx *wire.MsgTx // transaction to test
err error // expected error
}{{
name: "ok",
tx: &baseTx,
err: nil,
}, {
name: "transaction has no inputs",
tx: func() *wire.MsgTx {
tx := baseTx.Copy()
tx.TxIn = nil
return tx
}(),
err: ErrNoTxInputs,
}, {
name: "transaction has no outputs",
tx: func() *wire.MsgTx {
tx := baseTx.Copy()
tx.TxOut = nil
return tx
}(),
err: ErrNoTxOutputs,
}, {
name: "transaction too big",
tx: func() *wire.MsgTx {
tx := baseTx.Copy()
tx.TxOut[0].PkScript = bytes.Repeat([]byte{0x00}, maxTxSize)
return tx
}(),
err: ErrTxTooBig,
}, {
name: "transaction with negative output amount",
tx: func() *wire.MsgTx {
tx := baseTx.Copy()
tx.TxOut[0].Value = -1
return tx
}(),
err: ErrBadTxOutValue,
}, {
name: "transaction with single output amount > max per tx",
tx: func() *wire.MsgTx {
tx := baseTx.Copy()
tx.TxOut[0].Value = maxAtoms + 1
return tx
}(),
err: ErrBadTxOutValue,
}, {
name: "transaction with outputs sum > max per tx",
tx: func() *wire.MsgTx {
tx := baseTx.Copy()
tx.TxOut[0].Value = maxAtoms
tx.TxOut[1].Value = 1
return tx
}(),
err: ErrBadTxOutValue,
}, {
name: "transaction spending duplicate input",
tx: func() *wire.MsgTx {
tx := baseTx.Copy()
tx.TxIn[1].PreviousOutPoint = tx.TxIn[0].PreviousOutPoint
return tx
}(),
err: ErrDuplicateTxInputs,
}}
for _, test := range tests {
err := CheckTransactionSanity(test.tx, maxTxSize)
if !errors.Is(err, test.err) {
t.Errorf("%q: unexpected err -- got %v, want %v", test.name, err,
test.err)
continue
}
}
}