LCOV - code coverage report
Current view: top level - src/coinjoin - common.h (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 46 50 92.0 %
Date: 2026-06-25 07:23:51 Functions: 17 21 81.0 %

          Line data    Source code
       1             : // Copyright (c) 2014-2024 The Dash Core developers
       2             : // Distributed under the MIT/X11 software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef BITCOIN_COINJOIN_COMMON_H
       6             : #define BITCOIN_COINJOIN_COMMON_H
       7             : 
       8             : #include <util/helpers.h>
       9             : 
      10             : #include <consensus/amount.h>
      11             : #include <primitives/transaction.h>
      12             : 
      13             : #include <array>
      14             : #include <string>
      15             : 
      16             : /** Holds a mixing input
      17             :  */
      18           0 : class CTxDSIn : public CTxIn
      19             : {
      20             : public:
      21             :     // memory only
      22             :     CScript prevPubKey;
      23           2 :     bool fHasSig{false}; // flag to indicate if signed
      24           1 :     int nRounds{-10};
      25             : 
      26           2 :     CTxDSIn(const CTxIn& txin, CScript script, int nRounds) :
      27           1 :         CTxIn(txin),
      28           1 :         prevPubKey(std::move(script)),
      29           1 :         nRounds(nRounds)
      30           1 :     {
      31           2 :     }
      32             : 
      33           2 :     CTxDSIn() = default;
      34             : };
      35             : 
      36             : namespace CoinJoin
      37             : {
      38             : 
      39             : constexpr std::array<CAmount, 5> vecStandardDenominations{
      40             :         (10 * COIN) + 10000,
      41             :         (1 * COIN) + 1000,
      42             :         (COIN / 10) + 100,
      43             :         (COIN / 100) + 10,
      44             :         (COIN / 1000) + 1,
      45             : };
      46             : 
      47     1463199 : constexpr std::array<CAmount, 5> GetStandardDenominations() { return vecStandardDenominations; }
      48          81 : constexpr CAmount GetSmallestDenomination() { return vecStandardDenominations.back(); }
      49             : 
      50             : /*
      51             :     Return a bitshifted integer representing a denomination in vecStandardDenominations
      52             :     or 0 if none was found
      53             : */
      54      601950 : constexpr int AmountToDenomination(CAmount nInputAmount)
      55             : {
      56     3611688 :     for (size_t i = 0; i < vecStandardDenominations.size(); ++i) {
      57     3009750 :         if (nInputAmount == vecStandardDenominations[i]) {
      58          12 :             return 1 << i;
      59             :         }
      60     3009738 :     }
      61      601938 :     return 0;
      62      601950 : }
      63             : 
      64             : /*
      65             :     Returns:
      66             :     - one of standard denominations from vecStandardDenominations based on the provided bitshifted integer
      67             :     - 0 for non-initialized sessions (nDenom = 0)
      68             :     - a value below 0 if an error occurred while converting from one to another
      69             : */
      70           4 : constexpr CAmount DenominationToAmount(int nDenom)
      71             : {
      72           4 :     if (nDenom == 0) {
      73             :         // not initialized
      74           1 :         return 0;
      75             :     }
      76             : 
      77           3 :     size_t nMaxDenoms = vecStandardDenominations.size();
      78             : 
      79           3 :     if (nDenom >= (1 << nMaxDenoms) || nDenom < 0) {
      80             :         // out of bounds
      81           2 :         return -1;
      82             :     }
      83             : 
      84           1 :     if ((nDenom & (nDenom - 1)) != 0) {
      85             :         // non-denom
      86           0 :         return -2;
      87             :     }
      88             : 
      89           1 :     CAmount nDenomAmount{-3};
      90             : 
      91           5 :     for (size_t i = 0; i < nMaxDenoms; ++i) {
      92           5 :         if (nDenom & (1 << i)) {
      93           1 :             nDenomAmount = vecStandardDenominations[i];
      94           1 :             break;
      95             :         }
      96           4 :     }
      97             : 
      98           1 :     return nDenomAmount;
      99           4 : }
     100             : 
     101             : 
     102      601944 : constexpr bool IsDenominatedAmount(CAmount nInputAmount) { return AmountToDenomination(nInputAmount) > 0; }
     103           4 : constexpr bool IsValidDenomination(int nDenom) { return DenominationToAmount(nDenom) > 0; }
     104             : 
     105             : /*
     106             : Same as DenominationToAmount but returns a string representation
     107             : */
     108             : std::string DenominationToString(int nDenom);
     109             : 
     110          61 : constexpr CAmount GetCollateralAmount() { return GetSmallestDenomination() / 10; }
     111          22 : constexpr CAmount GetMaxCollateralAmount() { return GetCollateralAmount() * 4; }
     112             : 
     113          39 : constexpr bool IsCollateralAmount(CAmount nInputAmount)
     114             : {
     115             :     // collateral input can be anything between 1x and "max" (including both)
     116          39 :     return (nInputAmount >= GetCollateralAmount() && nInputAmount <= GetMaxCollateralAmount());
     117             : }
     118             : 
     119           4 : constexpr int CalculateAmountPriority(CAmount nInputAmount)
     120             : {
     121           4 :     if (nInputAmount < 0 || nInputAmount > MAX_MONEY) return 0;
     122           2 :     if (auto optDenom = util::find_if_opt(GetStandardDenominations(),
     123           6 :                                           [&nInputAmount](const auto& denom) { return nInputAmount == denom; })) {
     124           0 :         return (float)COIN / *optDenom * 10000;
     125             :     }
     126           1 :     if (nInputAmount < COIN) {
     127           0 :         return 20000;
     128             :     }
     129             : 
     130             :     //nondenom return largest first
     131           1 :     return -1 * (nInputAmount / COIN);
     132           4 : }
     133             : 
     134             : } // namespace CoinJoin
     135             : 
     136             : #endif // BITCOIN_COINJOIN_COMMON_H

Generated by: LCOV version 1.16