Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/blockencodings.h
Line
Count
Source
1
// Copyright (c) 2016-present 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_BLOCKENCODINGS_H
6
#define BITCOIN_BLOCKENCODINGS_H
7
8
#include <crypto/siphash.h>
9
#include <primitives/block.h>
10
11
#include <functional>
12
13
class CTxMemPool;
14
class BlockValidationState;
15
namespace Consensus {
16
struct Params;
17
};
18
19
// Transaction compression schemes for compact block relay can be introduced by writing
20
// an actual formatter here.
21
using TransactionCompression = DefaultFormatter;
22
23
class DifferenceFormatter
24
{
25
    uint64_t m_shift = 0;
26
27
public:
28
    template<typename Stream, typename I>
29
    void Ser(Stream& s, I v)
30
1.64k
    {
31
1.64k
        if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow");
32
1.64k
        WriteCompactSize(s, v - m_shift);
33
1.64k
        m_shift = uint64_t(v) + 1;
34
1.64k
    }
void DifferenceFormatter::Ser<DataStream, unsigned short>(DataStream&, unsigned short)
Line
Count
Source
30
5
    {
31
5
        if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow");
32
5
        WriteCompactSize(s, v - m_shift);
33
5
        m_shift = uint64_t(v) + 1;
34
5
    }
void DifferenceFormatter::Ser<VectorWriter, unsigned short>(VectorWriter&, unsigned short)
Line
Count
Source
30
1.63k
    {
31
1.63k
        if (v < m_shift || v >= std::numeric_limits<uint64_t>::max()) throw std::ios_base::failure("differential value overflow");
32
1.63k
        WriteCompactSize(s, v - m_shift);
33
1.63k
        m_shift = uint64_t(v) + 1;
34
1.63k
    }
35
    template<typename Stream, typename I>
36
    void Unser(Stream& s, I& v)
37
1.60k
    {
38
1.60k
        uint64_t n = ReadCompactSize(s);
39
1.60k
        m_shift += n;
40
1.60k
        if (m_shift < n || m_shift >= std::numeric_limits<uint64_t>::max() || m_shift < std::numeric_limits<I>::min() || m_shift > std::numeric_limits<I>::max()) throw std::ios_base::failure("differential value overflow");
41
1.60k
        v = I(m_shift++);
42
1.60k
    }
43
};
44
45
class BlockTransactionsRequest {
46
public:
47
    // A BlockTransactionsRequest message
48
    uint256 blockhash;
49
    std::vector<uint16_t> indexes;
50
51
    SERIALIZE_METHODS(BlockTransactionsRequest, obj)
52
1.21k
    {
53
1.21k
        READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
54
1.21k
    }
void BlockTransactionsRequest::SerializationOps<DataStream, BlockTransactionsRequest const, ActionSerialize>(BlockTransactionsRequest const&, DataStream&, ActionSerialize)
Line
Count
Source
52
2
    {
53
2
        READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
54
2
    }
void BlockTransactionsRequest::SerializationOps<DataStream, BlockTransactionsRequest, ActionUnserialize>(BlockTransactionsRequest&, DataStream&, ActionUnserialize)
Line
Count
Source
52
616
    {
53
616
        READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
54
616
    }
void BlockTransactionsRequest::SerializationOps<VectorWriter, BlockTransactionsRequest const, ActionSerialize>(BlockTransactionsRequest const&, VectorWriter&, ActionSerialize)
Line
Count
Source
52
597
    {
53
597
        READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
54
597
    }
55
};
56
57
class BlockTransactions {
58
public:
59
    // A BlockTransactions message
60
    uint256 blockhash;
61
    std::vector<CTransactionRef> txn;
62
63
16.8k
    BlockTransactions() = default;
64
    explicit BlockTransactions(const BlockTransactionsRequest& req) :
65
611
        blockhash(req.blockhash), txn(req.indexes.size()) {}
66
67
    SERIALIZE_METHODS(BlockTransactions, obj)
68
1.20k
    {
69
1.20k
        READWRITE(obj.blockhash, TX_WITH_WITNESS(Using<VectorFormatter<TransactionCompression>>(obj.txn)));
70
1.20k
    }
void BlockTransactions::SerializationOps<VectorWriter, BlockTransactions const, ActionSerialize>(BlockTransactions const&, VectorWriter&, ActionSerialize)
Line
Count
Source
68
610
    {
69
610
        READWRITE(obj.blockhash, TX_WITH_WITNESS(Using<VectorFormatter<TransactionCompression>>(obj.txn)));
70
610
    }
void BlockTransactions::SerializationOps<DataStream, BlockTransactions, ActionUnserialize>(BlockTransactions&, DataStream&, ActionUnserialize)
Line
Count
Source
68
594
    {
69
594
        READWRITE(obj.blockhash, TX_WITH_WITNESS(Using<VectorFormatter<TransactionCompression>>(obj.txn)));
70
594
    }
71
};
72
73
// Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and PartiallyDownloadedBlock
74
struct PrefilledTransaction {
75
    // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
76
    // as a proper transaction-in-block-index in PartiallyDownloadedBlock
77
    uint16_t index;
78
    CTransactionRef tx;
79
80
72.6k
    SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
void PrefilledTransaction::SerializationOps<DataStream, PrefilledTransaction, ActionUnserialize>(PrefilledTransaction&, DataStream&, ActionUnserialize)
Line
Count
Source
80
20.1k
    SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
void PrefilledTransaction::SerializationOps<DataStream, PrefilledTransaction const, ActionSerialize>(PrefilledTransaction const&, DataStream&, ActionSerialize)
Line
Count
Source
80
11
    SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
void PrefilledTransaction::SerializationOps<SizeComputer, PrefilledTransaction const, ActionSerialize>(PrefilledTransaction const&, SizeComputer&, ActionSerialize)
Line
Count
Source
80
33.7k
    SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
void PrefilledTransaction::SerializationOps<VectorWriter, PrefilledTransaction const, ActionSerialize>(PrefilledTransaction const&, VectorWriter&, ActionSerialize)
Line
Count
Source
80
18.7k
    SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
81
};
82
83
typedef enum ReadStatus_t
84
{
85
    READ_STATUS_OK,
86
    READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
87
    READ_STATUS_FAILED, // Failed to process object
88
} ReadStatus;
89
90
class CBlockHeaderAndShortTxIDs {
91
    mutable std::optional<PresaltedSipHasher> m_hasher;
92
    uint64_t nonce;
93
94
    void FillShortTxIDSelector() const;
95
96
    friend class PartiallyDownloadedBlock;
97
98
protected:
99
    std::vector<uint64_t> shorttxids;
100
    std::vector<PrefilledTransaction> prefilledtxn;
101
102
public:
103
    static constexpr int SHORTTXIDS_LENGTH = 6;
104
105
    CBlockHeader header;
106
107
    /**
108
     * Dummy for deserialization
109
     */
110
20.1k
    CBlockHeaderAndShortTxIDs() = default;
111
112
    /**
113
     * @param[in]  nonce  This should be randomly generated, and is used for the siphash secret key
114
     */
115
    CBlockHeaderAndShortTxIDs(const CBlock& block, uint64_t nonce);
116
117
    uint64_t GetShortID(const Wtxid& wtxid) const;
118
119
84.0k
    size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
120
121
    SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
122
72.6k
    {
123
72.6k
        READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
124
72.6k
        if (ser_action.ForRead()) {
125
20.1k
            if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
126
0
                throw std::ios_base::failure("indexes overflowed 16 bits");
127
0
            }
128
20.1k
            obj.FillShortTxIDSelector();
129
20.1k
        }
130
72.6k
    }
