Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
index
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
8
#include <
consensus/consensus.h
>
9
#include <
crypto/siphash.h
>
10
#include <
primitives/transaction_identifier.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
21
namespace
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
35
constexpr
uint8_t
DB_TXINDEX_HASHED
{
'x'
};
36
constexpr
uint8_t
DB_BLOCK_SEQ
{
's'
};
37
constexpr
uint8_t
DB_BLOCK_HASH
{
'h'
};
38
inline
const
std::string
DB_NEXT_BLOCK_SEQ
{
"next_block_seq"
};
39
inline
const
std::string
DB_TXID_HASH_SALT
{
"txid_hash_salt"
};
40
inline
const
std::string
DB_BEST_BLOCK_V2
{
"best_block_v2"
};
42
constexpr
uint8_t
DB_TXINDEX
{
't'
};
43
45
inline
constexpr
std::array<std::byte, 0>
EMPTY_VALUE
{};
46
48
constexpr
uint32_t
BLOCK_HEADER_SIZE
{80};
49
55
struct
BlockTxPosition
{
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
66
SERIALIZE_METHODS
(
BlockTxPosition
, obj)
67
{
68
READWRITE
(
VARINT
(obj.block_seq),
69
Using<
BigEndianFormatter<TX_OFFSET_SIZE>
>(obj.tx_offset_in_block));
70
}
71
};
72
74
struct
BlockSeqKey
{
75
uint32_t
block_seq
{0};
76
77
SERIALIZE_METHODS
(
BlockSeqKey
, obj)
78
{
79
uint8_t prefix{
DB_BLOCK_SEQ
};
80
READWRITE
(prefix);
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
87
struct
BlockHashKey
{
88
uint256
block_hash
;
89
90
SERIALIZE_METHODS
(
BlockHashKey
, obj)
91
{
92
uint8_t prefix{
DB_BLOCK_HASH
};
93
READWRITE
(prefix);
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
99
constexpr
int
HASH_PREFIX_SIZE
{5};
100
using
TxHashKeyPrefix
= uint64_t;
101
102
inline
TxHashKeyPrefix
CreateKeyPrefix
(
const
SipHasher13UJ
& hasher,
const
Txid
& txid)
103
{
104
return
hasher.
Hash
(txid.
ToUint256
()) >> (8 * (
sizeof
(
TxHashKeyPrefix
) -
HASH_PREFIX_SIZE
));
105
}
106
107
struct
DBKey
{
108
TxHashKeyPrefix
hash_prefix
{0};
109
BlockTxPosition
pos
;
110
111
SERIALIZE_METHODS
(
DBKey
, obj)
112
{
113
uint8_t prefix{
DB_TXINDEX_HASHED
};
114
READWRITE
(prefix);
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
121
inline
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
SipHasher13UJ
A custom weaker variant of SipHash-1-3 without padding, and supporting "jumbo" inputs.
Definition
siphash.h:161
SipHasher13UJ::Hash
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
transaction_identifier::ToUint256
const uint256 & ToUint256() const LIFETIMEBOUND
Definition
transaction_identifier.h:37
uint256
256-bit opaque blob.
Definition
uint256.h:196
consensus.h
txindex
Definition
txindex_key.h:21
txindex::DB_TXID_HASH_SALT
const std::string DB_TXID_HASH_SALT
Definition
txindex_key.h:39
txindex::EMPTY_VALUE
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
txindex::DB_TXINDEX
constexpr uint8_t DB_TXINDEX
Prefix of a legacy (pre-hashing) txindex row.
Definition
txindex_key.h:42
txindex::HASH_PREFIX_SIZE
constexpr int HASH_PREFIX_SIZE
Definition
txindex_key.h:99
txindex::DB_BLOCK_HASH
constexpr uint8_t DB_BLOCK_HASH
Definition
txindex_key.h:37
txindex::CreateKeyPrefix
TxHashKeyPrefix CreateKeyPrefix(const SipHasher13UJ &hasher, const Txid &txid)
Definition
txindex_key.h:102
txindex::BLOCK_HEADER_SIZE
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
txindex::DB_NEXT_BLOCK_SEQ
const std::string DB_NEXT_BLOCK_SEQ
Definition
txindex_key.h:38
txindex::TxHashKeyPrefix
uint64_t TxHashKeyPrefix
Definition
txindex_key.h:100
txindex::LegacyTxKey
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
txindex::DB_BEST_BLOCK_V2
const std::string DB_BEST_BLOCK_V2
Definition
txindex_key.h:40
txindex::DB_BLOCK_SEQ
constexpr uint8_t DB_BLOCK_SEQ
Definition
txindex_key.h:36
txindex::DB_TXINDEX_HASHED
constexpr uint8_t DB_TXINDEX_HASHED
Definition
txindex_key.h:35
serialize.h
VARINT
#define VARINT(obj)
Definition
serialize.h:494
BigEndianFormatter
CustomUintFormatter< Bytes, true > BigEndianFormatter
Definition
serialize.h:556
READWRITE
#define READWRITE(...)
Definition
serialize.h:148
siphash.h
txindex::BlockHashKey
Key for looking up the sequence number assigned to the block with the given hash.
Definition
txindex_key.h:87
txindex::BlockHashKey::SERIALIZE_METHODS
SERIALIZE_METHODS(BlockHashKey, obj)
Definition
txindex_key.h:90
txindex::BlockHashKey::block_hash
uint256 block_hash
Definition
txindex_key.h:88
txindex::BlockSeqKey
Key for looking up the hash of the block with the given sequence number.
Definition
txindex_key.h:74
txindex::BlockSeqKey::block_seq
uint32_t block_seq
Definition
txindex_key.h:75
txindex::BlockSeqKey::SERIALIZE_METHODS
SERIALIZE_METHODS(BlockSeqKey, obj)
Definition
txindex_key.h:77
txindex::BlockTxPosition
Definition
txindex_key.h:55
txindex::BlockTxPosition::tx_offset_in_block
uint32_t tx_offset_in_block
Definition
txindex_key.h:57
txindex::BlockTxPosition::SERIALIZE_METHODS
SERIALIZE_METHODS(BlockTxPosition, obj)
Definition
txindex_key.h:66
txindex::BlockTxPosition::block_seq
uint32_t block_seq
Definition
txindex_key.h:56
txindex::BlockTxPosition::operator==
friend bool operator==(const BlockTxPosition &, const BlockTxPosition &)=default
txindex::BlockTxPosition::TX_OFFSET_SIZE
static constexpr uint32_t TX_OFFSET_SIZE
Definition
txindex_key.h:63
txindex::DBKey
Definition
txindex_key.h:107
txindex::DBKey::pos
BlockTxPosition pos
Definition
txindex_key.h:109
txindex::DBKey::SERIALIZE_METHODS
SERIALIZE_METHODS(DBKey, obj)
Definition
txindex_key.h:111
txindex::DBKey::hash_prefix
TxHashKeyPrefix hash_prefix
Definition
txindex_key.h:108
transaction_identifier.h
Txid
transaction_identifier< false > Txid
Txid commits to all transaction fields except the witness.
Definition
transaction_identifier.h:60
uint256.h
Generated on
for Bitcoin Core by
1.18.0