Skip to content

Commit a64e32d

Browse files
committed
added fork check conditionals to proposal code and proposal consensus rules to connectblock
1 parent f9f4860 commit a64e32d

3 files changed

Lines changed: 82 additions & 25 deletions

File tree

src/main.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,7 +1492,9 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs,
14921492
return DoS(100, error("ConnectInputs() : nFees out of range"));
14931493
}
14941494
}
1495-
else {
1495+
1496+
// If the given transaction is coinbase then check for correct refund outputs
1497+
else if (VOTING_START >= pindexBlock->nHeight){
14961498
CBlock block;
14971499
if (!block.ReadFromDisk(pindexBlock))
14981500
return error("ConnectInputs() : ReadFromDisk for connect failed");
@@ -1612,7 +1614,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck)
16121614
else
16131615
nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK, CLIENT_VERSION) - (2 * GetSizeOfCompactSize(0)) + GetSizeOfCompactSize(vtx.size());
16141616

1615-
vector<uint256> vQueuedProposals;
1617+
vector<CTransaction> vQueuedTxProposals;
16161618
map<uint256, CTxIndex> mapQueuedChanges;
16171619
int64 nFees = 0;
16181620
int64 nValueIn = 0;
@@ -1674,11 +1676,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck)
16741676
return error("Proposal was not successfully extracted from transaction. This shouldn't happen.");
16751677
}
16761678

1677-
int nFee = CVoteProposal::BASE_FEE;
1678-
1679-
//Needs to have the proper fee or else it will not be counted
1680-
if (nTxValueIn - nTxValueOut >= nFee - MIN_TXOUT_AMOUNT)
1681-
vQueuedProposals.push_back(hashTx);
1679+
vQueuedTxProposals.push_back(tx);
16821680
}
16831681
}
16841682

@@ -1697,21 +1695,34 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck)
16971695
if (fJustCheck)
16981696
return true;
16991697

