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

Revert change to stake modifier #1566

Merged
merged 2 commits into from
Nov 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblock->deprecatedHeight = pindexPrev->nHeight + 1;
pblock->nBits = pos::GetNextWorkRequired(pindexPrev, pblock->nTime, consensus);
if (myIDs) {
const CKeyID key = nHeight >= consensus.GrandCentralHeight ? nodePtr->ownerAuthAddress : myIDs->first;
pblock->stakeModifier = pos::ComputeStakeModifier(pindexPrev->stakeModifier, key);
pblock->stakeModifier = pos::ComputeStakeModifier(pindexPrev->stakeModifier, myIDs->first);
}

pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
Expand Down
10 changes: 0 additions & 10 deletions src/pos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ bool CheckStakeModifier(const CBlockIndex* pindexPrev, const CBlockHeader& block
return error("%s: Can't extract minter key\n", __func__);
}

if (pindexPrev->nHeight + 1 >= Params().GetConsensus().GrandCentralHeight) {
auto nodeId = pcustomcsview->GetMasternodeIdByOperator(key);
if (!nodeId) {
return error("%s: No master operator found with minter key\n", __func__);
}
auto nodePtr = pcustomcsview->GetMasternode(*nodeId);
assert(nodePtr);
key = nodePtr->ownerAuthAddress;
}

return blockHeader.stakeModifier == pos::ComputeStakeModifier(pindexPrev->stakeModifier, key);
}

Expand Down
34 changes: 6 additions & 28 deletions src/test/pos_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ std::shared_ptr<CBlock> Block( const uint256& prev_hash, const uint64_t& height,
return pblock;
}

std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock, const CKey& minterKey, const uint256& prevStakeModifier, const CKeyID& modifierKey)
std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock, const uint256& masternodeID, const CKey& minterKey, const uint256& prevStakeModifier)
{
static uint64_t time = Params().GenesisBlock().nTime;
pblock->stakeModifier = pos::ComputeStakeModifier(prevStakeModifier, modifierKey);
pblock->stakeModifier = pos::ComputeStakeModifier(prevStakeModifier, minterKey.GetPubKey().GetID());
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
pblock->nTime = time + 10;
BOOST_CHECK(!pos::SignPosBlock(pblock, minterKey));

return pblock;
}

Expand Down Expand Up @@ -89,35 +88,14 @@ BOOST_AUTO_TEST_CASE(check_stake_modifier)

std::shared_ptr<CBlock> correctBlock = FinalizeBlock(
Block(Params().GenesisBlock().GetHash(), height, mintedBlocks),
masternodeID,
minterKey,
prevStakeModifier,
minterKey.GetPubKey().GetID());
prevStakeModifier);
BOOST_CHECK(pos::CheckStakeModifier(::ChainActive().Tip(), *(CBlockHeader*)correctBlock.get()));

correctBlock->SetNull();
correctBlock->hashPrevBlock = prev_hash;
BOOST_CHECK(!pos::CheckStakeModifier(::ChainActive().Tip(), *(CBlockHeader*)correctBlock.get()));

// Create masternode
const auto mnID = uint256S(std::string(64, 1));
CKey newMinterKey;
newMinterKey.MakeNewKey(true);
CMasternode masternode;
masternode.operatorType = 1;
masternode.ownerType = 1;
masternode.ownerAuthAddress = CKeyID{uint160{std::vector<unsigned char>(20, '0')}};
masternode.operatorAuthAddress = newMinterKey.GetPubKey().GetID();
BOOST_CHECK(pcustomcsview->CreateMasternode(mnID, masternode, 0));

// Check stake modifier calculated on owner address after fork
auto blockTip = *::ChainActive().Tip();
blockTip.nHeight = Params().GetConsensus().GrandCentralHeight;
const auto newModifierBlock = FinalizeBlock(
Block(blockTip.GetBlockHash(), blockTip.nHeight, blockTip.mintedBlocks),
newMinterKey,
blockTip.stakeModifier,
masternode.ownerAuthAddress);
BOOST_CHECK(pos::CheckStakeModifier(&blockTip, static_cast<CBlockHeader>(*newModifierBlock)));
}

BOOST_AUTO_TEST_CASE(check_header_signature)
Expand All @@ -138,9 +116,9 @@ BOOST_AUTO_TEST_CASE(check_header_signature)

FinalizeBlock(
block,
masternodeID,
minterKey,
prev_hash,
minterKey.GetPubKey().GetID());
prev_hash);

BOOST_CHECK(pos::CheckHeaderSignature(*(CBlockHeader*)block.get()));

Expand Down
6 changes: 3 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2648,9 +2648,9 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
__func__, nodeId->ToString(), nodePtr->mintedBlocks + 1, block.mintedBlocks), REJECT_INVALID, "bad-minted-blocks");
}
uint256 stakeModifierPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->stakeModifier;
const CKeyID key = pindex->nHeight >= chainparams.GetConsensus().GrandCentralHeight ? nodePtr->ownerAuthAddress : nodePtr->operatorAuthAddress;
const auto stakeModifier = pos::ComputeStakeModifier(stakeModifierPrevBlock, key);
if (block.stakeModifier != stakeModifier) { return state.Invalid(
const auto stakeModifier = pos::ComputeStakeModifier(stakeModifierPrevBlock, nodePtr->operatorAuthAddress);
if (block.stakeModifier != stakeModifier) {
return state.Invalid(
ValidationInvalidReason::CONSENSUS,
error("%s: block's stake Modifier should be %d, got %d!",
__func__, block.stakeModifier.ToString(), pos::ComputeStakeModifier(stakeModifierPrevBlock, nodePtr->operatorAuthAddress).ToString()),
Expand Down