From 4a7fd7a7124f84e010b01d0769ef0572bf031ee8 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Mon, 13 Jan 2020 15:08:52 +0000 Subject: [PATCH 1/2] tests: Add amount compression/decompression fuzzing to existing fuzzing harness: test compression round-trip --- src/test/fuzz/integer.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/test/fuzz/integer.cpp b/src/test/fuzz/integer.cpp index 723938bcdb7..b496ab53419 100644 --- a/src/test/fuzz/integer.cpp +++ b/src/test/fuzz/integer.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include @@ -56,7 +57,14 @@ void test_one_input(const std::vector& buffer) const Consensus::Params& consensus_params = Params().GetConsensus(); (void)CheckProofOfWork(u256, u32, consensus_params); - (void)CompressAmount(u64); + if (u64 <= MAX_MONEY) { + const uint64_t compressed_money_amount = CompressAmount(u64); + assert(u64 == DecompressAmount(compressed_money_amount)); + static const uint64_t compressed_money_amount_max = CompressAmount(MAX_MONEY - 1); + assert(compressed_money_amount <= compressed_money_amount_max); + } else { + (void)CompressAmount(u64); + } static const uint256 u256_min(uint256S("0000000000000000000000000000000000000000000000000000000000000000")); static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); const std::vector v256{u256, u256_min, u256_max}; From 7e9c7113afbed96cef80c327cc93e82000d6bb69 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Mon, 13 Jan 2020 15:09:36 +0000 Subject: [PATCH 2/2] compressor: Make the domain of CompressAmount(...) explicit --- src/compressor.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/compressor.h b/src/compressor.h index c1eda503c8d..acb34da4a06 100644 --- a/src/compressor.h +++ b/src/compressor.h @@ -19,7 +19,17 @@ bool CompressScript(const CScript& script, std::vector &out); unsigned int GetSpecialScriptSize(unsigned int nSize); bool DecompressScript(CScript& script, unsigned int nSize, const std::vector &out); +/** + * Compress amount. + * + * nAmount is of type uint64_t and thus cannot be negative. If you're passing in + * a CAmount (int64_t), make sure to properly handle the case where the amount + * is negative before calling CompressAmount(...). + * + * @pre Function defined only for 0 <= nAmount <= MAX_MONEY. + */ uint64_t CompressAmount(uint64_t nAmount); + uint64_t DecompressAmount(uint64_t nAmount); /** Compact serializer for scripts.