bitcoin-policy 0.1.16-alpha.0

policy code for checking transaction attributes
Documentation
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
crate::ix!();

//-------------------------------------------[.cpp/bitcoin/src/policy/policy.h]

/**
  | Used as the flags parameter to sequence
  | and nLocktime checks in non-consensus
  | code.
  |
  */
lazy_static!{
    static ref STANDARD_LOCKTIME_VERIFY_FLAGS: usize = 
        LOCKTIME_VERIFY_SEQUENCE 
        | LOCKTIME_MEDIAN_TIME_PAST;
}

/**
  | Default for -blockmaxweight, which
  | controls the range of block weights
  | the mining code will create *
  |
  */
pub const DEFAULT_BLOCK_MAX_WEIGHT: usize = MAX_BLOCK_WEIGHT - 4000;

/**
  | Default for -blockmintxfee, which
  | sets the minimum feerate for a transaction
  | in blocks created by mining code *
  |
  */
pub const DEFAULT_BLOCK_MIN_TX_FEE: usize = 1000;

/**
  | The maximum weight for transactions
  | we're willing to relay/mine
  |
  */
pub const MAX_STANDARD_TX_WEIGHT: usize = 400000;

/**
  | The minimum non-witness size for transactions
  | we're willing to relay/mine (1 segwit
  | input + 1 P2WPKH output = 82 bytes)
  |
  */
pub const MIN_STANDARD_TX_NONWITNESS_SIZE: usize = 82;

/**
  | Maximum number of signature check operations
  | in an IsStandard() P2SH script
  |
  */
pub const MAX_P2SH_SIGOPS: usize = 15;

/**
  | The maximum number of sigops we're willing
  | to relay/mine in a single tx
  |
  */
pub const MAX_STANDARD_TX_SIGOPS_COST: usize = MAX_BLOCK_SIGOPS_COST / 5;

/**
  | Default for -maxmempool, maximum megabytes
  | of mempool memory usage
  |
  */
pub const DEFAULT_MAX_MEMPOOL_SIZE: usize = 300;

/**
  | Default for -incrementalrelayfee,
  | which sets the minimum feerate increase
  | for mempool limiting or BIP 125 replacement
  | *
  |
  */
pub const DEFAULT_INCREMENTAL_RELAY_FEE: usize = 1000;

/**
  | Default for -bytespersigop
  |
  */
pub const DEFAULT_BYTES_PER_SIGOP: usize = 20;

/**
  | Default for -permitbaremultisig
  |
  */
pub const DEFAULT_PERMIT_BAREMULTISIG: bool = true;

/**
  | The maximum number of witness stack
  | items in a standard P2WSH script
  |
  */
pub const MAX_STANDARD_P2WSH_STACK_ITEMS: usize = 100;

/**
  | The maximum size in bytes of each witness
  | stack item in a standard P2WSH script
  |
  */
pub const MAX_STANDARD_P2WSH_STACK_ITEM_SIZE: usize = 80;

/**
  | The maximum size in bytes of each witness
  | stack item in a standard BIP 342 script
  | (Taproot, leaf version 0xc0)
  |
  */
pub const MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE: usize = 80;

/**
  | The maximum size in bytes of a standard
  | witnessScript
  |
  */
pub const MAX_STANDARD_P2WSH_SCRIPT_SIZE: usize = 3600;

/**
  | The maximum size of a standard ScriptSig
  |
  */
pub const MAX_STANDARD_SCRIPTSIG_SIZE: usize = 1650;

/**
  | Min feerate for defining dust. Historically
  | this has been based on the minRelayTxFee,
  | however changing the dust limit changes
  | which transactions are standard and
  | should be done with care and ideally
  | rarely. It makes sense to only increase
  | the dust limit after prior releases
  | were already not creating outputs below
  | the new threshold
  |
  */
pub const DUST_RELAY_TX_FEE: usize = 3000;

/**
  | Changing the default transaction version
  | requires a two step process: first adapting
  | relay policy by bumping
  | TX_MAX_STANDARD_VERSION, and then later
  | allowing the new transaction version in the
  | wallet/RPC.
  */
