Skip to content

Commit

Permalink
Static reward RC (#328)
Browse files Browse the repository at this point in the history
* static rewards added

* initial test and test util added

* added new test to rpc test file

* test updated

* adding sleep print

* removed extra bracket

* updated perms.

* cleanup test

* checking before the soft fork

* creating a few blocks to start

* removing pre check
  • Loading branch information
proletesseract authored and alex v committed Dec 2, 2018
1 parent 7730c7b commit 9601f85
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 17 deletions.
1 change: 1 addition & 0 deletions qa/pull-tester/rpc-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
'cfund-rawtx-proposal-vote.py',
'cfund-vote.py',
'reject-version-bit.py',
'staticr-staking-amount.py',

]
#if ENABLE_ZMQ:
Expand Down
46 changes: 46 additions & 0 deletions qa/rpc-tests/staticr-staking-amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# Copyright (c) 2018 The Navcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import NavCoinTestFramework
from test_framework.staticr_util import *

import time

class StaticRAmountTest(NavCoinTestFramework):
"""Tests the staking amount after softfork activation."""

def __init__(self):
super().__init__()
self.setup_clean_chain = True
self.num_nodes = 1

def setup_network(self, split=False):
self.nodes = self.setup_nodes()
self.is_network_split = split

def run_test(self):

#check the block reward is 2 NAV after the softfork

activate_staticr(self.nodes[0])

blockcount = self.nodes[0].getblockcount()
wallet_info1 = self.nodes[0].getwalletinfo()

# wait for a new block to be mined
while self.nodes[0].getblockcount() == blockcount:
print("waiting for a new block...")
time.sleep(5)

assert(blockcount != self.nodes[0].getblockcount())

wallet_info2 = self.nodes[0].getwalletinfo()
balance_diff = wallet_info1['balance'] - wallet_info2['balance']

# check that only 2 new NAV were created
assert(wallet_info2['immature_balance'] == wallet_info1['immature_balance'] + balance_diff + 2)

if __name__ == '__main__':
StaticRAmountTest().main()
30 changes: 30 additions & 0 deletions qa/rpc-tests/test_framework/staticr_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.


#
# Expanded helper routines for regression testing of the NAV Coin community fund
#

from test_framework.util import *


def activate_staticr(node):
slow_gen(node, 100)
# Verify the Community Fund is started
assert (get_bip9_status(node, "static")["status"] == "started")

slow_gen(node, 100)
# Verify the Community Fund is locked_in
assert (get_bip9_status(node, "static")["status"] == "locked_in")

slow_gen(node, 100)
# Verify the Community Fund is active
assert (get_bip9_status(node, "static")["status"] == "active")

