Skip to content

Commit

Permalink
Added settings to customize ledger
Browse files Browse the repository at this point in the history
Signed-off-by: artyom-yurin <artem_yrin@mail.ru>
  • Loading branch information
ortyomka authored and MBoldyrev committed Sep 13, 2019
1 parent cbc9a3e commit 306e3fa
Show file tree
Hide file tree
Showing 49 changed files with 908 additions and 51 deletions.
45 changes: 44 additions & 1 deletion docs/source/develop/api/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ Structure
"Source account ID", "ID of the account to withdraw the asset from", "already existent", "makoto@soramitsu"
"Destination account ID", "ID of the account to send the asset to", "already existent", "alex@california"
"Asset ID", "ID of the asset to transfer", "already existent", "usd#usa"
"Description", "Message to attach to the transfer", "Max length is 64", "here's my money take it"
"Description", "Message to attach to the transfer", "Max length of description (set in genesis block, by default is 64)", "here's my money take it"
"Amount", "amount of the asset to transfer", "0 <= precision <= 255", "200.20"

Validation
Expand Down Expand Up @@ -894,3 +894,46 @@ Possible Stateful Validation Errors
"2", "No such permissions", "Command's creator does not have permission to set and read account detail for this account", "Grant the necessary permission"
"3", "No such account", "Cannot find account to set account detail to", "Make sure account id is correct"
"4", "No match values", "Old values do not match", "Make sure old value is correct"

Set setting value
-----------------

Purpose
^^^^^^^

The purpose of set setting value command is to enable customization to your needs.


Schema
^^^^^^

.. code-block:: proto
message SetSettingValue {
string key = 1;
string value = 2;
}
Structure
^^^^^^^^^

.. csv-table::
:header: "Field", "Description", "Constraint", "Example"
:widths: 15, 30, 20, 15

"Key", "Key of the setting", "list of possible settings", "MaxDescriptionSize"
"Value", "Value of the setting", "type of setting", "255"


Validation
^^^^^^^^^^

1. Command can be executed only from genesis block

List of possible settings
^^^^^^^^^^^^^^^^^^^^^^^^^

.. csv-table::
:header: "Key", "Value constraint", "Description"

"MaxDescriptionSize", "Unsigned integer, 0 <= MaxDescriptionSize < 2^32", "Maximum transaction description length"
1 change: 1 addition & 0 deletions irohad/ametsuchi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ add_library(ametsuchi
impl/postgres_wsv_command.cpp
impl/peer_query_wsv.cpp
impl/postgres_block_query.cpp
impl/postgres_setting_query.cpp
impl/postgres_command_executor.cpp
impl/postgres_indexer.cpp
impl/postgres_block_index.cpp
Expand Down
32 changes: 32 additions & 0 deletions irohad/ametsuchi/impl/postgres_command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "interfaces/commands/revoke_permission.hpp"
#include "interfaces/commands/set_account_detail.hpp"
#include "interfaces/commands/set_quorum.hpp"
#include "interfaces/commands/set_setting_value.hpp"
#include "interfaces/commands/subtract_asset_quantity.hpp"
#include "interfaces/commands/transfer_asset.hpp"
#include "interfaces/common_objects/types.hpp"
Expand Down Expand Up @@ -1333,6 +1334,19 @@ namespace iroha {
R"( AND (SELECT * FROM has_perm))",
R"( AND (SELECT * FROM has_perm))",
R"( WHEN NOT (SELECT * FROM has_perm) THEN 2 )"});

set_setting_value_statements_ = makeCommandStatements(
sql_,
R"(INSERT INTO setting(setting_key, setting_value)
VALUES
(
:setting_key,
:setting_value
)
ON CONFLICT (setting_key)
DO UPDATE SET setting_value = EXCLUDED.setting_value
RETURNING 0)",
{});
}

std::string CommandError::toString() const {
Expand Down Expand Up @@ -1719,5 +1733,23 @@ namespace iroha {
return executor.execute();
}

CommandResult PostgresCommandExecutor::operator()(
const shared_model::interface::SetSettingValue &command,
const shared_model::interface::types::AccountIdType &creator_account_id,
bool do_validation) {
auto &key = command.key();
auto &value = command.value();

StatementExecutor executor(set_setting_value_statements_,
do_validation,
"SetSettingValue",
perm_converter_);

executor.use("setting_key", key);
executor.use("setting_value", value);

return executor.execute();
}

} // namespace ametsuchi
} // namespace iroha
8 changes: 8 additions & 0 deletions irohad/ametsuchi/impl/postgres_command_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace shared_model {
class SetQuorum;
class SubtractAssetQuantity;
class TransferAsset;
class SetSettingValue;
} // namespace interface
} // namespace shared_model

