20#include <validation.h>
23constexpr uint32_t FUZZ_MAX_HEADERS_RESULTS{16};
27 std::vector<CNode*> m_connections;
30 HeadersSyncSetup(
const ChainType chain_type, TestOpts opts) : TestingSetup(chain_type, opts)
32 PeerManager::Options peerman_opts;
45 m_node.banman.get(), *m_node.chainman,
46 *m_node.mempool, *m_node.warnings, peerman_opts);
48 CConnman::Options options;
50 m_node.connman->Init(options);
54 void SendMessage(FuzzedDataProvider& fuzzed_data_provider, CSerializedNetMsg&& msg)
58void HeadersSyncSetup::ResetAndInitialize()
60 m_connections.clear();
61 auto& connman =
static_cast<ConnmanTestMsg&
>(*m_node.connman);
65 std::vector<ConnectionType> conn_types = {
71 for (
auto conn_type : conn_types) {
73 m_connections.push_back(
new CNode(
id++,
nullptr, addr, 0, 0, addr,
"", conn_type,
false, 0));
74 CNode& p2p_node = *m_connections.back();
84 connman.AddTestNode(p2p_node);
88void HeadersSyncSetup::SendMessage(FuzzedDataProvider& fuzzed_data_provider, CSerializedNetMsg&& msg)
90 auto& connman =
static_cast<ConnmanTestMsg&
>(*m_node.connman);
91 CNode& connection = *
PickValue(fuzzed_data_provider, m_connections);
93 connman.FlushSendBuffer(connection);
94 (void)connman.ReceiveMsgFrom(connection, std::move(msg));
97 connman.ProcessMessagesOnce(connection);
98 }
catch (
const std::ios_base::failure&) {
100 m_node.peerman->SendMessages(connection);
103CBlockHeader ConsumeHeader(FuzzedDataProvider& fuzzed_data_provider,
const uint256& prev_hash, uint32_t prev_nbits)
117 header.
nBits = prev_nbits;
119 arith_uint256 lower_target =
UintToArith256(uint256{
"0000000000000000000342190000000000000000000000000000000000000000"});
120 arith_uint256 upper_target =
UintToArith256(uint256{
"00000000ffff0000000000000000000000000000000000000000000000000000"});
122 header.
nBits = target.GetCompact();
130CBlock ConsumeBlock(FuzzedDataProvider& fuzzed_data_provider,
const uint256& prev_hash, uint32_t prev_nbits)
132 auto header = ConsumeHeader(fuzzed_data_provider, prev_hash, prev_nbits);
135 CBlock block{header};
136 CMutableTransaction tx;
139 tx.
vout[0].nValue = 0;
140 tx.
vin[0].scriptSig.resize(2);
146void FinalizeHeader(CBlockHeader& header,
const ChainstateManager& chainman)
155HeadersSyncSetup* g_testing_setup;
162 .setup_validation_interface =
false,
165 g_testing_setup =
setup.get();
183 g_testing_setup->ResetAndInitialize();
189 std::vector<CBlockHeader> all_headers;
193 auto finalized_block = [&]() {
195 FinalizeHeader(block, chainman);
201 fuzzed_data_provider,
204 std::vector<CBlock> headers;
205 headers.resize(FUZZ_MAX_HEADERS_RESULTS);
206 for (
CBlock& header : headers) {
207 header = ConsumeHeader(fuzzed_data_provider, base.
GetHash(), base.
nBits);
208 FinalizeHeader(header, chainman);
212 all_headers.insert(all_headers.end(), headers.begin(), headers.end());
215 g_testing_setup->SendMessage(fuzzed_data_provider, std::move(headers_msg));
219 auto block = finalized_block();
222 all_headers.push_back(block);
225 g_testing_setup->SendMessage(fuzzed_data_provider, std::move(headers_msg));
229 auto block = finalized_block();
231 all_headers.push_back(block);
234 g_testing_setup->SendMessage(fuzzed_data_provider, std::move(headers_msg));
arith_uint256 UintToArith256(const uint256 &a)
std::vector< CTransactionRef > vtx
const CBlock & GenesisBlock() const
const Consensus::Params & GetConsensus() const
std::atomic_bool fPauseSend
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
const CChainParams & GetParams() const
const arith_uint256 & MinimumChainWork() const
node::BlockManager m_blockman
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
Helper to initialize the global MockableSteadyClock, let a duration elapse, and reset it after use in...
256-bit unsigned big integer.
@ BLOCK_RELAY
We use block-relay-only connections to help prevent against partition attacks.
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
@ INBOUND
Inbound connections are those initiated by a peer.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
CSerializedNetMsg Make(std::string msg_type, Args &&... args)
constexpr const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
constexpr const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids".
constexpr const char * BLOCK
The block message transmits a single serialized block.
util::Result< void > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
static constexpr TransactionSerParams TX_WITH_WITNESS
static CTransactionRef MakeTransactionRef(Tx &&txIn)
ServiceFlags
nServices flags
static const int PROTOCOL_VERSION
network protocol versioning
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
NetEventsInterface * m_msgproc
std::vector< CTxOut > vout
uint32_t max_headers_result
bool ignore_incoming_txs
Whether this node is running in -blocksonly mode.
Testing setup that configures a complete environment.
#define WITH_LOCK(cs, code)
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
arith_uint256 ConsumeArithUInt256InRange(FuzzedDataProvider &fuzzed_data_provider, const arith_uint256 &min, const arith_uint256 &max) noexcept
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
@ ZEROS
Seed with a compile time constant of zeros.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define NO_THREAD_SAFETY_ANALYSIS
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
constexpr auto TicksSinceEpoch(Timepoint t)
arith_uint256 CalculateClaimedHeadersWork(std::span< const CBlockHeader > headers)
Return the sum of the claimed work on a given set of headers.