diff --git a/src/Makefile.am b/src/Makefile.am index 386efe2cd..82fd763ae 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -244,6 +244,7 @@ BITCOIN_CORE_H = \ httprpc.h \ httpserver.h \ init.h \ + int128.h \ key.h \ key_constants.h \ key_io.h \ diff --git a/src/int128.h b/src/int128.h new file mode 100644 index 000000000..c887aa7cd --- /dev/null +++ b/src/int128.h @@ -0,0 +1,25 @@ +// Copyright (c) 2023 The Zcash developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or https://www.opensource.org/licenses/mit-license.php . + +#ifndef ZCASH_INT128_H +#define ZCASH_INT128_H + +#include + +// will define INT128_MAX iff (in some future world) it provides int128_t. +// Otherwise use the __int128 extension which is supported in clang and gcc. +#ifndef INT128_MAX +typedef __int128 int128_t; +#define INT128_MAX (std::numeric_limits::max()) +#define INT128_MIN (std::numeric_limits::min()) +#endif + +// will define UINT128_MAX iff (in some future world) it provides uint128_t. +// Otherwise use the __uint128_t extension which is supported in clang and gcc. +#ifndef UINT128_MAX +typedef __uint128_t uint128_t; +#define UINT128_MAX (std::numeric_limits::max()) +#endif + +#endif // ZCASH_INT128_H diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 03aafbf12..39491ac03 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -17,6 +17,8 @@ #include "util/moneystr.h" #include "validationinterface.h" #include "version.h" +#include "mempool_limit.h" +#include "zip317.h" #include @@ -298,6 +300,27 @@ void CTxMemPoolEntry::SetDirty() nModFeesWithDescendants = GetModifiedFee(); } +size_t CTxMemPoolEntry::GetUnpaidActionCount() const +{ + if (tx->IsCoinBase()) { + return 0; + } else { + return std::max( + int64_t {0}, + (int64_t) std::max(GRACE_ACTIONS, tx->GetLogicalActionCount()) - (GetModifiedFee() / MARGINAL_FEE)); + } +} + +// Return a fixed-point representation of the entry's weight ratio, where 1 is represented by WEIGHT_RATIO_SCALE. +int128_t CTxMemPoolEntry::GetWeightRatio() const +{ + // ensure that the result will always be nonzero + static_assert(WEIGHT_RATIO_SCALE > MAX_MONEY); + return std::min( + (int128_t {WEIGHT_RATIO_SCALE} * std::max(CAmount {1}, GetModifiedFee())) / tx->GetConventionalFee(), + int128_t {WEIGHT_RATIO_SCALE} * WEIGHT_RATIO_CAP); +} + void CTxMemPoolEntry::UpdateState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount) { if (!IsDirty()) { diff --git a/src/txmempool.h b/src/txmempool.h index 6e43461d4..74ed57de6 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -11,6 +11,7 @@ #include #include +#include "int128.h" #include "amount.h" #include "coins.h" #include "mempool_limit.h" @@ -87,6 +88,15 @@ public: const CTransaction& GetTx() const { return *this->tx; } std::shared_ptr GetSharedTx() const { return this->tx; } const CAmount& GetFee() const { return nFee; } + + // Return the number of unpaid actions calculated according to ZIP 317. + // + size_t GetUnpaidActionCount() const; + + // Return a fixed-point representation of the entry's weight ratio according + // to ZIP 317, where 1 is represented by WEIGHT_RATIO_SCALE. + int128_t GetWeightRatio() const; + size_t GetTxSize() const { return nTxSize; } int64_t GetTime() const { return nTime; } unsigned int GetHeight() const { return nHeight; }