diff --git a/src/assets/assets.cpp b/src/assets/assets.cpp index 44f4ee51b3..202e80611d 100644 --- a/src/assets/assets.cpp +++ b/src/assets/assets.cpp @@ -1226,23 +1226,6 @@ bool CAssetsCache::Flush(bool fSoftCopy, bool fFlushDB) bool dirty = false; std::string message; - // Save the assets that have been spent by erasing the quantity in the database - for (auto spentAsset : vSpentAssets) { - auto pair = make_pair(spentAsset.assetName, spentAsset.address); - if (mapAssetsAddressAmount.count(pair)) { - if (mapAssetsAddressAmount.at(make_pair(spentAsset.assetName, spentAsset.address)) == 0) { - if (!passetsdb->EraseAssetAddressQuantity(spentAsset.assetName, spentAsset.address)) { - dirty = true; - message = "_Failed Erasing a Spent Asset, from database"; - } - - if (dirty) { - return error("%s : %s", __func__, message); - } - } - } - } - // Remove new assets from the database for (auto newAsset : setNewAssetsToRemove) { passetsCache->Erase(newAsset.asset.strName); @@ -1379,7 +1362,7 @@ bool CAssetsCache::Flush(bool fSoftCopy, bool fFlushDB) if (mapAssetsAddressAmount.count(pair)) { if (!passetsdb->WriteAssetAddressQuantity(pair.first, pair.second, - mapAssetsAddressAmount[pair])) { + mapAssetsAddressAmount.at(pair))) { dirty = true; message = "_Failed Writing reissue asset quantity to the address quantity database"; } @@ -1452,6 +1435,32 @@ bool CAssetsCache::Flush(bool fSoftCopy, bool fFlushDB) } } + // Save the assets that have been spent by erasing the quantity in the database + for (auto spentAsset : vSpentAssets) { + auto pair = make_pair(spentAsset.assetName, spentAsset.address); + if (mapAssetsAddressAmount.count(pair)) { + if (mapAssetsAddressAmount.at(make_pair(spentAsset.assetName, spentAsset.address)) == 0) { + if (!passetsdb->EraseAssetAddressQuantity(spentAsset.assetName, spentAsset.address)) { + dirty = true; + message = "_Failed Erasing a Spent Asset, from database"; + } + + if (dirty) { + return error("%s : %s", __func__, message); + } + } else { + if (!passetsdb->WriteAssetAddressQuantity(spentAsset.assetName, spentAsset.address, mapAssetsAddressAmount.at(pair))) { + dirty = true; + message = "_Failed Erasing a Spent Asset, from database"; + } + + if (dirty) { + return error("%s : %s", __func__, message); + } + } + } + } + ClearDirtyCache(); } diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp index b01dc1c31e..9a7cddd390 100644 --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -323,7 +323,7 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c } //! Check to make sure that the inputs and outputs CAmount match exactly. -bool Consensus::CheckTxAssets(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs) +bool Consensus::CheckTxAssets(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, const bool fRunningUnitTests) { // are the actual inputs available? if (!inputs.HaveInputs(tx)) { @@ -370,10 +370,11 @@ bool Consensus::CheckTxAssets(const CTransaction& tx, CValidationState& state, c else totalOutputs.insert(make_pair(transfer.strName, transfer.nAmount)); - if (IsAssetNameAnOwner(transfer.strName)) { - if (transfer.nAmount != OWNER_ASSET_AMOUNT) - return state.DoS(100, false, REJECT_INVALID, "bad-txns-transfer-owner-amount-was-not-1"); - } else { + if (!fRunningUnitTests) { + if (IsAssetNameAnOwner(transfer.strName)) { + if (transfer.nAmount != OWNER_ASSET_AMOUNT) + return state.DoS(100, false, REJECT_INVALID, "bad-txns-transfer-owner-amount-was-not-1"); + } else { // For all other types of assets, make sure they are sending the right type of units CNewAsset asset; if (!passets->GetAssetIfExists(transfer.strName, asset)) @@ -385,6 +386,7 @@ bool Consensus::CheckTxAssets(const CTransaction& tx, CValidationState& state, c if (transfer.nAmount % int64_t(pow(10, (MAX_UNIT - asset.units))) != 0) return state.DoS(100, false, REJECT_INVALID, "bad-txns-transfer-asset-amount-not-match-units"); + } } } else if (txout.scriptPubKey.IsReissueAsset()) { CReissueAsset reissue; @@ -392,10 +394,12 @@ bool Consensus::CheckTxAssets(const CTransaction& tx, CValidationState& state, c if (!ReissueAssetFromScript(txout.scriptPubKey, reissue, address)) return state.DoS(100, false, REJECT_INVALID, "bad-tx-asset-reissue-bad-deserialize"); - std::string strError; - if (!reissue.IsValid(strError, *passets)) { - return state.DoS(100, false, REJECT_INVALID, - "bad-txns" + strError); + if (!fRunningUnitTests) { + std::string strError; + if (!reissue.IsValid(strError, *passets)) { + return state.DoS(100, false, REJECT_INVALID, + "bad-txns" + strError); + } } } diff --git a/src/consensus/tx_verify.h b/src/consensus/tx_verify.h index c80a9c78f2..2430525666 100644 --- a/src/consensus/tx_verify.h +++ b/src/consensus/tx_verify.h @@ -32,7 +32,7 @@ namespace Consensus { bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee); /** RVN START */ -bool CheckTxAssets(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs); +bool CheckTxAssets(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, const bool fRunningUnitTests = false); /** RVN END */ } // namespace Consensus diff --git a/src/test/assets/asset_tx_tests.cpp b/src/test/assets/asset_tx_tests.cpp index bc462ca54a..ee9c18f051 100644 --- a/src/test/assets/asset_tx_tests.cpp +++ b/src/test/assets/asset_tx_tests.cpp @@ -16,7 +16,6 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup) - BOOST_AUTO_TEST_CASE(asset_tx_valid) { SelectParams(CBaseChainParams::MAIN); @@ -57,7 +56,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup) // The inputs are spending 1000 Assets // The outputs are assigning a destination to 1000 Assets // This test should pass because all assets are assigned a destination - BOOST_CHECK_MESSAGE(Consensus::CheckTxAssets(tx, state, coins), "CheckTxAssets Failed"); + BOOST_CHECK_MESSAGE(Consensus::CheckTxAssets(tx, state, coins, true), "CheckTxAssets Failed"); } BOOST_AUTO_TEST_CASE(asset_tx_not_valid) { @@ -110,7 +109,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup) // The inputs of this transaction are spending 1000 Assets // The outputs are assigning a destination to only 100 Assets // This should fail because 900 Assets aren't being assigned a destination (Trying to burn 900 Assets) - BOOST_CHECK_MESSAGE(!Consensus::CheckTxAssets(tx, state, coins), "CheckTxAssets should of failed"); + BOOST_CHECK_MESSAGE(!Consensus::CheckTxAssets(tx, state, coins, true), "CheckTxAssets should of failed"); } BOOST_AUTO_TEST_CASE(asset_tx_valid_multiple_outs) { @@ -166,7 +165,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup) // The inputs are spending 1000 Assets // The outputs are assigned 100 Assets to 10 destinations (10 * 100) = 1000 // This test should pass all assets that are being spent are assigned to a destination - BOOST_CHECK_MESSAGE(Consensus::CheckTxAssets(tx, state, coins), "CheckTxAssets failed"); + BOOST_CHECK_MESSAGE(Consensus::CheckTxAssets(tx, state, coins, true), "CheckTxAssets failed"); } BOOST_AUTO_TEST_CASE(asset_tx_multiple_outs_invalid) { @@ -222,7 +221,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup) // The inputs are spending 1000 Assets // The outputs are assigning 100 Assets to 12 destinations (12 * 100 = 1200) // This test should fail because the Outputs are greater than the inputs - BOOST_CHECK_MESSAGE(!Consensus::CheckTxAssets(tx, state, coins), "CheckTxAssets passed when it should of failed"); + BOOST_CHECK_MESSAGE(!Consensus::CheckTxAssets(tx, state, coins, true), "CheckTxAssets passed when it should of failed"); } BOOST_AUTO_TEST_CASE(asset_tx_multiple_assets) { @@ -337,7 +336,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup) // The inputs are spending 3000 Assets (1000 of each RAVEN, RAVENTEST, RAVENTESTTEST) // The outputs are spending 100 Assets to 10 destinations (10 * 100 = 1000) (of each RAVEN, RAVENTEST, RAVENTESTTEST) // This test should pass because for each asset that is spent. It is assigned a destination - BOOST_CHECK_MESSAGE(Consensus::CheckTxAssets(tx, state, coins), "CheckTxAssets Failed"); + BOOST_CHECK_MESSAGE(Consensus::CheckTxAssets(tx, state, coins, true), "CheckTxAssets Failed"); // Try it not but only spend 900 of each asset instead of 1000 @@ -389,7 +388,7 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup) // Check the transaction that contains inputs that are spending 1000 Assets for 3 different assets // While only outputs only contain 900 Assets being sent to a destination // This should fail because 100 of each Asset isn't being sent to a destination (Trying to burn 100 Assets each) - BOOST_CHECK_MESSAGE(!Consensus::CheckTxAssets(tx2, state, coins), "CheckTxAssets should of failed"); + BOOST_CHECK_MESSAGE(!Consensus::CheckTxAssets(tx2, state, coins, true), "CheckTxAssets should of failed"); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file