Expand Down Expand Up @@ -165,6 +166,12 @@ namespace iroha {
&creator_account_id,
bool do_validation);

CommandResult operator()(
const shared_model::interface::SetSettingValue &command,
const shared_model::interface::types::AccountIdType
&creator_account_id,
bool do_validation);

private:
class CommandStatements;
class StatementExecutor;
Expand Down Expand Up @@ -200,6 +207,7 @@ namespace iroha {
std::unique_ptr<CommandStatements> set_quorum_statements_;
std::unique_ptr<CommandStatements> subtract_asset_quantity_statements_;
std::unique_ptr<CommandStatements> transfer_asset_statements_;
std::unique_ptr<CommandStatements> set_setting_value_statements_;
};
} // namespace ametsuchi
} // namespace iroha
Expand Down
68 changes: 68 additions & 0 deletions irohad/ametsuchi/impl/postgres_setting_query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "ametsuchi/impl/postgres_setting_query.hpp"

#include <boost/lexical_cast.hpp>
#include "interfaces/common_objects/types.hpp"
#include "logger/logger.hpp"

using namespace iroha::ametsuchi;

namespace {
template <typename T>
bool getValueFromDb(soci::session &sql,
const shared_model::interface::types::SettingKeyType &key,
T &destination) {
boost::optional<shared_model::interface::types::SettingValueType> value;

sql << "SELECT setting_value FROM setting WHERE setting_key = :key",
soci::into(value), soci::use(key, "key");

if (value) {
destination = boost::lexical_cast<T>(value.get());
return true;
}
return false;
}
} // namespace

PostgresSettingQuery::PostgresSettingQuery(std::unique_ptr<soci::session> sql,
logger::LoggerPtr log)
: psql_(std::move(sql)), sql_(*psql_), log_(std::move(log)) {}

iroha::expected::Result<
std::unique_ptr<const shared_model::validation::Settings>,
std::string>
PostgresSettingQuery::get() {
return update(shared_model::validation::getDefaultSettings());
}

iroha::expected::Result<
std::unique_ptr<const shared_model::validation::Settings>,
std::string>
PostgresSettingQuery::update(
std::unique_ptr<shared_model::validation::Settings> base) {
auto get_and_log =
[this](const shared_model::interface::types::SettingKeyType &key,
auto &destination) {
if (getValueFromDb(sql_, key, destination)) {
log_->info("Updated value for " + key + ": {}", destination);
} else {
log_->info("Kept value for " + key + ": {}", destination);
}
};

try {
get_and_log(kMaxDescriptionSizeKey, base->max_description_size);
} catch (std::exception &e) {
return expected::makeError(e.what());
}

return std::move(base);
}

const shared_model::interface::types::SettingKeyType
iroha::ametsuchi::kMaxDescriptionSizeKey = "MaxDescriptionSize";
48 changes: 48 additions & 0 deletions irohad/ametsuchi/impl/postgres_setting_query.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_POSTGRES_SETTING_QUERY_HPP
#define IROHA_POSTGRES_SETTING_QUERY_HPP

#include "ametsuchi/setting_query.hpp"

#include <soci/soci.h>
#include <boost/optional.hpp>
#include "logger/logger_fwd.hpp"

namespace iroha {
namespace ametsuchi {

/**
* Class which implements SettingQuery with a Postgres backend.
*/
class PostgresSettingQuery : public SettingQuery {
public:
PostgresSettingQuery(std::unique_ptr<soci::session> sql,
logger::LoggerPtr log);

expected::Result<
std::unique_ptr<const shared_model::validation::Settings>,
std::string>
get() override;

private:
expected::Result<
std::unique_ptr<const shared_model::validation::Settings>,
std::string>
update(std::unique_ptr<shared_model::validation::Settings> base);

std::unique_ptr<soci::session> psql_;
soci::session &sql_;

logger::LoggerPtr log_;
};

extern const shared_model::interface::types::SettingKeyType
kMaxDescriptionSizeKey;
} // namespace ametsuchi
} // namespace iroha

