6#ifndef VERIBLOCK_POP_CPP_STORAGE_ADAPTORS_BLOCK_PROVIDER_IMPL_HPP
7#define VERIBLOCK_POP_CPP_STORAGE_ADAPTORS_BLOCK_PROVIDER_IMPL_HPP
9#include <veriblock/pop/c/extern.h>
16#include <veriblock/pop/serde.hpp>
17#include <veriblock/pop/storage/block_batch.hpp>
18#include <veriblock/pop/storage/block_reader.hpp>
20#include "storage_interface.hpp"
21#include "veriblock/pop/entities/altblock.hpp"
22#include "veriblock/pop/entities/btcblock.hpp"
23#include "veriblock/pop/entities/vbkblock.hpp"
24#include "veriblock/pop/logger.hpp"
25#include "veriblock/pop/storage/block_iterator.hpp"
26#include "veriblock/pop/storage/stored_alt_block_addon.hpp"
27#include "veriblock/pop/storage/stored_block_index.hpp"
28#include "veriblock/pop/storage/stored_btc_block_addon.hpp"
29#include "veriblock/pop/storage/stored_vbk_block_addon.hpp"
30#include "veriblock/pop/uint.hpp"
31#include "veriblock/pop/validation_state.hpp"
38const char DB_BTC_BLOCK =
'Q';
39const char DB_BTC_PREV_BLOCK =
'R';
40const char DB_BTC_TIP =
'q';
41const char DB_VBK_BLOCK =
'W';
42const char DB_VBK_PREV_BLOCK =
'T';
43const char DB_VBK_TIP =
'w';
44const char DB_ALT_BLOCK =
'E';
45const char DB_ALT_PREV_BLOCK =
'Y';
46const char DB_ALT_TIP =
'e';
48template <
typename block_t>
49std::vector<uint8_t> tip_key();
52std::vector<uint8_t> tip_key<AltBlock>();
55std::vector<uint8_t> tip_key<VbkBlock>();
58std::vector<uint8_t> tip_key<BtcBlock>();
60template <
typename block_t>
61std::vector<uint8_t> block_key(
const typename block_t::hash_t& hash);
64std::vector<uint8_t> block_key<AltBlock>(
const AltBlock::hash_t& hash);
67std::vector<uint8_t> block_key<VbkBlock>(
const VbkBlock::hash_t& hash);
70std::vector<uint8_t> block_key<BtcBlock>(
const BtcBlock::hash_t& hash);
72template <
typename block_t>
73std::vector<uint8_t> block_prev_key(
const typename block_t::prev_hash_t& hash);
76std::vector<uint8_t> block_prev_key<AltBlock>(
77 const AltBlock::prev_hash_t& hash);
80std::vector<uint8_t> block_prev_key<VbkBlock>(
81 const VbkBlock::prev_hash_t& hash);
84std::vector<uint8_t> block_prev_key<BtcBlock>(
85 const BtcBlock::prev_hash_t& hash);
87template <
typename BlockT>
93 : it_(std::move(it)), params_(params) {}
95 void next()
override { it_->next(); }
98 std::vector<uint8_t> bytes;
99 if (!it_->value(bytes)) {
105 bytes, out, dummy)) {
106 VBK_LOG_ERROR(
"Can not deserialize StoredBlockIndex<%s>: %s",
115 bool key(
typename BlockT::hash_t& out)
const override {
116 std::vector<uint8_t> bytes;
117 if (!it_->key(bytes)) {
121 out = {bytes.begin() + 1, bytes.end()};
126 static const uint8_t prefix = block_key<BlockT>({})[0];
128 std::vector<uint8_t>
key;
129 return it_->valid() && it_->key(
key) && !
key.empty() &&
key[0] == prefix;
133 static const uint8_t prefix = block_key<BlockT>({})[0];
139 std::shared_ptr<StorageIterator> it_;
147 : storage_(storage), params_(params) {}
149 bool getAltTip(AltBlock::hash_t& out)
const override {
150 std::vector<uint8_t> bytes_out;
151 if (!storage_.read(tip_key<AltBlock>(), bytes_out)) {
158 std::vector<uint8_t> bytes_out;
159 if (!storage_.read(tip_key<VbkBlock>(), bytes_out)) {
166 std::vector<uint8_t> bytes_out;
167 if (!storage_.read(tip_key<BtcBlock>(), bytes_out)) {
174 bool getBlock(
const AltBlock::prev_hash_t& hash,
176 return getBlock<AltBlock>(hash, out);
181 return getBlock<VbkBlock>(hash, out);
186 return getBlock<BtcBlock>(hash, out);
189 std::shared_ptr<BlockIterator<AltBlock>> getAltBlockIterator()
191 return std::make_shared<BlockIteratorImpl<AltBlock>>(
192 storage_.generateIterator(), params_);
194 std::shared_ptr<BlockIterator<VbkBlock>> getVbkBlockIterator()
196 return std::make_shared<BlockIteratorImpl<VbkBlock>>(
197 storage_.generateIterator(), params_);
199 std::shared_ptr<BlockIterator<BtcBlock>> getBtcBlockIterator()
201 return std::make_shared<BlockIteratorImpl<BtcBlock>>(
202 storage_.generateIterator(), params_);
206 template <
typename block_t>
207 bool getBlock(
const typename block_t::prev_hash_t& prev_hash,
209 std::vector<uint8_t> hash_out;
210 if (!storage_.read(block_prev_key<block_t>(prev_hash), hash_out)) {
214 typename block_t::hash_t hash = hash_out;
215 std::vector<uint8_t> bytes_out;
216 if (!storage_.read(block_key<block_t>(hash), bytes_out)) {
222 bytes_out, out, dummy)) {
223 VBK_LOG_ERROR(
"Can not deserialize StoredBlockIndex<%s> block: %s",
242 const AltBlock::prev_hash_t& prev_hash,
244 batch_.write(block_prev_key<AltBlock>(prev_hash), hash);
251 batch_.write(block_prev_key<VbkBlock>(prev_hash), hash.asVector());
258 batch_.write(block_prev_key<BtcBlock>(prev_hash), hash.asVector());
262 void writeTip(
const AltBlock::hash_t& hash)
override {
263 batch_.write(tip_key<AltBlock>(), hash);
267 batch_.write(tip_key<VbkBlock>(), hash.asVector());
271 batch_.write(tip_key<BtcBlock>(), hash.asVector());
Class that is used for storing validation state.
bool DeserializeFromVbkEncoding(ReadStream &stream, AltBlockAddon &out, ValidationState &state)
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::vector< uint8_t > SerializeToVbkEncoding(const T &obj)
Serialize to VBK encoding.
Base class for all Altchain-related configs.
Contiguous byte array of fixed size.
An interface which represents single write batch of a group of blocks.
An abstraction over on-disk storage block iterator.
An abstraction over on-disk storage iterator.
void writeBlock(const AltBlock::hash_t &hash, const AltBlock::prev_hash_t &prev_hash, const StoredBlockIndex< AltBlock > &blk) override
Save BlockIndex on disk.
void writeBlock(const BtcBlock::hash_t &hash, const BtcBlock::prev_hash_t &prev_hash, const StoredBlockIndex< BtcBlock > &blk) override
This is an overloaded member function, provided for convenience. It differs from the above function o...
void writeTip(const VbkBlock::hash_t &hash) override
This is an overloaded member function, provided for convenience. It differs from the above function o...
void writeTip(const BtcBlock::hash_t &hash) override
This is an overloaded member function, provided for convenience. It differs from the above function o...
void writeBlock(const VbkBlock::hash_t &hash, const VbkBlock::prev_hash_t &prev_hash, const StoredBlockIndex< VbkBlock > &blk) override
This is an overloaded member function, provided for convenience. It differs from the above function o...
void writeTip(const AltBlock::hash_t &hash) override
Save Tree tip on disk.
void seek_start() override
Set iterator to the start of the collection.
void next() override
Iterate to the next block.
bool value(StoredBlockIndex< BlockT > &out) const override
Return the current block value.
bool key(typename BlockT::hash_t &out) const override
Return the current block hash key.
bool valid() const override
Return if valid iterator or not.