Skip to content

Commit

Permalink
Correctly handle spent collaterals for MNs that were registered in th…
Browse files Browse the repository at this point in the history
…e same block (#2553)

* Move spent collateral handling to the bottom of BuildNewListFromBlock

* Handle conflicts with spent collaterals in mempool
  • Loading branch information
codablock committed Dec 13, 2018
1 parent 18cd596 commit 1522656
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
31 changes: 18 additions & 13 deletions src/evo/deterministicmns.cpp
Expand Up @@ -535,19 +535,6 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const C
for (int i = 1; i < (int)block.vtx.size(); i++) {
const CTransaction& tx = *block.vtx[i];

// check if any existing MN collateral is spent by this transaction
for (const auto& in : tx.vin) {
auto dmn = newList.GetMNByCollateral(in.prevout);
if (dmn && dmn->collateralOutpoint == in.prevout) {
newList.RemoveMN(dmn->proTxHash);

if (debugLogs) {
LogPrintf("CDeterministicMNManager::%s -- MN %s removed from list because collateral was spent. collateralOutpoint=%s, nHeight=%d, mapCurMNs.allMNsCount=%d\n",
__func__, dmn->proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort(), nHeight, newList.GetAllMNsCount());
}
}
}

if (tx.nVersion != 3) {
// only interested in special TXs
continue;
Expand Down Expand Up @@ -709,6 +696,24 @@ bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const C
}
}

// we skip the coinbase
for (int i = 1; i < (int)block.vtx.size(); i++) {
const CTransaction& tx = *block.vtx[i];

// check if any existing MN collateral is spent by this transaction
for (const auto& in : tx.vin) {
auto dmn = newList.GetMNByCollateral(in.prevout);
if (dmn && dmn->collateralOutpoint == in.prevout) {
newList.RemoveMN(dmn->proTxHash);

if (debugLogs) {
LogPrintf("CDeterministicMNManager::%s -- MN %s removed from list because collateral was spent. collateralOutpoint=%s, nHeight=%d, mapCurMNs.allMNsCount=%d\n",
__func__, dmn->proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort(), nHeight, newList.GetAllMNsCount());
}
}
}
}

// The payee for the current block was determined by the previous block's list but it might have disappeared in the
// current block. We still pay that MN one last time however.
if (payee && newList.HasMN(payee->proTxHash)) {
Expand Down
12 changes: 10 additions & 2 deletions src/txmempool.cpp
Expand Up @@ -1280,8 +1280,16 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
}
if (mapProTxAddresses.count(proTx.addr) || mapProTxPubKeyIDs.count(proTx.keyIDOwner) || mapProTxBlsPubKeyHashes.count(proTx.pubKeyOperator.GetHash()))
return true;
if (!proTx.collateralOutpoint.hash.IsNull() && mapProTxCollaterals.count(proTx.collateralOutpoint))
return true;
if (!proTx.collateralOutpoint.hash.IsNull()) {
if (mapProTxCollaterals.count(proTx.collateralOutpoint)) {
// there is another ProRegTx that refers to the same collateral
return true;
}
if (mapNextTx.count(proTx.collateralOutpoint)) {
// there is another tx that spends the collateral
return true;
}
}
return false;
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
CProUpServTx proTx;
Expand Down

0 comments on commit 1522656

Please sign in to comment.