def end_cycle(node):
# Move to the end of the cycle
slow_gen(node, node.cfundstats()["votingPeriod"]["ending"] - node.cfundstats()["votingPeriod"][
"current"])
22 changes: 22 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class CMainParams : public CChainParams {
consensus.nPaymentRequestMaxVersion = 2;
consensus.nProposalMaxVersion = 2;
consensus.nMaxFutureDrift = 60;
consensus.nStaticReward = 2 * COIN;

consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
Expand Down Expand Up @@ -171,6 +172,10 @@ class CMainParams : public CChainParams {
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nStartTime = 1533081600; // Aug 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nTimeout = 1564617600; // Aug 1st, 2019

// Deployment of Static Reward
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].bit = 15;
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nStartTime = 1533081600; // August 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nTimeout = 1564617600; // August 1st, 2019

/**
* The message start string is designed to be unlikely to occur in normal data.
Expand Down Expand Up @@ -295,6 +300,7 @@ class CTestNetParams : public CChainParams {
consensus.nPaymentRequestMaxVersion = 2;
consensus.nProposalMaxVersion = 2;
consensus.nMaxFutureDrift = 60;
consensus.nStaticReward = 2 * COIN;

// Deployment of BIP68, BIP112, and BIP113.
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
Expand Down Expand Up @@ -331,6 +337,11 @@ class CTestNetParams : public CChainParams {
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nStartTime = 1533081600; // Aug 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nTimeout = 1564617600; // Aug 1st, 2019

// Deployment of Static Reward
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].bit = 15;
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nStartTime = 1533081600; // August 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nTimeout = 1564617600; // August 1st, 2019

/**
* 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 @@ -447,6 +458,7 @@ class CDevNetParams : public CChainParams {
consensus.nPaymentRequestMaxVersion = 2;
consensus.nProposalMaxVersion = 2;
consensus.nMaxFutureDrift = 60000;
consensus.nStaticReward = 2 * COIN;

// Deployment of BIP68, BIP112, and BIP113.
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
Expand Down Expand Up @@ -483,6 +495,10 @@ class CDevNetParams : public CChainParams {
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nStartTime = 1533081600; // Aug 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nTimeout = 1564617600; // Aug 1st, 2019

// Deployment of Static Reward
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].bit = 15;
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nStartTime = 1533081600; // August 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nTimeout = 1564617600; // August 1st, 2019

/**
* The message start string is designed to be unlikely to occur in normal data.
Expand Down Expand Up @@ -610,6 +626,7 @@ class CRegTestParams : public CChainParams {
consensus.nPaymentRequestMaxVersion = 2;
consensus.nProposalMaxVersion = 2;
consensus.nMaxFutureDrift = 60000;
consensus.nStaticReward = 2 * COIN;

// Deployment of BIP68, BIP112, and BIP113.
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
Expand Down Expand Up @@ -646,6 +663,11 @@ class CRegTestParams : public CChainParams {
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nStartTime = 1533081600; // Aug 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2].nTimeout = 1564617600; // Aug 1st, 2019

// Deployment of Static Reward
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].bit = 15;
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nStartTime = 1533081600; // August 1st, 2018
consensus.vDeployments[Consensus::DEPLOYMENT_STATIC_REWARD].nTimeout = 1564617600; // August 1st, 2019

/**
* 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
2 changes: 2 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum DeploymentPos
DEPLOYMENT_COMMUNITYFUND_ACCUMULATION_SPREAD,
DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2,
DEPLOYMENT_NTPSYNC,
DEPLOYMENT_STATIC_REWARD,
MAX_VERSION_BITS_DEPLOYMENTS
};

Expand Down Expand Up @@ -99,6 +100,7 @@ struct Params {
int64_t sigActivationTime;
int64_t nCoinbaseTimeActivationHeight;
int64_t nMaxFutureDrift;
CAmount nStaticReward;

int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
};
Expand Down
64 changes: 47 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,9 @@ int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Para
if(IsCommunityFundAmountV2Enabled(pindexPrev,Params().GetConsensus()))
nVersion |= nCFundAmountV2Mask;

if(IsStaticRewardEnabled(pindexPrev,Params().GetConsensus()))
nVersion |= nStaticRewardVersionMask;

return nVersion;
}

Expand Down Expand Up @@ -2917,6 +2920,11 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin

}

if(IsStaticRewardEnabled(pindex->pprev, Params().GetConsensus()) && nStakeReward != Params().GetConsensus().nStaticReward)
return state.DoS(100, error("ConnectBlock(): block has incorrect block reward (actual=%d vs consensus=%d)",
nStakeReward, Params().GetConsensus().nStaticReward),
REJECT_INVALID, "bad-static-stake-amount");

}

std::vector<CScriptCheck> vChecks;
Expand Down Expand Up @@ -4648,6 +4656,18 @@ bool IsCommunityFundLocked(const CBlockIndex* pindexPrev, const Consensus::Param
return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_COMMUNITYFUND, versionbitscache) == THRESHOLD_LOCKED_IN);
}

bool IsStaticRewardLocked(const CBlockIndex* pindexPrev, const Consensus::Params& params)
{
LOCK(cs_main);
return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_STATIC_REWARD, versionbitscache) == THRESHOLD_LOCKED_IN);
}

bool IsStaticRewardEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
{
LOCK(cs_main);
return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_STATIC_REWARD, versionbitscache) == THRESHOLD_ACTIVE);
}

// Compute at which vout of the block's coinbase transaction the witness
// commitment occurs, or -1 if not found.
static int GetWitnessCommitmentIndex(const CBlock& block)
Expand Down Expand Up @@ -4750,6 +4770,10 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
"rejected no nsync block");

if((block.nVersion & nStaticRewardVersionMask) != nStaticRewardVersionMask && IsStaticRewardEnabled(pindexPrev,Params().GetConsensus()))
return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
"rejected no static reward block");

return true;
}

Expand Down Expand Up @@ -8638,23 +8662,29 @@ bool CheckKernel(CBlockIndex* pindexPrev, unsigned int nBits, int64_t nTime, con
// staker's coin stake reward based on coin age spent (coin-days)
int64_t GetProofOfStakeReward(int nHeight, int64_t nCoinAge, int64_t nFees, CBlockIndex* pindexPrev)
{
int64_t nRewardCoinYear;
nRewardCoinYear = MAX_MINT_PROOF_OF_STAKE;

if(nHeight-1 < 7 * Params().GetConsensus().nDailyBlockCount)
nRewardCoinYear = 1 * MAX_MINT_PROOF_OF_STAKE;
else if(nHeight-1 < (365 * Params().GetConsensus().nDailyBlockCount))
nRewardCoinYear = 0.5 * MAX_MINT_PROOF_OF_STAKE;
else if(nHeight-1 < (730 * Params().GetConsensus().nDailyBlockCount))
nRewardCoinYear = 0.5 * MAX_MINT_PROOF_OF_STAKE;
else if(IsCommunityFundAccumulationEnabled(pindexPrev, Params().GetConsensus(), false))
nRewardCoinYear = 0.4 * MAX_MINT_PROOF_OF_STAKE;
else
nRewardCoinYear = 0.5 * MAX_MINT_PROOF_OF_STAKE;

int64_t nSubsidy = nCoinAge * nRewardCoinYear / 365;

return nSubsidy + nFees;
int64_t nSubsidy;

if(IsStaticRewardEnabled(pindexPrev, Params().GetConsensus())){
nSubsidy = Params().GetConsensus().nStaticReward;
} else {
int64_t nRewardCoinYear;
nRewardCoinYear = MAX_MINT_PROOF_OF_STAKE;

if(nHeight-1 < 7 * Params().GetConsensus().nDailyBlockCount)
nRewardCoinYear = 1 * MAX_MINT_PROOF_OF_STAKE;
else if(nHeight-1 < (365 * Params().GetConsensus().nDailyBlockCount))
nRewardCoinYear = 0.5 * MAX_MINT_PROOF_OF_STAKE;
else if(nHeight-1 < (730 * Params().GetConsensus().nDailyBlockCount))
nRewardCoinYear = 0.5 * MAX_MINT_PROOF_OF_STAKE;
else if(IsCommunityFundAccumulationEnabled(pindexPrev, Params().GetConsensus(), false))
nRewardCoinYear = 0.4 * MAX_MINT_PROOF_OF_STAKE;
else
nRewardCoinYear = 0.5 * MAX_MINT_PROOF_OF_STAKE;

nSubsidy = nCoinAge * nRewardCoinYear / 365;
}

return nSubsidy + nFees;
}

unsigned int ComputeMaxBits(arith_uint256 bnTargetLimit, unsigned int nBase, int64_t nTime)
Expand Down
3 changes: 3 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ bool IsCommunityFundAccumulationEnabled(const CBlockIndex* pindexPrev, const Con
bool IsCommunityFundAccumulationSpreadEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params);
bool IsCommunityFundAmountV2Enabled(const CBlockIndex* pindexPrev, const Consensus::Params& params);

/** Check whether the static reward has been activated **/
bool IsStaticRewardEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params);
bool IsStaticRewardLocked(const CBlockIndex* pindexPrev, const Consensus::Params& params);