lazy_static!{
    /*
    static constexpr decltype(CTransaction::nVersion) TX_MAX_STANDARD_VERSION{2};
    */
}

#[inline] pub fn get_virtual_transaction_size_from_transaction(tx: &Transaction) -> i64 {
    
    todo!();
        /*
            return GetVirtualTransactionSize(tx, 0, 0);
        */
}

#[inline] pub fn get_virtual_transaction_input_size(tx: &TxIn) -> i64 {
    
    todo!();
        /*
            return GetVirtualTransactionInputSize(tx, 0, 0);
        */
}

//-------------------------------------------[.cpp/bitcoin/src/policy/policy.cpp]

/**
  | @note
  | 
  | This file is intended to be customised
  | by the end user, and includes only local
  | node policy logic
  |
  */
pub fn get_dust_threshold(
        txout:             &TxOut,
        dust_relay_fee_in: &FeeRate) -> Amount {
    
    todo!();
        /*
            // "Dust" is defined in terms of dustRelayFee,
        // which has units satoshis-per-kilobyte.
        // If you'd pay more in fees than the value of the output
        // to spend something, then we consider it dust.
        // A typical spendable non-segwit txout is 34 bytes big, and will
        // need a CTxIn of at least 148 bytes to spend:
        // so dust is a spendable txout less than
        // 182*dustRelayFee/1000 (in satoshis).
        // 546 satoshis at the default rate of 3000 sat/kvB.
        // A typical spendable segwit P2WPKH txout is 31 bytes big, and will
        // need a CTxIn of at least 67 bytes to spend:
        // so dust is a spendable txout less than
        // 98*dustRelayFee/1000 (in satoshis).
        // 294 satoshis at the default rate of 3000 sat/kvB.
        if (txout.scriptPubKey.IsUnspendable())
            return 0;

        size_t nSize = GetSerializeSize(txout);
        int witnessversion = 0;
        std::vector<unsigned char> witnessprogram;

        // Note this computation is for spending a Segwit v0 P2WPKH output (a 33 bytes
        // public key + an ECDSA signature). For Segwit v1 Taproot outputs the minimum
        // satisfaction is lower (a single BIP340 signature) but this computation was
        // kept to not further reduce the dust level.
        // See discussion in https://github.com/bitcoin/bitcoin/pull/22779 for details.
        if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
            // sum the sizes of the parts of a transaction input
            // with 75% segwit discount applied to the script size.
            nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
        } else {
            nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
        }

        return dustRelayFeeIn.GetFee(nSize);
        */
}

pub fn is_dust(
        txout:             &TxOut,
        dust_relay_fee_in: &FeeRate) -> bool {
    
    todo!();
        /*
            return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
        */
}

pub fn is_standard(
        script_pub_key: &Script,
        which_type:     &mut TxoutType) -> bool {
    
    todo!();
        /*
            std::vector<std::vector<unsigned char> > vSolutions;
        whichType = Solver(scriptPubKey, vSolutions);

        if (whichType == TxoutType::NONSTANDARD) {
            return false;
        } else if (whichType == TxoutType::MULTISIG) {
            unsigned char m = vSolutions.front()[0];
            unsigned char n = vSolutions.back()[0];
            // Support up to x-of-3 multisig txns as standard
            if (n < 1 || n > 3)
                return false;
            if (m < 1 || m > n)
                return false;
        } else if (whichType == TxoutType::NULL_DATA &&
                   (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes)) {
              return false;
        }

        return true;
        */
}

/**
  | Check for standard transaction types
  | 
  | -----------
  | @return
  | 
  | True if all outputs (scriptPubKeys)
  | use only standard transaction forms
  |
  */