#endif // IROHA_POSTGRES_SETTING_QUERY_HPP
16 changes: 16 additions & 0 deletions irohad/ametsuchi/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ametsuchi/impl/postgres_command_executor.hpp"
#include "ametsuchi/impl/postgres_indexer.hpp"
#include "ametsuchi/impl/postgres_query_executor.hpp"
#include "ametsuchi/impl/postgres_setting_query.hpp"
#include "ametsuchi/impl/postgres_specific_query_executor.hpp"
#include "ametsuchi/impl/postgres_wsv_command.hpp"
#include "ametsuchi/impl/postgres_wsv_query.hpp"
Expand Down Expand Up @@ -453,6 +454,21 @@ namespace iroha {
log_manager_->getChild("PostgresBlockQuery")->getLogger());
}

boost::optional<std::unique_ptr<SettingQuery>>
StorageImpl::createSettingQuery() const {
std::shared_lock<std::shared_timed_mutex> lock(drop_mutex_);
if (not connection_) {
log_->info(
"getSettingQuery: connection to database is not initialised");
return boost::none;
}
std::unique_ptr<SettingQuery> setting_query_ptr =
std::make_unique<PostgresSettingQuery>(
std::make_unique<soci::session>(*connection_),
log_manager_->getChild("PostgresSettingQuery")->getLogger());
return boost::make_optional(std::move(setting_query_ptr));
}

rxcpp::observable<std::shared_ptr<const shared_model::interface::Block>>
StorageImpl::on_commit() {
return notifier_.get_observable();
Expand Down
3 changes: 3 additions & 0 deletions irohad/ametsuchi/impl/storage_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ namespace iroha {
boost::optional<std::shared_ptr<BlockQuery>> createBlockQuery()
const override;

boost::optional<std::unique_ptr<SettingQuery>> createSettingQuery()
const override;

boost::optional<std::shared_ptr<QueryExecutor>> createQueryExecutor(
std::shared_ptr<PendingTransactionStorage> pending_txs_storage,
std::shared_ptr<shared_model::interface::QueryResponseFactory>
Expand Down
31 changes: 31 additions & 0 deletions irohad/ametsuchi/setting_query.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_SETTING_QUERY_HPP
#define IROHA_SETTING_QUERY_HPP

#include <boost/optional.hpp>
#include "common/result.hpp"
#include "validators/settings.hpp"

namespace iroha {

namespace ametsuchi {
/**
* Public interface for get settings structure
*/
class SettingQuery {
public:
virtual ~SettingQuery() = default;

virtual expected::Result<
std::unique_ptr<const shared_model::validation::Settings>,
std::string>
get() = 0;
};
} // namespace ametsuchi
} // namespace iroha

#endif // IROHA_SETTING_QUERY_HPP
28 changes: 28 additions & 0 deletions irohad/ametsuchi/setting_query_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_SETTING_QUERY_FACTORY_HPP
#define IROHA_SETTING_QUERY_FACTORY_HPP

#include <boost/optional.hpp>

#include "ametsuchi/setting_query.hpp"

namespace iroha {
namespace ametsuchi {
class SettingQueryFactory {
public:
/**
* Creates a setting query
* @return Created setting query
*/
virtual boost::optional<std::unique_ptr<SettingQuery>>
createSettingQuery() const = 0;

virtual ~SettingQueryFactory() = default;
};
} // namespace ametsuchi
} // namespace iroha
#endif // IROHA_SETTING_QUERY_FACTORY_HPP
4 changes: 3 additions & 1 deletion irohad/ametsuchi/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ametsuchi/mutable_factory.hpp"
#include "ametsuchi/peer_query_factory.hpp"
#include "ametsuchi/query_executor_factory.hpp"
#include "ametsuchi/setting_query_factory.hpp"
#include "ametsuchi/temporary_factory.hpp"
#include "common/result.hpp"

Expand All @@ -38,7 +39,8 @@ namespace iroha {
public MutableFactory,
public PeerQueryFactory,
public BlockQueryFactory,
public QueryExecutorFactory {
public QueryExecutorFactory,
public SettingQueryFactory {
public:
virtual std::shared_ptr<WsvQuery> getWsvQuery() const = 0;

Expand Down

0 comments on commit 306e3fa

Please sign in to comment.