Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/evo/specialtx_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ static bool CheckProRegTx(const CTransaction& tx, const CBlockIndex* pindexPrev,
// This is checked only when pindexPrev is not null (thus during ConnectBlock-->CheckSpecialTx),
// because this is a contextual check: we need the updated utxo set, to verify that
// the coin exists and it is unspent.

// Spending the declared collateral in the same tx halts block production.
// AcceptToMemoryPool calls this before applying inputs to the view, so GetUTXOCoin
// below still finds the coin and the tx is accepted. ConnectBlock runs UpdateCoins
// (validation.cpp:1602) before ProcessSpecialTxsInBlock (:1671), so by then the coin
// is spent, CheckSpecialTx fails, and BlockAssembler throws instead of dropping the
// tx (blockassembler.cpp:253). Reject at mempool time. Checked ahead of GetUTXOCoin
// so the reject reason names the real cause at both call sites.
for (const CTxIn& txin : tx.vin) {
if (txin.prevout == pl.collateralOutpoint) {
return state.DoS(10, false, REJECT_INVALID, "bad-protx-collateral-used-as-input");
}
}

Coin coin;
if (!view->GetUTXOCoin(pl.collateralOutpoint, coin)) {
return state.DoS(10, false, REJECT_INVALID, "bad-protx-collateral");
Expand Down
33 changes: 33 additions & 0 deletions src/test/evo_deterministicmns_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,39 @@ BOOST_FIXTURE_TEST_CASE(dip3_protx, TestChain400Setup)
BOOST_CHECK(!WITH_LOCK(cs_main, return CheckSpecialTx(tx, chainTip, view, state); ));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-protx-collateral");
}
// Try to register spending the declared external collateral in the same tx.
// Without the guard this reaches the mempool, because AcceptToMemoryPool validates
// before applying inputs to the view, and then halts block production: ConnectBlock
// runs UpdateCoins first, so the collateral is spent by the time CheckSpecialTx runs
// and BlockAssembler throws rather than dropping the tx.
{
CMutableTransaction mtx;
const COutPoint& coll_out = CreateNewUTXO(utxos, mtx, coinbaseKey, Params().GetConsensus().nMNCollateralAmt);
CreateAndProcessBlock({mtx}, coinbaseKey);
chainTip = chainActive.Tip();
BOOST_CHECK_EQUAL(chainTip->nHeight, ++nHeight);
Coin coll_coin;
BOOST_CHECK(view->GetUTXOCoin(coll_out, coll_coin));

auto tx = CreateProRegTx(Optional<COutPoint>(coll_out), utxos, port, GenerateRandomAddress(), coinbaseKey, GetRandomKey(), GetRandomBLSKey().GetPublicKey());
// Spend the collateral, then make the tx valid in every other respect: refresh the
// inputs hash and prove collateral ownership. Without a fully valid tx this would
// be rejected by CheckStringSig and never exercise the collateral rule at all.
tx.vin.emplace_back(coll_out);
ProRegPL pl;
BOOST_CHECK(GetTxPayload(tx, pl));
pl.inputsHash = CalcTxInputsHash(CTransaction(tx));
pl.vchSig.clear();
BOOST_CHECK(CMessageSigner::SignMessage(pl.MakeSignString(), pl.vchSig, coinbaseKey));
SetTxPayload(tx, pl);
SignTransaction(tx, coinbaseKey);

CValidationState state;
BOOST_CHECK(!WITH_LOCK(cs_main, return CheckSpecialTx(tx, chainTip, view, state); ));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-protx-collateral-used-as-input");

utxos.emplace(coll_out, std::make_pair(coll_coin.nHeight, coll_coin.out.nValue));
}
// Try to register with invalid internal collateral
{
auto tx = CreateProRegTx(nullopt, utxos, port, GenerateRandomAddress(), coinbaseKey, GetRandomKey(), GetRandomBLSKey().GetPublicKey(), 0, true);
Expand Down
19 changes: 19 additions & 0 deletions test/functional/tiertwo_deterministicmns.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
assert_equal,
assert_raises_rpc_error,
create_new_dmn,
get_collateral_vout,
hex_str_to_bytes,
is_coin_locked_by,
spend_mn_collateral,
Expand Down Expand Up @@ -239,6 +240,24 @@ def run_test(self):
assert_raises_rpc_error(-1, "bad-protx-dup-owner-key",
self.protx_register_fund, miner, controller, dmn2c, dummy_add)

# A ProRegTx must not spend the collateral it declares: the mempool took
# it, then every block template failed. With only the collateral in its
# wallet, protx_register has to fund the fee from it.
self.log.info("Trying a ProRegTx that spends its own collateral...")
owner = self.nodes[dmn2.idx]
assert_equal(owner.getbalance(), 0)
coll_txid = miner.sendtoaddress(owner.getnewaddress(), Decimal('100'))
miner.generate(1)
self.sync_blocks()
coll_n = get_collateral_vout(owner.getrawtransaction(coll_txid, True))
dmn2d = create_new_dmn(dmn2.idx, owner, dummy_add, dmn2_keys)
assert_raises_rpc_error(-1, "bad-protx-collateral-used-as-input",
owner.protx_register, coll_txid, coll_n, dmn2d.ipport, dmn2d.owner,
dmn2d.operator_pk, dmn2d.voting, dmn2d.payee)
assert_equal(owner.getrawmempool(), [])
miner.generate(1)
self.sync_blocks()

# Finally, register it properly. This time setting 10% of the reward for the operator
op_rew = {"reward": 10.00, "address": self.nodes[dmn2.idx].getnewaddress()}
self.log.info("Reactivating the node with a new registration (with operator reward)...")
Expand Down
Loading