pub fn is_standard_tx(
        tx:                   &Transaction,
        permit_bare_multisig: bool,
        dust_relay_fee:       &FeeRate,
        reason:               &mut String) -> bool {
    
    todo!();
        /*
            if (tx.nVersion > TX_MAX_STANDARD_VERSION || tx.nVersion < 1) {
            reason = "version";
            return false;
        }

        // Extremely large transactions with lots of inputs can cost the network
        // almost as much to process as they cost the sender in fees, because
        // computing signature hashes is O(ninputs*txsize). Limiting transactions
        // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
        unsigned int sz = GetTransactionWeight(tx);
        if (sz > MAX_STANDARD_TX_WEIGHT) {
            reason = "tx-size";
            return false;
        }

        for (const CTxIn& txin : tx.vin)
        {
            // Biggest 'standard' txin involving only keys is a 15-of-15 P2SH
            // multisig with compressed keys (remember the 520 byte limit on
            // redeemScript size). That works out to a (15*(33+1))+3=513 byte
            // redeemScript, 513+1+15*(73+1)+3=1627 bytes of scriptSig, which
            // we round off to 1650(MAX_STANDARD_SCRIPTSIG_SIZE) bytes for
            // some minor future-proofing. That's also enough to spend a
            // 20-of-20 CHECKMULTISIG scriptPubKey, though such a scriptPubKey
            // is not considered standard.
            if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) {
                reason = "scriptsig-size";
                return false;
            }
            if (!txin.scriptSig.IsPushOnly()) {
                reason = "scriptsig-not-pushonly";
                return false;
            }
        }

        unsigned int nDataOut = 0;
        TxoutType whichType;
        for (const CTxOut& txout : tx.vout) {
            if (!::IsStandard(txout.scriptPubKey, whichType)) {
                reason = "scriptpubkey";
                return false;
            }

            if (whichType == TxoutType::NULL_DATA)
                nDataOut++;
            else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
                reason = "bare-multisig";
                return false;
            } else if (IsDust(txout, dust_relay_fee)) {
                reason = "dust";
                return false;
            }
        }

        // only one OP_RETURN txout is permitted
        if (nDataOut > 1) {
            reason = "multi-op-return";
            return false;
        }

        return true;
        */
}

/**
  | Check for standard transaction types
  | 
  | -----------
  | @param[in] mapInputs
  | 
  | Map of previous transactions that have
  | outputs we're spending
  | ----------
  | @param[in] taproot_active
  | 
  | Whether or taproot consensus rules
  | are active (used to decide whether spends
  | of them are permitted)
  | 
  | -----------
  | @return
  | 
  | True if all inputs (scriptSigs) use
  | only standard transaction forms
  |
  | -----------
  | Check transaction inputs to mitigate two
  | potential denial-of-service attacks:
  |
  | 1. scriptSigs with extra data stuffed into them,
  |    not consumed by scriptPubKey (or P2SH script)
  |
  | 2. P2SH scripts with a crazy number of expensive
  |    CHECKSIG/CHECKMULTISIG operations
  |
  | Why bother? To avoid denial-of-service attacks;
  | an attacker can submit a standard
  | HASH... OP_EQUAL transaction, which will get
  | accepted into blocks. 
  |
  |
  | The redemption script can be anything; an
  | attacker could use a very
  | expensive-to-check-upon-redemption script like:
  |
  |   DUP CHECKSIG DROP ... repeated 100 times... OP_1
  |
  | Note that only the non-witness portion of the
  | transaction is checked here.
  */
pub fn are_inputs_standard(
    tx:             &Transaction,
    map_inputs:     &CoinsViewCache,
    taproot_active: bool) -> bool {
    
    todo!();
        /*
            if (tx.IsCoinBase())
            return true; // Coinbases don't use vin normally

        for (unsigned int i = 0; i < tx.vin.size(); i++)
        {
            const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;

            std::vector<std::vector<unsigned char> > vSolutions;
            TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
            if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) {
                // WITNESS_UNKNOWN failures are typically also caught with a policy
                // flag in the script interpreter, but it can be helpful to catch
                // this type of NONSTANDARD transaction earlier in transaction
                // validation.
                return false;
            } else if (whichType == TxoutType::SCRIPTHASH) {
                std::vector<std::vector<unsigned char> > stack;
                // convert the scriptSig into a stack, so we can inspect the redeemScript
                if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
                    return false;
                if (stack.empty())
                    return false;
                CScript subscript(stack.back().begin(), stack.back().end());
                if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
                    return false;
                }
            } else if (whichType == TxoutType::WITNESS_V1_TAPROOT) {
                // Don't allow Taproot spends unless Taproot is active.
                if (!taproot_active) return false;
            }
        }

        return true;
        */
}

