Skip to content

Commit

Permalink
Merge #2309: [Consensus] New masternode payment logic
Browse files Browse the repository at this point in the history
6b7b9df [BUG][Test] Return directly block template without mempool txes (random-zebra)
f593706 [test] add mnsync and spork8 activation in deterministicmns_tests (furszy)
753986a [test] Add unit test coverage for invalid block payee (furszy)
a81abb4 [Trivial] Fix typo utoxs --> utxos (random-zebra)
f6aefd8 [Tests] Check masternode payments in evo_deterministicmns_tests (random-zebra)
4c731a9 [Consensus] New DMN payment logic (random-zebra)

Pull request description:

  Estracted from #2267
  This implements the new payment logic for deterministic masternodes, to be used after the deactivation of the legacy system with SPORK_21.

  Based on top of:
  - [x] #2275
  - [x] #2296

ACKs for top commit:
  furszy:
    ACK 6b7b9df

Tree-SHA512: 9328e2e2d820510845ede63e4db557ceb49df9cfb27f1ac1b36a20878c99728bc60b8f5c4cbfa00f161be5a3c5b7d72cba8ba4dc476383718f66a6d8cdddd70c
  • Loading branch information
random-zebra committed May 23, 2021
2 parents 1e40e2b + 6b7b9df commit d95f524
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 50 deletions.
14 changes: 10 additions & 4 deletions src/blockassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ bool SolveProofOfStake(CBlock* pblock, CBlockIndex* pindexPrev, CWallet* pwallet
return true;
}

bool CreateCoinbaseTx(CBlock* pblock, const CScript& scriptPubKeyIn, CBlockIndex* pindexPrev)
CMutableTransaction CreateCoinbaseTx(const CScript& scriptPubKeyIn, CBlockIndex* pindexPrev)
{
assert(pindexPrev);
const int nHeight = pindexPrev->nHeight + 1;
Expand All @@ -128,7 +128,12 @@ bool CreateCoinbaseTx(CBlock* pblock, const CScript& scriptPubKeyIn, CBlockIndex
txCoinbase.vout[0].nValue = GetBlockValue(nHeight);
}

pblock->vtx.emplace_back(MakeTransactionRef(txCoinbase));
return txCoinbase;
}

bool CreateCoinbaseTx(CBlock* pblock, const CScript& scriptPubKeyIn, CBlockIndex* pindexPrev)
{
pblock->vtx.emplace_back(MakeTransactionRef(CreateCoinbaseTx(scriptPubKeyIn, pindexPrev)));
return true;
}

Expand Down Expand Up @@ -165,7 +170,8 @@ void BlockAssembler::resetBlock()
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn,
CWallet* pwallet,
bool fProofOfStake,
std::vector<CStakeableOutput>* availableCoins)
std::vector<CStakeableOutput>* availableCoins,
bool fNoMempoolTx)
{
resetBlock();

Expand Down Expand Up @@ -194,7 +200,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
return nullptr;
}

{
if (!fNoMempoolTx) {
// Add transactions from mempool
LOCK2(cs_main,mempool.cs);
addPriorityTxs();
Expand Down
4 changes: 3 additions & 1 deletion src/blockassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class BlockAssembler
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn,
CWallet* pwallet = nullptr,
bool fProofOfStake = false,
std::vector<CStakeableOutput>* availableCoins = nullptr);
std::vector<CStakeableOutput>* availableCoins = nullptr,
bool fNoMempoolTx = false);

