MCPcopy Create free account
hub / github.com/ElementsProject/elements / FUZZ_TARGET_INIT

Function FUZZ_TARGET_INIT

src/test/fuzz/pow.cpp:24–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22}
23
24FUZZ_TARGET_INIT(pow, initialize_pow)
25{
26 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
27 const Consensus::Params& consensus_params = Params().GetConsensus();
28 std::vector<CBlockIndex> blocks;
29 const uint32_t fixed_time = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
30 const uint32_t fixed_bits = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
31 LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 10000) {
32 const std::optional<CBlockHeader> block_header = ConsumeDeserializable<CBlockHeader>(fuzzed_data_provider);
33 if (!block_header) {
34 continue;
35 }
36 CBlockIndex current_block{*block_header};
37 {
38 CBlockIndex* previous_block = blocks.empty() ? nullptr : &PickValue(fuzzed_data_provider, blocks);
39 const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0;
40 if (fuzzed_data_provider.ConsumeBool()) {
41 current_block.pprev = previous_block;
42 }
43 if (fuzzed_data_provider.ConsumeBool()) {
44 current_block.nHeight = current_height;
45 }
46 if (fuzzed_data_provider.ConsumeBool()) {
47 const uint32_t seconds = current_height * consensus_params.nPowTargetSpacing;
48 if (!AdditionOverflow(fixed_time, seconds)) {
49 current_block.nTime = fixed_time + seconds;
50 }
51 }
52 if (fuzzed_data_provider.ConsumeBool()) {
53 current_block.nBits = fixed_bits;
54 }
55 if (fuzzed_data_provider.ConsumeBool()) {
56 current_block.nChainWork = previous_block != nullptr ? previous_block->nChainWork + GetBlockProof(*previous_block) : arith_uint256{0};
57 } else {
58 current_block.nChainWork = ConsumeArithUInt256(fuzzed_data_provider);
59 }
60 blocks.push_back(current_block);
61 }
62 {
63 (void)GetBlockProof(current_block);
64 (void)CalculateNextWorkRequired(&current_block, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, std::numeric_limits<int64_t>::max()), consensus_params);
65 if (current_block.nHeight != std::numeric_limits<int>::max() && current_block.nHeight - (consensus_params.DifficultyAdjustmentInterval() - 1) >= 0) {
66 (void)GetNextWorkRequired(&current_block, &(*block_header), consensus_params);
67 }
68 }
69 {
70 const CBlockIndex* to = &PickValue(fuzzed_data_provider, blocks);
71 const CBlockIndex* from = &PickValue(fuzzed_data_provider, blocks);
72 const CBlockIndex* tip = &PickValue(fuzzed_data_provider, blocks);
73 try {
74 (void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
75 } catch (const uint_error&) {
76 }
77 }
78 {
79 const std::optional<uint256> hash = ConsumeDeserializable<uint256>(fuzzed_data_provider);
80 if (hash) {
81 (void)CheckProofOfWork(*hash, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), consensus_params);

Callers

nothing calls this directly

Calls 15

AdditionOverflowFunction · 0.85
GetBlockProofFunction · 0.85
ConsumeArithUInt256Function · 0.85
GetNextWorkRequiredFunction · 0.85
CheckProofOfWorkFunction · 0.85
remaining_bytesMethod · 0.80
ConsumeBoolMethod · 0.80
ParamsClass · 0.50
dataMethod · 0.45

Tested by

no test coverage detected