Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix chain split #380

Merged
merged 1 commit into from
May 12, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ DEFI_TESTS =\
test/multisig_tests.cpp \
test/net_tests.cpp \
test/netbase_tests.cpp \
test/mn_blocktime_tests.cpp \
test/oracles_tests.cpp \
test/pmt_tests.cpp \
test/policyestimator_tests.cpp \
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void CMasternodesView::SetMasternodeLastBlockTime(const CKeyID & minter, const u
WriteBy<Staker>(MNBlockTimeKey{*nodeId, blockHeight}, time);
}

boost::optional<int64_t> CMasternodesView::GetMasternodeLastBlockTime(const CKeyID & minter)
boost::optional<int64_t> CMasternodesView::GetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t height)
{
auto nodeId = GetMasternodeIdByOperator(minter);
assert(nodeId);
Expand All @@ -398,7 +398,7 @@ boost::optional<int64_t> CMasternodesView::GetMasternodeLastBlockTime(const CKey

// Get first result only and exit
return false;
}, MNBlockTimeKey{*nodeId, std::numeric_limits<uint32_t>::max()});
}, MNBlockTimeKey{*nodeId, height - 1});

if (time)
{
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class CMasternodesView : public virtual CStorageView
Res UnResignMasternode(uint256 const & nodeId, uint256 const & resignTx);

void SetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t &blockHeight, const int64_t &time);
boost::optional<int64_t> GetMasternodeLastBlockTime(const CKeyID & minter);
boost::optional<int64_t> GetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t height);
void EraseMasternodeLastBlockTime(const uint256 &minter, const uint32_t& blockHeight);

void ForEachMinterNode(std::function<bool(MNBlockTimeKey const &, CLazySerialize<int64_t>)> callback, MNBlockTimeKey const & start = {});
Expand Down
7 changes: 6 additions & 1 deletion src/masternodes/rpc_masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ UniValue mnToJSON(uint256 const & nodeId, CMasternode const& node, bool verbose)
obj.pushKV("banTx", node.banTx.GetHex());
obj.pushKV("state", CMasternode::GetHumanReadableState(node.GetState()));
obj.pushKV("mintedBlocks", (uint64_t) node.mintedBlocks);
obj.pushKV("targetMultiplier", pos::CalcCoinDayWeight(Params().GetConsensus(), node, GetTime()).getdouble());
int height;
{
LOCK(cs_main);
height = ChainActive().Height();
}
obj.pushKV("targetMultiplier", pos::CalcCoinDayWeight(Params().GetConsensus(), node, height, GetTime()).getdouble());

/// @todo add unlock height and|or real resign height
ret.pushKV(nodeId.GetHex(), obj);
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ namespace pos {
pblock->height = tip->nHeight + 1;
pblock->mintedBlocks = mintedBlocks + 1;
pblock->stakeModifier = pos::ComputeStakeModifier(tip->stakeModifier, args.minterKey.GetPubKey().GetID());
auto stakerBlockTime = pcustomcsview->GetMasternodeLastBlockTime(args.operatorID);
auto stakerBlockTime = pcustomcsview->GetMasternodeLastBlockTime(args.operatorID, pblock->height);

// No record. No stake blocks or post-fork createmastnode TX, use fork time.
if (!stakerBlockTime)
Expand Down
6 changes: 3 additions & 3 deletions src/pos_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace pos {
return Hash(ss.begin(), ss.end());
}

arith_uint256 CalcCoinDayWeight(const Consensus::Params& params, const CMasternode& node, const int64_t coinstakeTime, const int64_t stakersBlockTime)
arith_uint256 CalcCoinDayWeight(const Consensus::Params& params, const CMasternode& node, const int64_t coinstakeTime, const int64_t height, const int64_t stakersBlockTime)
{
// Default to min age
int64_t nTimeTx{params.pos.nStakeMinAge};
Expand All @@ -29,7 +29,7 @@ namespace pos {
else
{
// Lookup stored valid blocktime. no optional value indicate no previous staked block.
if (auto lastBlockTime = pcustomcsview->GetMasternodeLastBlockTime(node.operatorAuthAddress))
if (auto lastBlockTime = pcustomcsview->GetMasternodeLastBlockTime(node.operatorAuthAddress, height))
{
// Choose whatever is smaller, time since last stake or max age.
nTimeTx = std::min(coinstakeTime - *lastBlockTime, params.pos.nStakeMaxAge);
Expand Down Expand Up @@ -70,7 +70,7 @@ namespace pos {
return false;
}

arith_uint256 coinDayWeight = CalcCoinDayWeight(params, *node, coinstakeTime, stakersBlockTime);
arith_uint256 coinDayWeight = CalcCoinDayWeight(params, *node, coinstakeTime, height, stakersBlockTime);

// Increase target by coinDayWeight.
return (hashProofOfStake / static_cast<uint64_t>( GetMnCollateralAmount( static_cast<int>(height) ) ) ) <= targetProofOfStake * coinDayWeight;
Expand Down
2 changes: 1 addition & 1 deletion src/pos_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace pos {
uint256 CalcKernelHash(const uint256& stakeModifier, int64_t height, int64_t coinstakeTime, const uint256& masternodeID, const Consensus::Params& params);

// Calculate target multiplier
arith_uint256 CalcCoinDayWeight(const Consensus::Params& params, const CMasternode& node, const int64_t coinstakeTime, const int64_t stakersBlockTime = 0);
arith_uint256 CalcCoinDayWeight(const Consensus::Params& params, const CMasternode& node, const int64_t coinstakeTime, const int64_t height, const int64_t stakersBlockTime = 0);

/// Check whether stake kernel meets hash target
bool CheckKernelHash(const uint256& stakeModifier, uint32_t nBits, int64_t height, int64_t coinstakeTime, uint64_t blockHeight, const uint256& masternodeID, const Consensus::Params& params, const int64_t stakersBlockTime = 0);
Expand Down
43 changes: 43 additions & 0 deletions src/test/mn_blocktime_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <test/setup_common.h>

#include <masternodes/masternodes.h>

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(mn_blocktime_tests, TestingSetup)

BOOST_AUTO_TEST_CASE(retrieve_last_time)
{
CCustomCSView mnview(*pcustomcsview.get());

// Create masternode
CMasternode mn;
std::vector<unsigned char> vec(20, '1');
uint160 bytes{vec};
CKeyID minter(bytes);
mn.operatorType = 1;
mn.ownerType = 1;
mn.operatorAuthAddress = minter;
mn.ownerAuthAddress = minter;
uint256 mnId = uint256S("1111111111111111111111111111111111111111111111111111111111111111");
mnview.CreateMasternode(mnId, mn);

// Add time records
mnview.SetMasternodeLastBlockTime(minter, 100, 1000);
mnview.SetMasternodeLastBlockTime(minter, 200, 2000);
mnview.SetMasternodeLastBlockTime(minter, 300, 3000);

// Make sure result is found and returns result previous to 200
const auto time200 = mnview.GetMasternodeLastBlockTime(minter, 200);
BOOST_CHECK_EQUAL(*time200, 1000);

// Make sure result is found and returns result previous to 200
const auto time300 = mnview.GetMasternodeLastBlockTime(minter, 300);
BOOST_CHECK_EQUAL(*time300, 2000);

// For max value we expect the last result
const auto timeMax = mnview.GetMasternodeLastBlockTime(minter, std::numeric_limits<uint32_t>::max());
BOOST_CHECK_EQUAL(*timeMax, 3000);
}

BOOST_AUTO_TEST_SUITE_END()