/** Check whether NtpSync has been activated. */
bool IsNtpSyncEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params);
Expand Down
1 change: 1 addition & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,7 @@ UniValue getblockchaininfo(const UniValue& params, bool fHelp)
BIP9SoftForkDescPushBack(bip9_softforks, "ntpsync", consensusParams, Consensus::DEPLOYMENT_NTPSYNC);
BIP9SoftForkDescPushBack(bip9_softforks, "spread_cfund_accumulation", consensusParams, Consensus::DEPLOYMENT_COMMUNITYFUND_ACCUMULATION_SPREAD);
BIP9SoftForkDescPushBack(bip9_softforks, "communityfund_amount_v2", consensusParams, Consensus::DEPLOYMENT_COMMUNITYFUND_AMOUNT_V2);
BIP9SoftForkDescPushBack(bip9_softforks, "static", consensusParams, Consensus::DEPLOYMENT_STATIC_REWARD);
obj.push_back(Pair("softforks", softforks));
obj.push_back(Pair("bip9_softforks", bip9_softforks));

Expand Down
1 change: 1 addition & 0 deletions src/versionbits.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static const int32_t nNSyncVersionMask = 0x00000080;
static const int32_t nCFundAccVersionMask = 0x00000100;
static const int32_t nCFundAccSpreadVersionMask = 0x00004000;
static const int32_t nCFundAmountV2Mask = 0x00010000;
static const int32_t nStaticRewardVersionMask = 0x00008000;

enum ThresholdState {
THRESHOLD_DEFINED,
Expand Down

0 comments on commit 9601f85

Please sign in to comment.