Guest User

Untitled

a guest
Oct 29th, 2025
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. ==========================================================================================================
  2. // *** RELAY POLICY FILTER ***
  3. // Place in src/policy/policy.cpp, and call from within IsStandardTx() before returning:
  4. //     if (IsBulkDust(tx, reason))
  5. //         return false;   // reject as nonstandard
  6. // ==========================================================================================================
  7.  
  8. bool IsBulkDust(const CTransaction& tx, std::string& reason)
  9. {
  10.     static constexpr int     MAX_TINY_OUTPUTS        = 100;     // >=100 tiny outputs triggers ratio check
  11.     static constexpr double  TINY_RATIO_THRESHOLD    = 0.6;     // >=60% of all outputs tiny → reject
  12.     static constexpr CAmount BASE_TINY_THRESHOLD     = 4096;    // starting tiny threshold (sats)
  13.     static constexpr int64_t FIRST_TINY_HALVING_H    = 1260000; // first halving of tiny threshold
  14.     static constexpr int64_t HALVING_INTERVAL        = 210000;  // blocks per subsequent halving
  15.     static constexpr CAmount MIN_TINY_FLOOR          = 1;       // never below 1 sat
  16.  
  17.     const int total = tx.vout.size();
  18.     if (total == 0) return false;
  19.  
  20.     int currentHeight = chainActive.Tip() ? chainActive.Tip()->nHeight : 0;
  21.  
  22.     // Era index for TinyTx threshold, anchored at FIRST_TINY_HALVING_H (not subsidy eras)
  23.     int era = 0;
  24.     if (currentHeight >= FIRST_TINY_HALVING_H) {
  25.         era = 1 + static_cast<int>((currentHeight - FIRST_TINY_HALVING_H) / HALVING_INTERVAL);
  26.     }
  27.  
  28.     CAmount tinyThresh = BASE_TINY_THRESHOLD >> era;      // halve per era
  29.     if (tinyThresh < MIN_TINY_FLOOR) tinyThresh = MIN_TINY_FLOOR;
  30.  
  31.     int tiny = 0;
  32.     for (const auto& out : tx.vout) {
  33.         if (out.nValue < tinyThresh) ++tiny;
  34.     }
  35.  
  36.     if (tiny >= MAX_TINY_OUTPUTS && (static_cast<double>(tiny) / total) >= TINY_RATIO_THRESHOLD) {
  37.         reason = strprintf("too-many-tiny-outputs(%d of %d, %.2f%%, tiny<%d)",
  38.                            tiny, total, 100.0 * tiny / total, tinyThresh);
  39.         return true; // flag as bulk dust (nonstandard)
  40.     }
  41.     return false;
  42. }
  43.  
  44.  
  45. // ==========================================================================================================
  46. // *** CONSENSUS (soft-fork, hybrid activation) ***
  47. // Helpers in src/consensus/tx_check.cpp; activation/enforcement in src/validation.cpp
  48. // Also define deployment in: src/consensus/params.h, src/chainparams.cpp, src/versionbits.*
  49. // ==========================================================================================================
  50.  
  51. // -----------------------------------------------------------------------
  52. // --- In src/consensus/tx_check.cpp (helper only; no params needed) ---
  53. // -----------------------------------------------------------------------
  54.  
  55. static constexpr CAmount BASE_TINY_THRESHOLD     = 4096;
  56. static constexpr int64_t FIRST_TINY_HALVING_H    = 1260000;
  57. static constexpr int64_t HALVING_INTERVAL        = 210000;
  58. static constexpr int     MAX_TINY_OUTPUTS        = 100;
  59. static constexpr double  TINY_RATIO_THRESHOLD    = 0.6;
  60. static constexpr CAmount MIN_TINY_FLOOR          = 1;
  61.  
  62. bool IsBulkDust(const CTransaction& tx, int currentHeight) // expose via tx_check.h if needed
  63. {
  64.     const int total = tx.vout.size();
  65.     if (total == 0) return false;
  66.  
  67.     int era = 0;
  68.     if (currentHeight >= FIRST_TINY_HALVING_H) {
  69.         era = 1 + static_cast<int>((currentHeight - FIRST_TINY_HALVING_H) / HALVING_INTERVAL);
  70.     }
  71.  
  72.     CAmount tinyThresh = BASE_TINY_THRESHOLD >> era;
  73.     if (tinyThresh < MIN_TINY_FLOOR) tinyThresh = MIN_TINY_FLOOR;
  74.  
  75.     int tiny = 0;
  76.     for (const auto& out : tx.vout) {
  77.         if (out.nValue < tinyThresh) ++tiny;
  78.     }
  79.  
  80.     if (tiny >= MAX_TINY_OUTPUTS && (static_cast<double>(tiny) / total) >= TINY_RATIO_THRESHOLD)
  81.         return true;
  82.  
  83.     return false;
  84. }
  85.  
  86.  
  87. // -----------------------------------------------------------------------
  88. // --- In src/validation.cpp (enforcement with hybrid activation) ---
  89. // -----------------------------------------------------------------------
  90.  
  91. #include <consensus/tx_check.h>
  92. #include <versionbits.h>
  93.  
  94. const Consensus::Params& params = chainparams.GetConsensus();
  95. int currentHeight = chainActive.Tip() ? chainActive.Tip()->nHeight : 0;
  96.  
  97. const bool bulk_dust_active =
  98.     DeploymentActiveAtTip(params, Consensus::DEPLOYMENT_BULK_DUST_LIMIT) ||
  99.     (currentHeight >= params.BulkDustActivationHeight);
  100.  
  101. if (bulk_dust_active) {
  102.     if (IsBulkDust(tx, currentHeight)) {
  103.         return state.Invalid(TxValidationResult::TX_CONSENSUS, "too-many-tiny-outputs");
  104.     }
  105. }
  106.  
  107.  
  108. // -----------------------------------------------------------------------
  109. // --- In src/consensus/params.h ---
  110. // -----------------------------------------------------------------------
  111.  
  112. enum DeploymentPos {
  113.     // ...
  114.     DEPLOYMENT_BULK_DUST_LIMIT,
  115.     MAX_VERSION_BITS_DEPLOYMENTS
  116. };
  117.  
  118. struct Params {
  119.     // ...
  120.     int BulkDustActivationHeight; // height flag-day fallback
  121. };
  122.  
  123.  
  124. // -----------------------------------------------------------------------
  125. // --- In src/chainparams.cpp (per-network values; examples only) ---
  126. // -----------------------------------------------------------------------
  127.  
  128. consensus.vDeployments[Consensus::DEPLOYMENT_BULK_DUST_LIMIT].bit = 12;
  129. consensus.vDeployments[Consensus::DEPLOYMENT_BULK_DUST_LIMIT].nStartTime = 1767225600;  // 2026-01-01 UTC
  130. consensus.vDeployments[Consensus::DEPLOYMENT_BULK_DUST_LIMIT].nTimeout   = 1838160000;  // 2028-04-01 UTC
  131. consensus.vDeployments[Consensus::DEPLOYMENT_BULK_DUST_LIMIT].min_activation_height = 969696;
  132.  
  133. consensus.BulkDustActivationHeight = 1021021; // flag-day fallback
Advertisement
Add Comment
Please, Sign In to add comment