private:
// utility functions
Expand Down Expand Up @@ -100,6 +101,7 @@ int32_t ComputeBlockVersion(const Consensus::Params& consensusParams, int nHeigh

// Visible for testing purposes only
bool CreateCoinbaseTx(CBlock* pblock, const CScript& scriptPubKeyIn, CBlockIndex* pindexPrev);
CMutableTransaction CreateCoinbaseTx(const CScript& scriptPubKeyIn, CBlockIndex* pindexPrev);

// Visible for testing purposes only
uint256 CalculateSaplingTreeRoot(CBlock* pblock, int nHeight, const CChainParams& chainparams);
Expand Down
40 changes: 35 additions & 5 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,23 @@ std::string GetRequiredPaymentsString(int nBlockHeight)
bool CMasternodePayments::GetMasternodeTxOuts(const CBlockIndex* pindexPrev, std::vector<CTxOut>& voutMasternodePaymentsRet) const
{
if (deterministicMNManager->LegacyMNObsolete(pindexPrev->nHeight + 1)) {
// New payment logic (!TODO)
return false;
CAmount masternodeReward = GetMasternodePayment();
auto dmnPayee = deterministicMNManager->GetListForBlock(pindexPrev).GetMNPayee();
if (!dmnPayee) {
return error("%s: Failed to get payees for block at height %d", __func__, pindexPrev->nHeight + 1);
}
CAmount operatorReward = 0;
if (dmnPayee->nOperatorReward != 0 && !dmnPayee->pdmnState->scriptOperatorPayout.empty()) {
operatorReward = (masternodeReward * dmnPayee->nOperatorReward) / 10000;
masternodeReward -= operatorReward;
}
if (masternodeReward > 0) {
voutMasternodePaymentsRet.emplace_back(masternodeReward, dmnPayee->pdmnState->scriptPayout);
}
if (operatorReward > 0) {
voutMasternodePaymentsRet.emplace_back(operatorReward, dmnPayee->pdmnState->scriptOperatorPayout);
}
return true;
}

// Legacy payment logic. !TODO: remove when transition to DMN is complete
Expand All @@ -318,7 +333,6 @@ bool CMasternodePayments::GetMasternodeTxOuts(const CBlockIndex* pindexPrev, std

bool CMasternodePayments::GetLegacyMasternodeTxOut(int nHeight, std::vector<CTxOut>& voutMasternodePaymentsRet) const
{
if (nHeight == 0) return false;
voutMasternodePaymentsRet.clear();

CScript payee;
Expand Down Expand Up @@ -622,8 +636,24 @@ bool CMasternodePayments::IsTransactionValid(const CTransaction& txNew, const CB
{
const int nBlockHeight = pindexPrev->nHeight + 1;
if (deterministicMNManager->LegacyMNObsolete(nBlockHeight)) {
// !TODO
return false;
std::vector<CTxOut> vecMnOuts;
if (!GetMasternodeTxOuts(pindexPrev, vecMnOuts)) {
// No masternode scheduled to be paid.
return true;
}

for (const CTxOut& o : vecMnOuts) {
if (std::find(txNew.vout.begin(), txNew.vout.end(), o) == txNew.vout.end()) {
CTxDestination mnDest;
const std::string& payee = ExtractDestination(o.scriptPubKey, mnDest) ? EncodeDestination(mnDest)
: HexStr(o.scriptPubKey);
LogPrint(BCLog::MASTERNODE, "%s: Failed to find expected payee %s in block at height %d (tx %s)",
__func__, payee, pindexPrev->nHeight + 1, txNew.GetHash().ToString());
return false;
}
}
// all the expected payees have been found in txNew outputs
return true;
}

// Legacy payment logic. !TODO: remove when transition to DMN is complete
Expand Down
4 changes: 2 additions & 2 deletions src/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ void CSporkManager::ProcessGetSporks(CNode* pfrom, std::string& strCommand, CDat

bool CSporkManager::UpdateSpork(SporkId nSporkID, int64_t nValue)
{
CSporkMessage spork = CSporkMessage(nSporkID, nValue, GetTime());
CSporkMessage spork(nSporkID, nValue, GetTime());

if(spork.Sign(strMasterPrivKey)){
if (spork.Sign(strMasterPrivKey)) {
spork.Relay();
AddOrUpdateSporkMessage(spork);
return true;
Expand Down
Loading

0 comments on commit d95f524

Please sign in to comment.