From 8c0468c2d227e484f36ff24e3e420ad84312203a Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Thu, 25 Jun 2015 11:28:31 +0100 Subject: Add Dogecoin block subsidy calculations. --- src/dogecoin.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/dogecoin.cpp (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp new file mode 100644 index 000000000..72a9f3cc5 --- /dev/null +++ b/src/dogecoin.cpp @@ -0,0 +1,39 @@ +// Copyright (c) 2015 The Dogecoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include + +#include "dogecoin.h" + +int static generateMTRandom(unsigned int s, int range) +{ + boost::mt19937 gen(s); + boost::uniform_int<> dist(1, range); + return dist(gen); +} + +CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusParams, uint256 prevHash) +{ + int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; + + if (nHeight < 145000) // && !consensusParams.SimplifiedRewards()) + { + // Old-style rewards derived from the previous block hash + const std::string cseed_str = prevHash.ToString().substr(7, 7); + const char* cseed = cseed_str.c_str(); + char* endp = NULL; + long seed = strtol(cseed, &endp, 16); + CAmount maxReward = (1000000 >> halvings) - 1; + int rand = generateMTRandom(seed, maxReward); + + return (1 + rand) * COIN; + } else if (nHeight < (6 * consensusParams.nSubsidyHalvingInterval)) { + // New-style constant rewards for each halving interval + return (500000 * COIN) >> halvings; + } else { + // Constant inflation + return 10000 * COIN; + } +} -- cgit v1.2.3 From f0f9fd02926cc4b52ac1b73f494238d674a8c86c Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Tue, 18 Apr 2017 19:15:10 +0100 Subject: Add Dogecoin difficulty calculations --- src/dogecoin.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp index 72a9f3cc5..e946acad6 100644 --- a/src/dogecoin.cpp +++ b/src/dogecoin.cpp @@ -5,7 +5,9 @@ #include #include +#include "arith_uint256.h" #include "dogecoin.h" +#include "util.h" int static generateMTRandom(unsigned int s, int range) { @@ -14,6 +16,65 @@ int static generateMTRandom(unsigned int s, int range) return dist(gen); } +unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) +{ + int nHeight = pindexLast->nHeight + 1; + bool fNewDifficultyProtocol = (nHeight >= 145000); + // bool fNewDifficultyProtocol = (nHeight >= params.GetDigiShieldForkBlock()); + const int64_t retargetTimespan = fNewDifficultyProtocol + ? 60 // params.DigiShieldTargetTimespan() + : params.nPowTargetTimespan; + + const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; + int64_t nModulatedTimespan = nActualTimespan; + int64_t nMaxTimespan; + int64_t nMinTimespan; + + if (fNewDifficultyProtocol) //DigiShield implementation - thanks to RealSolid & WDC for this code + { + // amplitude filter - thanks to daft27 for this code + nModulatedTimespan = retargetTimespan + (nModulatedTimespan - retargetTimespan) / 8; + + nMinTimespan = retargetTimespan - (retargetTimespan / 4); + nMaxTimespan = retargetTimespan + (retargetTimespan / 2); + } else if (nHeight > 10000) { + nMinTimespan = retargetTimespan / 4; + nMaxTimespan = retargetTimespan * 4; + } else if (nHeight > 5000) { + nMinTimespan = retargetTimespan / 8; + nMaxTimespan = retargetTimespan * 4; + } else { + nMinTimespan = retargetTimespan / 16; + nMaxTimespan = retargetTimespan * 4; + } + + // Limit adjustment step + if (nModulatedTimespan < nMinTimespan) + nModulatedTimespan = nMinTimespan; + else if (nModulatedTimespan > nMaxTimespan) + nModulatedTimespan = nMaxTimespan; + + // Retarget + const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); + arith_uint256 bnNew; + arith_uint256 bnOld; + bnNew.SetCompact(pindexLast->nBits); + bnOld = bnNew; + bnNew *= nModulatedTimespan; + bnNew /= retargetTimespan; + + if (bnNew > bnPowLimit) + bnNew = bnPowLimit; + + /// debug print + LogPrintf("GetNextWorkRequired RETARGET\n"); + LogPrintf("params.nPowTargetTimespan = %d nActualTimespan = %d\n", params.nPowTargetTimespan, nActualTimespan); + LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString()); + LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); + + return bnNew.GetCompact(); +} + CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusParams, uint256 prevHash) { int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; -- cgit v1.2.3 From bc8cca48968dfa3f60b5eae6a2b92bdd2870eee3 Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Sun, 13 Aug 2017 13:31:12 +0100 Subject: Merge AuxPoW support from Namecore Changes are as below: Wrap CBlockHeader::nVersion into a new class (CBlockVersion). This allows to take care of interpreting the field into a base version, auxpow flag and the chain ID. Update getauxblock.py for new 'generate' RPC call. Add 'auxpow' to block JSON. Accept auxpow as PoW verification. Add unit tests for auxpow verification. Add check for memory-layout of CBlockVersion. Weaken auxpow chain ID checks for the testnet. Allow Params() to overrule when to check the auxpow chain ID and for legacy blocks. Use this to disable the checks on testnet. Introduce CPureBlockHeader. Split the block header part that is used by auxpow and the "real" block header (that uses auxpow) to resolve the cyclic dependency between the two. Differentiate between uint256 and arith_uint256. This change was done upstream, modify the auxpow code. Add missing lock in auxpow_tests. Fix REST header check for auxpow headers. Those can be longer, thus take that into account. Also perform the check actually on an auxpow header. Correctly set the coinbase for getauxblock results. Call IncrementExtraNonce in getauxblock so that the coinbase is actually initialised with the stuff it should be. (BIP30 block height and COINBASE_FLAGS.) Implement getauxblock plus regression test. Turn auxpow test into FIXTURE test. This allows using of the Params() calls. Move CMerkleTx code to auxpow.cpp. Otherwise we get linker errors when building without wallet. Fix rebase with BIP66. Update the code to handle BIP66's nVersion=3. Enforce that auxpow parent blocks have no auxpow block version. This is for compatibility with namecoind. See also https://github.com/namecoin/namecoin/pull/199. Move auxpow-related parameters to Consensus::Params. --- src/dogecoin.cpp | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp index e946acad6..8ac2782ba 100644 --- a/src/dogecoin.cpp +++ b/src/dogecoin.cpp @@ -21,9 +21,9 @@ unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex* pindexLast, in int nHeight = pindexLast->nHeight + 1; bool fNewDifficultyProtocol = (nHeight >= 145000); // bool fNewDifficultyProtocol = (nHeight >= params.GetDigiShieldForkBlock()); - const int64_t retargetTimespan = fNewDifficultyProtocol - ? 60 // params.DigiShieldTargetTimespan() - : params.nPowTargetTimespan; + const int64_t retargetTimespan = fNewDifficultyProtocol ? 60 // params.DigiShieldTargetTimespan() + : + params.nPowTargetTimespan; const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; int64_t nModulatedTimespan = nActualTimespan; @@ -75,6 +75,44 @@ unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex* pindexLast, in return bnNew.GetCompact(); } +bool CheckAuxPowProofOfWork(const CBlockHeader& block, const Consensus::Params& params) +{ + /* Except for legacy blocks with full version 1, ensure that + the chain ID is correct. Legacy blocks are not allowed since + the merge-mining start, which is checked in AcceptBlockHeader + where the height is known. */ + if (!block.IsLegacy() && params.fStrictChainId && block.GetChainId() != params.nAuxpowChainId) + return error("%s : block does not have our chain ID" + " (got %d, expected %d, full nVersion %d)", + __func__, block.GetChainId(), + params.nAuxpowChainId, block.nVersion); + + /* If there is no auxpow, just check the block hash. */ + if (!block.auxpow) + { + if (block.IsAuxpow()) + return error("%s : no auxpow on block with auxpow version", + __func__); + + if (!CheckProofOfWork(block.GetPoWHash(), block.nBits, params)) + return error("%s : non-AUX proof of work failed", __func__); + + return true; + } + + /* We have auxpow. Check it. */ + + if (!block.IsAuxpow()) + return error("%s : auxpow on block with non-auxpow version", __func__); + + if (!block.auxpow->check(block.GetHash(), block.GetChainId(), params)) + return error("%s : AUX POW is not valid", __func__); + if (!CheckProofOfWork(block.auxpow->getParentBlockPoWHash(), block.nBits, params)) + return error("%s : AUX proof of work failed", __func__); + + return true; +} + CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusParams, uint256 prevHash) { int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; -- cgit v1.2.3 From 1de15c70ce8065334fd827b137eba8fb899b527a Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Sun, 24 Sep 2017 15:56:19 +0100 Subject: Sync changes from Dogecoin 1.10 --- src/dogecoin.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp index 8ac2782ba..76db22f94 100644 --- a/src/dogecoin.cpp +++ b/src/dogecoin.cpp @@ -16,6 +16,24 @@ int static generateMTRandom(unsigned int s, int range) return dist(gen); } +// Dogecoin: Normally minimum difficulty blocks can only occur in between +// retarget blocks. However, once we introduce Digishield every block is +// a retarget, so we need to handle minimum difficulty on all blocks. +bool AllowDigishieldMinDifficultyForBlock(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) +{ + // check if the chain allows minimum difficulty blocks + if (!params.fPowAllowMinDifficultyBlocks) + return false; + + // check if the chain allows minimum difficulty blocks on recalc blocks + if (pindexLast->nHeight < 157500) + // if (!params.fPowAllowDigishieldMinDifficultyBlocks) + return false; + + // Allow for a minimum block time if the elapsed time > 2*nTargetSpacing + return (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2); +} + unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) { int nHeight = pindexLast->nHeight + 1; @@ -88,8 +106,7 @@ bool CheckAuxPowProofOfWork(const CBlockHeader& block, const Consensus::Params& params.nAuxpowChainId, block.nVersion); /* If there is no auxpow, just check the block hash. */ - if (!block.auxpow) - { + if (!block.auxpow) { if (block.IsAuxpow()) return error("%s : no auxpow on block with auxpow version", __func__); -- cgit v1.2.3 From 1be681a1b97b686f838af90682a57f2030d26015 Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Thu, 28 Dec 2017 15:04:08 +0000 Subject: Modify chain consensus parameters to be height aware (#1396) * Modify chain consensus parameters to be height aware * Correct implementation of simplified rewards in parameters * Correct max money * Use base block version in IsSuperMajority() instead of full version * Correct mining of blocks in AuxPoW tests * Add in missing pre-AuxPoW consensus checks --- src/dogecoin.cpp | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp index 76db22f94..f06849d61 100644 --- a/src/dogecoin.cpp +++ b/src/dogecoin.cpp @@ -37,18 +37,13 @@ bool AllowDigishieldMinDifficultyForBlock(const CBlockIndex* pindexLast, const C unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) { int nHeight = pindexLast->nHeight + 1; - bool fNewDifficultyProtocol = (nHeight >= 145000); - // bool fNewDifficultyProtocol = (nHeight >= params.GetDigiShieldForkBlock()); - const int64_t retargetTimespan = fNewDifficultyProtocol ? 60 // params.DigiShieldTargetTimespan() - : - params.nPowTargetTimespan; - + const int64_t retargetTimespan = params.nPowTargetTimespan; const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; int64_t nModulatedTimespan = nActualTimespan; int64_t nMaxTimespan; int64_t nMinTimespan; - if (fNewDifficultyProtocol) //DigiShield implementation - thanks to RealSolid & WDC for this code + if (params.fDigishieldDifficultyCalculation) //DigiShield implementation - thanks to RealSolid & WDC for this code { // amplitude filter - thanks to daft27 for this code nModulatedTimespan = retargetTimespan + (nModulatedTimespan - retargetTimespan) / 8; @@ -84,12 +79,6 @@ unsigned int CalculateDogecoinNextWorkRequired(const CBlockIndex* pindexLast, in if (bnNew > bnPowLimit) bnNew = bnPowLimit; - /// debug print - LogPrintf("GetNextWorkRequired RETARGET\n"); - LogPrintf("params.nPowTargetTimespan = %d nActualTimespan = %d\n", params.nPowTargetTimespan, nActualTimespan); - LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString()); - LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); - return bnNew.GetCompact(); } @@ -134,7 +123,7 @@ CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusP { int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; - if (nHeight < 145000) // && !consensusParams.SimplifiedRewards()) + if (!consensusParams.fSimplifiedRewards) { // Old-style rewards derived from the previous block hash const std::string cseed_str = prevHash.ToString().substr(7, 7); -- cgit v1.2.3 From 18850d359bbc4d3333d9f1ac33607242d205c2ce Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Sun, 7 Jan 2018 22:56:53 +0000 Subject: Add Dogecoin current fee calculation logic (#1413) Introduces 1 COIN/kb fees, rounded up to the next 1 COIN. --- src/dogecoin.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp index f06849d61..b34437ca0 100644 --- a/src/dogecoin.cpp +++ b/src/dogecoin.cpp @@ -5,9 +5,12 @@ #include #include +#include "policy/policy.h" #include "arith_uint256.h" #include "dogecoin.h" +#include "txmempool.h" #include "util.h" +#include "validation.h" int static generateMTRandom(unsigned int s, int range) { @@ -142,3 +145,44 @@ CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusP return 10000 * COIN; } } + +CAmount GetDogecoinMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree) +{ + { + LOCK(mempool.cs); + uint256 hash = tx.GetHash(); + double dPriorityDelta = 0; + CAmount nFeeDelta = 0; + mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta); + if (dPriorityDelta > 0 || nFeeDelta > 0) + return 0; + } + + CAmount nMinFee = ::minRelayTxFee.GetFee(nBytes); + nMinFee += GetDogecoinDustFee(tx.vout, ::minRelayTxFee); + + if (fAllowFree) + { + // There is a free transaction area in blocks created by most miners, + // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000 + // to be considered to fall into this category. We don't want to encourage sending + // multiple transactions instead of one big transaction to avoid fees. + if (nBytes < (DEFAULT_BLOCK_PRIORITY_SIZE - 1000)) + nMinFee = 0; + } + + if (!MoneyRange(nMinFee)) + nMinFee = MAX_MONEY; + return nMinFee; +} + +CAmount GetDogecoinDustFee(const std::vector &vout, CFeeRate &baseFeeRate) { + CAmount nFee = 0; + + // To limit dust spam, add base fee for each output less than DUST_SOFT_LIMIT + BOOST_FOREACH(const CTxOut& txout, vout) + if (txout.IsDust(::minRelayTxFee)) + nFee += baseFeeRate.GetFeePerK(); + + return nFee; +} -- cgit v1.2.3 From 3d363eccd436df49bcdfccf01bcd5148ba3cfb87 Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Wed, 10 Jan 2018 08:43:05 +0000 Subject: Bring 1.14 fees in line with 1.10 (#1425) * Disable free transactions * Updating remaining fee calculation constants * Round up to the nearest 1k, not up 1k so the UI fee estimator is correct --- src/dogecoin.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp index b34437ca0..3e57791fd 100644 --- a/src/dogecoin.cpp +++ b/src/dogecoin.cpp @@ -146,6 +146,17 @@ CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusP } } +unsigned int GetDogecoinTxSize(const unsigned int nTxBytes) +{ + // Dogecoin: Round TX bytes up to the next 1,000 bytes + unsigned int nMod = nTxBytes % 1000; + if (nMod > 0) { + return nTxBytes + 1000 - nMod; + } else { + return nTxBytes; + } +} + CAmount GetDogecoinMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree) { { @@ -179,9 +190,10 @@ CAmount GetDogecoinMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool CAmount GetDogecoinDustFee(const std::vector &vout, CFeeRate &baseFeeRate) { CAmount nFee = 0; - // To limit dust spam, add base fee for each output less than DUST_SOFT_LIMIT + // To limit dust spam, add base fee for each output less than a COIN BOOST_FOREACH(const CTxOut& txout, vout) - if (txout.IsDust(::minRelayTxFee)) + // if (txout.IsDust(::minRelayTxFee)) + if (txout.nValue < COIN) nFee += baseFeeRate.GetFeePerK(); return nFee; -- cgit v1.2.3 From 9c3a11b2488f3e01e6763bc1217598a4d1c69bde Mon Sep 17 00:00:00 2001 From: Ross Nicoll Date: Sun, 11 Feb 2018 21:00:50 +0000 Subject: Clean up RPC tests (#1465) * Enable full block tests * Fix invalidblocktest * Move watch only address funding to immediately before it's used, so node 0 doesn't spend the output before it checks it later. * Fix `fundrawtransaction` tests and sanitize fee calculation at the same time * Correct resolution of chain parameters when validating tx inputs, especially from previous coinbase transactions * Set block versions on full block tests so that the generated blocks are AuxPoW compatible --- src/dogecoin.cpp | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'src/dogecoin.cpp') diff --git a/src/dogecoin.cpp b/src/dogecoin.cpp index 3e57791fd..3f06e8a49 100644 --- a/src/dogecoin.cpp +++ b/src/dogecoin.cpp @@ -146,17 +146,6 @@ CAmount GetDogecoinBlockSubsidy(int nHeight, const Consensus::Params& consensusP } } -unsigned int GetDogecoinTxSize(const unsigned int nTxBytes) -{ - // Dogecoin: Round TX bytes up to the next 1,000 bytes - unsigned int nMod = nTxBytes % 1000; - if (nMod > 0) { - return nTxBytes + 1000 - nMod; - } else { - return nTxBytes; - } -} - CAmount GetDogecoinMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree) { { -- cgit v1.2.3