OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
locking_shard.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_LOCKING_SHARD_LOCKING_SHARD_H_
7#define OPENCBDC_TX_SRC_LOCKING_SHARD_LOCKING_SHARD_H_
8
9#include "client.hpp"
10#include "interface.hpp"
11#include "status_interface.hpp"
14#include "util/common/hash.hpp"
17
18#include <filesystem>
19#include <future>
20#include <leveldb/db.h>
21#include <memory>
22#include <string>
23#include <unordered_map>
24#include <unordered_set>
25#include <variant>
26#include <vector>
27
28namespace cbdc::locking_shard {
44 class locking_shard final : public interface, public status_interface {
45 public:
55 locking_shard(const std::pair<uint8_t, uint8_t>& output_range,
56 std::shared_ptr<logging::log> logger,
57 size_t completed_txs_cache_size,
58 const std::string& preseed_file,
59 config::options opts);
60 locking_shard() = delete;
61
76 auto lock_outputs(std::vector<tx>&& txs, const hash_t& dtx_id)
77 -> std::optional<std::vector<bool>> final;
78
95 auto apply_outputs(std::vector<bool>&& complete_txs,
96 const hash_t& dtx_id) -> bool final;
97
105 auto discard_dtx(const hash_t& dtx_id) -> bool final;
106
110 void stop() final;
111
116 [[nodiscard]] auto check_unspent(const hash_t& uhs_id)
117 -> std::optional<bool> final;
118
124 [[nodiscard]] auto check_tx_id(const hash_t& tx_id)
125 -> std::optional<bool> final;
126
127 private:
128 auto read_preseed_file(const std::string& preseed_file) -> bool;
129 auto check_and_lock_tx(const tx& t) -> bool;
130
131 struct prepared_dtx {
132 std::vector<tx> m_txs;
133 std::vector<bool> m_results;
134 };
135 std::atomic_bool m_running{true};
136
137 std::shared_ptr<logging::log> m_logger;
138 mutable std::shared_mutex m_mut;
139 std::unordered_set<hash_t, hashing::null> m_uhs;
140 std::unordered_set<hash_t, hashing::null> m_locked;
141 std::unordered_map<hash_t, prepared_dtx, hashing::null>
142 m_prepared_dtxs;
143 std::unordered_set<hash_t, hashing::null> m_applied_dtxs;
145 config::options m_opts;
146 };
147}
148
149#endif // OPENCBDC_TX_SRC_LOCKING_SHARD_LOCKING_SHARD_H_
Thread-safe set with a maximum size.
Definition cache_set.hpp:21
In-memory implementation of interface and status_interface.
auto apply_outputs(std::vector< bool > &&complete_txs, const hash_t &dtx_id) -> bool final
Selectively applies the transactions from a previous lock operation.
auto lock_outputs(std::vector< tx > &&txs, const hash_t &dtx_id) -> std::optional< std::vector< bool > > final
Attempts to lock the input hashes for the given batch of transactions.
auto check_tx_id(const hash_t &tx_id) -> std::optional< bool > final
Queries whether the given TX ID is confirmed in the cache of recently confirmed TX IDs.
auto discard_dtx(const hash_t &dtx_id) -> bool final
Discards any cached information about a given distributed transaction.
void stop() final
Stops the locking shard from processing further commands.
auto check_unspent(const hash_t &uhs_id) -> std::optional< bool > final
Queries whether the shard's UHS contains the given UHS ID.
Interface for querying the read-only state of a locking shard.
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
Project-wide configuration options.
Definition config.hpp:132
Transaction type processed by locking shards.