Skip to content

Commit

Permalink
Resolve undefined behaviour to fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Oct 18, 2023
1 parent 508846e commit 0f1aba8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 60 deletions.
25 changes: 1 addition & 24 deletions src/dfi/consensus/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,35 +72,12 @@ Res CAccountsConsensus::operator()(const CAccountToAccountMessage &obj) const {
return res;
}

if (txn == 999) {
std::cout << "CAccountToAccountMessage: from: " << obj.from.GetHex() << std::endl;
}
for (const auto &[dest, balance] : obj.to) {
if (txn == 999) {
std::cout << "CAccountToAccountMessage: to: " << dest.GetHex() << " amounts: " << balance.ToString()
<< std::endl;
}
}

// transfer
if (auto res = SubBalanceDelShares(obj.from, SumAllTransfers(obj.to)); !res) {
if (txn == 999) {
std::cout << "CAccountToAccountMessage: SubBalanceDelShares failure" << std::endl;
}
return res;
}

auto res = AddBalancesSetShares(obj.to);

if (!res && txn == 999) {
std::cout << "CAccountToAccountMessage: AddBalancesSetShares failure" << std::endl;
}

if (txn == 999) {
std::cout << "CAccountToAccountMessage: success" << std::endl;
}

return res;
return AddBalancesSetShares(obj.to);
}