1698+
//TODO: LOG ERRORS OR TERMINATE METHOD AND RETURN ERROR?
1699+
vector<CTransaction> vOrderedTxProposals;
1700+
if(!proposalManager.GetDeterministicOrdering(pindex->pprev->hashProofOfStake, vQueuedTxProposals, vOrderedTxProposals)) {
1701+
printf("ConnectBlock() : encountered error when determining deterministic ordering of proposals.");
1702+
}
1703+
1704+
vector<CTransaction> vAcceptedTxProposals;
1705+
if(!proposalManager.GetAcceptedTxProposals(vtx[0], vOrderedTxProposals, vAcceptedTxProposals)) {
1706+
printf("ConnectBlock() : encountered error when extracting accepted proposals from coinbase");
1707+
}
1708+
17001709
// Keep track of any vote proposals that were added to the blockchain
17011710
CVoteDB voteDB;
1702-
if (vQueuedProposals.size()) {
1703-
for (const CTransaction& tx : vtx) {
1704-
uint256 txid = tx.GetHash();
1705-
if (count(vQueuedProposals.begin(), vQueuedProposals.end(), txid)) {
1706-
CVoteProposal proposal;
1707-
if (ProposalFromTransaction(tx, proposal)) {
1708-
mapProposals[txid] = proposal.GetHash();
1709-
if (!voteDB.WriteProposal(txid, proposal))
1710-
printf("%s : failed to record proposal to db\n", __func__);
1711-
else if (!proposalManager.Add(proposal))
1712-
printf("%s: failed to add proposal %s to manager\n", __func__, txid.GetHex().c_str());
1713-
}
1714-
}
1711+
for (const CTransaction& txProposal: vAcceptedTxProposals) {
1712+
1713+
// if the proposal isn't able to be extracted from transaction then skip it
1714+
CVoteProposal proposal;
1715+
if(!ProposalFromTransaction(txProposal, proposal)) {
1716+
printf("Proposal was not successfully extracted from transaction. This shouldn't happen.");
1717+
continue;
1718+
}
1719+
1720+
// store proposal hash in mapProposals and store proposal on disk for later use
1721+
mapProposals[txProposal.GetHash()] = proposal.GetHash();
1722+
if (!voteDB.WriteProposal(txProposal.GetHash(), proposal)) {
1723+
printf("%s : failed to record proposal to db\n", __func__);
1724+
} else if (!proposalManager.Add(proposal)) {
1725+
printf("%s: failed to add proposal %s to manager\n", __func__, txProposal.GetHash().GetHex().c_str());
17151726
}
17161727
}
17171728

src/voteproposalmanager.cpp

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,13 @@ bool CVoteProposalManager::CheckRefundTransaction(const std::vector<CTransaction
297297
CVoteProposal proposal;
298298
VoteLocation location;
299299

300-
// Skip this txProposal if a proposal object cannot be extracted from it
300+
// return error if a proposal object cannot be extracted from the tx
301301
if(!ProposalFromTransaction(txProposal, proposal)) {
302302
return error("CheckRefundTransaction() : Proposal was not able to be extracted from transaction.");
303303
}
304304

305305
// input variables
306-
int nTxFee = (int)MIN_TX_FEE; //TODO: MAKE THIS HIGHER
306+
int nTxFee = (int)CVoteProposal::BASE_FEE; //TODO: MAKE THIS HIGHER
307307
int nBitCount = proposal.GetBitCount();
308308
int nStartHeight = proposal.GetStartHeight();
309309
int nCheckSpan = proposal.GetCheckSpan();
@@ -312,7 +312,7 @@ bool CVoteProposalManager::CheckRefundTransaction(const std::vector<CTransaction
312312
if(!GetNextLocation(nBitCount, nStartHeight, nCheckSpan, location)) {
313313
AddRefundToCoinBase(proposal, nRequiredFee, nTxFee, false, txExpectedCoinBase);
314314
} else {
315-
// If a fee cannot be calculated then skip this proposal without creating a refund tx
315+
// If a fee cannot be calculated then return error
316316
proposal.SetLocation(location);
317317
if (!GetFee(proposal, nRequiredFee)) {
318318
return error("CheckRefundTransaction() : Calculating fee for proposal failed.");
@@ -330,11 +330,55 @@ bool CVoteProposalManager::CheckRefundTransaction(const std::vector<CTransaction
330330

331331
for(int i = 0; i < txCoinBase.vout.size(); i++) {
332332
if(txCoinBase.vout.at(i).scriptPubKey.GetID().GetHex() != txExpectedCoinBase.vout.at(i).scriptPubKey.GetID().GetHex()) {
333-
return error("CheckRefundTransaction() : The scriptPubKey of the refund transaction isn't what it should be.");
333+
return error("CheckRefundTransaction() : The scriptPubKey of the refund transaction isn't what it should be"
334+
"according to the deterministic ordering.");
334335
}
335336

336337
if(txCoinBase.vout.at(i).nValue != txExpectedCoinBase.vout.at(i).nValue) {
337-
return error("CheckRefundTransaction() : The value of the refund isn't what it should be.");
338+
return error("CheckRefundTransaction() : The value of the refund isn't what it should be according to the"
339+
"deterministic ordering.");
340+
}
341+
}
342+
343+
return true;
344+
}
345+
346+
bool CVoteProposalManager::GetAcceptedTxProposals(const CTransaction& txCoinBase, const std::vector<CTransaction>& vOrderedTxProposals,
347+
std::vector<CTransaction>& vAcceptedTxProposals)
348+
{
349+
if (!txCoinBase.IsCoinBase()) {
350+
return error("GetAcceptedTxProposals() : Given transaction is not a coinbase.");
351+
}
352+
353+
vAcceptedTxProposals.clear();
354+
355+
for(auto txProposal: vOrderedTxProposals) {
356+
// output variables
357+
int nRequiredFee;
358+
CVoteProposal proposal;
359+
VoteLocation location;
360+
361+
// return error if a proposal object cannot be extracted from the tx
362+
if(!ProposalFromTransaction(txProposal, proposal)) {
363+
return error("GetAcceptedTxProposals() : Proposal was not able to be extracted from transaction.");
364+
}
365+
366+
// input variables
367+
int nBitCount = proposal.GetBitCount();
368+
int nStartHeight = proposal.GetStartHeight();
369+
int nCheckSpan = proposal.GetCheckSpan();
370+
371+
if(GetNextLocation(nBitCount, nStartHeight, nCheckSpan, location)){
372+
// If a fee cannot be calculated then return error
373+
proposal.SetLocation(location);
374+
if (!GetFee(proposal, nRequiredFee)) {
375+
return error("GetAcceptedTxProposals() : Calculating fee for proposal failed.");
376+
}
377+
378+
// If the max fee provided by the txProposal exceeds the required fee the accept the tx as a valid proposal
379+
if (nRequiredFee >= proposal.GetMaxFee()) {
380+
vAcceptedTxProposals.emplace_back(txProposal);
381+
}
338382
}
339383
}
340384

src/voteproposalmanager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class CVoteProposalManager
4444
bool AddRefundToCoinBase(const CVoteProposal &proposal, const int &nRequiredFee, const int &nTxFee,
4545
const bool bProposalAccepted, CTransaction &txCoinBase);
4646
bool CheckRefundTransaction(const std::vector<CTransaction> &vOrderedTxProposals, const CTransaction &txCoinBase);
47+
bool GetAcceptedTxProposals(const CTransaction& txCoinBase, const std::vector<CTransaction>& vOrderedTxProposals,
48+
std::vector<CTransaction>& vAcceptedTxProposals);
4749

4850
std::map<uint256, CProposalMetaData> GetAllProposals() const { return mapProposalData; };
4951
bool CheckProposal (const CVoteProposal& proposal);

0 commit comments

Comments
 (0)