Bitcoin ABC 0.32.6
P2P Digital Currency
blockencodings.h
Go to the documentation of this file.
1// Copyright (c) 2016 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 <primitives/block.h>
9#include <serialize.h>
10#include <shortidprocessor.h>
11
12#include <cstdint>
13#include <memory>
14#include <vector>
15
16class Config;
17class CTxMemPool;
18
19// Transaction compression schemes for compact block relay can be introduced by
20// writing an actual formatter here.
22
24public:
25 // A BlockTransactionsRequest message
27 std::vector<uint32_t> indices;
28
30 READWRITE(obj.blockhash,
32 }
33};
34
36public:
37 // A BlockTransactions message
39 std::vector<CTransactionRef> txn;
40
43 : blockhash(req.blockhash), txn(req.indices.size()) {}
44
46 READWRITE(obj.blockhash,
48 }
49};
50
51// Dumb serialization/storage-helper for CBlockHeaderAndShortTxIDs and
52// PartiallyDownloadedBlock
54 // Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs
55 uint32_t index;
57
58 template <typename Stream> void SerData(Stream &s) { s << tx; }
59 template <typename Stream> void UnserData(Stream &s) { s >> tx; }
60};
61
63 uint32_t getIndex(const PrefilledTransaction &pt) const { return pt.index; }
65 return pt.tx;
66 }
67};
68
69typedef enum ReadStatus_t {
71 // Invalid object, peer is sending bogus crap.
72 // FIXME: differenciate bogus crap from crap that do not fit our policy.
74 // Failed to process object.
76 // Used only by FillBlock to indicate a failure in CheckBlock.
79
81private:
82 mutable uint64_t shorttxidk0, shorttxidk1;
83 uint64_t nonce;
84
85 void FillShortTxIDSelector() const;
86
88
89protected:
90 std::vector<uint64_t> shorttxids;
91 std::vector<PrefilledTransaction> prefilledtxn;
92
93public:
94 static constexpr int SHORTTXIDS_LENGTH = 6;
95
97
102
107 explicit CBlockHeaderAndShortTxIDs(const CBlock &block,
108 const uint64_t nonce);
109
110 uint64_t GetShortID(const TxHash &txhash) const;
111
112 size_t BlockTxCount() const {
113 return shorttxids.size() + prefilledtxn.size();
114 }
115
117 READWRITE(
118 obj.header, obj.nonce,
120 obj.shorttxids),
122 obj.prefilledtxn));
123
124 if (ser_action.ForRead() && obj.prefilledtxn.size() > 0) {
125 // Thanks to the DifferenceFormatter, the index values in the
126 // deserialized prefilled txs are absolute and sorted, so the last
127 // vector item has the highest index value.
128 uint64_t highestPrefilledIndex = obj.prefilledtxn.back().index;
129
130 // Make sure the indexes do not overflow 32 bits.
131 if (highestPrefilledIndex + obj.shorttxids.size() >
132 std::numeric_limits<uint32_t>::max()) {
133 throw std::ios_base::failure("indexes overflowed 32 bits");
134 }
135
136 // Make sure the indexes are contiguous. E.g. if there is no shortid
137 // but 2 prefilled txs with absolute indexes 0 and 2, then the tx at
138 // index 1 cannot be recovered.
139 if (highestPrefilledIndex >= obj.BlockTxCount()) {
140 throw std::ios_base::failure("non contiguous indexes");
141 }
142
143 obj.FillShortTxIDSelector();
144 }
145 }
146};
147
151 const CTransactionRef &rhs) const {
152 return lhs->GetHash() == rhs->GetHash();
153 }
154 };
155
160
161 // FIXME This better fits a unique_ptr, but the unit tests needs a copy
162 // operator for this class. It can be trivially changed when the unit tests
163 // are refactored.
164 std::shared_ptr<TransactionShortIdProcessor> shortidProcessor;
165
166protected:
170
171public:
174 : pool(poolIn), config(&configIn) {}
175
176 // extra_txn is a list of extra orphan/conflicted/etc transactions to look
177 // at
179 const std::vector<CTransactionRef> &extra_txn);
180 bool IsTxAvailable(size_t index) const;
182 const std::vector<CTransactionRef> &vtx_missing);
183};
184
185#endif // BITCOIN_BLOCKENCODINGS_H
ReadStatus_t
@ READ_STATUS_OK
@ READ_STATUS_INVALID
@ READ_STATUS_CHECKBLOCK_FAILED
@ READ_STATUS_FAILED
enum ReadStatus_t ReadStatus
std::vector< CTransactionRef > txn
BlockTransactions(const BlockTransactionsRequest &req)
SERIALIZE_METHODS(BlockTransactions, obj)
std::vector< uint32_t > indices
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
uint64_t GetShortID(const TxHash &txhash) const
void FillShortTxIDSelector() const
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
CBlockHeaderAndShortTxIDs()
Dummy for deserialization.
std::vector< PrefilledTransaction > prefilledtxn
static constexpr int SHORTTXIDS_LENGTH
std::vector< uint64_t > shorttxids
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
Definition: block.h:60
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:221
Definition: config.h:19
const CTxMemPool * pool
ReadStatus InitData(const CBlockHeaderAndShortTxIDs &cmpctblock, const std::vector< CTransactionRef > &extra_txn)
bool IsTxAvailable(size_t index) const
ReadStatus FillBlock(CBlock &block, const std::vector< CTransactionRef > &vtx_missing)
PartiallyDownloadedBlock(const Config &configIn, CTxMemPool *poolIn)
std::shared_ptr< TransactionShortIdProcessor > shortidProcessor
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:630
#define READWRITE(...)
Definition: serialize.h:176
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:662
Default formatter.
Definition: serialize.h:964
bool operator()(const CTransactionRef &lhs, const CTransactionRef &rhs) const
CTransactionRef tx
void SerData(Stream &s)
void UnserData(Stream &s)
uint32_t getIndex(const PrefilledTransaction &pt) const
CTransactionRef getItem(const PrefilledTransaction &pt) const
A TxHash is the double sha256 hash of the full transaction data.
Definition: txid.h:22
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:774