Implement `GetUnpaidActionCount` and `GetWeightRatio` for ZIP 317.

Signed-off-by: Daira Emma Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Emma Hopwood 2023-04-15 01:09:05 +01:00
parent 6cb4c9e144
commit c111bff3d7
4 changed files with 59 additions and 0 deletions

View File

@ -244,6 +244,7 @@ BITCOIN_CORE_H = \
httprpc.h \
httpserver.h \
init.h \
int128.h \
key.h \
key_constants.h \
key_io.h \

25
src/int128.h Normal file
View File

@ -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 <cstdint>
// <cstdint> 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<int128_t>::max())
#define INT128_MIN (std::numeric_limits<int128_t>::min())
#endif
// <cstdint> 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<uint128_t>::max())
#endif
#endif // ZCASH_INT128_H

View File

@ -17,6 +17,8 @@
#include "util/moneystr.h"
#include "validationinterface.h"
#include "version.h"
#include "mempool_limit.h"
#include "zip317.h"
#include <optional>
@ -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()) {

View File

@ -11,6 +11,7 @@
#include <memory>
#include <set>
#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<const CTransaction> GetSharedTx() const { return this->tx; }
const CAmount& GetFee() const { return nFee; }
// Return the number of unpaid actions calculated according to ZIP 317.
// <https://zips.z.cash/zip-0317#recommended-algorithm-for-block-template-construction>
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; }