Skip to content

Commit

Permalink
Merge pull request #366 from meritlabs/feature/pog2
Browse files Browse the repository at this point in the history
Proof of Growth Community Changes (PoG2)
  • Loading branch information
mempko authored Aug 15, 2018
2 parents d7a6129 + 620e231 commit da2c144
Show file tree
Hide file tree
Showing 34 changed files with 2,973 additions and 485 deletions.
6 changes: 6 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ MERIT_CORE_H = \
pog/wrs.h \
pog/invitebuffer.h \
pog/reward.h \
pog2/cgs.h \
pog2/select.h \
pog2/reward.h \
protocol.h \
random.h \
refdb.h \
Expand Down Expand Up @@ -228,6 +231,9 @@ libmerit_server_a_SOURCES = \
pog/select.cpp \
pog/wrs.cpp \
pog/invitebuffer.cpp \
pog2/cgs.cpp \
pog2/reward.cpp \
pog2/select.cpp \
policy/fees.cpp \
policy/policy.cpp \
policy/rbf.cpp \
Expand Down
79 changes: 63 additions & 16 deletions src/addressindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ struct CAddressUnspentKey {
isCoinbase = ser_readdata8(s);
}

CAddressUnspentKey(unsigned int addressType, uint160 addressHash, uint256 txid, size_t indexValue, bool isCoinbaseIn, bool isInviteIn) {
CAddressUnspentKey(
unsigned int addressType,
uint160 addressHash,
uint256 txid,
size_t indexValue,
bool isCoinbaseIn,
bool isInviteIn) {

isInvite = isInviteIn;
type = addressType;
hashBytes = addressHash;
Expand All @@ -65,6 +72,36 @@ struct CAddressUnspentKey {
isCoinbase = false;
isInvite = false;
}

bool operator==(const CAddressUnspentKey& o) const {
return
type == o.type &&
hashBytes == o.hashBytes &&
txhash == o.txhash &&
index == o.index &&
isCoinbase == o.isCoinbase &&
isInvite == o.isInvite;
}

//lexigraphical sort on order of serialization
bool operator<(const CAddressUnspentKey& o) const {
if(isInvite == o.isInvite) {
if(type == o.type) {
if(hashBytes == o.hashBytes) {
if(txhash == o.txhash) {
if(index == o.index) {
return isCoinbase < o.isCoinbase;
}
return index < o.index;
}
return txhash < o.txhash;
}
return hashBytes < o.hashBytes;
}
return type < o.type;
}
return isInvite < o.isInvite;
}
};

struct CAddressUnspentValue {
Expand Down Expand Up @@ -240,7 +277,11 @@ struct CAddressIndexIteratorHeightKey {
blockHeight = ser_readdata32be(s);
}

CAddressIndexIteratorHeightKey(unsigned int addressType, uint160 addressHash, int height) {
CAddressIndexIteratorHeightKey(
unsigned int addressType,
uint160 addressHash,
int height) {

type = addressType;
hashBytes = addressHash;
blockHeight = height;
Expand Down Expand Up @@ -290,7 +331,14 @@ struct CMempoolAddressDeltaKey
int spending;
bool invite;

CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, int s, bool is_invite) {
CMempoolAddressDeltaKey(
int addressType,
uint160 addressHash,
uint256 hash,
unsigned int i,
int s,
bool is_invite) {

type = addressType;
addressBytes = addressHash;
txhash = hash;
Expand All @@ -310,24 +358,23 @@ struct CMempoolAddressDeltaKey

struct CMempoolAddressDeltaKeyCompare
{
bool operator()(const CMempoolAddressDeltaKey& a, const CMempoolAddressDeltaKey& b) const {
bool operator()(
const CMempoolAddressDeltaKey& a,
const CMempoolAddressDeltaKey& b) const {

if (a.type == b.type) {
if (a.addressBytes == b.addressBytes) {
if (a.txhash == b.txhash) {
if (a.index == b.index) {
return a.spending < b.spending;
} else {
return a.index < b.index;
}
} else {
return a.txhash < b.txhash;
}
} else {
return a.addressBytes < b.addressBytes;
}
} else {
return a.type < b.type;
}
}
return a.index < b.index;
}
return a.txhash < b.txhash;
}
return a.addressBytes < b.addressBytes;
}
return a.type < b.type;
}
};

Expand Down
58 changes: 51 additions & 7 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ class CMainParams : public CChainParams
consensus.nPowTargetTimespan = 24 * 60 * 60; // one day for nBits adjustment
consensus.nEdgeBitsTargetThreshold = 4; // adjust nEdgeBits if block time is 4x more/less than expected
consensus.nPowTargetSpacing = 1 * 60; // one minute for a block

const double blocks_per_minute = 60.0 / consensus.nPowTargetSpacing;

consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 1368; // 95% of 2016
Expand All @@ -153,7 +156,7 @@ class CMainParams : public CChainParams
consensus.vDeployments[Consensus::DEPLOYMENT_GENESIS].nTimeout = 1230767999; // December 31, 2008

consensus.daedalus_max_invites_per_block = 10; //20 merit over 2
consensus.daedalus_block_window = 60 * 24 * 3; //Window used to compute invites.
consensus.daedalus_block_window = blocks_per_minute * 60 * 24 * 3; //Window used to compute invites.
//Looks at blocks over a 3 day period.
consensus.daedalus_min_one_invite_for_every_x_blocks = 10; //Minimum of 1 invite every 10 minutes, or 144 per day.
consensus.daedalus_max_outstanding_invites_per_address = 500;
Expand All @@ -173,11 +176,23 @@ class CMainParams : public CChainParams

// Block where improved invites turn on
consensus.imp_invites_blockheight = 180980; //Sat, Jan 5th 6:11:00 ish GMT
consensus.imp_block_window = 60 * 24 * 1; //1 days of blocks
consensus.imp_block_window = blocks_per_minute * 60 * 24 * 1; //1 days of blocks
consensus.imp_min_one_invite_for_every_x_blocks = 10; //invite every 10 minutes at a minumum, 144 per day.
consensus.imp_miner_reward_for_every_x_blocks = 10; //invite every 10 minutes at a minumum, 144 per day.
consensus.imp_weights = {60, 40};

// PoG v2 settings.
consensus.pog2_blockheight = 334750; //Around Fri, 17 Aug 2018 09:14:38 +0000
consensus.pog2_total_winning_ambassadors = 20;
consensus.pog2_ambassador_percent_cut = 50; //50%
consensus.pog2_pow_target_timespan = 5 * 60 * 60; // every 5 hours
consensus.pog2_initial_ambassador_stake = 20_merit;
consensus.pog2_coin_maturity = blocks_per_minute * 60 * 24 * 30; //Month for coin maturity
consensus.pog2_new_coin_maturity = blocks_per_minute * 60 * 24 * 30; //Month for coin maturity
consensus.pog2_max_outstanding_invites_per_address = 50;
consensus.pog2_convex_b = 0.2;
consensus.pog2_convex_s = 0.05;

/**
* The message start string is designed to be unlikely to occur in normal data.
* The characters are rarely used upper ASCII, not valid as UTF-8, and produce
Expand Down Expand Up @@ -288,6 +303,8 @@ class CTestNetParams : public CChainParams
consensus.nPowTargetTimespan = 24 * 60 * 60; // one day for nBits adjustment
consensus.nEdgeBitsTargetThreshold = 4; // adjust nEdgeBits if block time is twice more/less than expected
consensus.nPowTargetSpacing = 1 * 60; // one minute for a block
const double blocks_per_minute = 60.0 / consensus.nPowTargetSpacing;

consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 1080; // 75% for testchains
Expand Down Expand Up @@ -326,6 +343,18 @@ class CTestNetParams : public CChainParams
consensus.imp_miner_reward_for_every_x_blocks = 10; //invite every 10 minutes at a minumum, 144 per day.
consensus.imp_weights = {60, 40};

// PoG v2 settings.
consensus.pog2_blockheight = 216810;
consensus.pog2_total_winning_ambassadors = 20;
consensus.pog2_ambassador_percent_cut = 50; //50%
consensus.pog2_pow_target_timespan = 5 * 60 * 60; // every 5 hours
consensus.pog2_initial_ambassador_stake = 20_merit;
consensus.pog2_coin_maturity = blocks_per_minute * 60 * 24 * 30; //Month for coin maturity
consensus.pog2_new_coin_maturity = blocks_per_minute * 60 * 24 * 30; //Month for coin maturity
consensus.pog2_max_outstanding_invites_per_address = 50;
consensus.pog2_convex_b = 0.2;
consensus.pog2_convex_s = 0.05;

pchMessageStart[0] = 0x0b;
pchMessageStart[1] = 0x11;
pchMessageStart[2] = 0x09;
Expand Down Expand Up @@ -411,8 +440,11 @@ class CRegTestParams : public CChainParams
uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
*consensus.sEdgeBitsAllowed.begin()};
consensus.nPowTargetTimespan = 60; // one minute for nBits adjustment
consensus.nEdgeBitsTargetThreshold = 2; // adjust nEdgeBits if block time is twice more/less than expected
consensus.nPowTargetSpacing = 10; // 10 seconds per block

const double blocks_per_minute = 60.0 / consensus.nPowTargetSpacing;

consensus.nEdgeBitsTargetThreshold = 2; // adjust nEdgeBits if block time is twice more/less than expected
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = true;
consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
Expand All @@ -430,10 +462,10 @@ class CRegTestParams : public CChainParams
consensus.daedalus_max_invites_per_block = 10;
consensus.daedalus_block_window = 4;
consensus.daedalus_min_one_invite_for_every_x_blocks = 1;
consensus.daedalus_max_outstanding_invites_per_address = 3;
consensus.daedalus_max_outstanding_invites_per_address = 100;

consensus.vDeployments[Consensus::DEPLOYMENT_DAEDALUS].bit = 27;
consensus.vDeployments[Consensus::DEPLOYMENT_DAEDALUS].start_block = 10;
consensus.vDeployments[Consensus::DEPLOYMENT_DAEDALUS].start_block = 5;
consensus.vDeployments[Consensus::DEPLOYMENT_DAEDALUS].end_block = std::numeric_limits<int>::max();

// The best chain should have at least this much work.
Expand All @@ -443,15 +475,27 @@ class CRegTestParams : public CChainParams
consensus.defaultAssumeValid = uint256S("0x00");

// Block where safer aliases are enabled.
consensus.safer_alias_blockheight = 20;
consensus.safer_alias_blockheight = 5;

// Block where improved invites turn on
consensus.imp_invites_blockheight = 12;
consensus.imp_invites_blockheight = 8;
consensus.imp_block_window = 4;
consensus.imp_min_one_invite_for_every_x_blocks = 1; //invite every minute
consensus.imp_miner_reward_for_every_x_blocks = 10; //invite every 10 minutes at a minumum, 144 per day.
consensus.imp_weights = {60, 40};

// PoG v2 settings.
consensus.pog2_blockheight = 10;
consensus.pog2_total_winning_ambassadors = 20;
consensus.pog2_ambassador_percent_cut = 50; //50%
consensus.pog2_pow_target_timespan = 30 * 60; // every 30 min
consensus.pog2_initial_ambassador_stake = 20_merit;
consensus.pog2_coin_maturity = blocks_per_minute * 20; //20 minutes for coin maturity
consensus.pog2_new_coin_maturity = blocks_per_minute * 10; //10 minutes for coin maturity
consensus.pog2_max_outstanding_invites_per_address = 50;
consensus.pog2_convex_b = 0.2;
consensus.pog2_convex_s = 0.05;

pchMessageStart[0] = 0xfa;
pchMessageStart[1] = 0xbf;
pchMessageStart[2] = 0xb5;
Expand Down
6 changes: 4 additions & 2 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ size_t CCoinsViewCache::DynamicMemoryUsage() const {

CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
CCoinsMap::iterator it = cacheCoins.find(outpoint);
if (it != cacheCoins.end())
if (it != cacheCoins.end()) {
return it;
}
Coin tmp;
if (!base->GetCoin(outpoint, tmp))
if (!base->GetCoin(outpoint, tmp)) {
return cacheCoins.end();
}
CCoinsMap::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
if (ret->second.coin.IsSpent()) {
// The parent only has an empty entry for this outpoint; we can consider our
Expand Down
18 changes: 17 additions & 1 deletion src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define MERIT_CONSENSUS_PARAMS_H

#include "uint256.h"
#include "amount.h"
#include <map>
#include <set>
#include <string>
Expand Down Expand Up @@ -73,7 +74,10 @@ struct Params {
int64_t nPowTargetSpacing; // target time for a block
int64_t nPowTargetTimespan; // target time for nBits adjustments
int64_t nEdgeBitsTargetThreshold; // threshold for nEdgeBits adjustments
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
int64_t DifficultyAdjustmentInterval(int height) const {
return (height >= pog2_blockheight ? pog2_pow_target_timespan : nPowTargetTimespan)
/ nPowTargetSpacing;
}
int64_t ambassador_percent_cut;
uint64_t total_winning_ambassadors;
uint64_t initial_block_reward;
Expand All @@ -99,6 +103,18 @@ struct Params {
int imp_miner_reward_for_every_x_blocks;
std::vector<double> imp_weights;

/** PoG version 2 */
int pog2_blockheight;
int64_t pog2_total_winning_ambassadors;
int64_t pog2_ambassador_percent_cut;
int64_t pog2_pow_target_timespan;
CAmount pog2_initial_ambassador_stake;
int pog2_coin_maturity;
int pog2_new_coin_maturity;
int pog2_max_outstanding_invites_per_address;
double pog2_convex_b;
double pog2_convex_s;

};
} // namespace Consensus

