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
45 changes: 27 additions & 18 deletions src/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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();
}

Expand Down
22 changes: 13 additions & 9 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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))
Expand All @@ -385,17 +386,20 @@ 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;
std::string address;
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);
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/tx_verify.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 6 additions & 7 deletions src/test/assets/asset_tx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup)


BOOST_AUTO_TEST_CASE(asset_tx_valid) {

SelectParams(CBaseChainParams::MAIN);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()