11#include <test/util/setup_common.h>
13#include <boost/test/unit_test.hpp>
19 struct TestCompactProofs {
20 static std::vector<uint64_t> getShortProofIds(
const CompactProofs &cp) {
24 static std::vector<PrefilledProof>
29 static void addPrefilledProof(
CompactProofs &cp, uint32_t index,
41BOOST_FIXTURE_TEST_SUITE(compactproofs_tests, TestingSetup)
65 TestCompactProofs::addPrefilledProof(
67 TestCompactProofs::addPrefilledProof(
68 cp, std::numeric_limits<uint32_t>::max(),
74 auto prefilledProofs = TestCompactProofs::getPrefilledProofs(cp);
79 std::numeric_limits<uint32_t>::max());
82 auto checkCompactProof = [&](
size_t numofProof,
83 size_t numofPrefilledProof) {
85 for (
size_t i = 0; i < numofProof; i++) {
93 uint32_t prefilledProofIndex = 0;
94 for (
size_t i = 0; i < numofPrefilledProof; i++) {
95 TestCompactProofs::addPrefilledProof(
96 cpw, prefilledProofIndex++,
99 auto prefilledProofs = TestCompactProofs::getPrefilledProofs(cpw);
114 return lhs.
index == rhs.index &&
119 auto prefilledProofsCpr = TestCompactProofs::getPrefilledProofs(cpr);
121 prefilledProofsCpr.end(),
122 prefilledProofs.begin(), comparePrefilledProof));
124 auto shortIds = TestCompactProofs::getShortProofIds(cpr);
127 const ProofId &proofid = pLeaf->getId();
137 checkCompactProof(0, 0);
140 checkCompactProof(1000, 0);
143 checkCompactProof(0, 1000);
146 checkCompactProof(1000, 1000);
154 TestCompactProofs::addPrefilledProof(
156 TestCompactProofs::addPrefilledProof(
160 BOOST_CHECK_EXCEPTION(ss << cp, std::ios_base::failure,
161 HasReason(
"differential value overflow"));
167 TestCompactProofs::addPrefilledProof(
169 TestCompactProofs::addPrefilledProof(
173 BOOST_CHECK_EXCEPTION(ss << cp, std::ios_base::failure,
174 HasReason(
"differential value overflow"));
180 TestCompactProofs::addPrefilledProof(
181 cp, std::numeric_limits<uint32_t>::max(),
183 TestCompactProofs::addPrefilledProof(
187 BOOST_CHECK_EXCEPTION(ss << cp, std::ios_base::failure,
188 HasReason(
"differential value overflow"));
194 ss << uint64_t(0) << uint64_t(0);
199 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
200 HasReason(
"ReadCompactSize(): size too large"));
206 ss << uint64_t(0) << uint64_t(0);
213 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
214 HasReason(
"ReadCompactSize(): size too large"));
220 ss << uint64_t(0) << uint64_t(0);
231 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
232 HasReason(
"ReadCompactSize(): size too large"));
236 const uint64_t overflow =
237 uint64_t(std::numeric_limits<uint32_t>::max()) + 1;
240 BOOST_CHECK_GE(overflow,
MAX_SIZE + 1);
241 const uint64_t overflowIter = overflow / (
MAX_SIZE + 1);
244 BOOST_CHECK_LE(overflowIter, std::numeric_limits<uint32_t>::max());
245 BOOST_CHECK_LE(overflowIter,
MAX_SIZE);
246 uint32_t remainder = uint32_t(overflow - ((
MAX_SIZE + 1) * overflowIter));
251 ss << uint64_t(0) << uint64_t(0);
256 for (uint32_t i = 0; i < overflowIter; i++) {
267 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
268 HasReason(
"differential value overflow"));
274 ss << uint64_t(0) << uint64_t(0);
281 for (uint32_t i = 0; i < overflowIter; i++) {
294 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
295 HasReason(
"indexes overflowed 32 bits"));
301 ss << uint64_t(0) << uint64_t(0);
317 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
318 HasReason(
"non contiguous indexes"));
322BOOST_AUTO_TEST_SUITE_END()
#define Assert(val)
Identity function.
Double ended buffer combining vector and stream-like interfaces.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
std::vector< PrefilledProof > prefilledProofs
uint64_t getShortID(const ProofId &proofid) const
std::pair< uint64_t, uint64_t > getKeys() const
std::vector< uint64_t > shortproofids
const SchnorrSig & getSignature() const
const ProofId & getId() const
BOOST_AUTO_TEST_CASE(compactproofs_roundtrip)
ProofRef buildRandomProof(Chainstate &active_chainstate, uint32_t score, int height, const CKey &masterKey)
constexpr uint32_t MIN_VALID_PROOF_SCORE
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK_NO_THROW(stmt)
#define BOOST_CHECK(expr)
static constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
bool forEachLeaf(Callable &&func) const
bool insert(const RCUPtr< T > &value)
Insert a value into the tree.
avalanche::ProofRef proof
static const int PROTOCOL_VERSION
network protocol versioning