Skip to content

Commit

Permalink
Merge pull request bitcoin#25 from StarbuckBG/master
Browse files Browse the repository at this point in the history
Implement difficulty adjustment each block
  • Loading branch information
h4x3rotab committed Oct 17, 2017
2 parents 78150b1 + 0e6c2d3 commit 5e907e3
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 144 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

*.exe
src/bitcoin
src/bitcoind
src/bitcoin-cli
src/bgoldd
src/bgold-cli
src/bitcoin-tx
src/test/test_bitcoin
src/test/test_bitcoin_fuzzy
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIR([build-aux/m4])

BITCOIN_DAEMON_NAME=bgoldd
BITCOIN_GUI_NAME=bgold-qt
BITCOIN_GUI_NAME=bitcoin-qt
BITCOIN_CLI_NAME=bgold-cli
BITCOIN_TX_NAME=bgold-tx
BITCOIN_TX_NAME=bitcoin-tx

dnl Unless the user specified ARFLAGS, force it to be cr
AC_ARG_VAR(ARFLAGS, [Flags for the archiver, defaults to <cr> if not set])
Expand Down
32 changes: 16 additions & 16 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ TESTS =
BENCHMARKS =

if BUILD_BITCOIND
bin_PROGRAMS += bitcoind
bin_PROGRAMS += bgoldd
endif

if BUILD_BITCOIN_UTILS
bin_PROGRAMS += bitcoin-cli bitcoin-tx
bin_PROGRAMS += bgold-cli bitcoin-tx
endif

.PHONY: FORCE check-symbols check-security
Expand Down Expand Up @@ -367,16 +367,16 @@ nodist_libbitcoin_util_a_SOURCES = $(srcdir)/obj/build.h
#

# bitcoind binary #
bitcoind_SOURCES = bitcoind.cpp
bitcoind_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
bitcoind_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
bitcoind_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
bgoldd_SOURCES = bitcoind.cpp
bgoldd_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
bgoldd_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
bgoldd_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)

if TARGET_WINDOWS
bitcoind_SOURCES += bitcoind-res.rc
bgoldd_SOURCES += bitcoind-res.rc
endif

bitcoind_LDADD = \
bgoldd_LDADD = \
$(LIBBITCOIN_SERVER) \
$(LIBBITCOIN_COMMON) \
$(LIBUNIVALUE) \
Expand All @@ -390,25 +390,25 @@ bitcoind_LDADD = \
$(LIBMEMENV) \
$(LIBSECP256K1)

bitcoind_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(ZMQ_LIBS)
bgoldd_LDADD += $(BOOST_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(ZMQ_LIBS)

# bitcoin-cli binary #
bitcoin_cli_SOURCES = bitcoin-cli.cpp
bitcoin_cli_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CFLAGS)
bitcoin_cli_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
bitcoin_cli_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
bgold_cli_SOURCES = bitcoin-cli.cpp
bgold_cli_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CFLAGS)
bgold_cli_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
bgold_cli_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)

if TARGET_WINDOWS
bitcoin_cli_SOURCES += bitcoin-cli-res.rc
bgold_cli_SOURCES += bitcoin-cli-res.rc
endif

bitcoin_cli_LDADD = \
bgold_cli_LDADD = \
$(LIBBITCOIN_CLI) \
$(LIBUNIVALUE) \
$(LIBBITCOIN_UTIL) \
$(LIBBITCOIN_CRYPTO)

