OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
distributed_tx.hpp
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#ifndef OPENCBDC_TX_SRC_COORDINATOR_DISTRIBUTED_TX_H_
7#define OPENCBDC_TX_SRC_COORDINATOR_DISTRIBUTED_TX_H_
8
9#include "state_machine.hpp"
13#include "util/raft/node.hpp"
14
15#include <memory>
16#include <vector>
17
18namespace cbdc::coordinator {
24 public:
32 const hash_t& dtx_id,
33 std::vector<std::shared_ptr<locking_shard::interface>> shards,
34 std::shared_ptr<logging::log> logger);
35
41 [[nodiscard]] auto execute() -> std::optional<std::vector<bool>>;
42
47 auto add_tx(const transaction::compact_tx& tx) -> size_t;
48
51 [[nodiscard]] auto get_id() const -> hash_t;
52
53 using discard_cb_t = std::function<bool(const hash_t&)>;
54 using done_cb_t = std::function<bool(const hash_t&)>;
56 = std::function<bool(const hash_t&,
57 const std::vector<bool>&,
58 const std::vector<std::vector<uint64_t>>&)>;
59 using prepare_cb_t
60 = std::function<bool(const hash_t&,
61 const std::vector<transaction::compact_tx>&)>;
62
69 void set_prepare_cb(const prepare_cb_t& cb);
70
80 void set_commit_cb(const commit_cb_t& cb);
81
88 void set_discard_cb(const discard_cb_t& cb);
89
96 void set_done_cb(const done_cb_t& cb);
97
101 void recover_prepare(const std::vector<transaction::compact_tx>& txs);
102
112 void recover_commit(const std::vector<bool>& complete_txs,
113 const std::vector<std::vector<uint64_t>>& tx_idxs);
114
117 void recover_discard();
118
121 [[nodiscard]] auto size() const -> size_t;
122
123 enum class dtx_state {
125 start,
127 prepare,
129 commit,
131 discard,
133 done,
140 failed
141 };
142
145 [[nodiscard]] auto get_state() const -> dtx_state;
146
147 private:
148 [[nodiscard]] auto prepare() -> std::optional<std::vector<bool>>;
149
150 [[nodiscard]] auto commit(const std::vector<bool>& complete_txs)
151 -> bool;
152
153 auto discard() -> bool;
154
155 hash_t m_dtx_id;
156 std::vector<std::shared_ptr<locking_shard::interface>> m_shards;
157 std::vector<std::vector<locking_shard::tx>> m_txs;
158 std::vector<transaction::compact_tx> m_full_txs;
159 std::vector<std::vector<uint64_t>> m_tx_idxs;
160 prepare_cb_t m_prepare_cb;
161 commit_cb_t m_commit_cb;
162 discard_cb_t m_discard_cb;
163 done_cb_t m_done_cb;
165 std::vector<bool> m_complete_txs;
166 std::shared_ptr<logging::log> m_logger;
167 };
168}
169
170#endif // OPENCBDC_TX_SRC_COORDINATOR_DISTRIBUTED_TX_H_
Class to manage a single distributed transaction (dtx) batch between shards.
std::function< bool(const hash_t &)> discard_cb_t
void set_commit_cb(const commit_cb_t &cb)
Registers a callback to be called before starting the commit phase of the dtx.
auto size() const -> size_t
Returns the number of transactions in the dtx.
auto get_id() const -> hash_t
Returns the dtx ID associated with this coordinator instance.
void recover_commit(const std::vector< bool > &complete_txs, const std::vector< std::vector< uint64_t > > &tx_idxs)
Sets the state of the dtx to commit and sets the state from the end of the prepare phase so that exec...
auto execute() -> std::optional< std::vector< bool > >
Executes the dtx batch to completion or failure, either from start, or an intermediate state if one o...
void set_discard_cb(const discard_cb_t &cb)
Registers a callback to be called before the discard phase of the dtx is started.
@ failed
dtx was interrupted and needs recovery.
@ start
dtx initial state, no action has been performed yet
@ commit
dtx is calling commit on shards
void recover_discard()
Sets the state of the dtx to discard so that execute() will start from the discard phase.
auto add_tx(const transaction::compact_tx &tx) -> size_t
Adds a TX to the batch managed by this coordinator and dtx ID.
void recover_prepare(const std::vector< transaction::compact_tx > &txs)
Sets the state of the dtx to prepare and re-adds all the txs included in the batch.
std::function< bool(const hash_t &, const std::vector< transaction::compact_tx > &)> prepare_cb_t
std::function< bool(const hash_t &, const std::vector< bool > &, const std::vector< std::vector< uint64_t > > &)> commit_cb_t
auto get_state() const -> dtx_state
Returns the current state of the dtx.
distributed_tx(const hash_t &dtx_id, std::vector< std::shared_ptr< locking_shard::interface > > shards, std::shared_ptr< logging::log > logger)
Constructs a new transaction coordinator instance.
void set_prepare_cb(const prepare_cb_t &cb)
Registers a callback to be called before starting the prepare phase of the dtx.
std::function< bool(const hash_t &)> done_cb_t
void set_done_cb(const done_cb_t &cb)
Registers a callback to be called before the done phase of the dtx is started.
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
Pseudorandom number generator (PRNG) for generating random data from a given entropy source.
A condensed, hash-only transaction representation.