amount.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-2021 The Limenka developers
   3  // Distributed under the MIT software license, see the accompanying
   4  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   5  
   6  #ifndef LIMENKA_CONSENSUS_AMOUNT_H
   7  #define LIMENKA_CONSENSUS_AMOUNT_H
   8  
   9  #include <cstdint>
  10  
  11  /** Amount in satoshis (Can be negative).
  12   *  Using __int128 to provide 64 extra bits of sub-satoshi precision,
  13   *  futureproofing against fiat inflation for the foreseeable future.
  14   *  COIN = 10^-8 BTC is unchanged; the extra bits sit below satoshis. */
  15  typedef __int128 CAmount;
  16  
  17  /** The amount of satoshis in one BTC. */
  18  static constexpr CAmount COIN = 100000000;
  19  
  20  /** Attosats per satoshi (10^-18 = 18 decimals of sub-satoshi precision).
  21   *  CT committed values and the CT kernel fee use attosat units. */
  22  static constexpr CAmount ATTOSATS_PER_SATOSHI = 1000000000000000000;
  23  
  24  /** No amount larger than this (in satoshi) is valid.
  25   *
  26   * Note that this constant is *not* the total money supply, which in Limenka
  27   * currently happens to be less than 21,000,000 BTC for various reasons, but
  28   * rather a sanity check. As this sanity check is used by consensus-critical
  29   * validation code, the exact value of the MAX_MONEY constant is consensus
  30   * critical; in unusual circumstances like a(nother) overflow bug that allowed
  31   * for the creation of coins out of thin air modification could lead to a fork.
  32   * */
  33  static constexpr CAmount MAX_MONEY = 21000000 * COIN;
  34  inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
  35  
  36  #include <ostream>
  37  
  38  /** Stream insertion for __int128 (not provided by standard library). */
  39  inline std::ostream& operator<<(std::ostream& os, const __int128& n) {
  40      __int128 abs_n = n;
  41      if (abs_n < 0) { os << '-'; abs_n = -abs_n; }
  42      char buf[40]; int p = sizeof(buf);
  43      do { buf[--p] = '0' + static_cast<int>(abs_n % 10); abs_n /= 10; } while (abs_n > 0);
  44      return os.write(buf + p, sizeof(buf) - p);
  45  }
  46  
  47  #endif // LIMENKA_CONSENSUS_AMOUNT_H
  48