Bitcoin ABC  0.26.3
P2P Digital Currency
stakingrewards.cpp
Go to the documentation of this file.
1 // Copyright (c) 2023 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 
7 #include <avalanche/avalanche.h>
8 #include <avalanche/processor.h>
9 #include <blockindex.h>
10 #include <consensus/amount.h>
11 #include <logging.h>
12 #include <primitives/block.h>
13 
14 #include <vector>
15 
21 static constexpr int STAKING_REWARD_RATIO = 25;
22 
24  if (!m_blockIndex.pprev) {
25  return true;
26  }
27 
28  assert(m_block.vtx.size());
29 
30  const BlockHash blockhash = m_blockIndex.GetBlockHash();
31 
32  CScript winner;
34  !gArgs.GetBoolArg("-avalanchestakingrewards",
36  !g_avalanche->getStakingRewardWinner(m_blockIndex.pprev->GetBlockHash(),
37  winner)) {
39  "Staking rewards for block %s: not ready yet\n",
40  blockhash.ToString());
41  return true;
42  }
43 
45  for (auto &o : m_block.vtx[0]->vout) {
46  if (o.nValue < required) {
47  // This output doesn't qualify because its amount is too low.
48  continue;
49  }
50 
51  if (o.scriptPubKey == winner) {
52  return true;
53  }
54  }
55 
57  "Staking rewards for block %s: payout script mismatch!\n",
58  blockhash.ToString());
59 
61  "policy-bad-staking-reward",
62  strprintf("Block %s violates staking reward policy",
64 }
65 
66 Amount GetStakingRewardsAmount(const Amount &coinbaseValue) {
67  return STAKING_REWARD_RATIO * coinbaseValue / 100;
68 }
bool isAvalancheEnabled(const ArgsManager &argsman)
Definition: avalanche.cpp:9
static constexpr bool AVALANCHE_DEFAULT_STAKING_REWARDS
Is avalanche staking rewards enabled by default.
Definition: avalanche.h:65
std::unique_ptr< avalanche::Processor > g_avalanche
Global avalanche instance.
Definition: processor.cpp:35
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:601
std::vector< CTransactionRef > vtx
Definition: block.h:58
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:33
BlockHash GetBlockHash() const
Definition: blockindex.h:147
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
const CBlock & m_block
bool operator()(BlockPolicyValidationState &state) override
const Amount & m_blockReward
const CBlockIndex & m_blockIndex
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:102
std::string ToString() const
Definition: uint256.h:78
#define LogPrint(category,...)
Definition: logging.h:209
@ AVALANCHE
Definition: logging.h:62
@ POLICY_VIOLATION
A block policy rule was violated. This block should be parked.
static constexpr int STAKING_REWARD_RATIO
Percentage of the block reward to be sent to staking rewards.
Amount GetStakingRewardsAmount(const Amount &coinbaseValue)
Definition: amount.h:19
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
ArgsManager gArgs
Definition: system.cpp:79
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
assert(!tx.IsCoinBase())