Skip to content

Commit

Permalink
Merge pull request hyperledger-iroha#732 from hyperledger/feature/nes…
Browse files Browse the repository at this point in the history
…ted-kv-storage

Feature/nested kv storage
  • Loading branch information
grimadas authored Dec 8, 2017
2 parents af991d6 + 4b8c9be commit c81dd57
Show file tree
Hide file tree
Showing 21 changed files with 164 additions and 135 deletions.
22 changes: 15 additions & 7 deletions irohad/ametsuchi/impl/postgres_wsv_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,24 @@ namespace iroha {
}

bool PostgresWsvCommand::setAccountKV(const std::string &account_id,
const std::string &creator_account_id,
const std::string &key,
const std::string &val) {
try {
transaction_.exec("UPDATE account SET data = jsonb_set(data,"
+ transaction_.quote("{" + key + "}")
+ ","
+ transaction_.quote("\"" + val + "\"")
+ ") WHERE account_id="
+ transaction_.quote(account_id)
+ ";");
transaction_.exec(
"UPDATE account SET data = jsonb_set(CASE WHEN data ?"
+ transaction_.quote(creator_account_id)
+ " THEN data ELSE jsonb_set(data, "
+ transaction_.quote("{" + creator_account_id + "}")
+ ","
+ transaction_.quote("{}")
+ ") END,"
+ transaction_.quote("{" + creator_account_id + ", " + key + "}")
+ ","
+ transaction_.quote("\"" + val + "\"")
+ ") WHERE account_id="
+ transaction_.quote(account_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return false;
Expand Down
1 change: 1 addition & 0 deletions irohad/ametsuchi/impl/postgres_wsv_command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace iroha {
bool insertAccount(const model::Account &account) override;
bool updateAccount(const model::Account &account) override;
bool setAccountKV(const std::string &account_id,
const std::string &creator_account_id,
const std::string &key,
const std::string &val) override;
bool insertAsset(const model::Asset &asset) override;
Expand Down
44 changes: 30 additions & 14 deletions irohad/ametsuchi/impl/postgres_wsv_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ namespace iroha {
result = transaction_.exec(
"SELECT * FROM account_has_grantable_permissions WHERE "
"permittee_account_id = "
+ transaction_.quote(permitee_account_id) + " AND account_id = "
+ transaction_.quote(account_id) + " AND permission_id = "
+ transaction_.quote(permission_id) + ";");
+ transaction_.quote(permitee_account_id)
+ " AND account_id = "
+ transaction_.quote(account_id)
+ " AND permission_id = "
+ transaction_.quote(permission_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return false;
Expand All @@ -58,7 +61,8 @@ namespace iroha {
try {
result = transaction_.exec(
"SELECT role_id FROM account_has_roles WHERE account_id = "
+ transaction_.quote(account_id) + ";");
+ transaction_.quote(account_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand All @@ -76,7 +80,8 @@ namespace iroha {
try {
result = transaction_.exec(
"SELECT permission_id FROM role_has_permissions WHERE role_id = "
+ transaction_.quote(role_name) + ";");
+ transaction_.quote(role_name)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand Down Expand Up @@ -107,7 +112,8 @@ namespace iroha {
pqxx::result result;
try {
result = transaction_.exec("SELECT * FROM account WHERE account_id = "
+ transaction_.quote(account_id) + ";");
+ transaction_.quote(account_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand All @@ -127,12 +133,17 @@ namespace iroha {
}

nonstd::optional<std::string> PostgresWsvQuery::getAccountDetail(
const std::string &account_id, const std::string &detail) {
const std::string &account_id,
const std::string &creator_account_id,
const std::string &detail) {
pqxx::result result;
try {
result = transaction_.exec("SELECT data->>" + transaction_.quote(detail)
+ " FROM account WHERE account_id = "
+ transaction_.quote(account_id) + ";");
result = transaction_.exec(
"SELECT data#>>"
+ transaction_.quote("{" + creator_account_id + ", " + detail + "}")
+ " FROM account WHERE account_id = "
+ transaction_.quote(account_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand All @@ -158,7 +169,8 @@ namespace iroha {
try {
result = transaction_.exec(
"SELECT public_key FROM account_has_signatory WHERE account_id = "
+ transaction_.quote(account_id) + ";");
+ transaction_.quote(account_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand All @@ -177,7 +189,8 @@ namespace iroha {
pqxx::result result;
try {
result = transaction_.exec("SELECT * FROM asset WHERE asset_id = "
+ transaction_.quote(asset_id) + ";");
+ transaction_.quote(asset_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand All @@ -204,7 +217,9 @@ namespace iroha {
result = transaction_.exec(
"SELECT * FROM account_has_asset WHERE account_id = "
+ transaction_.quote(account_id)
+ " AND asset_id = " + transaction_.quote(asset_id) + ";");
+ " AND asset_id = "
+ transaction_.quote(asset_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand All @@ -228,7 +243,8 @@ namespace iroha {
pqxx::result result;
try {
result = transaction_.exec("SELECT * FROM domain WHERE domain_id = "
+ transaction_.quote(domain_id) + ";");
+ transaction_.quote(domain_id)
+ ";");
} catch (const std::exception &e) {
log_->error(e.what());
return nullopt;
Expand Down
4 changes: 3 additions & 1 deletion irohad/ametsuchi/impl/postgres_wsv_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ namespace iroha {
nonstd::optional<model::Account> getAccount(
const std::string &account_id) override;
nonstd::optional<std::string> getAccountDetail(
const std::string &account_id, const std::string &detail) override;
const std::string &account_id,
const std::string &creator_account_id,
const std::string &detail) override;
nonstd::optional<std::vector<pubkey_t>> getSignatories(
const std::string &account_id) override;
nonstd::optional<model::Asset> getAsset(
Expand Down
43 changes: 23 additions & 20 deletions irohad/ametsuchi/wsv_command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#ifndef IROHA_WSV_COMMAND_HPP
#define IROHA_WSV_COMMAND_HPP

#include <common/types.hpp>
#include <model/account.hpp>
#include <model/account_asset.hpp>
#include <model/asset.hpp>
#include <model/domain.hpp>
#include <model/peer.hpp>
#include <string>
#include <set>
#include <string>
#include "common/types.hpp"
#include "model/account.hpp"
#include "model/account_asset.hpp"
#include "model/asset.hpp"
#include "model/domain.hpp"
#include "model/peer.hpp"

namespace iroha {
namespace ametsuchi {
Expand Down Expand Up @@ -53,14 +53,14 @@ namespace iroha {
virtual bool insertAccountRole(const std::string &account_id,
const std::string &role_name) = 0;


/**
* Bind role and permissions
* @param role_id
* @param permissions
* @return true is insert successful, false otherwise
*/
virtual bool insertRolePermissions(const std::string &role_id,
virtual bool insertRolePermissions(
const std::string &role_id,
const std::set<std::string> &permissions) = 0;

/**
Expand All @@ -72,18 +72,21 @@ namespace iroha {
*/
virtual bool insertAccountGrantablePermission(
const std::string &permittee_account_id,
const std::string &account_id, const std::string &permission_id) = 0;
const std::string &account_id,
const std::string &permission_id) = 0;

/**
* Delete grantable permission
* @param permittee_account_id to who the grant permission was previously granted
* @param permittee_account_id to who the grant permission was previously
* granted
* @param account_id on which account
* @param permission_id what permission
* @return true is execution is successful
*/
virtual bool deleteAccountGrantablePermission(
const std::string &permittee_account_id,
const std::string &account_id, const std::string &permission_id) = 0;
const std::string &account_id,
const std::string &permission_id) = 0;

/**
*
Expand All @@ -101,11 +104,14 @@ namespace iroha {

/**
* @param account_id account in which update key value
* @param creator_account_id creator's account who wants to update
* account_id
* @param key - key to set
* @param val - value of the key/value pair
* @return true if no error occurred, false otherwise
*/
virtual bool setAccountKV(const std::string &account_id,
const std::string &creator_account_id,
const std::string &key,
const std::string &val) = 0;

Expand Down Expand Up @@ -136,27 +142,24 @@ namespace iroha {
* @param signatory
* @return
*/
virtual bool insertAccountSignatory(
const std::string &account_id,
const pubkey_t &signatory) = 0;
virtual bool insertAccountSignatory(const std::string &account_id,
const pubkey_t &signatory) = 0;

/**
* Delete account signatory relationship
* @param account_id
* @param signatory
* @return
*/
virtual bool deleteAccountSignatory(
const std::string &account_id,
const pubkey_t &signatory) = 0;
virtual bool deleteAccountSignatory(const std::string &account_id,
const pubkey_t &signatory) = 0;

/**
* Delete signatory
* @param signatory
* @return
*/
virtual bool deleteSignatory(
const pubkey_t &signatory) = 0;
virtual bool deleteSignatory(const pubkey_t &signatory) = 0;

/**
*
Expand Down
5 changes: 4 additions & 1 deletion irohad/ametsuchi/wsv_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ namespace iroha {
/**
* Get accounts information from its key-value storage
* @param account_id
* @param creator_account_id
* @param detail
* @return
*/
virtual nonstd::optional<std::string> getAccountDetail(
const std::string &account_id, const std::string &detail) = 0;
const std::string &account_id,
const std::string &creator_account_id,
const std::string &detail) = 0;

/**
* Get signatories of account by user account_id
Expand Down
15 changes: 3 additions & 12 deletions irohad/model/commands/create_account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,14 @@ namespace iroha {
*/
pubkey_t pubkey;

/**
* Json data for the account
*/
std::string json_data;

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

CreateAccount() : json_data("{}") {}
CreateAccount() {}

CreateAccount(const std::string &account_name,
const std::string &domain_id,
const pubkey_t &pubkey,
const std::string &data)
: account_name(account_name),
domain_id(domain_id),
pubkey(pubkey),
json_data(data) {}
const pubkey_t &pubkey)
: account_name(account_name), domain_id(domain_id), pubkey(pubkey) {}
};
} // namespace model
} // namespace iroha
Expand Down
14 changes: 3 additions & 11 deletions irohad/model/converters/impl/json_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,17 @@ namespace iroha {
document.AddMember("domain_id", create_account->domain_id, allocator);
document.AddMember(
"pubkey", create_account->pubkey.to_hexstring(), allocator);
document.AddMember("json_data", create_account->json_data, allocator);

return document;
}

optional_ptr<Command> JsonCommandFactory::deserializeCreateAccount(
const Value &document) {
auto des = makeFieldDeserializer(document);
auto des_val = make_optional_ptr<CreateAccount>()
return make_optional_ptr<CreateAccount>()
| des.String(&CreateAccount::account_name, "account_name")
| des.String(&CreateAccount::domain_id, "domain_id")
| des.String(&CreateAccount::pubkey, "pubkey");

if (document.HasMember("json_data")) {
des_val =
des_val | des.String(&CreateAccount::json_data, "json_data");
}

return des_val | toCommand;
| des.String(&CreateAccount::pubkey, "pubkey")
| toCommand;
}

// Set Account Detail
Expand Down
23 changes: 11 additions & 12 deletions irohad/model/converters/impl/pb_command_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
*/

#include "model/converters/pb_command_factory.hpp"
#include "model/converters/pb_common.hpp"

#include <commands.pb.h>
#include <model/permissions.hpp>
#include <set>

#include <boost/assign/list_inserter.hpp>
#include "model/converters/pb_common.hpp"

namespace iroha {
namespace model {
Expand Down Expand Up @@ -60,8 +55,6 @@ namespace iroha {
(protocol::RolePermission::can_create_domain, can_create_domain)
// Can create account
(protocol::RolePermission::can_create_account, can_create_account)
// Can set quorum
(protocol::RolePermission::can_set_quorum, can_set_quorum)
// Can add peer
(protocol::RolePermission::can_add_peer, can_add_peer)
// Can add asset quantity
Expand Down Expand Up @@ -93,11 +86,14 @@ namespace iroha {
// Can grant add signatory
(protocol::RolePermission::can_grant_add_signatory,
can_grant + can_add_signatory)
// Can grant + can_transfer
(protocol::RolePermission::can_grant_can_transfer,
// Can grant + can_transfer
(protocol::RolePermission::can_grant_can_transfer,
can_grant + can_transfer)
// Can get roles
(protocol::RolePermission::can_get_roles, can_get_roles);
(protocol::RolePermission::can_get_roles, can_get_roles)
// Can write details to other accounts
(protocol::RolePermission::can_grant_can_set_detail,
can_grant + can_set_detail);

boost::assign::insert(pb_grant_map_)
// Can add my signatory
Expand All @@ -107,7 +103,10 @@ namespace iroha {
(protocol::GrantablePermission::can_remove_my_signatory,
can_remove_signatory)
// Can set my quorum
(protocol::GrantablePermission::can_set_my_quorum, can_set_quorum);
(protocol::GrantablePermission::can_set_my_quorum, can_set_quorum)
// Can write details to other accounts
(protocol::GrantablePermission::can_set_my_account_detail,
can_set_detail);
}

// asset quantity
Expand Down
Loading

0 comments on commit c81dd57

Please sign in to comment.