OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
uhs/twophase/locking_shard/client.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "client.hpp"
7
8#include "format.hpp"
10
12 client::client(std::vector<network::endpoint_t> endpoints,
13 const std::pair<uint8_t, uint8_t>& output_range,
14 logging::log& logger)
15 : interface(output_range),
16 m_log(logger) {
17 m_client = std::make_unique<decltype(m_client)::element_type>(
18 std::move(endpoints));
19 }
20
22 stop();
23 }
24
25 auto client::init() -> bool {
26 return m_client->init();
27 }
28
29 auto client::lock_outputs(std::vector<tx>&& txs, const hash_t& dtx_id)
30 -> std::optional<std::vector<bool>> {
31 auto req = request{dtx_id, std::move(txs)};
32 auto resp = send_request(req);
33 if(!resp.has_value()) {
34 return std::nullopt;
35 }
36 return std::get<lock_response>(resp.value());
37 }
38
39 auto client::apply_outputs(std::vector<bool>&& complete_txs,
40 const hash_t& dtx_id) -> bool {
41 auto req = request{dtx_id, std::move(complete_txs)};
42 auto res = send_request(req);
43 return res.has_value();
44 }
45
46 auto client::discard_dtx(const hash_t& dtx_id) -> bool {
47 auto req = request{dtx_id, discard_params()};
48 auto res = send_request(req);
49 return res.has_value();
50 }
51
52 auto client::send_request(const request& req) -> std::optional<response> {
53 auto result_timeout = std::chrono::seconds(3);
54 constexpr auto max_result_timeout = std::chrono::seconds(10);
55 constexpr auto retry_delay = std::chrono::seconds(1);
56 auto res = std::optional<response>();
57 while(m_running && !res.has_value()) {
58 res = m_client->call(req, result_timeout);
59 if(!res.has_value()) {
60 m_log.warn("Shard request failed");
61 if(m_running) {
62 std::this_thread::sleep_for(retry_delay);
63 result_timeout
64 = std::min(max_result_timeout, result_timeout * 2);
65 }
66 }
67 }
68 return res;
69 }
70
71 void client::stop() {
72 m_running = false;
73 m_client.reset();
74 }
75}
void stop() override
Shuts down the client and unblocks any existing requests waiting for a response.
auto lock_outputs(std::vector< tx > &&txs, const hash_t &dtx_id) -> std::optional< std::vector< bool > > override
Issues a lock RPC to the remote shard and returns its response.
auto init() -> bool
Initializes the RPC client.
auto discard_dtx(const hash_t &dtx_id) -> bool override
Issues a discard RPC to the remote shard and returns its response.
auto apply_outputs(std::vector< bool > &&complete_txs, const hash_t &dtx_id) -> bool override
Issues an apply RPC to the remote shard and returns its response.
Generalized logging class.
Definition logging.hpp:51
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.