Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Remove account_id from Add/Subtract commands (#1511)
Browse files Browse the repository at this point in the history
Signed-off-by: Kitsu <mail@kitsu.me>
  • Loading branch information
l4l committed Jul 25, 2018
1 parent 2c674f3 commit bf22e01
Show file tree
Hide file tree
Showing 47 changed files with 254 additions and 648 deletions.
16 changes: 5 additions & 11 deletions docs/source/api/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ Schema
.. code-block:: proto
message AddAssetQuantity {
string account_id = 1;
string asset_id = 2;
Amount amount = 3;
string asset_id = 1;
Amount amount = 2;
}
message uint256 {
Expand All @@ -48,7 +47,6 @@ Structure
:header: "Field", "Description", "Constraint", "Example"
:widths: 15, 30, 20, 15

"Account ID", "account id in which to add asset", "<account_name>@<domain_id>", "alex@morgan"
"Asset ID", "id of the asset", "<asset_name>#<domain_id>", "usd#morgan"
"Amount", "positive amount of the asset to add", "> 0", "200.02"

Expand All @@ -58,7 +56,6 @@ Validation
1. Asset and account should exist
2. Added quantity precision should be equal to asset precision
3. Creator of a transaction should have a role which has permissions for issuing assets
4. Creator of a transaction adds account quantity to his/her account only

Add peer
--------
Expand Down Expand Up @@ -557,10 +554,9 @@ Schema

.. code-block:: proto
message AddAssetQuantity {
string account_id = 1;
string asset_id = 2;
Amount amount = 3;
message SubtractAssetQuantity {
string asset_id = 1;
Amount amount = 2;
}
message uint256 {
Expand All @@ -586,7 +582,6 @@ Structure
:header: "Field", "Description", "Constraint", "Example"
:widths: 15, 30, 20, 15

"Account ID", "account id from which to subtract asset", "already existent", "makoto@soramitsu"
"Asset ID", "id of the asset", "<asset_name>#<domain_id>", "usd#morgan"
"Amount", "positive amount of the asset to subtract", "> 0", "200"

Expand All @@ -596,7 +591,6 @@ Validation
1. Asset and account should exist
2. Added quantity precision should be equal to asset precision
3. Creator of the transaction should have a role which has permissions for subtraction of assets
4. Creator of transaction subtracts account quantity in his/her account only

Transfer asset
--------------
Expand Down
2 changes: 1 addition & 1 deletion docs/source/guides/libraries/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Create asset quantity:
tx = tx_builder.creatorAccountId(creator) \
.createdTime(current_time) \
.addAssetQuantity("admin@test", "coin#domain", "1000.2").build()
.addAssetQuantity("coin#domain", "1000.2").build()
send_tx(tx, key_pair)
print_status(tx)
Expand Down
2 changes: 1 addition & 1 deletion example/python/tx-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def add_coin_to_admin():
"""
tx = tx_builder.creatorAccountId(creator) \
.createdTime(current_time) \
.addAssetQuantity("admin@test", "coin#domain", "1000.00").build()
.addAssetQuantity("coin#domain", "1000.00").build()

send_tx(tx, key_pair)
print_status_streaming(tx)
Expand Down
5 changes: 2 additions & 3 deletions iroha-cli/interactive/impl/interactive_transaction_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ namespace iroha_cli {
std::shared_ptr<iroha::model::Command>
InteractiveTransactionCli::parseAddAssetQuantity(
std::vector<std::string> params) {
auto account_id = params[0];
auto asset_id = params[1];
auto asset_id = params[0];
auto val_int =
parser::parseValue<boost::multiprecision::uint256_t>(params[2]);
auto precision = parser::parseValue<uint32_t>(params[3]);
Expand All @@ -344,7 +343,7 @@ namespace iroha_cli {
}
std::cout << val_int.value() << " " << precision.value() << std::endl;
iroha::Amount amount(val_int.value(), precision.value());
return generator_.generateAddAssetQuantity(account_id, asset_id, amount);
return generator_.generateAddAssetQuantity(asset_id, amount);
}

std::shared_ptr<iroha::model::Command>
Expand Down
42 changes: 12 additions & 30 deletions irohad/execution/impl/command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ namespace iroha {
}
auto command_amount =
makeAmountWithPrecision(command.amount(), asset.value()->precision());
if (not queries->getAccount(command.accountId())) {
if (not queries->getAccount(creator_account_id)) {
return makeCommandError(
(boost::format("account %s is absent") % command.accountId()).str(),
(boost::format("account %s is absent") % creator_account_id).str(),
command_name);
}
auto account_asset =
queries->getAccountAsset(command.accountId(), command.assetId());
queries->getAccountAsset(creator_account_id, command.assetId());

auto new_balance = command_amount | [this](const auto &amount) {
return amount_builder_.precision(amount->precision())
Expand All @@ -109,13 +109,13 @@ namespace iroha {
result = (*new_balance_val.value + account_asset.value()->balance())
| [this, &command](const auto &balance) {
return account_asset_builder_.balance(*balance)
.accountId(command.accountId())
.accountId(creator_account_id)
.assetId(command.assetId())
.build();
};
} else {
result = account_asset_builder_.balance(*new_balance_val.value)
.accountId(command.accountId())
.accountId(creator_account_id)
.assetId(command.assetId())
.build();
}
Expand Down Expand Up @@ -359,10 +359,10 @@ namespace iroha {
auto command_amount =
makeAmountWithPrecision(command.amount(), asset.value()->precision());
auto account_asset =
queries->getAccountAsset(command.accountId(), command.assetId());
queries->getAccountAsset(creator_account_id, command.assetId());
if (not account_asset) {
return makeCommandError((boost::format("%s do not have %s")
% command.accountId() % command.assetId())
% creator_account_id % command.assetId())
.str(),
command_name);
}
Expand Down Expand Up @@ -495,18 +495,9 @@ namespace iroha {
ametsuchi::WsvQuery &queries,
const shared_model::interface::types::AccountIdType &creator_account_id) {
auto command_name = "AddAssetQuantity";
// Check if creator has MoneyCreator permission.
// One can only add to his/her account
// TODO: 03.02.2018 grimadas IR-935, Separate asset creation for distinct
// asset types, now: anyone having permission "can_add_asset_qty" can add
// any asset
if (creator_account_id != command.accountId()) {
return makeCommandError(
"has permission command validation failed: creator account "
+ creator_account_id + " is not command account "
+ command.accountId(),
command_name);
}
if (not checkAccountRolePermission(
creator_account_id, queries, Role::kAddAssetQty)) {
return makeCommandError(
Expand Down Expand Up @@ -850,23 +841,14 @@ namespace iroha {
ametsuchi::WsvQuery &queries,
const shared_model::interface::types::AccountIdType &creator_account_id) {
auto command_name = "SubtractAssetQuantity";
if (creator_account_id == command.accountId()) {
if (checkAccountRolePermission(
creator_account_id, queries, Role::kSubtractAssetQty)) {
return {};
} else {
return makeCommandError(
"has permission command validation failed: account "
+ creator_account_id + " does not have permission "
+ toString(Role::kSubtractAssetQty) + " for his own account",
command_name);
}
if (checkAccountRolePermission(
creator_account_id, queries, Role::kSubtractAssetQty)) {
return {};
} else {
return makeCommandError(
"has permission command validation failed: account "
+ creator_account_id
+ " cannot subtract asset quantity from account "
+ command.accountId(),
+ creator_account_id + " does not have permission "
+ toString(Role::kSubtractAssetQty) + " for his own account",
command_name);
}
}
Expand Down
11 changes: 2 additions & 9 deletions irohad/model/commands/add_asset_quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ namespace iroha {
* Add amount of asset to an account
*/
struct AddAssetQuantity : public Command {
/**
* Account where to add assets
*/
std::string account_id;

/**
* Asset to issue
* Note: must exist in the system
Expand All @@ -49,10 +44,8 @@ namespace iroha {

AddAssetQuantity() {}

AddAssetQuantity(const std::string &account_id,
const std::string &asset_id,
Amount amount)
: account_id(account_id), asset_id(asset_id), amount(amount) {}
AddAssetQuantity(const std::string &asset_id, Amount amount)
: asset_id(asset_id), amount(amount) {}
};
} // namespace model
} // namespace iroha
Expand Down
11 changes: 2 additions & 9 deletions irohad/model/commands/subtract_asset_quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ namespace iroha {
* Subtract amount of asset to an account
*/
struct SubtractAssetQuantity : public Command {
/**
* Account where to subtract assets
*/
std::string account_id;

/**
* Asset to issue
* Note: must exist in the system
Expand All @@ -48,10 +43,8 @@ namespace iroha {

SubtractAssetQuantity() {}

SubtractAssetQuantity(const std::string &account_id,
const std::string &asset_id,
Amount amount)
: account_id(account_id), asset_id(asset_id), amount(amount) {}
SubtractAssetQuantity(const std::string &asset_id, Amount amount)
: asset_id(asset_id), amount(amount) {}
};
} // namespace model
} // namespace iroha
Expand Down
16 changes: 6 additions & 10 deletions irohad/model/converters/impl/json_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ namespace iroha {
rapidjson::Document dd;
precision = des.document["precision"].GetUint();

return boost::make_optional(Amount(value, static_cast<uint8_t>(precision)));
return boost::make_optional(
Amount(value, static_cast<uint8_t>(precision)));
}
};

Expand All @@ -80,9 +81,10 @@ namespace iroha {
return boost::none;
}

return boost::make_optional(Peer(address.value(),
iroha::hexstringToArray<iroha::pubkey_t::size()>(pubkey.value())
.value()));
return boost::make_optional(Peer(
address.value(),
iroha::hexstringToArray<iroha::pubkey_t::size()>(pubkey.value())
.value()));
}
};

Expand Down Expand Up @@ -149,8 +151,6 @@ namespace iroha {

document.SetObject();
document.AddMember("command_type", "AddAssetQuantity", allocator);
document.AddMember(
"account_id", add_asset_quantity->account_id, allocator);
document.AddMember("asset_id", add_asset_quantity->asset_id, allocator);

Value amount;
Expand All @@ -169,7 +169,6 @@ namespace iroha {
const Value &document) {
auto des = makeFieldDeserializer(document);
return make_optional_ptr<AddAssetQuantity>()
| des.String(&AddAssetQuantity::account_id, "account_id")
| des.String(&AddAssetQuantity::asset_id, "asset_id")
| des.Object(&AddAssetQuantity::amount, "amount") | toCommand;
}
Expand Down Expand Up @@ -563,8 +562,6 @@ namespace iroha {

document.SetObject();
document.AddMember("command_type", "SubtractAssetQuantity", allocator);
document.AddMember(
"account_id", subtract_asset_quantity->account_id, allocator);
document.AddMember(
"asset_id", subtract_asset_quantity->asset_id, allocator);

Expand All @@ -587,7 +584,6 @@ namespace iroha {
const Value &document) {
auto des = makeFieldDeserializer(document);
return make_optional_ptr<SubtractAssetQuantity>()
| des.String(&SubtractAssetQuantity::account_id, "account_id")
| des.String(&SubtractAssetQuantity::asset_id, "asset_id")
| des.Object(&SubtractAssetQuantity::amount, "amount") | toCommand;
}
Expand Down
6 changes: 0 additions & 6 deletions irohad/model/converters/impl/pb_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ namespace iroha {
protocol::AddAssetQuantity PbCommandFactory::serializeAddAssetQuantity(
const model::AddAssetQuantity &add_asset_quantity) {
protocol::AddAssetQuantity pb_add_asset_quantity;
pb_add_asset_quantity.set_account_id(add_asset_quantity.account_id);
pb_add_asset_quantity.set_asset_id(add_asset_quantity.asset_id);
auto amount = pb_add_asset_quantity.mutable_amount();
amount->CopyFrom(serializeAmount(add_asset_quantity.amount));
Expand All @@ -169,7 +168,6 @@ namespace iroha {
model::AddAssetQuantity PbCommandFactory::deserializeAddAssetQuantity(
const protocol::AddAssetQuantity &pb_add_asset_quantity) {
model::AddAssetQuantity add_asset_quantity;
add_asset_quantity.account_id = pb_add_asset_quantity.account_id();
add_asset_quantity.asset_id = pb_add_asset_quantity.asset_id();
add_asset_quantity.amount =
deserializeAmount(pb_add_asset_quantity.amount());
Expand All @@ -182,8 +180,6 @@ namespace iroha {
PbCommandFactory::serializeSubtractAssetQuantity(
const model::SubtractAssetQuantity &subtract_asset_quantity) {
protocol::SubtractAssetQuantity pb_subtract_asset_quantity;
pb_subtract_asset_quantity.set_account_id(
subtract_asset_quantity.account_id);
pb_subtract_asset_quantity.set_asset_id(
subtract_asset_quantity.asset_id);
auto amount = pb_subtract_asset_quantity.mutable_amount();
Expand All @@ -195,8 +191,6 @@ namespace iroha {
PbCommandFactory::deserializeSubtractAssetQuantity(
const protocol::SubtractAssetQuantity &pb_subtract_asset_quantity) {
model::SubtractAssetQuantity subtract_asset_quantity;
subtract_asset_quantity.account_id =
pb_subtract_asset_quantity.account_id();
subtract_asset_quantity.asset_id =
pb_subtract_asset_quantity.asset_id();
subtract_asset_quantity.amount =
Expand Down
8 changes: 2 additions & 6 deletions irohad/model/generators/command_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,10 @@ namespace iroha {
const std::string &account_id, uint32_t quorum);

std::shared_ptr<Command> generateAddAssetQuantity(
const std::string &account_id,
const std::string &asset_id,
const Amount &amount);
const std::string &asset_id, const Amount &amount);

std::shared_ptr<Command> generateSubtractAssetQuantity(
const std::string &account_id,
const std::string &asset_id,
const Amount &amount);
const std::string &asset_id, const Amount &amount);
/**
* Generate transfer assets from source account_id to target account_id
* @param src_account_id - source account identifier
Expand Down
13 changes: 4 additions & 9 deletions irohad/model/generators/impl/command_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,13 @@ namespace iroha {
}

std::shared_ptr<Command> CommandGenerator::generateAddAssetQuantity(
const std::string &account_id,
const std::string &asset_id,
const Amount &amount) {
return generateCommand<AddAssetQuantity>(account_id, asset_id, amount);
const std::string &asset_id, const Amount &amount) {
return generateCommand<AddAssetQuantity>(asset_id, amount);
}

std::shared_ptr<Command> CommandGenerator::generateSubtractAssetQuantity(
const std::string &account_id,
const std::string &asset_id,
const Amount &amount) {
return generateCommand<SubtractAssetQuantity>(
account_id, asset_id, amount);
const std::string &asset_id, const Amount &amount) {
return generateCommand<SubtractAssetQuantity>(asset_id, amount);
}

std::shared_ptr<Command> CommandGenerator::generateSetQuorum(
Expand Down
6 changes: 2 additions & 4 deletions irohad/model/impl/model_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ namespace iroha {
if (! instanceof <AddAssetQuantity>(command))
return false;
auto add_asset_quantity = static_cast<const AddAssetQuantity &>(command);
return add_asset_quantity.account_id == account_id
&& add_asset_quantity.asset_id == asset_id
return add_asset_quantity.asset_id == asset_id
&& add_asset_quantity.amount == amount;
}

Expand All @@ -108,8 +107,7 @@ namespace iroha {
return false;
auto subtract_asset_quantity =
static_cast<const SubtractAssetQuantity &>(command);
return subtract_asset_quantity.account_id == account_id
&& subtract_asset_quantity.asset_id == asset_id
return subtract_asset_quantity.asset_id == asset_id
&& subtract_asset_quantity.amount == amount;
}

Expand Down
Loading

0 comments on commit bf22e01

Please sign in to comment.