Res CAccountsConsensus::operator()(const CAnyAccountsToAccountsMessage &obj) const {
Expand Down
13 changes: 0 additions & 13 deletions src/dfi/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,6 @@ Res ApplyCustomTx(CCustomCSView &mnview, BlockContext &blockCtx, const Transacti
const auto metadataValidation = height >= static_cast<uint32_t>(consensus.DF11FortCanningHeight);
const auto txType = GuessCustomTxType(tx, metadata, metadataValidation);

if (txn == 999) {
std::cout << "ApplyCustomTx TxType: " << ToString(txType) << std::endl;
}

auto attributes = mnview.GetAttributes();
assert(attributes);

Expand Down Expand Up @@ -562,19 +558,13 @@ Res ApplyCustomTx(CCustomCSView &mnview, BlockContext &blockCtx, const Transacti
auto txMessage = customTypeToMessage(txType);
CAccountsHistoryWriter view(mnview, height, txn, tx.GetHash(), uint8_t(txType));
if ((res = CustomMetadataParse(height, consensus, metadata, txMessage))) {
if (txn == 999) {
std::cout << "ApplyCustomTx: CustomMetadataParse success" << std::endl;
}
if (mnview.GetHistoryWriters().GetVaultView()) {
PopulateVaultHistoryData(mnview.GetHistoryWriters(), view, txMessage, txType, txCtx);
}

res = CustomTxVisit(view, txMessage, blockCtx, txCtx);

if (res) {
if (txn == 999) {
std::cout << "ApplyCustomTx: CustomTxVisit success" << std::endl;
}
if (canSpend && txType == CustomTxType::UpdateMasternode) {
auto obj = std::get<CUpdateMasterNodeMessage>(txMessage);
for (const auto &item : obj.updates) {
Expand Down Expand Up @@ -613,9 +603,6 @@ Res ApplyCustomTx(CCustomCSView &mnview, BlockContext &blockCtx, const Transacti
}
// list of transactions which aren't allowed to fail:
if (!res) {
if (txn == 999) {
std::cout << "ApplyCustomTx: " << res.msg << std::endl;
}
res.msg = strprintf("%sTx: %s", ToString(txType), res.msg);

if (IsBelowDakotaMintTokenOrAccountToUtxos(txType, height)) {
Expand Down
41 changes: 18 additions & 23 deletions src/test/applytx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(neg_token_amounts)
}

{ // it is possible to create neg TokenAmount, but can't manipulate with it
CTokenAmount val{DCT_ID{0}, -100};
CTokenAmount val{DCT_ID{}, -100};
auto res = val.Add(100);
BOOST_CHECK(!res.ok);
BOOST_CHECK_EQUAL(res.msg, "negative amount");
Expand All @@ -46,27 +46,26 @@ BOOST_AUTO_TEST_CASE(neg_token_amounts)
BOOST_AUTO_TEST_CASE(neg_token_balances)
{
LOCK(cs_main);
assert(pcustomcsview);

CCustomCSView mnview(*pcustomcsview);

CScript const owner = CScript(1);
DCT_ID const DFI{0};
DCT_ID const DFI{};
{
// Initial value
auto dfi100 = CTokenAmount{DCT_ID{0}, 100};
auto dfi100 = CTokenAmount{DFI, 100};
auto res = mnview.AddBalance(owner, dfi100);
BOOST_CHECK(res.ok);
BOOST_CHECK_EQUAL(mnview.GetBalance(owner, DFI), dfi100);

// Fail to add negative
res = mnview.AddBalance(owner, CTokenAmount{DCT_ID{0}, -100});
res = mnview.AddBalance(owner, CTokenAmount{DFI, -100});
BOOST_CHECK(!res.ok);
BOOST_CHECK_EQUAL(res.msg, "negative amount: -0.00000100");
BOOST_CHECK_EQUAL(mnview.GetBalance(owner, DFI), dfi100);

// Fail to sub negative
res = mnview.SubBalance(owner, CTokenAmount{DCT_ID{0}, -100});
res = mnview.SubBalance(owner, CTokenAmount{DFI, -100});
BOOST_CHECK(!res.ok);
BOOST_CHECK_EQUAL(res.msg, "negative amount: -0.00000100");
BOOST_CHECK_EQUAL(mnview.GetBalance(owner, DFI), dfi100);
Expand All @@ -88,7 +87,6 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)
amkCheated.DF1AMKHeight = 0;

LOCK(cs_main);
assert(pcustomcsview);

CCustomCSView mnview(*pcustomcsview);
CCoinsViewCache coinview(&::ChainstateActive().CoinsTip());
Expand All @@ -104,8 +102,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)
const auto dfi100 = CTokenAmount{DFI, 100};
auto res = mnview.AddBalance(owner, dfi100);
BOOST_CHECK(res.ok);
const auto balance = mnview.GetBalance(owner, DFI);
BOOST_CHECK_EQUAL(balance, dfi100);
BOOST_CHECK_EQUAL(mnview.GetBalance(owner, DFI), dfi100);

// create templates for msg and tx:
CAccountToAccountMessage msg{};
Expand All @@ -116,8 +113,6 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

BlockContext blockCtx;

BOOST_TEST_CHECKPOINT("BlockContext created");

// try to send "A:-1@DFI"
{
msg.to = {
Expand All @@ -126,21 +121,18 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

const auto tx = CTransaction(rawTx);
const auto txCtx = TransactionContext{
coinview,
CTransaction(rawTx),
tx,
amkCheated,
1,
0,
999,
0,
};

BOOST_TEST_CHECKPOINT("TransactionContext created");

res = ApplyCustomTx(mnview, blockCtx, txCtx);

BOOST_TEST_CHECKPOINT("ApplyCustomTx returned");

BOOST_CHECK(!res.ok);
BOOST_CHECK_NE(res.msg.find("negative amount"), std::string::npos);
// check that nothing changes:
Expand All @@ -156,13 +148,14 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

const auto tx = CTransaction(rawTx);
const auto txCtx = TransactionContext{
coinview,
CTransaction(rawTx),
tx,
amkCheated,
1,
0,
999,
0,
};

res = ApplyCustomTx(mnview, blockCtx, txCtx);
Expand All @@ -182,13 +175,14 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

const auto tx = CTransaction(rawTx);
const auto txCtx = TransactionContext{
coinview,
CTransaction(rawTx),
tx,
amkCheated,
1,
0,
999,
0,
};

res = ApplyCustomTx(mnview, blockCtx, txCtx);
Expand All @@ -208,13 +202,14 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

rawTx.vout[0].scriptPubKey = CreateMetaA2A(msg);

const auto tx = CTransaction(rawTx);
const auto txCtx = TransactionContext{
coinview,
CTransaction(rawTx),
tx,
amkCheated,
1,
0,
999,
0,
};

res = ApplyCustomTx(mnview, blockCtx, txCtx);
Expand Down

0 comments on commit 0f1aba8

Please sign in to comment.