10 #include <test/util/setup_common.h>
12 #include <boost/test/unit_test.hpp>
18 struct TestCompactProofs {
19 static std::vector<uint64_t> getShortProofIds(
const CompactProofs &cp) {
23 static std::vector<PrefilledProof>
28 static void addPrefilledProof(
CompactProofs &cp, uint32_t index,
64 TestCompactProofs::addPrefilledProof(
66 TestCompactProofs::addPrefilledProof(
67 cp, std::numeric_limits<uint32_t>::max(),
73 auto prefilledProofs = TestCompactProofs::getPrefilledProofs(cp);
78 std::numeric_limits<uint32_t>::max());
81 auto checkCompactProof = [&](
size_t numofProof,
82 size_t numofPrefilledProof) {
84 for (
size_t i = 0; i < numofProof; i++) {
92 uint32_t prefilledProofIndex = 0;
93 for (
size_t i = 0; i < numofPrefilledProof; i++) {
94 TestCompactProofs::addPrefilledProof(
95 cpw, prefilledProofIndex++,
98 GetRand(std::numeric_limits<uint32_t>::max())));
100 auto prefilledProofs = TestCompactProofs::getPrefilledProofs(cpw);
115 return lhs.
index == rhs.index &&
120 auto prefilledProofsCpr = TestCompactProofs::getPrefilledProofs(cpr);
122 prefilledProofsCpr.end(),
123 prefilledProofs.begin(), comparePrefilledProof));
125 auto shortIds = TestCompactProofs::getShortProofIds(cpr);
128 const ProofId &proofid = pLeaf->getId();
138 checkCompactProof(0, 0);
141 checkCompactProof(1000, 0);
144 checkCompactProof(0, 1000);
147 checkCompactProof(1000, 1000);
155 TestCompactProofs::addPrefilledProof(
157 TestCompactProofs::addPrefilledProof(
161 BOOST_CHECK_EXCEPTION(ss << cp, std::ios_base::failure,
162 HasReason(
"differential value overflow"));
168 TestCompactProofs::addPrefilledProof(
170 TestCompactProofs::addPrefilledProof(
174 BOOST_CHECK_EXCEPTION(ss << cp, std::ios_base::failure,
175 HasReason(
"differential value overflow"));
181 TestCompactProofs::addPrefilledProof(
182 cp, std::numeric_limits<uint32_t>::max(),
184 TestCompactProofs::addPrefilledProof(
188 BOOST_CHECK_EXCEPTION(ss << cp, std::ios_base::failure,
189 HasReason(
"differential value overflow"));
195 ss << uint64_t(0) << uint64_t(0);
200 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
201 HasReason(
"ReadCompactSize(): size too large"));
207 ss << uint64_t(0) << uint64_t(0);
214 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
215 HasReason(
"ReadCompactSize(): size too large"));
221 ss << uint64_t(0) << uint64_t(0);
232 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
233 HasReason(
"ReadCompactSize(): size too large"));
237 const uint64_t overflow =
238 uint64_t(std::numeric_limits<uint32_t>::max()) + 1;
241 BOOST_CHECK_GE(overflow,
MAX_SIZE + 1);
242 const uint64_t overflowIter = overflow / (
MAX_SIZE + 1);
245 BOOST_CHECK_LE(overflowIter, std::numeric_limits<uint32_t>::max());
246 BOOST_CHECK_LE(overflowIter,
MAX_SIZE);
247 uint32_t remainder = uint32_t(overflow - ((
MAX_SIZE + 1) * overflowIter));
252 ss << uint64_t(0) << uint64_t(0);
257 for (uint32_t i = 0; i < overflowIter; i++) {
268 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
269 HasReason(
"differential value overflow"));
275 ss << uint64_t(0) << uint64_t(0);
282 for (uint32_t i = 0; i < overflowIter; i++) {
295 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
296 HasReason(
"indexes overflowed 32 bits"));
302 ss << uint64_t(0) << uint64_t(0);
318 BOOST_CHECK_EXCEPTION(ss >> cp, std::ios_base::failure,
319 HasReason(
"non contiguous indexes"));
#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 ProofId & getId() const
const SchnorrSig & getSignature() 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_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK_NO_THROW(stmt)
#define BOOST_CHECK(expr)
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
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