Skip to content

Commit

Permalink
Merge branch 'feature/pb-factories' of https://github.com/hyperledger…
Browse files Browse the repository at this point in the history
…/iroha into feature/pb-factories
  • Loading branch information
muratovv authored and lebdron committed Jul 19, 2017
2 parents 88b7a6d + 2eb1b62 commit f675e32
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 52 deletions.
42 changes: 29 additions & 13 deletions irohad/ametsuchi/impl/block_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,12 @@ namespace iroha {
writer.String(add_asset_quantity.asset_id.c_str());

writer.String("amount");

writer.Double(
std::decimal::decimal64_to_double(add_asset_quantity.amount));
writer.StartArray();
writer.String("int_part");
writer.Uint64(add_asset_quantity.amount.int_part);
writer.String("frac_part");
writer.Uint64(add_asset_quantity.amount.frac_part);
writer.EndArray();

writer.EndObject();
}
Expand Down Expand Up @@ -405,7 +408,12 @@ namespace iroha {
writer.String(transfer_asset.asset_id.c_str());

writer.String("amount");
writer.Double(std::decimal::decimal64_to_double(transfer_asset.amount));
writer.StartArray();
writer.String("int_part");
writer.Uint64(transfer_asset.amount.int_part);
writer.String("frac_part");
writer.Uint64(transfer_asset.amount.frac_part);
writer.EndArray();

writer.EndObject();
}
Expand Down Expand Up @@ -574,14 +582,16 @@ namespace iroha {
std::make_shared<model::CreateDomain>(create_domain.value()));
}
} else if (command_type == "RemoveSignatory") {
if (auto remove_signatory = deserialize_remove_signatory(json_command)) {
commands.push_back(
std::make_shared<model::RemoveSignatory>(remove_signatory.value()));
if (auto remove_signatory =
deserialize_remove_signatory(json_command)) {
commands.push_back(std::make_shared<model::RemoveSignatory>(
remove_signatory.value()));
}
} else if (command_type == "SetAccountPermissions") {
if (auto set_account_permissions = deserialize_set_account_permissions(json_command)) {
commands.push_back(
std::make_shared<model::SetAccountPermissions>(set_account_permissions.value()));
if (auto set_account_permissions =
deserialize_set_account_permissions(json_command)) {
commands.push_back(std::make_shared<model::SetAccountPermissions>(
set_account_permissions.value()));
}
} else if (command_type == "SetQuorum") {
if (auto set_quorum = deserialize_set_quorum(json_command)) {
Expand Down Expand Up @@ -627,7 +637,10 @@ namespace iroha {
add_asset_quantity.asset_id = json_command["asset_id"].GetString();

// amount
auto amount = std::decimal::decimal64(json_command["amount"].GetDouble());
auto json_amount = json_command["amount"].GetObject();
Amount amount;
amount.int_part = json_amount["int_part"].GetUint64();
amount.frac_part = json_amount["frac_part"].GetUint64();
add_asset_quantity.amount = amount;

return add_asset_quantity;
Expand Down Expand Up @@ -802,8 +815,11 @@ namespace iroha {
transferAsset.asset_id = json_command["asset_id"].GetString();

// amount
transferAsset.amount =
std::decimal::decimal64(json_command["amount"].GetDouble());
auto json_amount = json_command["amount"].GetObject();
Amount amount;
amount.int_part = json_amount["int_part"].GetUint64();
amount.frac_part = json_amount["frac_part"].GetUint64();
transferAsset.amount = amount;

return transferAsset;
}
Expand Down
42 changes: 15 additions & 27 deletions irohad/model/impl/command_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,23 @@ namespace iroha {
return false;

auto account_asset = queries.getAccountAsset(account_id, asset_id);
auto precision = asset.value().precision;

AccountAsset accountAsset;
// Such accountAsset not found
if (!account_asset) {
// No wallet found -> create new
accountAsset = AccountAsset();
accountAsset.asset_id = asset_id;
accountAsset.account_id = account_id;
accountAsset.balance =
static_cast<uint64_t>(std::decimal::decimal_to_double(amount) *
std::pow(10, asset.value().precision));
accountAsset.balance = amount.get_joint_amount(precision);
} else {
accountAsset = account_asset.value();
auto current_balance = std::decimal::make_decimal64(
(unsigned long long int)account_asset.value().balance,
-asset.value().precision);
auto new_balance = current_balance + amount;
// TODO: handle non trivial arithmetic
auto new_balance =
account_asset.value().balance + amount.get_joint_amount(precision);
// TODO: handle overflow
accountAsset.balance =
static_cast<uint64_t>(std::decimal::decimal_to_double(new_balance) *
std::pow(10, asset.value().precision));
accountAsset.balance = new_balance;
}

// accountAsset.value().balance += amount;
Expand Down Expand Up @@ -174,38 +171,29 @@ namespace iroha {
auto precision = asset.value().precision;

// Get src balance
auto src_balance = std::decimal::make_decimal64(
(unsigned long long int)src_account_assert.value().balance,
-precision);
//
src_balance -= amount;
auto src_balance = src_account_assert.value().balance;
// TODO: handle non-trivial arithmetic
src_balance -= amount.get_joint_amount(precision);
// Set new balance for source account
src_account_assert.value().balance =
static_cast<uint64_t>(std::decimal::decimal_to_double(src_balance) *
std::pow(10, precision));
src_account_assert.value().balance = src_balance;

if (!dest_account_assert) {
// This assert is new for this account - create new AccountAsset
dest_AccountAssert = AccountAsset();
dest_AccountAssert.asset_id = asset_id;
dest_AccountAssert.account_id = dest_account_id;
// Set new balance for dest account
dest_AccountAssert.balance = static_cast<uint64_t>(
std::decimal::decimal_to_double(amount) * std::pow(10, precision));
dest_AccountAssert.balance = amount.get_joint_amount(precision);

} else {
// Account already has such asset
dest_AccountAssert = dest_account_assert.value();
// Get balance dest account
auto dest_balance = std::decimal::make_decimal64(
(unsigned long long int)dest_account_assert.value().balance,
-precision);
auto dest_balance = dest_account_assert.value().balance;

dest_balance += amount;
dest_balance += amount.get_joint_amount(precision);
// Set new balance for dest
dest_AccountAssert.balance = static_cast<uint64_t>(
std::decimal::decimal_to_double(dest_balance) *
std::pow(10, precision));
dest_AccountAssert.balance = dest_balance;
}

return commands.upsertAccountAsset(dest_AccountAssert) &&
Expand Down
23 changes: 11 additions & 12 deletions irohad/model/impl/stateful_command_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,17 @@ namespace iroha {
if (!account_asset || !asset) return false;
// Amount is formed wrong
if (amount.get_frac_number() > asset.value().precision) return false;
auto precision = asset.value().precision;
amount.get_joint_amount(precision);

return std::decimal::make_decimal64(
(unsigned long long int)account_asset.value().balance,
-asset.value().precision) < amount &&
// Check if src_account exist
queries.getAccount(src_account_id) &&
// Can account transfer assets
creator.permissions.can_transfer &&
// Creator can transfer only from their account
creator.account_id == src_account_id;

return
// Check if src_account exist
queries.getAccount(src_account_id) &&
// Can account transfer assets
creator.permissions.can_transfer &&
// Creator can transfer only from their account
creator.account_id == src_account_id &&
// Balance in your wallet should be at least amount of transfer
account_asset.value().balance >=
amount.get_joint_amount(asset.value().precision);
}

} // namespace model
Expand Down

0 comments on commit f675e32

Please sign in to comment.