OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
error_cache.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 "error_cache.hpp"
7
9
10namespace cbdc::watchtower {
11 error_cache::error_cache(size_t k) : m_k_errs(k) {
12 m_tx_id_errs.reserve(k);
13 m_uhs_errs.reserve(k);
14 }
15
16 void error_cache::push_errors(std::vector<tx_error>&& errs) {
17 for(auto&& err : errs) {
18 if((m_k_errs != 0) && (m_errs.size() == m_k_errs)) {
19 auto& old_err = m_errs.front();
20 m_tx_id_errs.erase(old_err->tx_id());
21
22 std::visit(overloaded{
23 [](const tx_error_sync& /* unused */) {},
24 [&](const tx_error_inputs_dne& arg) {
25 for(auto& uhs_id : arg.input_uhs_ids()) {
26 m_uhs_errs.erase(uhs_id);
27 }
28 },
29 [&](const tx_error_inputs_spent& arg) {
30 for(const auto& uhs_id :
31 arg.input_uhs_ids()) {
32 m_uhs_errs.erase(uhs_id);
33 }
34 },
35 [](const tx_error_stxo_range& /* unused */) {},
36 [](const tx_error_incomplete& /* unused */) {},
37 },
38 old_err->info());
39
40 m_errs.pop();
41 }
42
43 auto new_err = std::make_shared<tx_error>(std::move(err));
44 m_errs.push(new_err);
45 m_tx_id_errs.insert({new_err->tx_id(), new_err});
46 std::visit(overloaded{
47 [](const tx_error_sync& /* unused */) {},
48 [&](const tx_error_inputs_dne& arg) {
49 for(auto& uhs_id : arg.input_uhs_ids()) {
50 m_uhs_errs.insert({uhs_id, new_err});
51 }
52 },
53 [&](const tx_error_inputs_spent& arg) {
54 for(const auto& uhs_id : arg.input_uhs_ids()) {
55 m_uhs_errs.insert({uhs_id, new_err});
56 }
57 },
58 [](const tx_error_stxo_range& /* unused */) {},
59 [](const tx_error_incomplete& /* unused */) {},
60 },
61 new_err->info());
62 }
63 }
64
65 auto error_cache::check_tx_id(const hash_t& tx_id) const
66 -> std::optional<tx_error> {
67 auto res = m_tx_id_errs.find(tx_id);
68 if(res == m_tx_id_errs.end()) {
69 return std::nullopt;
70 }
71 return *res->second;
72 }
73
74 auto error_cache::check_uhs_id(const hash_t& uhs_id) const
75 -> std::optional<tx_error> {
76 auto res = m_uhs_errs.find(uhs_id);
77 if(res == m_uhs_errs.end()) {
78 return std::nullopt;
79 }
80 return *res->second;
81 }
82}
auto check_tx_id(const hash_t &tx_id) const -> std::optional< tx_error >
Checks the cache for an error associated with the given Tx ID.
void push_errors(std::vector< tx_error > &&errs)
Moves an error into the error cache, evicting the oldest error if the cache has reached its maximum s...
auto check_uhs_id(const hash_t &uhs_id) const -> std::optional< tx_error >
Checks the cache for an error associated with the given UHS ID.
Indicates a shard that tried to process a given transaction could not locate one or more of the trans...
Indicates that the given transaction contains one or more inputs that have already been spent in othe...
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
Variant handler template.
Indicates that the atomizer did not receive enough attestations for a particular transaction from sha...
Indicates that a shard did not attest to this transaction recently enough for the atomizer to check i...
Indicates a shard that tried to process a given transaction was out of sync with the atomizer,...