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

common objects factory for wsv query #1573

Merged
merged 20 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions irohad/ametsuchi/impl/mutable_storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
#include "ametsuchi/impl/postgres_wsv_command.hpp"
#include "ametsuchi/impl/postgres_wsv_query.hpp"
#include "ametsuchi/wsv_command.hpp"
#include "interfaces/common_objects/common_objects_factory.hpp"
#include "model/sha3_hash.hpp"

namespace iroha {
namespace ametsuchi {
MutableStorageImpl::MutableStorageImpl(
shared_model::interface::types::HashType top_hash,
std::unique_ptr<soci::session> sql)
std::unique_ptr<soci::session> sql,
std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory)
: top_hash_(top_hash),
sql_(std::move(sql)),
wsv_(std::make_shared<PostgresWsvQuery>(*sql_)),
wsv_(std::make_shared<PostgresWsvQuery>(*sql_, factory)),
executor_(std::make_shared<PostgresWsvCommand>(*sql_)),
block_index_(std::make_unique<PostgresBlockIndex>(*sql_)),
command_executor_(std::make_shared<CommandExecutor>(wsv_, executor_)),
Expand Down
5 changes: 4 additions & 1 deletion irohad/ametsuchi/impl/mutable_storage_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "ametsuchi/mutable_storage.hpp"
#include "execution/command_executor.hpp"
#include "interfaces/common_objects/common_objects_factory.hpp"
#include "logger/logger.hpp"

namespace iroha {
Expand All @@ -37,7 +38,9 @@ namespace iroha {

public:
MutableStorageImpl(shared_model::interface::types::HashType top_hash,
std::unique_ptr<soci::session> sql);
std::unique_ptr<soci::session> sql,
std::shared_ptr<shared_model::interface::CommonObjectsFactory>
factory);

bool apply(
const shared_model::interface::Block &block,
Expand Down
2 changes: 2 additions & 0 deletions irohad/ametsuchi/impl/postgres_wsv_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include "ametsuchi/impl/postgres_wsv_command.hpp"

#include <boost/format.hpp>

#include "backend/protobuf/permissions.hpp"
#include "interfaces/common_objects/asset.hpp"

namespace iroha {
namespace ametsuchi {
Expand Down
96 changes: 7 additions & 89 deletions irohad/ametsuchi/impl/postgres_wsv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#include <soci/boost-tuple.h>
#include <soci/soci.h>

#include "builders/default_builders.hpp"
#include "common/result.hpp"
#include "interfaces/common_objects/common_objects_factory.hpp"
#include "logger/logger.hpp"

namespace iroha {
Expand Down Expand Up @@ -54,21 +54,6 @@ namespace iroha {
return values;
}

/**
* Execute build function and return error in case it throws
* @tparam T - result value type
* @param f - function which returns BuilderResult
* @return whatever f returns, or error in case exception has been thrown
*/
template <typename BuildFunc>
static inline auto tryBuild(BuildFunc &&f) noexcept -> decltype(f()) {
try {
return f();
} catch (std::exception &e) {
return expected::makeError(std::make_shared<std::string>(e.what()));
}
}

template <typename ParamType, typename Function>
void processSoci(soci::statement &st,
soci::indicator &ind,
Expand All @@ -85,74 +70,6 @@ namespace iroha {
}
}

static inline shared_model::builder::BuilderResult<
shared_model::interface::Account>
makeAccount(const std::string &account_id,
const std::string &domain_id,
const shared_model::interface::types::QuorumType &quorum,
const std::string &data) noexcept {
return tryBuild([&] {
return shared_model::builder::DefaultAccountBuilder()
.accountId(account_id)
.domainId(domain_id)
.quorum(quorum)
.jsonData(data)
.build();
});
}

static inline shared_model::builder::BuilderResult<
shared_model::interface::Asset>
makeAsset(const std::string &asset_id,
const std::string &domain_id,
const int32_t precision) noexcept {
return tryBuild([&] {
return shared_model::builder::DefaultAssetBuilder()
.assetId(asset_id)
.domainId(domain_id)
.precision(precision)
.build();
});
}

static inline shared_model::builder::BuilderResult<
shared_model::interface::AccountAsset>
makeAccountAsset(const std::string &account_id,
const std::string &asset_id,
const std::string &amount) noexcept {
return tryBuild([&] {
return shared_model::builder::DefaultAccountAssetBuilder()
.accountId(account_id)
.assetId(asset_id)
.balance(shared_model::interface::Amount(amount))
.build();
});
}

static inline shared_model::builder::BuilderResult<
shared_model::interface::Peer>
makePeer(const soci::row &row) noexcept {
return tryBuild([&row] {
return shared_model::builder::DefaultPeerBuilder()
.pubkey(shared_model::crypto::PublicKey(
shared_model::crypto::Blob::fromHexString(
row.get<std::string>(0))))
.address(row.get<std::string>(1))
.build();
});
}

static inline shared_model::builder::BuilderResult<
shared_model::interface::Domain>
makeDomain(const std::string &domain_id, const std::string &role) noexcept {
return tryBuild([&domain_id, &role] {
return shared_model::builder::DefaultDomainBuilder()
.domainId(domain_id)
.defaultRole(role)
.build();
});
}

/**
* Transforms result to optional
* value -> optional<value>
Expand All @@ -162,13 +79,14 @@ namespace iroha {
* @return optional<T>
*/
template <typename T>
static inline boost::optional<std::shared_ptr<T>> fromResult(
const shared_model::builder::BuilderResult<T> &result) {
inline boost::optional<std::shared_ptr<T>> fromResult(
shared_model::interface::CommonObjectsFactory::FactoryResult<
std::unique_ptr<T>> &&result) {
return result.match(
[](const expected::Value<std::shared_ptr<T>> &v) {
return boost::make_optional(v.value);
[](expected::Value<std::unique_ptr<T>> &v) {
return boost::make_optional(std::shared_ptr<T>(std::move(v.value)));
},
[](const expected::Error<std::shared_ptr<std::string>> &e)
[](expected::Error<std::string>)
-> boost::optional<std::shared_ptr<T>> { return boost::none; });
}
} // namespace ametsuchi
Expand Down
51 changes: 31 additions & 20 deletions irohad/ametsuchi/impl/postgres_wsv_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "ametsuchi/impl/postgres_wsv_query.hpp"
#include "backend/protobuf/permissions.hpp"
#include "common/result.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This include is presented in postgres_wsv_common.hpp. It is not necessary to have it here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better not to have transitive includes, so that if we change one header, we won't break stuff


namespace iroha {
namespace ametsuchi {
Expand All @@ -36,12 +37,17 @@ namespace iroha {
const std::string kAccountId = "account_id";
const std::string kDomainId = "domain_id";

PostgresWsvQuery::PostgresWsvQuery(soci::session &sql)
: sql_(sql), log_(logger::log("PostgresWsvQuery")) {}
PostgresWsvQuery::PostgresWsvQuery(
soci::session &sql,
std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory)
: sql_(sql), factory_(factory), log_(logger::log("PostgresWsvQuery")) {}

PostgresWsvQuery::PostgresWsvQuery(std::unique_ptr<soci::session> sql_ptr)
PostgresWsvQuery::PostgresWsvQuery(
std::unique_ptr<soci::session> sql_ptr,
std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory)
: sql_ptr_(std::move(sql_ptr)),
sql_(*sql_ptr_),
factory_(factory),
log_(logger::log("PostgresWsvQuery")) {}

bool PostgresWsvQuery::hasAccountGrantablePermission(
Expand Down Expand Up @@ -133,8 +139,8 @@ namespace iroha {
return boost::none;
}

return fromResult(
makeAccount(account_id, domain_id.get(), quorum.get(), data.get()));
return fromResult(factory_->createAccount(
account_id, domain_id.get(), quorum.get(), data.get()));
}

boost::optional<std::string> PostgresWsvQuery::getAccountDetail(
Expand Down Expand Up @@ -221,7 +227,8 @@ namespace iroha {
return boost::none;
}

return fromResult(makeAsset(asset_id, domain_id.get(), precision.get()));
return fromResult(
factory_->createAsset(asset_id, domain_id.get(), precision.get()));
}

boost::optional<
Expand All @@ -234,8 +241,11 @@ namespace iroha {
std::vector<std::shared_ptr<shared_model::interface::AccountAsset>>
assets;
for (auto &t : st) {
fromResult(makeAccountAsset(account_id, t.get<1>(), t.get<2>())) |
[&assets](const auto &asset) { assets.push_back(asset); };
fromResult(factory_->createAccountAsset(
account_id,
t.get<1>(),
shared_model::interface::Amount(t.get<2>())))
| [&assets](const auto &asset) { assets.push_back(asset); };
}

return boost::make_optional(assets);
Expand All @@ -258,7 +268,8 @@ namespace iroha {
return boost::none;
}

return fromResult(makeAccountAsset(account_id, asset_id, amount.get()));
return fromResult(factory_->createAccountAsset(
account_id, asset_id, shared_model::interface::Amount(amount.get())));
}

boost::optional<std::shared_ptr<shared_model::interface::Domain>>
Expand All @@ -275,7 +286,7 @@ namespace iroha {
return boost::none;
}

return fromResult(makeDomain(domain_id, role.get()));
return fromResult(factory_->createDomain(domain_id, role.get()));
}

boost::optional<std::vector<std::shared_ptr<shared_model::interface::Peer>>>
Expand All @@ -284,16 +295,16 @@ namespace iroha {
(sql_.prepare << "SELECT public_key, address FROM peer");
std::vector<std::shared_ptr<shared_model::interface::Peer>> peers;

auto results = transform<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 'transform' from postgres_wsv_common.hpp called from somewhere? Can the method be removed?

shared_model::builder::BuilderResult<shared_model::interface::Peer>>(
rows, makePeer);
for (auto &r : results) {
r.match(
[&](expected::Value<std::shared_ptr<shared_model::interface::Peer>>
&v) { peers.push_back(v.value); },
[&](expected::Error<std::shared_ptr<std::string>> &e) {
log_->info(*e.error);
});
for (auto &row : rows) {
auto address = row.get<std::string>(1);
auto key = shared_model::crypto::PublicKey(
shared_model::crypto::Blob::fromHexString(row.get<std::string>(0)));

auto peer = factory_->createPeer(address, key);
peer.match(
[&](expected::Value<std::unique_ptr<shared_model::interface::Peer>>
&v) { peers.push_back(std::move(v.value)); },
[&](expected::Error<std::string> &e) { log_->info(e.error); });
}
return peers;
}
Expand Down
13 changes: 11 additions & 2 deletions irohad/ametsuchi/impl/postgres_wsv_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@

#include "ametsuchi/wsv_query.hpp"

#include "interfaces/common_objects/common_objects_factory.hpp"
#include "postgres_wsv_common.hpp"

namespace iroha {
namespace ametsuchi {
class PostgresWsvQuery : public WsvQuery {
public:
explicit PostgresWsvQuery(soci::session &sql);
explicit PostgresWsvQuery(std::unique_ptr<soci::session> sql_ptr);
PostgresWsvQuery(
soci::session &sql,
std::shared_ptr<shared_model::interface::CommonObjectsFactory>
factory);

PostgresWsvQuery(
std::unique_ptr<soci::session> sql_ptr,
std::shared_ptr<shared_model::interface::CommonObjectsFactory>
factory);

boost::optional<std::vector<shared_model::interface::types::RoleIdType>>
getAccountRoles(const shared_model::interface::types::AccountIdType
Expand Down Expand Up @@ -84,6 +92,7 @@ namespace iroha {
private:
std::unique_ptr<soci::session> sql_ptr_;
soci::session &sql_;
std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory_;
logger::Logger log_;
};
} // namespace ametsuchi
Expand Down
21 changes: 14 additions & 7 deletions irohad/ametsuchi/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ namespace iroha {
StorageImpl::StorageImpl(std::string block_store_dir,
PostgresOptions postgres_options,
std::unique_ptr<KeyValueStorage> block_store,
std::shared_ptr<soci::connection_pool> connection)
std::shared_ptr<soci::connection_pool> connection,
std::shared_ptr<shared_model::interface::CommonObjectsFactory> factory)
: block_store_dir_(std::move(block_store_dir)),
postgres_options_(std::move(postgres_options)),
block_store_(std::move(block_store)),
connection_(connection),
factory_(factory),
log_(logger::log("StorageImpl")) {}

expected::Result<std::unique_ptr<TemporaryWsv>, std::string>
StorageImpl::createTemporaryWsv() {
auto sql = std::make_unique<soci::session>(*connection_);

return expected::makeValue<std::unique_ptr<TemporaryWsv>>(
std::make_unique<TemporaryWsvImpl>(std::move(sql)));
std::make_unique<TemporaryWsvImpl>(std::move(sql), factory_));
}

expected::Result<std::unique_ptr<MutableStorage>, std::string>
Expand All @@ -74,7 +76,8 @@ namespace iroha {
[](expected::Error<std::string> &) {
return shared_model::interface::types::HashType("");
}),
std::move(sql)));
std::move(sql),
factory_));
}

bool StorageImpl::insertBlock(const shared_model::interface::Block &block) {
Expand Down Expand Up @@ -212,8 +215,11 @@ DROP TABLE IF EXISTS index_by_id_height_asset;
};

expected::Result<std::shared_ptr<StorageImpl>, std::string>
StorageImpl::create(std::string block_store_dir,
std::string postgres_options) {
StorageImpl::create(
std::string block_store_dir,
std::string postgres_options,
std::shared_ptr<shared_model::interface::CommonObjectsFactory>
factory) {
boost::optional<std::string> string_res = boost::none;

PostgresOptions options(postgres_options);
Expand Down Expand Up @@ -243,7 +249,8 @@ DROP TABLE IF EXISTS index_by_id_height_asset;
new StorageImpl(block_store_dir,
options,
std::move(ctx.value.block_store),
connection.value)));
connection.value,
factory)));
},
[&](expected::Error<std::string> &error) { storage = error; });
},
Expand All @@ -270,7 +277,7 @@ DROP TABLE IF EXISTS index_by_id_height_asset;

std::shared_ptr<WsvQuery> StorageImpl::getWsvQuery() const {
auto sql = std::make_unique<soci::session>(*connection_);
return std::make_shared<PostgresWsvQuery>(std::move(sql));
return std::make_shared<PostgresWsvQuery>(std::move(sql), factory_);
}

std::shared_ptr<BlockQuery> StorageImpl::getBlockQuery() const {
Expand Down
Loading