-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEntitiesProviders.hpp
88 lines (67 loc) · 2.1 KB
/
EntitiesProviders.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2019-2022 Xenios SEZC
// https://www.veriblock.org
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
#ifndef VERIBLOCK_POP_CPP_ENTITIESPROVIDERS_HPP
#define VERIBLOCK_POP_CPP_ENTITIESPROVIDERS_HPP
#include <veriblock/pop/blockchain/block_index.hpp>
#include "FuzzedDataProvider.hpp"
namespace fuzz {
using namespace altintegration;
template <size_t N>
bool ConsumeArray(FuzzedDataProvider& p, Blob<N>& b) {
if (p.remaining_bytes() < N) {
return false;
}
auto bytes = p.ConsumeBytes<uint8_t>(N);
b = bytes;
return true;
}
template <size_t N = 32>
bool ConsumeArray(FuzzedDataProvider& p, std::vector<uint8_t>& out) {
if (p.remaining_bytes() < N) {
return false;
}
out = p.ConsumeBytes<uint8_t>(N);
return true;
}
template <typename E>
bool ConsumeEndorsement(FuzzedDataProvider& p, E& e) {
// clang-format off
return ConsumeArray(p, e.id) &&
ConsumeArray(p, e.blockOfProof) &&
ConsumeArray(p, e.containingHash) &&
ConsumeArray(p, e.endorsedHash);
// clang-format on
}
inline bool ConsumeAddon(FuzzedDataProvider& p, BtcBlockAddon& addon) {
if (!ConsumeArray(p, addon.chainWork)) {
return false;
}
return true;
}
inline bool ConsumeAddon(FuzzedDataProvider& p, VbkBlockAddon& addon) {
if (!ConsumeArray(p, addon.chainWork)) {
return false;
}
const auto size = p.ConsumeIntegral<uint32_t>();
for (auto i = 0u; i < size; i++) {
VbkEndorsement e;
if (!ConsumeEndorsement(p, e)) {
return false;
}
addon.insertContainingEndorsement(std::make_shared<VbkEndorsement>(e));
}
return true;
}
template <typename Block>
bool ConsumeBlockIndex(FuzzedDataProvider& p, BlockIndex<Block>& index) {
index.setHeight(p.template ConsumeIntegral<int32_t>());
index.setHeader(Block{});
index.setStatus(p.template ConsumeIntegral<uint32_t>());
using addon_t = typename BlockIndex<Block>::addon_t;
addon_t& addon = index;
return ConsumeAddon(p, addon);
}
} // namespace fuzz
#endif // VERIBLOCK_POP_CPP_ENTITIESPROVIDERS_HPP