Skip to content

Commit

Permalink
Add Ametsuchi stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
lebdron committed Jun 28, 2017
1 parent 8120f55 commit 76e7f5f
Show file tree
Hide file tree
Showing 25 changed files with 541 additions and 58 deletions.
22 changes: 7 additions & 15 deletions irohad/ametsuchi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#add_subdirectory(block_store)
#add_subdirectory(index)
#add_subdirectory(wsv)

add_library(storage
# impl/ametsuchi_impl.cpp
# index_mediator.cpp
add_library(ametsuchi
command_executor_stub.cpp
temporary_wsv_stub.cpp
mutable_storage_stub.cpp
ametsuchi_stub.cpp
)

target_link_libraries(storage PUBLIC
schema
cpp_redis
pqxx
optional
block_store
index
wsv
target_link_libraries(ametsuchi
rxcpp
optional
)
2 changes: 1 addition & 1 deletion irohad/ametsuchi/ametsuchi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace iroha {
* MutableStorage.
* @param mutableStorage
*/
virtual void commit(std::unique_ptr<MutableStorage>& mutableStorage) = 0;
virtual void commit(MutableStorage& mutableStorage) = 0;
};

} // namespace ametsuchi
Expand Down
89 changes: 89 additions & 0 deletions irohad/ametsuchi/ametsuchi_stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <ametsuchi/ametsuchi_stub.hpp>
#include <ametsuchi/mutable_storage_stub.hpp>
#include <ametsuchi/temporary_wsv_stub.hpp>

namespace iroha {
namespace ametsuchi {

std::unique_ptr<TemporaryWsv> AmetsuchiStub::createTemporaryWsv() {
return std::make_unique<TemporaryWsvStub>(*this);
}

std::unique_ptr<MutableStorage> AmetsuchiStub::createMutableStorage() {
return std::make_unique<MutableStorageStub>(*this);
}

void AmetsuchiStub::commit(MutableStorage &mutableStorage) { return; }

rxcpp::observable<dao::Transaction> AmetsuchiStub::get_account_transactions(
iroha::crypto::ed25519::pubkey_t pub_key) {
return rxcpp::observable<>::create<dao::Transaction>(
[](rxcpp::subscriber<dao::Transaction> s) {
s.on_next(dao::Transaction{});
s.on_completed();
});
}

rxcpp::observable<dao::Transaction> AmetsuchiStub::get_asset_transactions(
std::string asset_full_name) {
return rxcpp::observable<>::create<dao::Transaction>(
[](rxcpp::subscriber<dao::Transaction> s) {
s.on_next(dao::Transaction{});
s.on_completed();
});
}

rxcpp::observable<dao::Transaction> AmetsuchiStub::get_wallet_transactions(
std::string wallet_id) {
return rxcpp::observable<>::create<dao::Transaction>(
[](rxcpp::subscriber<dao::Transaction> s) {
s.on_next(dao::Transaction{});
s.on_completed();
});
}

dao::Account AmetsuchiStub::get_account(
iroha::crypto::ed25519::pubkey_t pub_key) {
return dao::Account{};
}

dao::Asset AmetsuchiStub::get_asset(std::string asset_full_name) {
return dao::Asset{};
}

dao::Domain AmetsuchiStub::get_domain(std::string domain_full_name) {
return dao::Domain{};
}

dao::Wallet AmetsuchiStub::get_wallet(std::string wallet_id) {
return dao::Wallet{};
}

std::vector<dao::Wallet> AmetsuchiStub::get_account_wallets(
iroha::crypto::ed25519::pubkey_t pub_key) {
return std::vector<dao::Wallet>{dao::Wallet{}};
}

std::vector<dao::Asset> AmetsuchiStub::get_domain_assets(
std::string domain_full_name) {
return std::vector<dao::Asset>{dao::Asset{}};
}
} // namespace ametsuchi
} // namespace iroha
52 changes: 52 additions & 0 deletions irohad/ametsuchi/ametsuchi_stub.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_AMETSUCHI_STUB_HPP
#define IROHA_AMETSUCHI_STUB_HPP

#include <ametsuchi/ametsuchi.hpp>
#include <memory>

namespace iroha {
namespace ametsuchi {

class AmetsuchiStub : public Ametsuchi {
public:
std::unique_ptr<TemporaryWsv> createTemporaryWsv() override;
std::unique_ptr<MutableStorage> createMutableStorage() override;
void commit(MutableStorage &mutableStorage) override;
rxcpp::observable<iroha::dao::Transaction> get_account_transactions(
iroha::crypto::ed25519::pubkey_t pub_key) override;
rxcpp::observable<iroha::dao::Transaction> get_asset_transactions(
std::string asset_full_name) override;
rxcpp::observable<iroha::dao::Transaction> get_wallet_transactions(
std::string wallet_id) override;
dao::Account get_account(
iroha::crypto::ed25519::pubkey_t pub_key) override;
dao::Asset get_asset(std::string asset_full_name) override;
dao::Domain get_domain(std::string domain_full_name) override;
dao::Wallet get_wallet(std::string wallet_id) override;
std::vector<dao::Wallet> get_account_wallets(
iroha::crypto::ed25519::pubkey_t pub_key) override;
std::vector<dao::Asset> get_domain_assets(
std::string domain_full_name) override;
};

} // namespace ametsuchi
} // namespace iroha

#endif // IROHA_AMETSUCHI_STUB_HPP
3 changes: 3 additions & 0 deletions irohad/ametsuchi/block_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#ifndef IROHA_BLOCK_QUERY_HPP
#define IROHA_BLOCK_QUERY_HPP

#include <rxcpp/rx-observable.hpp>
#include <dao/dao.hpp>

namespace iroha {

namespace ametsuchi {
Expand Down
3 changes: 2 additions & 1 deletion irohad/ametsuchi/command_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ namespace iroha {
* Applies command to the world state view
*/
class CommandExecutor {
public:
/**
* Executes a command in a temporary state
* @see TemporaryWsv, MutableStorage
* @param command Command to execute
* @return True if the command is successfully executed, false otherwise
*/
virtual bool execute(dao::Command command) = 0;
virtual bool execute(const dao::Command& command) = 0;
};

} // namespace ametsuchi
Expand Down
39 changes: 39 additions & 0 deletions irohad/ametsuchi/command_executor_stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <ametsuchi/command_executor_stub.hpp>

namespace iroha {
namespace ametsuchi {

bool CommandExecutorStub::execute(const dao::Command &command) {
auto handler = map_.find(command);
return handler.value_or([](const dao::Command &) { return false; })(
command);
}

bool CommandExecutorStub::executeAddPeer(const dao::AddPeer &command) {
return true;
}

CommandExecutorStub::CommandExecutorStub(WsvQuery &query) : query_(query) {
// https://stackoverflow.com/questions/9998402/c11-does-not-deduce-type-when-stdfunction-or-lambda-functions-are-involved
map_.insert<dao::AddPeer>(std::bind(&CommandExecutorStub::executeAddPeer,
this, std::placeholders::_1));
}
} // namespace ametsuchi
} // namespace iroha
46 changes: 46 additions & 0 deletions irohad/ametsuchi/command_executor_stub.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_COMMAND_EXECUTOR_STUB_HPP
#define IROHA_COMMAND_EXECUTOR_STUB_HPP

#include <ametsuchi/command_executor.hpp>
#include <ametsuchi/wsv_query.hpp>
#include <dao/dao.hpp>
#include <functional>
#include <handler_map/handler_map.hpp>
#include <memory>
#include <unordered_map>

namespace iroha {
namespace ametsuchi {

class CommandExecutorStub : public CommandExecutor {
public:
CommandExecutorStub(WsvQuery &query);
bool execute(const dao::Command &command) override;

private:
bool executeAddPeer(const dao::AddPeer &command);
WsvQuery &query_;
HandlerMap<dao::Command, bool> map_;
};

} // namespace ametsuchi
} // namespace iroha

#endif // IROHA_COMMAND_EXECUTOR_STUB_HPP
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion irohad/ametsuchi/mutable_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#define IROHA_MUTABLESTORAGE_HPP

#include <ametsuchi/command_executor.hpp>
#include <ametsuchi/wsv_query.hpp>
#include <ametsuchi/block_query.hpp>

namespace iroha {
namespace ametsuchi {
Expand All @@ -27,6 +29,7 @@ namespace iroha {
* Allows to query the world state view, transactions, and blocks.
*/
class MutableStorage : public WsvQuery, public BlockQuery {
public:
/**
* Applies a block to current mutable state
* using logic specified in function
Expand All @@ -44,7 +47,7 @@ namespace iroha {
*/
virtual bool apply(
dao::Block block,
std::function<bool(dao::Block, CommandExecutor, WsvQuery)>
std::function<bool(dao::Block&, CommandExecutor&, WsvQuery&)>
function) = 0;
};

Expand Down
75 changes: 75 additions & 0 deletions irohad/ametsuchi/mutable_storage_stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <ametsuchi/mutable_storage_stub.hpp>

namespace iroha {
namespace ametsuchi {

rxcpp::observable<dao::Transaction>
MutableStorageStub::get_account_transactions(
iroha::crypto::ed25519::pubkey_t pub_key) {
return ametsuchi_.get_account_transactions(pub_key);
}

rxcpp::observable<dao::Transaction>
MutableStorageStub::get_asset_transactions(std::string asset_full_name) {
return ametsuchi_.get_asset_transactions(asset_full_name);
}

rxcpp::observable<dao::Transaction>
MutableStorageStub::get_wallet_transactions(std::string wallet_id) {
return ametsuchi_.get_wallet_transactions(wallet_id);
}

dao::Account MutableStorageStub::get_account(
iroha::crypto::ed25519::pubkey_t pub_key) {
return ametsuchi_.get_account(pub_key);
}

dao::Asset MutableStorageStub::get_asset(std::string asset_full_name) {
return ametsuchi_.get_asset(asset_full_name);
}

dao::Domain MutableStorageStub::get_domain(std::string domain_full_name) {
return ametsuchi_.get_domain(domain_full_name);
}

dao::Wallet MutableStorageStub::get_wallet(std::string wallet_id) {
return ametsuchi_.get_wallet(wallet_id);
}

std::vector<dao::Wallet> MutableStorageStub::get_account_wallets(
iroha::crypto::ed25519::pubkey_t pub_key) {
return ametsuchi_.get_account_wallets(pub_key);
}

std::vector<dao::Asset> MutableStorageStub::get_domain_assets(
std::string domain_full_name) {
return ametsuchi_.get_domain_assets(domain_full_name);
}

bool MutableStorageStub::apply(
dao::Block block,
std::function<bool(dao::Block&, CommandExecutor&, WsvQuery&)> function) {
return function(block, executor_, ametsuchi_);
}

MutableStorageStub::MutableStorageStub(AmetsuchiStub &ametsuchi)
: ametsuchi_(ametsuchi), executor_(*this) {}
} // namespace ametsuchi
} // namespace iroha
Loading

0 comments on commit 76e7f5f

Please sign in to comment.