bitcoin_cli_LDADD += $(BOOST_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(EVENT_LIBS)
bgold_cli_LDADD += $(BOOST_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(EVENT_LIBS)
#

# bitcoin-tx binary #
Expand Down
36 changes: 27 additions & 9 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "chainparams.h"
#include "consensus/merkle.h"

#include "uint256.h"
#include "arith_uint256.h"

#include "tinyformat.h"
#include "util.h"
#include "utilstrencodings.h"
Expand Down Expand Up @@ -70,6 +73,9 @@ void CChainParams::UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64
* + Contains no strange transactions
*/

const arith_uint256 maxUint = UintToArith256(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));


class CMainParams : public CChainParams {
public:
CMainParams() {
Expand All @@ -79,10 +85,14 @@ class CMainParams : public CChainParams {
consensus.BIP34Hash = uint256S("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8");
consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
consensus.BIP66Height = 363725; // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
consensus.BTGHeight = 487427; // Around 10/1/2017 12:00 UTC
consensus.BTGPremineWindow = 16000;
consensus.BTGHeight = 491407; // Around 10/25/2017 12:00 UTC
consensus.BTGPremineWindow = 8000;
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowAveragingWindow = 17;
assert(maxUint/UintToArith256(consensus.powLimit) >= consensus.nPowAveragingWindow);
consensus.nPowMaxAdjustDown = 32; // 32% adjustment down
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPowTargetTimespanLegacy = 14 * 24 * 60 * 60;; // 10 minutes
consensus.nPowTargetSpacing = 10 * 60;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
Expand Down Expand Up @@ -117,7 +127,7 @@ class CMainParams : public CChainParams {
pchMessageStart[1] = 0xbe;
pchMessageStart[2] = 0xb4;
pchMessageStart[3] = 0xd9;
nDefaultPort = 8333;
nDefaultPort = 8338; // different port than Bitcoin
nPruneAfterHeight = 100000;

genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);
Expand All @@ -126,6 +136,7 @@ class CMainParams : public CChainParams {
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));

// Note that of those with the service bits flag, most only support a subset of possible options
// TODO: Shall change this to our
vSeeds.emplace_back("seed.bitcoin.sipa.be", true); // Pieter Wuille, only supports x1, x5, x9, and xd
vSeeds.emplace_back("dnsseed.bluematt.me", true); // Matt Corallo, only supports x9
vSeeds.emplace_back("dnsseed.bitcoin.dashjr.org", false); // Luke Dashjr
Expand Down Expand Up @@ -185,10 +196,14 @@ class CTestNetParams : public CChainParams {
consensus.BIP34Hash = uint256S("0x0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8");
consensus.BIP65Height = 581885; // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
consensus.BIP66Height = 330776; // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
consensus.BTGHeight = 100000000; // Not activated yet.
consensus.BTGHeight = 1210647; // 10/20/2017 12:00 UTC.
consensus.BTGPremineWindow = 16000;
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowAveragingWindow = 17;
assert(maxUint/UintToArith256(consensus.powLimit) >= consensus.nPowAveragingWindow);
consensus.nPowMaxAdjustDown = 32; // 32% adjustment down
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPowTargetTimespanLegacy = 10 * 60; // 10 minutes
consensus.nPowTargetSpacing = 10 * 60;
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = false;
Expand Down Expand Up @@ -218,7 +233,7 @@ class CTestNetParams : public CChainParams {
pchMessageStart[1] = 0x11;
pchMessageStart[2] = 0x09;
pchMessageStart[3] = 0x07;
nDefaultPort = 18333;
nDefaultPort = 18338;
nPruneAfterHeight = 1000;

genesis = CreateGenesisBlock(1296688602, 414098458, 0x1d00ffff, 1, 50 * COIN);
Expand Down Expand Up @@ -275,10 +290,13 @@ class CRegTestParams : public CChainParams {
consensus.BIP34Hash = uint256();
consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in rpc activation tests)
consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in rpc activation tests)
consensus.BTGHeight = 100000000; // Not activated yet.
consensus.BTGHeight = 1210647; // 10/20/2017 12:00 UTC
consensus.BTGPremineWindow = 16000;
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowAveragingWindow = 17;
consensus.nPowMaxAdjustDown = 32; // 32% adjustment down
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPowTargetTimespanLegacy = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 10 * 60;
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = true;
Expand Down
14 changes: 12 additions & 2 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ struct Params {
bool fPowAllowMinDifficultyBlocks;
bool fPowNoRetargeting;
int64_t nPowTargetSpacing;
int64_t nPowTargetTimespan;
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
int64_t nPowTargetTimespanLegacy;
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespanLegacy / nPowTargetSpacing; }
uint256 nMinimumChainWork;
uint256 defaultAssumeValid;

//Zcash logic for diff adjustment
int64_t nPowAveragingWindow;
int64_t nPowMaxAdjustDown;
int64_t nPowMaxAdjustUp;
int64_t AveragingWindowTimespan() const { return nPowAveragingWindow * nPowTargetSpacing; }
int64_t MinActualTimespan() const { return (AveragingWindowTimespan() * (100 - nPowMaxAdjustUp )) / 100; }
int64_t MaxActualTimespan() const { return (AveragingWindowTimespan() * (100 + nPowMaxAdjustDown)) / 100; }


};
} // namespace Consensus

Expand Down
100 changes: 80 additions & 20 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,74 @@

unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
{
assert(pindexLast != nullptr);
unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();

int nHeightNext = pindexLast->nHeight + 1;
if (nHeightNext >= params.BTGHeight && nHeightNext < params.BTGHeight + params.BTGPremineWindow)
{
// Lowest difficulty for Bitcoin GPU premining period.

unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();

// Genesis block
if (pindexLast == NULL)
return nProofOfWorkLimit;

if (pindexLast->nHeight < params.BTGHeight) {
return BitcoinGetNextWorkRequired(pindexLast, pblock, params);
}
else if (nHeightNext % params.DifficultyAdjustmentInterval() != 0)
else if (pindexLast->nHeight < params.BTGHeight + params.BTGPremineWindow) {
return nProofOfWorkLimit;
}

const CBlockIndex* pindexFirst = pindexLast;
arith_uint256 bnTot {0};
for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) {
arith_uint256 bnTmp;
bnTmp.SetCompact(pindexFirst->nBits);
bnTot += bnTmp;
pindexFirst = pindexFirst->pprev;
}

if (pindexFirst == NULL)
return nProofOfWorkLimit;

arith_uint256 bnAvg {bnTot / params.nPowAveragingWindow};


return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params);
}

unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, int64_t nLastBlockTime, int64_t nFirstBlockTime, const Consensus::Params& params)
{

// Limit adjustment
int64_t nActualTimespan = nLastBlockTime - nFirstBlockTime;
nActualTimespan = params.AveragingWindowTimespan() + (nActualTimespan - params.AveragingWindowTimespan())/4;

if (nActualTimespan < params.MinActualTimespan())
nActualTimespan = params.MinActualTimespan();
if (nActualTimespan > params.MaxActualTimespan())
nActualTimespan = params.MaxActualTimespan();

// Retarget
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
arith_uint256 bnNew {bnAvg};
bnNew /= params.AveragingWindowTimespan();
bnNew *= nActualTimespan;

if (bnNew > bnPowLimit)
bnNew = bnPowLimit;

return bnNew.GetCompact();
}


