Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check units when issuing assets #249

Merged
merged 2 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 44 additions & 14 deletions src/assets/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,28 +239,52 @@ bool CNewAsset::IsValid(std::string& strError, CAssetsCache& assetCache, bool fC
}
}

if (!IsAssetNameValid(std::string(strName)))
if (!IsAssetNameValid(std::string(strName))) {
strError = "Invalid parameter: asset_name must only consist of valid characters and have a size between 3 and 30 characters. See help for more details.";
return false;
}

if (IsAssetNameAnOwner(std::string(strName)))
if (IsAssetNameAnOwner(std::string(strName))) {
strError = "Invalid parameters: asset_name can't have a '!' at the end of it. See help for more details.";
return false;
}

if (nAmount <= 0)
strError = "Invalid parameter: asset amount can't be equal to or less than zero.";
if (nAmount <= 0) {
strError = "Invalid parameter: asset amount can't be equal to or less than zero.";
return false;
}

if (units < 0 || units > 8)
strError = "Invalid parameter: units must be between 0-8.";
if (nAmount > MAX_MONEY) {
strError = "Invalid parameter: asset amount greater than max.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could put MAX_MONEY into strError

return false;
}

if (nReissuable != 0 && nReissuable != 1)
strError = "Invalid parameter: reissuable must be 0 or 1";
if (units < 0 || units > 8) {
strError = "Invalid parameter: units must be between 0-8.";
return false;
}

if (nHasIPFS != 0 && nHasIPFS != 1)
strError = "Invalid parameter: has_ipfs must be 0 or 1.";
if (!CheckAmountWithUnits(nAmount, units)) {
strError = "bad-txns-transfer-asset-amount-not-match-units";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is transfer a cpasta? generalize message? also not in the style of the others -- maybe reuse the amount must be divisable... from below?

return false;
}

if (nHasIPFS && strIPFSHash.size() != 34)
strError = "Invalid parameter: ipfs_hash must be 34 bytes.";
if (nReissuable != 0 && nReissuable != 1) {
strError = "Invalid parameter: reissuable must be 0 or 1";
return false;
}

return strError == "";
if (nHasIPFS != 0 && nHasIPFS != 1) {
strError = "Invalid parameter: has_ipfs must be 0 or 1.";
return false;
}

if (nHasIPFS && strIPFSHash.size() != 34) {
strError = "Invalid parameter: ipfs_hash must be 34 bytes.";
return false;
}

return true;
}

CNewAsset::CNewAsset(const CNewAsset& asset)
Expand Down Expand Up @@ -696,7 +720,7 @@ bool CReissueAsset::IsValid(std::string &strError, CAssetsCache& assetCache) con
return false;
}

if (nAmount % int64_t(pow(10, (MAX_UNIT - asset.units))) != 0) {
if (!CheckAmountWithUnits(nAmount, asset.units)) {
strError = "Unable to reissue asset: amount must be divisable by the smaller unit assigned to the asset";
return false;
}
Expand Down Expand Up @@ -2472,4 +2496,10 @@ bool VerifyWalletHasAsset(const std::string& asset_name, std::pair<int, std::str

pairError = std::make_pair(RPC_INVALID_REQUEST, strprintf("Wallet doesn't have asset: %s", asset_name));
return false;
}

// Return true if the amount is valid with the units passed in
bool CheckAmountWithUnits(const CAmount& nAmount, const uint8_t nUnits)
{
return nAmount % int64_t(pow(10, (MAX_UNIT - nUnits))) == 0;
}
2 changes: 2 additions & 0 deletions src/assets/assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ bool CheckOwnerDataTx(const CTxOut& txOut);
bool CheckReissueDataTx(const CTxOut& txOut);
bool CheckTransferOwnerTx(const CTxOut& txOut);

bool CheckAmountWithUnits(const CAmount& nAmount, const uint8_t nUnits);

bool IsScriptNewAsset(const CScript& scriptPubKey, int& nStartingIndex);
bool IsScriptOwnerAsset(const CScript& scriptPubKey, int& nStartingIndex);
bool IsScriptReissueAsset(const CScript& scriptPubKey, int& nStartingIndex);
Expand Down
5 changes: 2 additions & 3 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,8 @@ bool Consensus::CheckTxAssets(const CTransaction& tx, CValidationState& state, c
if (asset.strName != transfer.strName)
return state.DoS(100, false, REJECT_INVALID, "bad-txns-asset-database-corrupted");

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");
if (!CheckAmountWithUnits(transfer.nAmount, asset.units))
return state.DoS(100, false, REJECT_INVALID, "bad-txns-transfer-asset-amount-not-match-units");
}
}
} else if (txout.scriptPubKey.IsReissueAsset()) {
Expand Down
58 changes: 58 additions & 0 deletions src/test/assets/asset_tx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,62 @@ BOOST_FIXTURE_TEST_SUITE(asset_tx_tests, BasicTestingSetup)
BOOST_CHECK_MESSAGE(!Consensus::CheckTxAssets(tx2, state, coins, vReissueAssets, true), "CheckTxAssets should of failed");
}

BOOST_AUTO_TEST_CASE(asset_tx_issue_units) {

std::string error;
CAssetsCache cache;

// Amount = 1.00000000
CNewAsset asset("ASSET", CAmount(100000000), 8, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test1: " + error);

// Amount = 1.00000000
asset = CNewAsset("ASSET", CAmount(100000000), 0, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test2: " + error);

// Amount = 0.10000000
asset = CNewAsset("ASSET", CAmount(10000000), 8, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test3: " + error);

// Amount = 0.10000000
asset = CNewAsset("ASSET", CAmount(10000000), 2, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test4: " + error);

// Amount = 0.10000000
asset = CNewAsset("ASSET", CAmount(10000000), 0, false, false, "");
BOOST_CHECK_MESSAGE(!asset.IsValid(error, cache, false, false), "Test5: " + error);

// Amount = 0.01000000
asset = CNewAsset("ASSET", CAmount(1000000), 0, false, false, "");
BOOST_CHECK_MESSAGE(!asset.IsValid(error, cache, false, false), "Test6: " + error);

// Amount = 0.01000000
asset = CNewAsset("ASSET", CAmount(1000000), 1, false, false, "");
BOOST_CHECK_MESSAGE(!asset.IsValid(error, cache, false, false), "Test7: " + error);

// Amount = 0.01000000
asset = CNewAsset("ASSET", CAmount(1000000), 2, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test8: " + error);

// Amount = 0.00000001
asset = CNewAsset("ASSET", CAmount(1), 8, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test9: " + error);

// Amount = 0.00000010
asset = CNewAsset("ASSET", CAmount(10), 7, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test10: " + error);

// Amount = 0.00000001
asset = CNewAsset("ASSET", CAmount(1), 7 , false, false, "");
BOOST_CHECK_MESSAGE(!asset.IsValid(error, cache, false, false), "Test11: " + error);

// Amount = 0.00000100
asset = CNewAsset("ASSET", CAmount(100), 6, false, false, "");
BOOST_CHECK_MESSAGE(asset.IsValid(error, cache, false, false), "Test12: " + error);

// Amount = 0.00000100
asset = CNewAsset("ASSET", CAmount(100), 5, false, false, "");
BOOST_CHECK_MESSAGE(!asset.IsValid(error, cache, false, false), "Test13: " + error);
}

BOOST_AUTO_TEST_SUITE_END()