Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mining] [ProgPow] Reduce DAG size #1014

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ libbitcoin_consensus_a_SOURCES = \
amount.h \
arith_uint256.cpp \
arith_uint256.h \
chainparams.cpp \
consensus/merkle.cpp \
consensus/merkle.h \
consensus/params.h \
Expand Down
38 changes: 38 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ class CMainParams : public CChainParams {
nHeightLightZerocoin = 335975;
nValueBlacklist = (282125 + 60540) * COIN;
nHeightEnforceBlacklist = 336413;
nHeightProgPowDAGSizeReduction = 2100000;
nPreferredMintsPerBlock = 70; //Miner will not include more than this many mints per block
nPreferredMintsPerTx = 15; //Do not consider a transaction as standard that includes more than this many mints

Expand Down Expand Up @@ -552,6 +553,7 @@ class CTestNetParams : public CChainParams {

nHeightLightZerocoin = 9428;
nHeightEnforceBlacklist = 0;
nHeightProgPowDAGSizeReduction = 1125000;

/** RingCT/Stealth **/
nDefaultRingSize = 11;
Expand Down Expand Up @@ -723,6 +725,7 @@ class CDevNetParams : public CChainParams {

nHeightLightZerocoin = 1000;
nHeightEnforceBlacklist = 0;
nHeightProgPowDAGSizeReduction = 1071250;

/** RingCT/Stealth **/
nDefaultRingSize = 11;
Expand Down Expand Up @@ -933,6 +936,41 @@ bool CChainParams::CheckKIenforced(int nSpendHeight) const
return false;
}

int CChainParams::GetProgPowEpochNumber(int blockNumber) const
{
int epochLength = blockNumber >= HeightProgPowDAGSizeReduction() ?
ETHASH_EPOCH_LENGTH_750MB_PER_YEAR :
ETHASH_EPOCH_LENGTH;

if (blockNumber >= HeightProgPowDAGSizeReduction()) {
blockNumber -= HeightProgPowDAGSizeReduction();
}

return blockNumber ? blockNumber / epochLength : 0;
}

std::pair<int, int> CChainParams::GetProgPowNextEpoch(int blockNumber) const {
int epochLength = blockNumber >= HeightProgPowDAGSizeReduction() ?
ETHASH_EPOCH_LENGTH_750MB_PER_YEAR :
ETHASH_EPOCH_LENGTH;

int epochBaseHeight = 0;
if (blockNumber >= HeightProgPowDAGSizeReduction()) {
epochBaseHeight = HeightProgPowDAGSizeReduction();
}

int currentEpoch = (blockNumber - epochBaseHeight) / epochLength;
int currentEpochStart = currentEpoch * epochLength + epochBaseHeight;

if (blockNumber < HeightProgPowDAGSizeReduction() &&
currentEpochStart + epochLength >= HeightProgPowDAGSizeReduction()) {
// Next epoch is the transition back to epoch 0
return { 0, HeightProgPowDAGSizeReduction() };
}

return { currentEpoch + 1, currentEpochStart + epochLength };
}


std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain)
{
Expand Down
11 changes: 11 additions & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class CChainParams
int HeightCheckDenom() const { return nHeightCheckDenom; }
int HeightLightZerocoin() const { return nHeightLightZerocoin; }
int HeightEnforceBlacklist() const { return nHeightEnforceBlacklist; }
int HeightProgPowDAGSizeReduction() const { return nHeightProgPowDAGSizeReduction; }

uint32_t PowUpdateTimestamp() const { return nPowUpdateTimestamp; }
uint64_t KIforkTimestamp() const { return nTimeKIfork; }
Expand All @@ -164,6 +165,15 @@ class CChainParams
bool CheckKIenforced(const CBlockIndex* pindex) const;
bool CheckKIenforced(int nSpendHeight) const;

/**
* Returns the current epoch number for the specified block number
*/
int GetProgPowEpochNumber(int blockNumber) const;
/**
* Returns a pair with <next epoch, height of next epoch> for the specified block number
*/
std::pair<int, int> GetProgPowNextEpoch(int blockNumber) const;


protected:
CChainParams() {}
Expand Down Expand Up @@ -225,6 +235,7 @@ class CChainParams
int nHeightCheckDenom;
int nHeightLightZerocoin;
int nHeightEnforceBlacklist;
int nHeightProgPowDAGSizeReduction;

//Settings that are not chain critical, but should not be edited unless the person changing understands the consequence
int nMaxHeaderRequestWithoutPoW;
Expand Down
1 change: 1 addition & 0 deletions src/crypto/ethash/include/ethash/ethash.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
#define ETHASH_REVISION "23"

#define ETHASH_EPOCH_LENGTH 5525
#define ETHASH_EPOCH_LENGTH_750MB_PER_YEAR 8175
#define ETHASH_LIGHT_CACHE_ITEM_SIZE 64
#define ETHASH_FULL_DATASET_ITEM_SIZE 128
#define ETHASH_NUM_DATASET_ACCESSES 64
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/ethash/include/ethash/ethash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace ethash
{
constexpr auto revision = ETHASH_REVISION;

static constexpr int epoch_length = ETHASH_EPOCH_LENGTH;
static constexpr int light_cache_item_size = ETHASH_LIGHT_CACHE_ITEM_SIZE;
static constexpr int full_dataset_item_size = ETHASH_FULL_DATASET_ITEM_SIZE;
static constexpr int num_dataset_accesses = ETHASH_NUM_DATASET_ACCESSES;
Expand Down Expand Up @@ -72,9 +71,10 @@ static constexpr auto calculate_epoch_seed = ethash_calculate_epoch_seed;


/// Calculates the epoch number out of the block number.
[[deprecated("Use CChainParams::GetProgPowEpochNumber() instead")]]
inline constexpr int get_epoch_number(int block_number) noexcept
{
return block_number ? block_number / epoch_length : 0;
return block_number ? block_number / ETHASH_EPOCH_LENGTH : 0;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <chainparams.h>
#include <hash.h>
#include <crypto/common.h>
#include <crypto/hmac_sha512.h>
Expand Down Expand Up @@ -272,7 +273,7 @@ uint256 ProgPowHash(const CBlockHeader& blockHeader, uint256& mix_hash)
{
LOCK(cs_context_builder);
// Get the context from the block height
const auto epoch_number = ethash::get_epoch_number(blockHeader.nHeight);
const auto epoch_number = Params().GetProgPowEpochNumber(blockHeader.nHeight);
if (!progpow_context || progpow_context->epoch_number != epoch_number)
progpow_context = ethash::create_epoch_context(epoch_number);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn
if (block.IsProgPow()) {
result.pushKV("prog_header_hash", block.GetProgPowHeaderHash().GetHex());
result.pushKV("prog_header_hex", HexStr(ss.begin(), ss.end()));
result.pushKV("epoch_number", ethash::get_epoch_number(block.nHeight));
result.pushKV("epoch_number", Params().GetProgPowEpochNumber(block.nHeight));
}
if (blockindex->pprev)
result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex());
Expand Down
10 changes: 8 additions & 2 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,19 @@ static UniValue getblocktemplate_impl(const std::string &strMode, const UniValue
if (mapProgPowTemplates.count(lastheader)) {
if (pblock->nTime - 60 < mapProgPowTemplates.at(lastheader).nTime) {
result.pushKV("pprpcheader", lastheader);
result.pushKV("pprpcepoch", ethash::get_epoch_number(pblock->nHeight));
result.pushKV("pprpcepoch", Params().GetProgPowEpochNumber(pblock->nHeight));
auto nextEpoch = Params().GetProgPowNextEpoch(pblock->nHeight);
result.pushKV("pprpcnextepoch", nextEpoch.first);
result.pushKV("pprpcnextepochheight", nextEpoch.second);
return result;
}
}

result.pushKV("pprpcheader", pblock->GetProgPowHeaderHash().GetHex());
result.pushKV("pprpcepoch", progpow::get_epoch_number(pblock->nHeight));
result.pushKV("pprpcepoch", Params().GetProgPowEpochNumber(pblock->nHeight));
auto nextEpoch = Params().GetProgPowNextEpoch(pblock->nHeight);
result.pushKV("pprpcnextepoch", nextEpoch.first);
result.pushKV("pprpcnextepochheight", nextEpoch.second);
mapProgPowTemplates[pblock->GetProgPowHeaderHash().GetHex()] = *pblock;
lastheader = pblock->GetProgPowHeaderHash().GetHex();
}
Expand Down