/**
  | Check if the transaction is over standard
  | P2WSH resources limit: 3600bytes witnessScript
  | size, 80bytes per witness stack element,
  | 100 witness stack elements
  | 
  | These limits are adequate for multisignatures
  | up to n-of-100 using OP_CHECKSIG, OP_ADD,
  | and OP_EQUAL.
  | 
  | Also enforce a maximum stack item size
  | limit and no annexes for tapscript spends.
  |
  */
pub fn is_witness_standard(
        tx:         &Transaction,
        map_inputs: &CoinsViewCache) -> bool {
    
    todo!();
        /*
            if (tx.IsCoinBase())
            return true; // Coinbases are skipped

        for (unsigned int i = 0; i < tx.vin.size(); i++)
        {
            // We don't care if witness for this input is empty, since it must not be bloated.
            // If the script is invalid without witness, it would be caught sooner or later during validation.
            if (tx.vin[i].scriptWitness.IsNull())
                continue;

            const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;

            // get the scriptPubKey corresponding to this input:
            CScript prevScript = prev.scriptPubKey;

            bool p2sh = false;
            if (prevScript.IsPayToScriptHash()) {
                std::vector <std::vector<unsigned char> > stack;
                // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
                // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
                // If the check fails at this stage, we know that this txid must be a bad one.
                if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
                    return false;
                if (stack.empty())
                    return false;
                prevScript = CScript(stack.back().begin(), stack.back().end());
                p2sh = true;
            }

            int witnessversion = 0;
            std::vector<unsigned char> witnessprogram;

            // Non-witness program must not be associated with any witness
            if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
                return false;

            // Check P2WSH standard limits
            if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
                if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
                    return false;
                size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
                if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
                    return false;
                for (unsigned int j = 0; j < sizeWitnessStack; j++) {
                    if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
                        return false;
                }
            }

            // Check policy limits for Taproot spends:
            // - MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE limit for stack item size
            // - No annexes
            if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) {
                // Taproot spend (non-P2SH-wrapped, version 1, witness program size 32; see BIP 341)
                auto stack = MakeSpan(tx.vin[i].scriptWitness.stack);
                if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) {
                    // Annexes are nonstandard as long as no semantics are defined for them.
                    return false;
                }
                if (stack.size() >= 2) {
                    // Script path spend (2 or more stack elements after removing optional annex)
                    const auto& control_block = SpanPopBack(stack);
                    SpanPopBack(stack); // Ignore script
                    if (control_block.empty()) return false; // Empty control block is invalid
                    if ((control_block[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) {
                        // Leaf version 0xc0 (aka Tapscript, see BIP 342)
                        for (const auto& item : stack) {
                            if (item.size() > MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE) return false;
                        }
                    }
                } else if (stack.size() == 1) {
                    // Key path spend (1 stack element after removing optional annex)
                    // (no policy rules apply)
                } else {
                    // 0 stack elements; this is already invalid by consensus rules
                    return false;
                }
            }
        }
        return true;
        */
}

/**
  | Compute the virtual transaction size
  | (weight reinterpreted as bytes).
  |
  */
pub fn get_virtual_transaction_size_for_weight(
        n_weight:        i64,
        n_sig_op_cost:   i64,
        bytes_per_sigop: u32) -> i64 {
    
    todo!();
        /*
            return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
        */
}

pub fn get_virtual_transaction_size_for_tx(
        tx:              &Transaction,
        n_sig_op_cost:   i64,
        bytes_per_sigop: u32) -> i64 {
    
    todo!();
        /*
            return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop);
        */
}

pub fn get_virtual_transaction_input_size_for_txin(
        txin:            &TxIn,
        n_sig_op_cost:   i64,
        bytes_per_sigop: u32) -> i64 {
    
    todo!();
        /*
            return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
        */
}