-
Notifications
You must be signed in to change notification settings - Fork 122
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
Prevent transactions in blocks from going below balance. #136
Changes from 8 commits
2d421fc
6e674f5
f0aa70a
da1658d
2ab5eb7
4bfda3e
932a670
26cd1eb
97cc3b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
#include <txmempool.h> | ||
|
||
#include <chainparams.h> | ||
#include <consensus/consensus.h> | ||
#include <consensus/tx_verify.h> | ||
#include <consensus/validation.h> | ||
|
@@ -577,6 +578,49 @@ void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigne | |
removeConflicts(*tx); | ||
ClearPrioritisation(tx->GetHash()); | ||
} | ||
|
||
bool accountConflict{false}; | ||
|
||
// Check if any custom TXs are in mempool with conflict | ||
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); ++it) { | ||
std::vector<unsigned char> metadata; | ||
CustomTxType txType = GuessCustomTxType(it->GetTx(), metadata); | ||
if (NotAllowedToFail(txType)) { | ||
auto res = ApplyCustomTx(*pcustomcsview, g_chainstate->CoinsTip(), it->GetTx(), Params().GetConsensus(), nBlockHeight, 0, true); | ||
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) { | ||
accountConflict = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Account conflict, check entire mempool | ||
if (accountConflict) { | ||
std::set<CTransactionRef> txsToRemove; | ||
CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&::ChainstateActive().CoinsTip())); | ||
CAmount txfee = 0; | ||
|
||
// Check custom TX consensus types are now not in conflict with account layer | ||
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); ++it) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
CValidationState state; | ||
if (!Consensus::CheckTxInputs(it->GetTx(), state, mempoolDuplicate, pcustomcsview.get(), nBlockHeight, txfee, Params())) { | ||
LogPrintf("%s: Remove conflicting TX: %s\n", __func__, it->GetTx().GetHash().GetHex()); | ||
txsToRemove.insert(it->GetSharedTx()); | ||
} | ||
} | ||
|
||
for (auto& tx : txsToRemove) { | ||
txiter it = mapTx.find(tx->GetHash()); | ||
if (it != mapTx.end()) { | ||
setEntries stage; | ||
stage.insert(it); | ||
RemoveStaged(stage, true, MemPoolRemovalReason::CONFLICT); | ||
} | ||
removeConflicts(*tx); | ||
ClearPrioritisation(tx->GetHash()); | ||
} | ||
} | ||
|
||
lastRollingFeeUpdate = GetTime(); | ||
blockSinceLastRollingFeeBump = true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2014-2019 The Bitcoin Core developers | ||
# Copyright (c) DeFi Blockchain Developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
"""Test account mining behaviour""" | ||
|
||
from test_framework.test_framework import DefiTestFramework | ||
from test_framework.util import assert_equal | ||
|
||
class AccountMiningTest(DefiTestFramework): | ||
def set_test_params(self): | ||
self.num_nodes = 1 | ||
self.setup_clean_chain = True | ||
self.extra_args = [['-txnotokens=0', '-amkheight=50']] | ||
|
||
def run_test(self): | ||
node = self.nodes[0] | ||
node.generate(120) | ||
|
||
# Get addresses and set up account | ||
account = node.getnewaddress() | ||
destination = node.getnewaddress() | ||
node.utxostoaccount({account: "10@0"}) | ||
node.generate(1) | ||
|
||
# Check we have expected balance | ||
assert_equal(node.getaccount(account)[0], "10.00000000@DFI") | ||
|
||
# Send double the amount we have in account | ||
for _ in range(100): | ||
node.accounttoutxos(account, {destination: "2@DFI"}) | ||
|
||
# Store block height | ||
blockcount = node.getblockcount() | ||
|
||
# Generate a block | ||
node.generate(1) | ||
|
||
# Check the blockchain has extended as expected | ||
assert_equal(node.getblockcount(), blockcount + 1) | ||
|
||
# Account should now be empty | ||
assert_equal(node.getaccount(account), []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can also check the count of transactions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add it. We would expect five TXs in the block plus coinbase. This test will also be extended to include other account TXs. |
||
|
||
if __name__ == '__main__': | ||
AccountMiningTest().main () |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
auto
instead ofindexed_transaction_set::const_iterator