Bitcoin Core 31.99.0
P2P Digital Currency
txindex_key.h
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_INDEX_TXINDEX_KEY_H
6#define BITCOIN_INDEX_TXINDEX_KEY_H
7
9#include <crypto/siphash.h>
11#include <serialize.h>
12#include <uint256.h>
13
14#include <array>
15#include <cstddef>
16#include <cstdint>
17#include <ios>
18#include <string>
19#include <utility>
20
21namespace txindex {
22/*
23 * Database layout:
24 *
25 * ['x', hash prefix, block seq, tx offset] -> (empty)
26 * ['s', block seq] -> block hash
27 * ['h', block hash] -> block seq
28 * ["next_block_seq"] -> next block seq to assign
29 * ["txid_hash_salt"] -> txid hasher salt
30 * ["best_block_v2"] -> current sync locator
31 * ['t', txid] -> legacy CDiskTxPos
32 * ['B'] -> legacy sync locator
33 */
34
35constexpr uint8_t DB_TXINDEX_HASHED{'x'};
36constexpr uint8_t DB_BLOCK_SEQ{'s'};
37constexpr uint8_t DB_BLOCK_HASH{'h'};
38inline const std::string DB_NEXT_BLOCK_SEQ{"next_block_seq"};
39inline const std::string DB_TXID_HASH_SALT{"txid_hash_salt"};
40inline const std::string DB_BEST_BLOCK_V2{"best_block_v2"};
42constexpr uint8_t DB_TXINDEX{'t'};
43
45inline constexpr std::array<std::byte, 0> EMPTY_VALUE{};
46
48constexpr uint32_t BLOCK_HEADER_SIZE{80};
49
56 uint32_t block_seq{0};
57 uint32_t tx_offset_in_block{0};
58
59 friend bool operator==(const BlockTxPosition&, const BlockTxPosition&) = default;
60
61 // tx_offset is encoded in 3-byte big-endian integer.
62 // This can hold up to 16,777,216, which is >4x the maximum 4 million block weight position
63 static constexpr uint32_t TX_OFFSET_SIZE{3};
64 static_assert(MAX_BLOCK_SERIALIZED_SIZE <= BigEndianFormatter<TX_OFFSET_SIZE>::MAX);
65
67 {
68 READWRITE(VARINT(obj.block_seq),
69 Using<BigEndianFormatter<TX_OFFSET_SIZE>>(obj.tx_offset_in_block));
70 }
71};
72
75 uint32_t block_seq{0};
76
78 {
79 uint8_t prefix{DB_BLOCK_SEQ};
81 if (ser_action.ForRead() && prefix != DB_BLOCK_SEQ) throw std::ios_base::failure("Invalid format for txindex block seq key");
82 READWRITE(VARINT(obj.block_seq));
83 }
84};
85
89
91 {
92 uint8_t prefix{DB_BLOCK_HASH};
94 if (ser_action.ForRead() && prefix != DB_BLOCK_HASH) throw std::ios_base::failure("Invalid format for txindex block hash key");
95 READWRITE(obj.block_hash);
96 }
97};
98
99constexpr int HASH_PREFIX_SIZE{5};
100using TxHashKeyPrefix = uint64_t;
101
102inline TxHashKeyPrefix CreateKeyPrefix(const SipHasher13UJ& hasher, const Txid& txid)
103{
104 return hasher.Hash(txid.ToUint256()) >> (8 * (sizeof(TxHashKeyPrefix) - HASH_PREFIX_SIZE));
105}
106
107struct DBKey {
110
112 {
113 uint8_t prefix{DB_TXINDEX_HASHED};
115 if (ser_action.ForRead() && prefix != DB_TXINDEX_HASHED) throw std::ios_base::failure("Invalid format for txindex DB key");
116 READWRITE(Using<BigEndianFormatter<HASH_PREFIX_SIZE>>(obj.hash_prefix), obj.pos);
117 }
118};
119
121inline std::pair<uint8_t, uint256> LegacyTxKey(const Txid& txid)
122{
123 return {DB_TXINDEX, txid.ToUint256()};
124}
125
126} // namespace txindex
127
128#endif // BITCOIN_INDEX_TXINDEX_KEY_H
A custom weaker variant of SipHash-1-3 without padding, and supporting "jumbo" inputs.
Definition: siphash.h:161
ALWAYS_INLINE uint64_t Hash(const uint256 &hash) const noexcept
Hash a jumbo block after the data written so far and finalize without modifying the object.
Definition: siphash.h:175
const uint256 & ToUint256() const LIFETIMEBOUND
256-bit opaque blob.
Definition: uint256.h:196
const std::string DB_TXID_HASH_SALT
Definition: txindex_key.h:39
constexpr std::array< std::byte, 0 > EMPTY_VALUE
Empty value of a hashed txindex row, whose position is encoded in its key.
Definition: txindex_key.h:45
constexpr uint8_t DB_TXINDEX
Prefix of a legacy (pre-hashing) txindex row.
Definition: txindex_key.h:42
constexpr int HASH_PREFIX_SIZE
Definition: txindex_key.h:99
constexpr uint8_t DB_BLOCK_HASH
Definition: txindex_key.h:37
TxHashKeyPrefix CreateKeyPrefix(const SipHasher13UJ &hasher, const Txid &txid)
Definition: txindex_key.h:102
constexpr uint32_t BLOCK_HEADER_SIZE
Serialized size of a block header, the offset of the first byte after it.
Definition: txindex_key.h:48
const std::string DB_NEXT_BLOCK_SEQ
Definition: txindex_key.h:38
uint64_t TxHashKeyPrefix
Definition: txindex_key.h:100
std::pair< uint8_t, uint256 > LegacyTxKey(const Txid &txid)
Key of a legacy (pre-hashing) txindex row: the full txid under the 't' prefix.
Definition: txindex_key.h:121
const std::string DB_BEST_BLOCK_V2
Definition: txindex_key.h:40
constexpr uint8_t DB_BLOCK_SEQ
Definition: txindex_key.h:36
constexpr uint8_t DB_TXINDEX_HASHED
Definition: txindex_key.h:35
const char * prefix
Definition: rest.cpp:1180
#define VARINT(obj)
Definition: serialize.h:494
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:491
#define READWRITE(...)
Definition: serialize.h:148
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:525
Key for looking up the sequence number assigned to the block with the given hash.
Definition: txindex_key.h:87
SERIALIZE_METHODS(BlockHashKey, obj)
Definition: txindex_key.h:90
Key for looking up the hash of the block with the given sequence number.
Definition: txindex_key.h:74
SERIALIZE_METHODS(BlockSeqKey, obj)
Definition: txindex_key.h:77
The location of a transaction: the sequence number of the block that contains it and the transaction'...
Definition: txindex_key.h:55
SERIALIZE_METHODS(BlockTxPosition, obj)
Definition: txindex_key.h:66
friend bool operator==(const BlockTxPosition &, const BlockTxPosition &)=default
static constexpr uint32_t TX_OFFSET_SIZE
Definition: txindex_key.h:63
BlockTxPosition pos
Definition: txindex_key.h:109
SERIALIZE_METHODS(DBKey, obj)
Definition: txindex_key.h:111
TxHashKeyPrefix hash_prefix
Definition: txindex_key.h:108