Skip to content

Commit

Permalink
wsv peer query: added peers query by key
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Boldyrev <miboldyrev@gmail.com>
  • Loading branch information
MBoldyrev committed Oct 3, 2019
1 parent 6055bdf commit 0a7be00
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 12 deletions.
5 changes: 5 additions & 0 deletions irohad/ametsuchi/impl/peer_query_wsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ namespace iroha {
return wsv_->getPeers();
}

boost::optional<PeerQuery::wPeer> PeerQueryWsv::getLedgerPeerByPublicKey(
const shared_model::interface::types::PubkeyType &public_key) const {
return wsv_->getPeerByPublicKey(public_key);
}

} // namespace ametsuchi
} // namespace iroha
10 changes: 10 additions & 0 deletions irohad/ametsuchi/impl/peer_query_wsv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <memory>
#include <vector>

#include "interfaces/common_objects/types.hpp"

namespace iroha {
namespace ametsuchi {

Expand All @@ -29,6 +31,14 @@ namespace iroha {
*/
boost::optional<std::vector<wPeer>> getLedgerPeers() override;

/**
* Fetch peer with given public key from ledger
* @return the peer if found, none otherwise
*/
boost::optional<PeerQuery::wPeer> getLedgerPeerByPublicKey(
const shared_model::interface::types::PubkeyType &public_key)
const override;

private:
std::shared_ptr<WsvQuery> wsv_;
};
Expand Down
43 changes: 33 additions & 10 deletions irohad/ametsuchi/impl/postgres_wsv_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
#include "cryptography/public_key.hpp"
#include "logger/logger.hpp"

namespace {
template <typename T>
boost::optional<std::vector<std::shared_ptr<shared_model::interface::Peer>>>
getPeersFromSociRowSet(T &&rowset) {
return iroha::ametsuchi::flatMapValues<
std::vector<std::shared_ptr<shared_model::interface::Peer>>>(
std::forward<T>(rowset), [&](auto &public_key, auto &address) {
return boost::make_optional(
std::make_shared<shared_model::plain::Peer>(
address,
shared_model::crypto::PublicKey{
shared_model::crypto::Blob::fromHexString(public_key)}));
});
}
} // namespace

namespace iroha {
namespace ametsuchi {

Expand Down Expand Up @@ -60,16 +76,23 @@ namespace iroha {
return (sql_.prepare << "SELECT public_key, address FROM peer");
});

return flatMapValues<
std::vector<std::shared_ptr<shared_model::interface::Peer>>>(
result, [&](auto &public_key, auto &address) {
return boost::make_optional(
std::make_shared<shared_model::plain::Peer>(
address,
shared_model::crypto::PublicKey{
shared_model::crypto::Blob::fromHexString(
public_key)}));
});
return getPeersFromSociRowSet(result);
}

boost::optional<std::shared_ptr<shared_model::interface::Peer>>
PostgresWsvQuery::getPeerByPublicKey(const PubkeyType &public_key) {
using T = boost::tuple<std::string, AddressType>;
auto result = execute<T>([&] {
return (sql_.prepare << R"(
SELECT public_key, address
FROM peer
WHERE public_key = :public_key)",
soci::use(public_key.hex(), "public_key"));
});

return getPeersFromSociRowSet(result) | [](auto &&peers) {
return boost::make_optional(std::move(peers.front()));
};
}
} // namespace ametsuchi
} // namespace iroha
4 changes: 4 additions & 0 deletions irohad/ametsuchi/impl/postgres_wsv_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ namespace iroha {
std::vector<std::shared_ptr<shared_model::interface::Peer>>>
getPeers() override;

boost::optional<std::shared_ptr<shared_model::interface::Peer>>
getPeerByPublicKey(const shared_model::interface::types::PubkeyType
&public_key) override;

private:
/**
* Executes given lambda of type F, catches exceptions if any, logs the
Expand Down
9 changes: 9 additions & 0 deletions irohad/ametsuchi/peer_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/optional.hpp>
#include <memory>
#include <vector>
#include "interfaces/common_objects/types.hpp"

namespace shared_model {
namespace interface {
Expand All @@ -35,6 +36,14 @@ namespace iroha {
*/
virtual boost::optional<std::vector<wPeer>> getLedgerPeers() = 0;

/**
* Fetch peer with given public key from ledger
* @return the peer if found, none otherwise
*/
virtual boost::optional<PeerQuery::wPeer> getLedgerPeerByPublicKey(
const shared_model::interface::types::PubkeyType &public_key)
const = 0;

virtual ~PeerQuery() = default;
};

Expand Down
12 changes: 10 additions & 2 deletions irohad/ametsuchi/wsv_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ namespace iroha {
const shared_model::interface::types::AccountIdType &account_id) = 0;

/**
*
* @return
* Fetch peers stored in ledger
* @return list of peers in insertion to ledger order
*/
virtual boost::optional<
std::vector<std::shared_ptr<shared_model::interface::Peer>>>
getPeers() = 0;

/**
* Fetch peer with given public key from ledger
* @return the peer if found, none otherwise
*/
virtual boost::optional<std::shared_ptr<shared_model::interface::Peer>>
getPeerByPublicKey(
const shared_model::interface::types::PubkeyType &public_key) = 0;
};

} // namespace ametsuchi
Expand Down
5 changes: 5 additions & 0 deletions test/module/irohad/ametsuchi/mock_peer_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ namespace iroha {
MockPeerQuery() = default;

MOCK_METHOD0(getLedgerPeers, boost::optional<std::vector<wPeer>>());

MOCK_CONST_METHOD1(
getLedgerPeerByPublicKey,
boost::optional<PeerQuery::wPeer>(
const shared_model::interface::types::PubkeyType &));
};

} // namespace ametsuchi
Expand Down
5 changes: 5 additions & 0 deletions test/module/irohad/ametsuchi/mock_wsv_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ namespace iroha {
getPeers,
boost::optional<
std::vector<std::shared_ptr<shared_model::interface::Peer>>>());

MOCK_METHOD1(
getPeerByPublicKey,
boost::optional<std::shared_ptr<shared_model::interface::Peer>>(
const shared_model::interface::types::PubkeyType &public_key));
};

} // namespace ametsuchi
Expand Down

0 comments on commit 0a7be00

Please sign in to comment.