OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
block_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 "block_cache.hpp"
7
8namespace cbdc::watchtower {
9 block_cache::block_cache(size_t k) : m_k_blks(k) {
10 static constexpr auto puts_per_tx = 2;
11 static constexpr auto txs_per_block = 1000000;
12 m_spent_ids.reserve(k * txs_per_block * puts_per_tx);
13 m_unspent_ids.reserve(k * txs_per_block * puts_per_tx);
14 }
15
17 if((m_k_blks != 0) && (m_blks.size() == m_k_blks)) {
18 auto& old_blk = m_blks.front();
19 for(auto& tx : old_blk.m_transactions) {
20 for(auto& in : tx.m_inputs) {
21 m_spent_ids.erase(in);
22 }
23 for(auto& out : tx.m_uhs_outputs) {
24 m_unspent_ids.erase(out);
25 }
26 }
27 m_blks.pop();
28 }
29
30 m_blks.push(std::forward<cbdc::atomizer::block>(blk));
31
32 auto blk_height = m_blks.back().m_height;
33 for(auto& tx : m_blks.back().m_transactions) {
34 for(auto& in : tx.m_inputs) {
35 m_unspent_ids.erase(in);
36 m_spent_ids.insert(
37 {{in, std::make_pair(blk_height, tx.m_id)}});
38 }
39 for(auto& out : tx.m_uhs_outputs) {
40 m_unspent_ids.insert(
41 {{out, std::make_pair(blk_height, tx.m_id)}});
42 }
43 }
44 m_best_blk_height = std::max(m_best_blk_height, blk_height);
45 }
46
47 auto block_cache::check_unspent(const hash_t& uhs_id) const
48 -> std::optional<block_cache_result> {
49 auto res = m_unspent_ids.find(uhs_id);
50 if(res == m_unspent_ids.end()) {
51 return std::nullopt;
52 }
53 return res->second;
54 }
55
56 auto block_cache::check_spent(const hash_t& uhs_id) const
57 -> std::optional<block_cache_result> {
58 auto res = m_spent_ids.find(uhs_id);
59 if(res == m_spent_ids.end()) {
60 return std::nullopt;
61 }
62 return res->second;
63 }
64 auto block_cache::best_block_height() const -> uint64_t {
65 return m_best_blk_height;
66 }
67}
void push_block(cbdc::atomizer::block &&blk)
Moves a block into the block cache, evicting the oldest block if the cache has reached its maximum si...
auto check_unspent(const hash_t &uhs_id) const -> std::optional< block_cache_result >
Checks to see if the given UHS ID is spendable according to the blocks in the cache.
auto best_block_height() const -> uint64_t
Returns the block height of the highest observed block.
auto check_spent(const hash_t &uhs_id) const -> std::optional< block_cache_result >
Checks to see if the given UHS ID has been spent according to the blocks in the cache.
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
Batch of compact transactions settled by the atomizer.
Definition block.hpp:19