Skip to content

Commit

Permalink
Introduce an optional title parameter to the AddAssetQuantity and Sub…
Browse files Browse the repository at this point in the history
…tractAssetQuantity, enhancing functionality within Iroha Core for improved message communication. Ensure corresponding handling capabilities in Iroha CLI for seamless integration. (#4003)

* Implement addition of new parameters to Iroha Core for enhanced message communication, with corresponding handling capabilities in Iroha CLI.

* Improve GTests in reference to param title

* Enhance test coverage in the JsonCommandTest class by addressing serialization and deserialization scenarios for various command types. This commit ensures comprehensive testing, including edge cases and unknown command types, to guarantee the robustness of the JsonCommandFactory's functionality.

* Enhance test cases related to the SubtractAccountAsset functionality to improve reliability, readability, and maintainability. This commit refactors and strengthens the existing test suite, addressing scenarios such as permission checks, domain permission validity, asset presence, precision validation, and asset quantity sufficiency. These improvements contribute to a more robust testing environment for the SubtractAccountAsset functionality.

Signed-off-by: dominious1 <piotr567@proton.me>

---------

Signed-off-by: dominious1 <piotr567@proton.me>
Signed-off-by: dominious1 <140593941+dominious1@users.noreply.github.com>
Co-authored-by: G. Bazior <bazior@agh.edu.pl>
  • Loading branch information
dominious1 and baziorek authored Jan 19, 2024
1 parent 91b8b5a commit cd31f16
Show file tree
Hide file tree
Showing 36 changed files with 220 additions and 83 deletions.
6 changes: 4 additions & 2 deletions iroha-cli/interactive/impl/interactive_transaction_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ namespace iroha_cli {
std::vector<std::string> params) {
auto asset_id = params[0];
auto amount = params[1];
return generator_.generateAddAssetQuantity(asset_id, amount);
auto description = params[2];
return generator_.generateAddAssetQuantity(asset_id, amount, description);
}

std::shared_ptr<iroha::model::Command>
Expand Down Expand Up @@ -405,7 +406,8 @@ namespace iroha_cli {
std::vector<std::string> params) {
auto asset_id = params[0];
auto amount = params[1];
return generator_.generateSubtractAssetQuantity(asset_id, amount);
auto description = params[2];
return generator_.generateSubtractAssetQuantity(asset_id, amount, description);
}

std::shared_ptr<iroha::model::Command>
Expand Down
2 changes: 1 addition & 1 deletion iroha-lib/examples/DomainAssetCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ iroha::protocol::Transaction generateTransactionWhichAddsAssetQuantiti(
return iroha_lib::Tx(
account_name,
keypair)
.addAssetQuantity(assetIdWithDomain, assetAmount)
.addAssetQuantity(assetIdWithDomain, assetAmount, "")
.signAndAddSignature();
}

Expand Down
12 changes: 8 additions & 4 deletions iroha-lib/model/Tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ void Tx::addCommand(const iroha::protocol::Command& command)

Tx& Tx::addAssetQuantity(
const std::string& asset_id,
const std::string& amount)
const std::string& amount,
const std::string& description)
{
auto cmd = cmd_generator_.generateAddAssetQuantity(
asset_id,
amount);
amount,
description);
addCommand(*cmd);
return *this;
}
Expand Down Expand Up @@ -191,11 +193,13 @@ Tx& Tx::setAccountQuorum(

Tx& Tx::subtractAssetQuantity(
const std::string& asset_id,
const std::string& amount)
const std::string& amount,
const std::string& description)
{
auto cmd = cmd_generator_.generateSubtractAssetQuantity(
asset_id,
amount);
amount,
description);
addCommand(*cmd);
return *this;
}
Expand Down
8 changes: 5 additions & 3 deletions iroha-lib/model/Tx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class Tx {
void addCommand(const iroha::protocol::Command& command);

Tx& addAssetQuantity(
const std::string& account_id,
const std::string& role_name);
const std::string& asset_id,
const std::string& amount,
const std::string& title);
Tx& addPeer(
const std::string& address,
const std::string& pubkey,
Expand Down Expand Up @@ -83,7 +84,8 @@ class Tx {
uint32_t quorum);
Tx& subtractAssetQuantity(
const std::string& asset_id,
const std::string& amount);
const std::string& amount,
const std::string& title);
Tx& transferAsset(
const std::string& account_id,
const std::string& dest_account_id,
Expand Down
13 changes: 11 additions & 2 deletions iroha-lib/model/generators/CommandGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ namespace iroha_lib {

std::shared_ptr<Command> CommandGenerator::generateAddAssetQuantity(
const std::string& asset_id,
const std::string& amount)
const std::string& amount,
const std::string& description)
{
AddAssetQuantity addAssetQuantity;
addAssetQuantity.set_asset_id(asset_id);
addAssetQuantity.set_amount(amount);
if (!description.empty()) {
addAssetQuantity.set_description(description);
}

auto cmd = Command();
cmd.set_allocated_add_asset_quantity(new AddAssetQuantity(addAssetQuantity));
Expand Down Expand Up @@ -227,11 +231,16 @@ std::shared_ptr<Command> CommandGenerator::generateSetAccountQuorum(

std::shared_ptr<Command> CommandGenerator::generateSubtractAssetQuantity(
const std::string& asset_id,
const std::string& amount)
const std::string& amount,
const std::string& description)
{
SubtractAssetQuantity subtractAssetQuantity;
subtractAssetQuantity.set_asset_id(asset_id);
subtractAssetQuantity.set_amount(amount);
std::optional<std::string> description_optional = description;
if (description_optional) {
subtractAssetQuantity.set_description(*description_optional);
}

auto cmd = Command();
cmd.set_allocated_subtract_asset_quantity(new SubtractAssetQuantity(subtractAssetQuantity));
Expand Down
6 changes: 4 additions & 2 deletions iroha-lib/model/generators/CommandGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class CommandGenerator {

std::shared_ptr<Command> generateAddAssetQuantity(
const std::string& asset_id,
const std::string& amount);
const std::string& amount,
const std::string& title);
std::shared_ptr<Command> generateAddPeer(
const std::string& address,
const std::string& pubkey,
Expand Down Expand Up @@ -67,7 +68,8 @@ class CommandGenerator {
const std::string& account_id, uint32_t quorum);
std::shared_ptr<Command> generateSubtractAssetQuantity(
const std::string& asset_id,
const std::string& amount);
const std::string& amount,
const std::string& title);
std::shared_ptr<Command> generateTransferAsset(
const std::string& account_id,
const std::string& dest_account_id,
Expand Down
9 changes: 7 additions & 2 deletions irohad/model/commands/add_asset_quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ namespace iroha {
*/
std::string amount;

/**
* Description
*/
std::string description;

bool operator==(const Command &command) const override;

AddAssetQuantity() {}

AddAssetQuantity(const std::string &asset_id, const std::string &amount)
: asset_id(asset_id), amount(amount) {}
AddAssetQuantity(const std::string &asset_id, const std::string &amount, const std::string &description)
: asset_id(asset_id), amount(amount), description(description) {}
};
} // namespace model
} // namespace iroha
Expand Down
10 changes: 8 additions & 2 deletions irohad/model/commands/subtract_asset_quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ namespace iroha {
*/
std::string amount;

/**
* Description
*/
std::string description;

bool operator==(const Command &command) const override;

SubtractAssetQuantity() {}

SubtractAssetQuantity(const std::string &asset_id,
const std::string &amount)
: asset_id(asset_id), amount(amount) {}
const std::string &amount,
const std::string &title)
: asset_id(asset_id), amount(amount), description(description) {}
};
} // namespace model
} // namespace iroha
Expand Down
9 changes: 7 additions & 2 deletions irohad/model/converters/impl/json_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ namespace iroha {
document.AddMember("command_type", "AddAssetQuantity", allocator);
document.AddMember("asset_id", add_asset_quantity->asset_id, allocator);
document.AddMember("amount", add_asset_quantity->amount, allocator);
document.AddMember("description", add_asset_quantity->description, allocator);

return document;
}
Expand All @@ -122,7 +123,8 @@ namespace iroha {
auto des = makeFieldDeserializer(document);
return make_optional_ptr<AddAssetQuantity>()
| des.String(&AddAssetQuantity::asset_id, "asset_id")
| des.String(&AddAssetQuantity::amount, "amount") | toCommand;
| des.String(&AddAssetQuantity::amount, "amount")
| des.String(&AddAssetQuantity::description, "description") | toCommand;
}

// AddPeer
Expand Down Expand Up @@ -510,6 +512,8 @@ namespace iroha {
"asset_id", subtract_asset_quantity->asset_id, allocator);
document.AddMember(
"amount", subtract_asset_quantity->amount, allocator);
document.AddMember(
"description", subtract_asset_quantity->description, allocator);

return document;
}
Expand All @@ -520,7 +524,8 @@ namespace iroha {
auto des = makeFieldDeserializer(document);
return make_optional_ptr<SubtractAssetQuantity>()
| des.String(&SubtractAssetQuantity::asset_id, "asset_id")
| des.String(&SubtractAssetQuantity::amount, "amount") | toCommand;
| des.String(&SubtractAssetQuantity::amount, "amount")
| des.String(&SubtractAssetQuantity::description, "description") | toCommand;
}

// Abstract
Expand Down
2 changes: 1 addition & 1 deletion irohad/model/converters/impl/pb_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ namespace iroha {
subtract_asset_quantity.asset_id =
pb_subtract_asset_quantity.asset_id();
subtract_asset_quantity.amount = pb_subtract_asset_quantity.amount();

subtract_asset_quantity.description = pb_subtract_asset_quantity.description();
return subtract_asset_quantity;
}

Expand Down
4 changes: 2 additions & 2 deletions irohad/model/generators/command_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ namespace iroha {
const std::string &account_id, uint32_t quorum);

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

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

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

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

std::shared_ptr<Command> CommandGenerator::generateSetQuorum(
Expand Down
3 changes: 2 additions & 1 deletion irohad/model/impl/model_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ namespace iroha {
auto subtract_asset_quantity =
static_cast<const SubtractAssetQuantity &>(command);
return subtract_asset_quantity.asset_id == asset_id
&& subtract_asset_quantity.amount == amount;
&& subtract_asset_quantity.amount == amount
&& subtract_asset_quantity.description == description;
}

/* AddPeer */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ namespace shared_model {
return amount_;
}

const std::string &AddAssetQuantity::description() const {
return description_;
}

} // namespace proto
} // namespace shared_model
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ namespace shared_model {
return amount_;
}

const std::string &SubtractAssetQuantity::description() const {
return description_;
}

} // namespace proto
} // namespace shared_model
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ namespace shared_model {

const interface::Amount &amount() const override;

const std::string &description() const override;

private:
const iroha::protocol::AddAssetQuantity &add_asset_quantity_;

const interface::Amount amount_;
const std::string description_;
};
} // namespace proto
} // namespace shared_model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ namespace shared_model {

const interface::Amount &amount() const override;

const std::string &description() const override;

private:
const iroha::protocol::SubtractAssetQuantity &subtract_asset_quantity_;

const interface::Amount amount_;
const std::string description_;
};

} // namespace proto
Expand Down
2 changes: 2 additions & 0 deletions shared_model/interfaces/commands/add_asset_quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace shared_model {
*/
virtual const Amount &amount() const = 0;

virtual const std::string &description() const = 0;

std::string toString() const override;

bool operator==(const ModelType &rhs) const override;
Expand Down
3 changes: 2 additions & 1 deletion shared_model/interfaces/commands/impl/add_asset_quantity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ namespace shared_model {
.init("AddAssetQuantity")
.appendNamed("asset_id", assetId())
.appendNamed("amount", amount())
.appendNamed("description", description())
.finalize();
}

bool AddAssetQuantity::operator==(const ModelType &rhs) const {
return assetId() == rhs.assetId() and amount() == rhs.amount();
return assetId() == rhs.assetId() and amount() == rhs.amount() and description() == rhs.description();
}

} // namespace interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ namespace shared_model {
.init("SubtractAssetQuantity")
.appendNamed("asset_id", assetId())
.appendNamed("amount", amount())
.appendNamed("description", description())
.finalize();
}

bool SubtractAssetQuantity::operator==(const ModelType &rhs) const {
return assetId() == rhs.assetId() and amount() == rhs.amount();
return assetId() == rhs.assetId() and amount() == rhs.amount() and description() == rhs.description();
}

} // namespace interface
Expand Down
5 changes: 5 additions & 0 deletions shared_model/interfaces/commands/subtract_asset_quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ namespace shared_model {
*/
virtual const Amount &amount() const = 0;

/**
* @return description
*/
virtual const std::string &description() const = 0;

std::string toString() const override;

bool operator==(const ModelType &rhs) const override;
Expand Down
2 changes: 2 additions & 0 deletions shared_model/interfaces/common_objects/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace shared_model {
using DomainIdType = std::string;
/// Type of asset id
using AssetIdType = std::string;
/// Type of description
using DescriptionType = std::string;
/// Permission type used in permission commands
using PermissionNameType = std::string;
/// Permission set
Expand Down
2 changes: 2 additions & 0 deletions shared_model/schema/commands.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import "primitive.proto";
message AddAssetQuantity {
string asset_id = 1;
string amount = 2;
optional string description = 3;
}

message AddPeer {
Expand Down Expand Up @@ -97,6 +98,7 @@ message RevokePermission {
message SubtractAssetQuantity {
string asset_id = 1;
string amount = 2;
optional string description = 3;
}

message CompareAndSetAccountDetail {
Expand Down
Loading

0 comments on commit cd31f16

Please sign in to comment.