void CBlockHeaderAndShortTxIDs::SerializationOps<DataStream, CBlockHeaderAndShortTxIDs const, ActionSerialize>(CBlockHeaderAndShortTxIDs const&, DataStream&, ActionSerialize)
Line
Count
Source
122
4
    {
123
4
        READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
124
4
        if (ser_action.ForRead()) {
125
0
            if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
126
0
                throw std::ios_base::failure("indexes overflowed 16 bits");
127
0
            }
128
0
            obj.FillShortTxIDSelector();
129
0
        }
130
4
    }
void CBlockHeaderAndShortTxIDs::SerializationOps<DataStream, CBlockHeaderAndShortTxIDs, ActionUnserialize>(CBlockHeaderAndShortTxIDs&, DataStream&, ActionUnserialize)
Line
Count
Source
122
20.1k
    {
123
20.1k
        READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
124
20.1k
        if (ser_action.ForRead()) {
125
20.1k
            if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
126
0
                throw std::ios_base::failure("indexes overflowed 16 bits");
127
0
            }
128
20.1k
            obj.FillShortTxIDSelector();
129
20.1k
        }
130
20.1k
    }
void CBlockHeaderAndShortTxIDs::SerializationOps<SizeComputer, CBlockHeaderAndShortTxIDs const, ActionSerialize>(CBlockHeaderAndShortTxIDs const&, SizeComputer&, ActionSerialize)
Line
Count
Source
122
33.7k
    {
123
33.7k
        READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
124
33.7k
        if (ser_action.ForRead()) {
125
0
            if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
126
0
                throw std::ios_base::failure("indexes overflowed 16 bits");
127
0
            }
128
0
            obj.FillShortTxIDSelector();
129
0
        }
130
33.7k
    }
void CBlockHeaderAndShortTxIDs::SerializationOps<VectorWriter, CBlockHeaderAndShortTxIDs const, ActionSerialize>(CBlockHeaderAndShortTxIDs const&, VectorWriter&, ActionSerialize)
Line
Count
Source
122
18.7k
    {
123
18.7k
        READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
124
18.7k
        if (ser_action.ForRead()) {
125
0
            if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
126
0
                throw std::ios_base::failure("indexes overflowed 16 bits");
127
0
            }
128
0
            obj.FillShortTxIDSelector();
129
0
        }
130
18.7k
    }
131
};
132
133
class PartiallyDownloadedBlock {
134
protected:
135
    std::vector<CTransactionRef> txn_available;
136
    size_t prefilled_count = 0, mempool_count = 0, extra_count = 0;
137
    const CTxMemPool* pool;
138
public:
139
    CBlockHeader header;
140
141
    // Can be overridden for testing
142
    using IsBlockMutatedFn = std::function<bool(const CBlock&, bool)>;
143
    IsBlockMutatedFn m_check_block_mutated_mock{nullptr};
144
145
16.8k
    explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
146
147
    // extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
148
    ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<Wtxid, CTransactionRef>>& extra_txn);
149
    bool IsTxAvailable(size_t index) const;
150
    // segwit_active enforces witness mutation checks just before reporting a healthy status
151
    ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing, bool segwit_active);
152
};
153
154
#endif // BITCOIN_BLOCKENCODINGS_H