Dogecoin Core  1.14.2
P2P Digital Currency
dogecoin.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015 The Dogecoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <boost/random/uniform_int.hpp>
6 #include <boost/random/mersenne_twister.hpp>
7 
8 #include "policy/policy.h"
9 #include "arith_uint256.h"
10 #include "dogecoin.h"
11 #include "txmempool.h"
12 #include "util.h"
13 #include "validation.h"
14 
15 int static generateMTRandom(unsigned int s, int range)
16 {
17  boost::mt19937 gen(s);
18  boost::uniform_int<> dist(1, range);
19  return dist(gen);
20 }
21 
22 // Dogecoin: Normally minimum difficulty blocks can only occur in between
23 // retarget blocks. However, once we introduce Digishield every block is
24 // a retarget, so we need to handle minimum difficulty on all blocks.
25 bool AllowDigishieldMinDifficultyForBlock(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
26 {
27  // check if the chain allows minimum difficulty blocks
28  if (!params.fPowAllowMinDifficultyBlocks)
29  return false;
30 
31  // check if the chain allows minimum difficulty blocks on recalc blocks
32  if (pindexLast->nHeight < 157500)
33  // if (!params.fPowAllowDigishieldMinDifficultyBlocks)
34  return false;
35 
36  // Allow for a minimum block time if the elapsed time > 2*nTargetSpacing
37  return (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2);
38 }
39 
40 unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
41 {
42  int nHeight = pindexLast->nHeight + 1;
43  const int64_t retargetTimespan = params.nPowTargetTimespan;
44  const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
45  int64_t nModulatedTimespan = nActualTimespan;
46  int64_t nMaxTimespan;
47  int64_t nMinTimespan;
48 
49  if (params.fDigishieldDifficultyCalculation) //DigiShield implementation - thanks to RealSolid & WDC for this code
50  {
51  // amplitude filter - thanks to daft27 for this code
52  nModulatedTimespan = retargetTimespan + (nModulatedTimespan - retargetTimespan) / 8;
53 
54  nMinTimespan = retargetTimespan - (retargetTimespan / 4);
55  nMaxTimespan = retargetTimespan + (retargetTimespan / 2);
56  } else if (nHeight > 10000) {
57  nMinTimespan = retargetTimespan / 4;
58  nMaxTimespan = retargetTimespan * 4;
59  } else if (nHeight > 5000) {
60  nMinTimespan = retargetTimespan / 8;
61  nMaxTimespan = retargetTimespan * 4;
62  } else {
63  nMinTimespan = retargetTimespan / 16;
64  nMaxTimespan = retargetTimespan * 4;
65  }
66 
67  // Limit adjustment step
68  if (nModulatedTimespan < nMinTimespan)
69  nModulatedTimespan = nMinTimespan;
70  else if (nModulatedTimespan > nMaxTimespan)
71  nModulatedTimespan = nMaxTimespan;
72 
73  // Retarget
74  const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
75  arith_uint256 bnNew;
76  arith_uint256 bnOld;
77  bnNew.SetCompact(pindexLast->nBits);
78  bnOld = bnNew;
79  bnNew *= nModulatedTimespan;
80  bnNew /= retargetTimespan;
81 
82  if (bnNew > bnPowLimit)
83  bnNew = bnPowLimit;
84 
85  return bnNew.GetCompact();
86 }
87 
88 bool CheckAuxPowProofOfWork(const CBlockHeader& block, const Consensus::Params& params)
89 {
90  /* Except for legacy blocks with full version 1, ensure that
91  the chain ID is correct. Legacy blocks are not allowed since
92  the merge-mining start, which is checked in AcceptBlockHeader
93  where the height is known. */
94  if (!block.IsLegacy() && params.fStrictChainId && block.GetChainId() != params.nAuxpowChainId)
95  return error("%s : block does not have our chain ID"
96  " (got %d, expected %d, full nVersion %d)",
97  __func__, block.GetChainId(),
98  params.nAuxpowChainId, block.nVersion);
99 
100  /* If there is no auxpow, just check the block hash. */
101  if (!block.auxpow) {
102  if (block.IsAuxpow())
103  return error("%s : no auxpow on block with auxpow version",
104  __func__);
105 
106  if (!CheckProofOfWork(block.GetPoWHash(), block.nBits, params))
107  return error("%s : non-AUX proof of work failed", __func__);
108 
109  return true;
110  }
111 
112  /* We have auxpow. Check it. */
113 
114  if (!block.IsAuxpow())
115  return error("%s : auxpow on block with non-auxpow version", __func__);
116 
117  if (!block.auxpow->check(block.GetHash(), block.GetChainId(), params))
118  return error("%s : AUX POW is not valid", __func__);
119  if (!CheckProofOfWork(block.auxpow->getParentBlockPoWHash(), block.nBits, params))
120  return error("%s : AUX proof of work failed", __func__);
121 
122  return true;
123 }
124 
125 CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusParams, uint256 prevHash)
126 {
127  int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
128 
129  if (!consensusParams.fSimplifiedRewards)
130  {
131  // Old-style rewards derived from the previous block hash
132  const std::string cseed_str = prevHash.ToString().substr(7, 7);
133  const char* cseed = cseed_str.c_str();
134  char* endp = NULL;
135  long seed = strtol(cseed, &endp, 16);
136  CAmount maxReward = (1000000 >> halvings) - 1;
137  int rand = generateMTRandom(seed, maxReward);
138 
139  return (1 + rand) * COIN;
140  } else if (nHeight < (6 * consensusParams.nSubsidyHalvingInterval)) {
141  // New-style constant rewards for each halving interval
142  return (500000 * COIN) >> halvings;
143  } else {
144  // Constant inflation
145  return 10000 * COIN;
146  }
147 }
148 
149 CAmount GetDogecoinMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree)
150 {
151  {
152  LOCK(mempool.cs);
153  uint256 hash = tx.GetHash();
154  double dPriorityDelta = 0;
155  CAmount nFeeDelta = 0;
156  mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
157  if (dPriorityDelta > 0 || nFeeDelta > 0)
158  return 0;
159  }
160 
161  CAmount nMinFee = ::minRelayTxFee.GetFee(nBytes);
162  nMinFee += GetDogecoinDustFee(tx.vout, ::minRelayTxFee);
163 
164  if (fAllowFree)
165  {
166  // There is a free transaction area in blocks created by most miners,
167  // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
168  // to be considered to fall into this category. We don't want to encourage sending
169  // multiple transactions instead of one big transaction to avoid fees.
170  if (nBytes < (DEFAULT_BLOCK_PRIORITY_SIZE - 1000))
171  nMinFee = 0;
172  }
173 
174  if (!MoneyRange(nMinFee))
175  nMinFee = MAX_MONEY;
176  return nMinFee;
177 }
178 
179 CAmount GetDogecoinDustFee(const std::vector<CTxOut> &vout, CFeeRate &baseFeeRate) {
180  CAmount nFee = 0;
181 
182  // To limit dust spam, add base fee for each output less than a COIN
183  BOOST_FOREACH(const CTxOut& txout, vout)
184  if (txout.IsDust(::minRelayTxFee))
185  nFee += baseFeeRate.GetFeePerK();
186 
187  return nFee;
188 }
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:32
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:15
arith_uint256 UintToArith256(const uint256 &a)
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:25
boost::shared_ptr< CAuxPow > auxpow
Definition: block.h:28
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
unsigned int nBits
Definition: chain.h:200
int64_t GetBlockTime() const
Definition: chain.h:273
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:170
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: amount.h:38
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: amount.cpp:23
CAmount GetFeePerK() const
Return the fee in satoshis for a size of 1000 bytes.
Definition: amount.h:55
int32_t nVersion
Definition: pureheader.h:29
bool IsAuxpow() const
Check if the auxpow flag is set in the version.
Definition: pureheader.h:129
int32_t GetChainId() const
Extract the chain ID.
Definition: pureheader.h:110
uint256 GetPoWHash() const
Definition: pureheader.cpp:25
int64_t GetBlockTime() const
Definition: pureheader.h:73
uint256 GetHash() const
Definition: pureheader.cpp:20
bool IsLegacy() const
Check whether this is a "legacy" block without chain ID.
Definition: pureheader.h:150
uint32_t nBits
Definition: pureheader.h:33
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:308
const std::vector< CTxOut > vout
Definition: transaction.h:327
const uint256 & GetHash() const
Definition: transaction.h:358
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta) const
Definition: txmempool.cpp:957
CCriticalSection cs
Definition: txmempool.h:483
An output of a transaction.
Definition: transaction.h:133
bool IsDust(const CFeeRate &minRelayTxFee) const
Definition: transaction.h:201
256-bit unsigned big integer.
uint32_t GetCompact(bool fNegative=false) const
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=NULL, bool *pfOverflow=NULL)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
std::string ToString() const
Definition: uint256.cpp:65
256-bit opaque blob.
Definition: uint256.h:123
CAmount GetDogecoinMinRelayFee(const CTransaction &tx, unsigned int nBytes, bool fAllowFree)
Definition: dogecoin.cpp:149
unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params &params)
Definition: dogecoin.cpp:40
bool CheckAuxPowProofOfWork(const CBlockHeader &block, const Consensus::Params &params)
Check proof-of-work of a block header, taking auxpow into account.
Definition: dogecoin.cpp:88
CAmount GetDogecoinDustFee(const std::vector< CTxOut > &vout, CFeeRate &baseFeeRate)
Definition: dogecoin.cpp:179
CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params &consensusParams, uint256 prevHash)
Definition: dogecoin.cpp:125
bool AllowDigishieldMinDifficultyForBlock(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: dogecoin.cpp:25
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:115
Parameters that influence chain consensus.
Definition: params.h:39
bool fDigishieldDifficultyCalculation
Dogecoin-specific parameters.
Definition: params.h:71
bool fStrictChainId
Definition: params.h:80
bool fSimplifiedRewards
Definition: params.h:73
int32_t nAuxpowChainId
Auxpow parameters.
Definition: params.h:79
int nSubsidyHalvingInterval
Definition: params.h:41
int64_t nPowTargetTimespan
Definition: params.h:67
uint256 powLimit
Proof of work parameters.
Definition: params.h:63
int64_t nPowTargetSpacing
Definition: params.h:66
bool fPowAllowMinDifficultyBlocks
Definition: params.h:64
#define LOCK(cs)
Definition: sync.h:177
bool error(const char *fmt, const Args &... args)
Definition: util.h:87
CTxMemPool mempool(::minRelayTxFee)
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation)
Definition: validation.cpp:86