Bitcoin ABC  0.29.2
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/activation.h>
11 #include <consensus/amount.h>
12 #include <logging.h>
13 #include <primitives/block.h>
14 
15 #include <vector>
16 
20 static constexpr int STAKING_REWARD_RATIO = 10;
21 
23  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  std::vector<CScript> winners;
34  !g_avalanche->getStakingRewardWinners(
35  m_blockIndex.pprev->GetBlockHash(), winners)) {
37  "Staking rewards for block %s: not ready yet\n",
38  blockhash.ToString());
39  return true;
40  }
41 
43  for (auto &o : m_block.vtx[0]->vout) {
44  if (o.nValue < required) {
45  // This output doesn't qualify because its amount is too low.
46  continue;
47  }
48 
49  if (std::find(winners.begin(), winners.end(), o.scriptPubKey) !=
50  winners.end()) {
51  return true;
52  }
53  }
54 
56  "Staking rewards for block %s: payout script mismatch!\n",
57  blockhash.ToString());
58 
60  "policy-bad-staking-reward",
61  strprintf("Block %s violates staking reward policy",
63 }
64 
65 Amount GetStakingRewardsAmount(const Amount &coinbaseValue) {
66  return STAKING_REWARD_RATIO * coinbaseValue / 100;
67 }
68 
70  const CBlockIndex *pprev) {
71  return IsCowperthwaiteEnabled(params, pprev) && g_avalanche &&
73  gArgs.GetBoolArg("-avalanchestakingrewards",
74  params.enableStakingRewards);
75 }
bool IsCowperthwaiteEnabled(const Consensus::Params &params, int32_t nHeight)
Check if Nov 15th, 2023 protocol upgrade has activated.
Definition: activation.cpp:104
bool isAvalancheEnabled(const ArgsManager &argsman)
Definition: avalanche.cpp:9
std::unique_ptr< avalanche::Processor > g_avalanche
Global avalanche instance.
Definition: processor.cpp:38
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:665
std::vector< CTransactionRef > vtx
Definition: block.h:63
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:33
BlockHash GetBlockHash() const
Definition: blockindex.h:147
const CBlock & m_block
bool operator()(BlockPolicyValidationState &state) override
const Consensus::Params & m_consensusParams
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:94
std::string ToString() const
Definition: uint256.h:80
#define LogPrint(category,...)
Definition: logging.h:210
@ AVALANCHE
Definition: logging.h:62
@ POLICY_VIOLATION
A block policy rule was violated. This block should be parked.
bool IsStakingRewardsActivated(const Consensus::Params &params, const CBlockIndex *pprev)
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
Parameters that influence chain consensus.
Definition: params.h:34
bool enableStakingRewards
Enable or disable the staking rewards by default.
Definition: params.h:73
ArgsManager gArgs
Definition: system.cpp:80
#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())