Expand Down
5 changes: 5 additions & 0 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ class CDBWrapper
return WriteBatch(batch, true);
}

CDBIterator *NewIterator(const leveldb::ReadOptions& options)
{
return new CDBIterator(*this, pdb->NewIterator(options));
}

CDBIterator *NewIterator()
{
return new CDBIterator(*this, pdb->NewIterator(iteroptions));
Expand Down
17 changes: 16 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,11 @@ bool AppInitLockDataDirectory()
return true;
}

namespace pog2
{
extern void SetupCgsThreadPool(size_t threads);
}

bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
{
const CChainParams& chainparams = Params();
Expand All @@ -1255,6 +1260,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
LogPrintf("Using config file %s\n", GetConfigFile(gArgs.GetArg("-conf", MERIT_CONF_FILENAME)).string());
LogPrintf("Using at most %i automatic connections (%i file descriptors available)\n", nMaxConnections, nFD);

pog2::SetupCgsThreadPool(boost::thread::hardware_concurrency());
InitSignatureCache();
InitScriptExecutionCache();

Expand Down Expand Up @@ -1434,8 +1440,12 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
nBlockTreeDBCache = std::min(nBlockTreeDBCache,
(gArgs.GetBoolArg(flags::ConvertToCliFlag(flags::txindex), DEFAULT_TXINDEX) ? nMaxBlockDBAndTxIndexCache : nMaxBlockDBCache) << 20);
}
size_t nReferralDBCache = 0; // todo: figure out how much to cache
nTotalCache -= nBlockTreeDBCache;
int64_t nReferralDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23));
nReferralDBCache = std::min(nReferralDBCache, nMaxReferralDBCache << 20); // cap total referrals db cache

nTotalCache -= nReferralDBCache;

int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
nTotalCache -= nCoinDBCache;
Expand All @@ -1446,6 +1456,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
LogPrintf("* Max cache setting possible %.1fMiB\n", nMaxDbCache);
LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for referral database\n", nReferralDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of unused mempool space and %.1fMiB of unused referrals mempool space)\n",
nCoinCacheUsage * (1.0 / 1024 / 1024),
nMempoolSizeMax * (1.0 / 1024 / 1024),
Expand Down Expand Up @@ -1558,6 +1569,10 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
}
}

LogPrintf("Caching Unspent Coins...");
pblocktree->CacheAllUnspent();
LogPrintf("Cached\n");

if (!is_coinsview_empty) {
uiInterface.InitMessage(_("Verifying blocks..."));
if (fHavePruned && gArgs.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS) > MIN_BLOCKS_TO_KEEP) {
Expand Down
Loading

0 comments on commit da2c144

Please sign in to comment.