Skip to content
Bitcoin Roots

v30.3-roots.1 · Commit 3: [PATCH 03/68] feat(policy): preserve Roots relay and network controls on Core 30.3

src/policy/policy.cpp

Before: src/policy/policy.cpp · After: src/policy/policy.cpp

File metadata
index 3da6cb7489b1731ee51d6afe1b272b24c9e9e313..79f46f540f7fbdfb71284714eeec68374a292dd9 100644

Download original patch · Release notes

Diff rows 401–479 of 479. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/policy/policy.cpp CHANGED
@@ -387,3 +531,97 @@ int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, un (continued)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
549
+
550
+ int witnessversion = 0;
551
+ std::vector<unsigned char> witnessprogram;
552
+
553
+ if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram)) {
554
+ // For P2SH, scriptSig is always push-only, so the actual script is only the last stack item
555
+ // For non-P2SH, prevScript is likely the real script, but not part of this transaction, and scriptSig could very well be executable, so return the latter instead
556
+ return std::make_pair(p2sh ? prevScript : txin.scriptSig, WITNESS_SCALE_FACTOR);
557
+ }
558
+
559
+ std::span stack{txin.scriptWitness.stack};
560
+
561
+ if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
562
+ if (stack.empty()) return std::make_pair(CScript(), 0); // invalid
563
+ auto& script_data = stack.back();
564
+ prevScript = CScript(script_data.begin(), script_data.end());
565
+ return std::make_pair(prevScript, 1);
566
+ }
567
+
568
+ if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE && !p2sh) {
569
+ if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) {
570
+ SpanPopBack(stack);
571
+ }
572
+ if (stack.size() >= 2) {
573
+ SpanPopBack(stack); // Ignore control block
574
+ prevScript = CScript(stack.back().begin(), stack.back().end());
575
+ return std::make_pair(prevScript, 1);
576
+ }
577
+ }
578
+
579
+ return std::make_pair(CScript(), 0);
580
+ }
581
+
582
+ std::pair<size_t, size_t> DatacarrierBytes(const CTransaction& tx, const CCoinsViewCache& view)
583
+ {
584
+ std::pair<size_t, size_t> ret{0, 0};
585
+
586
+ for (const CTxIn& txin : tx.vin) {
587
+ const CTxOut &utxo = view.AccessCoin(txin.prevout).out;
588
+ auto[script, consensus_weight_per_byte] = GetScriptForTransactionInput(utxo.scriptPubKey, txin);
589
+ const auto dcb = script.DatacarrierBytes(0, &txin.scriptWitness);
590
+ ret.first += dcb.first;
591
+ ret.second += dcb.second;
592
+ }
593
+ for (size_t i{tx.vout.size()}; i; ) {
594
+ const CTxOut& txout = tx.vout[--i];
595
+ const auto dcb = txout.scriptPubKey.DatacarrierBytes(tx.vout.size() - i);
596
+ ret.first += dcb.first;
597
+ ret.second += dcb.second;
598
+ }
599
+
600
+ return ret;
601
+ }
602
+
603
+ int32_t CalculateExtraTxWeight(const CTransaction& tx, const CCoinsViewCache& view, const unsigned int weight_per_data_byte)
604
+ {
605
+ int64_t mod_weight{0};
606
+
607
+ // Add in any extra weight for data bytes
608
+ if (weight_per_data_byte > 1) {
609
+ for (const CTxIn& txin : tx.vin) {
610
+ const CTxOut &utxo = view.AccessCoin(txin.prevout).out;
611
+ auto[script, consensus_weight_per_byte] = GetScriptForTransactionInput(utxo.scriptPubKey, txin);
612
+ if (weight_per_data_byte > consensus_weight_per_byte) {
613
+ const auto dcb = script.DatacarrierBytes(0, &txin.scriptWitness);
614
+ mod_weight += int64_t(dcb.first + dcb.second) * (weight_per_data_byte - consensus_weight_per_byte);
615
+ }
616
+ }
617
+ if (weight_per_data_byte > WITNESS_SCALE_FACTOR) {
618
+ for (size_t i{tx.vout.size()}; i; ) {
619
+ const CTxOut& txout = tx.vout[--i];
620
+ const auto dcb = txout.scriptPubKey.DatacarrierBytes(tx.vout.size() - i);
621
+ mod_weight += int64_t(dcb.first + dcb.second) * (weight_per_data_byte - WITNESS_SCALE_FACTOR);
622
+ }
623
+ }
624
+ }
625
+
626
+ return int32_t(std::min(mod_weight, int64_t{std::numeric_limits<int32_t>::max()}));
627
+ }