Skip to content

Commit

Permalink
Add unit test for enforced value
Browse files Browse the repository at this point in the history
  • Loading branch information
blondfrogs committed Jul 6, 2020
1 parent 62c30f9 commit 905952c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fChe
if (fMempoolCheck) {
// Don't accept to the mempool no matter what on these types of transactions
if (txout.nValue != 0) {
return state.DoS(100, false, REJECT_INVALID, "bad-txns-asset-reissued-amount-isn't-zero");
return state.DoS(100, false, REJECT_INVALID, "bad-mempool-txns-asset-reissued-amount-isn't-zero");
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/test/assets/asset_tx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <base58.h>
#include <consensus/validation.h>
#include <consensus/tx_verify.h>
#include <validation.h>

BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup)

Expand Down Expand Up @@ -477,4 +478,66 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup)
BOOST_CHECK_MESSAGE(!CheckNewAsset(asset, error), "Test13: " + error);
}

BOOST_AUTO_TEST_CASE(asset_tx_enforce_value_test)
{
BOOST_TEST_MESSAGE("Running Asset TX Enforce Value Test");

SelectParams(CBaseChainParams::MAIN);

// Create the reissue asset
CReissueAsset reissueAsset("ENFORCE_VALUE", 100, 8, true, "");
CScript scriptPubKey = GetScriptForDestination(DecodeDestination(GetParams().GlobalBurnAddress()));
reissueAsset.ConstructTransaction(scriptPubKey);

// Create an invalid reissue asset with nValue not equal to zero
CTxOut txOut;
txOut.nValue = 500;
txOut.scriptPubKey = scriptPubKey;

// Create views
CCoinsView view;
CCoinsViewCache coins(&view);
CAssetsCache assetCache;

// Create a random hash
uint256 hash = uint256S("BF50CB9A63BE0019171456252989A459A7D0A5F494735278290079D22AB704A2");

// Add the coin to the cache
COutPoint outpoint(hash, 1);
coins.AddCoin(outpoint, Coin(txOut, 10, 0), true);

// Create input
CTxIn in;
in.prevout = outpoint;

// Create transaction and input for the outpoint of the coin we just created
CMutableTransaction mutTx;

// Add the input, and an output into the transaction
mutTx.vin.emplace_back(in);
mutTx.vout.emplace_back(txOut);

CTransaction tx(mutTx);
CValidationState state;

bool fCheckMempool = true;
bool fCheckBlock = false;

// Check that the CheckTransaction will fail when trying to add it to the mempool
bool fCheck = !CheckTransaction(tx, state, true, fCheckMempool, fCheckBlock);

BOOST_CHECK(fCheck);
BOOST_CHECK(state.GetRejectReason() == "bad-mempool-txns-asset-reissued-amount-isn't-zero");

// Check that the CheckTransaction will fail when trying to add it to a block
fCheckMempool = false;
fCheckBlock = true;
// Turn on the BIP that enforces the block check
SetEnforcedValues(true);

fCheck = !CheckTransaction(tx, state, true, fCheckMempool, fCheckBlock);
BOOST_CHECK(fCheck);
BOOST_CHECK(state.GetRejectReason() == "bad-txns-asset-reissued-amount-isn't-zero");
}

BOOST_AUTO_TEST_SUITE_END()
6 changes: 6 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5703,6 +5703,12 @@ double GuessVerificationProgress(const ChainTxData& data, CBlockIndex *pindex) {
}

/** RVN START */

// Only used by test framework
void SetEnforcedValues(bool value) {
fEnforcedValuesIsActive = value;
}

bool AreEnforcedValuesDeployed()
{
if (fEnforcedValuesIsActive)
Expand Down
3 changes: 3 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ bool AreRestrictedAssetsDeployed();

bool AreEnforcedValuesDeployed();

// Only used by test framework
void SetEnforcedValues(bool value);

bool IsRip5Active();


Expand Down

0 comments on commit 905952c

Please sign in to comment.