Implement ZIP 317 computations.
Signed-off-by: Daira Emma Hopwood <daira@jacaranda.org>
This commit is contained in:
parent
dd246587a3
commit
4fd3493291
|
@ -535,6 +535,7 @@ libbitcoin_common_a_SOURCES = \
|
|||
transaction_builder.cpp \
|
||||
util/test.cpp \
|
||||
warnings.cpp \
|
||||
zip317.cpp \
|
||||
$(BITCOIN_CORE_H) \
|
||||
$(LIBZCASH_H)
|
||||
|
||||
|
@ -699,7 +700,8 @@ libzcash_script_la_SOURCES = \
|
|||
script/interpreter.cpp \
|
||||
script/script.cpp \
|
||||
uint256.cpp \
|
||||
util/strencodings.cpp
|
||||
util/strencodings.cpp \
|
||||
zip317.cpp
|
||||
|
||||
if GLIBC_BACK_COMPAT
|
||||
libzcash_script_la_SOURCES += compat/glibc_compat.cpp
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "hash.h"
|
||||
#include "tinyformat.h"
|
||||
#include "util/strencodings.h"
|
||||
#include "zip317.h"
|
||||
|
||||
#include <rust/transaction.h>
|
||||
|
||||
|
@ -385,6 +386,20 @@ unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const
|
|||
return nTxSize;
|
||||
}
|
||||
|
||||
CAmount CTransaction::GetConventionalFee() const {
|
||||
return CalculateConventionalFee(GetLogicalActionCount());
|
||||
}
|
||||
|
||||
size_t CTransaction::GetLogicalActionCount() const {
|
||||
return CalculateLogicalActionCount(
|
||||
vin,
|
||||
vout,
|
||||
vJoinSplit.size(),
|
||||
vShieldedSpend.size(),
|
||||
vShieldedOutput.size(),
|
||||
orchardBundle.GetNumActions());
|
||||
}
|
||||
|
||||
std::string CTransaction::ToString() const
|
||||
{
|
||||
std::string str;
|
||||
|
|
|
@ -977,6 +977,14 @@ public:
|
|||
// Compute modified tx size for priority calculation (optionally given tx size)
|
||||
unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const;
|
||||
|
||||
// Return the conventional fee for this transaction calculated according to
|
||||
// <https://zips.z.cash/zip-0317#fee-calculation>.
|
||||
CAmount GetConventionalFee() const;
|
||||
|
||||
// Return the number of logical actions calculated according to
|
||||
// <https://zips.z.cash/zip-0317#fee-calculation>.
|
||||
size_t GetLogicalActionCount() const;
|
||||
|
||||
bool IsCoinBase() const
|
||||
{
|
||||
return (vin.size() == 1 && vin[0].prevout.IsNull());
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) 2023-2023 The Zcash developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
||||
|
||||
#include "zip317.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
static size_t ceil_div(size_t num, size_t den) {
|
||||
return (num + den - 1)/den;
|
||||
}
|
||||
|
||||
CAmount CalculateConventionalFee(size_t logicalActionCount) {
|
||||
return MARGINAL_FEE * std::max(GRACE_ACTIONS, logicalActionCount);
|
||||
}
|
||||
|
||||
size_t CalculateLogicalActionCount(
|
||||
const std::vector<CTxIn>& vin,
|
||||
const std::vector<CTxOut>& vout,
|
||||
unsigned int joinSplitCount,
|
||||
unsigned int saplingSpendCount,
|
||||
unsigned int saplingOutputCount,
|
||||
unsigned int orchardActionCount) {
|
||||
const size_t tx_in_total_size = GetSerializeSize(vin, SER_NETWORK, PROTOCOL_VERSION);
|
||||
const size_t tx_out_total_size = GetSerializeSize(vout, SER_NETWORK, PROTOCOL_VERSION);
|
||||
|
||||
return std::max(ceil_div(tx_in_total_size, P2PKH_STANDARD_INPUT_SIZE),
|
||||
ceil_div(tx_out_total_size, P2PKH_STANDARD_OUTPUT_SIZE)) +
|
||||
2 * joinSplitCount +
|
||||
std::max(saplingSpendCount, saplingOutputCount) +
|
||||
orchardActionCount;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
// Copyright (c) 2023-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_ZIP317_H
|
||||
#define ZCASH_ZIP317_H
|
||||
|
||||
#include "amount.h"
|
||||
#include "primitives/transaction.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
// Constants for fee calculation.
|
||||
static const CAmount MARGINAL_FEE = 5000;
|
||||
static const size_t GRACE_ACTIONS = 2;
|
||||
static const size_t P2PKH_STANDARD_INPUT_SIZE = 150;
|
||||
static const size_t P2PKH_STANDARD_OUTPUT_SIZE = 34;
|
||||
|
||||
// Constants for block template construction.
|
||||
static const int64_t WEIGHT_RATIO_SCALE = INT64_C(10000000000000000);
|
||||
static const int64_t WEIGHT_RATIO_CAP = 4;
|
||||
static const size_t BLOCK_UNPAID_ACTION_LIMIT = 50;
|
||||
|
||||
/// Return the conventional fee for the given `logicalActionCount` calculated according to
|
||||
/// <https://zips.z.cash/zip-0317#fee-calculation>.
|
||||
CAmount CalculateConventionalFee(size_t logicalActionCount);
|
||||
|
||||
/// Return the number of logical actions calculated according to
|
||||
/// <https://zips.z.cash/zip-0317#fee-calculation>.
|
||||
size_t CalculateLogicalActionCount(
|
||||
const std::vector<CTxIn>& vin,
|
||||
const std::vector<CTxOut>& vout,
|
||||
unsigned int joinSplitCount,
|
||||
unsigned int saplingSpendCount,
|
||||
unsigned int saplingOutputCount,
|
||||
unsigned int orchardActionCount);
|
||||
|
||||
#endif // ZCASH_ZIP317_H
|
Loading…
Reference in New Issue