// Depricated for Bitcoin Gold
unsigned int BitcoinGetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
{
assert(pindexLast != nullptr);
unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();

int nHeightNext = pindexLast->nHeight + 1;
int diffAdjustmentInterval = 2016; // every 2016 blocks for Bitcoin

if (nHeightNext % params.DifficultyAdjustmentInterval() != 0)
{
// Difficulty adjustment interval is not finished. Keep the last value.
if (params.fPowAllowMinDifficultyBlocks)
Expand All @@ -42,38 +100,40 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
}
return pindexLast->nBits;
}

// Go back by what we want to be 14 days worth of blocks
int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
assert(nHeightFirst >= 0);
const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
assert(pindexFirst);

return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
return BitcoinCalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
}

unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)

// Depricated for Bitcoin Gold
unsigned int BitcoinCalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{
if (params.fPowNoRetargeting)
return pindexLast->nBits;

// Limit adjustment step
int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
if (nActualTimespan < params.nPowTargetTimespan/4)
nActualTimespan = params.nPowTargetTimespan/4;
if (nActualTimespan > params.nPowTargetTimespan*4)
nActualTimespan = params.nPowTargetTimespan*4;

if (nActualTimespan < params.nPowTargetTimespanLegacy/4)
nActualTimespan = params.nPowTargetTimespanLegacy/4;
if (nActualTimespan > params.nPowTargetTimespanLegacy*4)
nActualTimespan = params.nPowTargetTimespanLegacy*4;
// Retarget
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
arith_uint256 bnNew;
bnNew.SetCompact(pindexLast->nBits);
bnNew *= nActualTimespan;
bnNew /= params.nPowTargetTimespan;

bnNew /= params.nPowTargetTimespanLegacy;
if (bnNew > bnPowLimit)
bnNew = bnPowLimit;

return bnNew.GetCompact();
}

Expand Down
6 changes: 5 additions & 1 deletion src/pow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BITCOIN_POW_H
#define BITCOIN_POW_H

#include "arith_uint256.h"
#include "consensus/params.h"

#include <stdint.h>
Expand All @@ -15,7 +16,10 @@ class CBlockIndex;
class uint256;

unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&);
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&);
unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, int64_t nLastBlockTime, int64_t nFirstBlockTime, const Consensus::Params& params);

unsigned int BitcoinGetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params);
unsigned int BitcoinCalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params);

/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&);
Expand Down
Loading

0 comments on commit 5e907e3

Please sign in to comment.