Commit 73758ae8 authored by Volodymyr Shamray's avatar Volodymyr Shamray

Merge branch '197-prevent-submit-proposals-during-finalization' into 'Current-dev'

Resolve "Prevent users from preparing and submitting a new proposal within 1440 blocks of the superblock"

See merge request !131
parents ebb29735 05d4cc7a
......@@ -206,11 +206,11 @@ void CBudgetManager::SubmitBudgetDraft()
nCurrentHeight = chainActive.Height();
}
int nBlockStart = nCurrentHeight - nCurrentHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
if(nSubmittedHeight >= nBlockStart)
const int blockStart = GetNextSuperblock(nCurrentHeight);
if(nSubmittedHeight >= blockStart)
return;
if(nBlockStart - nCurrentHeight > BlocksBeforeSuperblockToSubmitBudgetDraft())
if(blockStart - nCurrentHeight > BlocksBeforeSuperblockToSubmitBudgetDraft())
return; // allow submitting final budget only when 2 days left before payments
std::vector<CBudgetProposal*> vBudgetProposals = budget.GetBudget();
......@@ -243,7 +243,7 @@ void CBudgetManager::SubmitBudgetDraft()
return;
}
BudgetDraftBroadcast budgetDraftBroadcast(nBlockStart, vecTxBudgetPayments, activeMasternode.vin, key2);
BudgetDraftBroadcast budgetDraftBroadcast(blockStart, vecTxBudgetPayments, activeMasternode.vin, key2);
if(mapSeenBudgetDrafts.count(budgetDraftBroadcast.GetHash())) {
LogPrintf("CBudgetManager::SubmitBudgetDraft - Budget already exists - %s\n", budgetDraftBroadcast.GetHash().ToString());
......@@ -266,7 +266,7 @@ void CBudgetManager::SubmitBudgetDraft()
}
else
{
BudgetDraftBroadcast tempBudget(nBlockStart, vecTxBudgetPayments, uint256());
BudgetDraftBroadcast tempBudget(blockStart, vecTxBudgetPayments, uint256());
if(mapSeenBudgetDrafts.count(tempBudget.GetHash())) {
LogPrintf("CBudgetManager::SubmitBudgetDraft - Budget already exists - %s\n", tempBudget.GetHash().ToString());
nSubmittedHeight = nCurrentHeight;
......@@ -324,7 +324,7 @@ void CBudgetManager::SubmitBudgetDraft()
}
//create the proposal incase we're the first to make it
BudgetDraftBroadcast budgetDraftBroadcast(nBlockStart, vecTxBudgetPayments, txidCollateral);
BudgetDraftBroadcast budgetDraftBroadcast(blockStart, vecTxBudgetPayments, txidCollateral);
std::string strError = "";
if(!budgetDraftBroadcast.IsValid(strError)){
......@@ -600,9 +600,9 @@ std::vector<CBudgetProposal*> CBudgetManager::GetBudget()
CBlockIndex* pindexPrev = chainActive.Tip();
if(pindexPrev == NULL) return vBudgetProposalsRet;
int nBlockStart = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
int nBlockEnd = nBlockStart + GetBudgetPaymentCycleBlocks() - 1;
CAmount nTotalBudget = GetTotalBudget(nBlockStart);
const int blockStart = GetNextSuperblock(pindexPrev->nHeight);
const int blockEnd = blockStart + GetBudgetPaymentCycleBlocks() - 1;
CAmount totalBudget = GetTotalBudget(blockStart);
std::vector<std::pair<CBudgetProposal*, int> >::iterator it2 = vBudgetPorposalsSort.begin();
......@@ -611,12 +611,12 @@ std::vector<CBudgetProposal*> CBudgetManager::GetBudget()
CBudgetProposal* pbudgetProposal = (*it2).first;
//prop start/end should be inside this period
if(pbudgetProposal->fValid && pbudgetProposal->nBlockStart <= nBlockStart &&
pbudgetProposal->nBlockEnd >= nBlockEnd &&
if(pbudgetProposal->fValid && pbudgetProposal->nBlockStart <= blockStart &&
pbudgetProposal->nBlockEnd >= blockEnd &&
pbudgetProposal->GetYeas() - pbudgetProposal->GetNays() > mnodeman.CountEnabled(MIN_BUDGET_PEER_PROTO_VERSION)/10 &&
pbudgetProposal->IsEstablished())
{
if(pbudgetProposal->GetAmount() + nBudgetAllocated <= nTotalBudget) {
if(pbudgetProposal->GetAmount() + nBudgetAllocated <= totalBudget) {
pbudgetProposal->SetAllotted(pbudgetProposal->GetAmount());
nBudgetAllocated += pbudgetProposal->GetAmount();
vBudgetProposalsRet.push_back(pbudgetProposal);
......
......@@ -47,6 +47,10 @@ extern CBudgetManager budget;
// Define amount of blocks in budget payment cycle
int GetBudgetPaymentCycleBlocks();
int GetNextSuperblock(int height);
CAmount GetVotingThreshold();
//Check the collateral transaction for the budget proposal/finalized budget
bool IsBudgetCollateralValid(uint256 nTxCollateralHash, uint256 nExpectedHash, std::string& strError, int64_t& nTime, int& nConf);
......
This diff is collapsed.
......@@ -50,6 +50,7 @@ public Q_SLOTS:
void updateMyNodeList(bool reset = false);
void updateNodeList();
void updateVoteList(bool reset = false);
void updateNextSuperblock();
SendCollateralDialog* getSendCollateralDialog()
{
return sendDialog;
......
......@@ -73,9 +73,6 @@ SystemnodeList::SystemnodeList(QWidget *parent) :
updateNodeList();
//CBlockIndex* pindexPrev = chainActive.Tip();
//int nNext = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
//ui->superblockLabel->setText(QString::number(nNext));
sendDialog = new SendCollateralDialog(SendCollateralDialog::SYSTEMNODE, this);
}
......
......@@ -48,8 +48,7 @@ Value mnbudget(const Array& params, bool fHelp)
CBlockIndex* pindexPrev = chainActive.Tip();
if(!pindexPrev) return "unknown";
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
return nNext;
return GetNextSuperblock(pindexPrev->nHeight);
}
if(strCommand == "prepare")
......@@ -80,8 +79,8 @@ Value mnbudget(const Array& params, bool fHelp)
int nBlockStart = params[4].get_int();
if(nBlockStart % GetBudgetPaymentCycleBlocks() != 0){
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
return strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext);
const int nextBlock = GetNextSuperblock(pindexPrev->nHeight);;
return strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nextBlock);
}
int nBlockEnd = nBlockStart + GetBudgetPaymentCycleBlocks() * nPaymentCount;
......@@ -92,6 +91,14 @@ Value mnbudget(const Array& params, bool fHelp)
if(nBlockEnd < pindexPrev->nHeight)
return "Invalid ending block, starting block + (payment_cycle*payments) must be more than current height.";
if (pindexPrev)
{
const int64_t submissionBlock = pindexPrev->nHeight + BUDGET_FEE_CONFIRMATIONS + 1;
const int64_t nextBlock = GetNextSuperblock(pindexPrev->nHeight);
if (submissionBlock >= nextBlock - GetVotingThreshold())
return "Sorry, your proposal can not be added as we're too close to the proposal payment. Please submit your proposal for the next proposal payment.";
}
CBitcoinAddress address(params[5].get_str());
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Crown address");
......@@ -153,8 +160,8 @@ Value mnbudget(const Array& params, bool fHelp)
int nBlockStart = params[4].get_int();
if(nBlockStart % GetBudgetPaymentCycleBlocks() != 0){
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % GetBudgetPaymentCycleBlocks() + GetBudgetPaymentCycleBlocks();
return strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nNext);
const int nextBlock = GetNextSuperblock(pindexPrev->nHeight);
return strprintf("Invalid block start - must be a budget cycle block. Next valid block: %d", nextBlock);
}
int nBlockEnd = nBlockStart + (GetBudgetPaymentCycleBlocks()*nPaymentCount);
......@@ -187,10 +194,6 @@ Value mnbudget(const Array& params, bool fHelp)
return "Must wait for client to sync with masternode network. Try again in a minute or so.";
}
// if(!budgetProposalBroadcast.IsValid(strError)){
// return "Proposal is not valid - " + budgetProposalBroadcast.GetHash().ToString() + " - " + strError;
// }
budget.mapSeenMasternodeBudgetProposals.insert(make_pair(budgetProposalBroadcast.GetHash(), budgetProposalBroadcast));
budgetProposalBroadcast.Relay();
budget.AddProposal(budgetProposalBroadcast);
......
......@@ -1543,3 +1543,24 @@ BOOST_FIXTURE_TEST_SUITE(SuperblockPayment, SuperblockPaymentFixture)
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE(TestGetNextSuperblock)
BOOST_AUTO_TEST_CASE(InBetween)
{
const int current = 100500;
const int expected = 129600;
BOOST_REQUIRE_EQUAL(expected, GetNextSuperblock(current));
}
BOOST_AUTO_TEST_CASE(EqualToSuperblock)
{
const int current = 129600;
const int expected = 172800;
BOOST_REQUIRE_EQUAL(expected, GetNextSuperblock(current));
}
BOOST_AUTO_TEST_SUITE_END()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment