Skip to content

Commit

Permalink
Implement addition of new parameters to Iroha Core for enhanced messa…
Browse files Browse the repository at this point in the history
…ge communication, with corresponding handling capabilities in Iroha CLI.

Signed-off-by: dominious1 <[email protected]>
  • Loading branch information
dominious1 committed Nov 12, 2023
1 parent f2d96e3 commit 5858cf0
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 30 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 title = params[2];
return generator_.generateAddAssetQuantity(asset_id, amount, title);
}

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 title = params[2];
return generator_.generateSubtractAssetQuantity(asset_id, amount, title);
}

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& title)
{
auto cmd = cmd_generator_.generateAddAssetQuantity(
asset_id,
amount);
amount,
title);
addCommand(*cmd);
return *this;
}
Expand Down Expand Up @@ -180,11 +182,13 @@ Tx& Tx::setAccountQuorum(

Tx& Tx::subtractAssetQuantity(
const std::string& asset_id,
const std::string& amount)
const std::string& amount,
const std::string& title)
{
auto cmd = cmd_generator_.generateSubtractAssetQuantity(
asset_id,
amount);
amount,
title);
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& title)
{
AddAssetQuantity addAssetQuantity;
addAssetQuantity.set_asset_id(asset_id);
addAssetQuantity.set_amount(amount);
if (!title.empty()) {
addAssetQuantity.set_title(title);
}

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& title)
{
SubtractAssetQuantity subtractAssetQuantity;
subtractAssetQuantity.set_asset_id(asset_id);
subtractAssetQuantity.set_amount(amount);
std::optional<std::string> title_optional = title;
if (title_optional) {
subtractAssetQuantity.set_title(*title_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;

/**
* Title
*/
std::string title;

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 &title)
: asset_id(asset_id), amount(amount), title(title) {}
};
} // 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;

/**
* Title
*/
std::string title;

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), title(title) {}
};
} // namespace model
} // namespace iroha
Expand Down
7 changes: 5 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("title", add_asset_quantity->title, 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::title, "title") | toCommand;
}

// AddPeer
Expand Down Expand Up @@ -520,7 +522,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::title, "title") | 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.title = pb_subtract_asset_quantity.title();
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 &title) {
return generateCommand<AddAssetQuantity>(asset_id, amount, title);
}

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 &title) {
return generateCommand<SubtractAssetQuantity>(asset_id, amount, title);
}

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.title == title;
}

/* 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::title() const {
return title_;
}

} // 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::title() const {
return title_;
}

} // 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 &title() const override;

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

const interface::Amount amount_;
const std::string title_;
};
} // 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 &title() const override;

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

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

} // 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 &title() 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("title", title())
.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 title() == rhs.title();
}

} // 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("title", title())
.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 title() == rhs.title();
}

} // 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 title
*/
virtual const std::string &title() const = 0;

std::string toString() const override;

bool operator==(const ModelType &rhs) const override;
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 title = 3;
}

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

message CompareAndSetAccountDetail {
Expand Down
2 changes: 2 additions & 0 deletions test/module/irohad/model/converters/json_commands_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ TEST_F(JsonCommandTest, InvalidWhenUnknownCommandType) {
"command_type": "Unknown",
"account_id": "admin@test",
"asset_id": "usd#test",
"title": "testtitle",
"amount": {
"int_part": -20,
"frac_part": 0
Expand All @@ -95,6 +96,7 @@ TEST_F(JsonCommandTest, add_asset_quantity) {

orig_command->amount = "1.50";
orig_command->asset_id = "23";
orig_command->title = "testtitle";

auto json_command = factory.serializeAddAssetQuantity(orig_command);
auto serial_command = factory.deserializeAddAssetQuantity(json_command);
Expand Down
Loading

0 comments on commit 5858cf0

Please sign in to comment.