mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 12:02:48 +01:00
merge bitcoin#21966: Remove double serialization; use software encoder for fee estimation
continuation of f946c68f8
from dash#4197
This commit is contained in:
parent
1d6aafea47
commit
d0b4e560a6
@ -151,6 +151,7 @@ BITCOIN_TESTS =\
|
||||
test/script_tests.cpp \
|
||||
test/script_standard_tests.cpp \
|
||||
test/scriptnum_tests.cpp \
|
||||
test/serfloat_tests.cpp \
|
||||
test/serialize_tests.cpp \
|
||||
test/settings_tests.cpp \
|
||||
test/sighash_tests.cpp \
|
||||
|
@ -36,11 +36,6 @@ static_assert(std::numeric_limits<double>::is_iec559, "IEEE 754 double assumed")
|
||||
// Example(s): Everywhere :-)
|
||||
static_assert(std::numeric_limits<unsigned char>::digits == 8, "8-bit byte assumed");
|
||||
|
||||
// Assumption: We assume floating-point widths.
|
||||
// Example(s): Type punning in serialization code (ser_{float,double}_to_uint{32,64}).
|
||||
static_assert(sizeof(float) == 4, "32-bit float assumed");
|
||||
static_assert(sizeof(double) == 8, "64-bit double assumed");
|
||||
|
||||
// Assumption: We assume integer widths.
|
||||
// Example(s): GetSizeOfCompactSize and WriteCompactSize in the serialization
|
||||
// code.
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include <logging.h>
|
||||
#include <streams.h>
|
||||
#include <txmempool.h>
|
||||
#include <util/serfloat.h>
|
||||
#include <util/system.h>
|
||||
|
||||
static const char* FEE_ESTIMATES_FILENAME = "fee_estimates.dat";
|
||||
@ -25,6 +26,26 @@ std::string StringForFeeEstimateHorizon(FeeEstimateHorizon horizon)
|
||||
} // no default case, so the compiler can warn about missing cases
|
||||
assert(false);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
struct EncodedDoubleFormatter
|
||||
{
|
||||
template<typename Stream> void Ser(Stream &s, double v)
|
||||
{
|
||||
s << EncodeDouble(v);
|
||||
}
|
||||
|
||||
template<typename Stream> void Unser(Stream& s, double& v)
|
||||
{
|
||||
uint64_t encoded;
|
||||
s >> encoded;
|
||||
v = DecodeDouble(encoded);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
/**
|
||||
* We will instantiate an instance of this class to track transactions that were
|
||||
* included in a block. We will lump transactions into a bucket according to their
|
||||
@ -355,12 +376,12 @@ double TxConfirmStats::EstimateMedianVal(int confTarget, double sufficientTxVal,
|
||||
|
||||
void TxConfirmStats::Write(CAutoFile& fileout) const
|
||||
{
|
||||
fileout << decay;
|
||||
fileout << Using<EncodedDoubleFormatter>(decay);
|
||||
fileout << scale;
|
||||
fileout << m_feerate_avg;
|
||||
fileout << txCtAvg;
|
||||
fileout << confAvg;
|
||||
fileout << failAvg;
|
||||
fileout << Using<VectorFormatter<EncodedDoubleFormatter>>(m_feerate_avg);
|
||||
fileout << Using<VectorFormatter<EncodedDoubleFormatter>>(txCtAvg);
|
||||
fileout << Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(confAvg);
|
||||
fileout << Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(failAvg);
|
||||
}
|
||||
|
||||
void TxConfirmStats::Read(CAutoFile& filein, int nFileVersion, size_t numBuckets)
|
||||
@ -371,7 +392,7 @@ void TxConfirmStats::Read(CAutoFile& filein, int nFileVersion, size_t numBuckets
|
||||
size_t maxConfirms, maxPeriods;
|
||||
|
||||
// The current version will store the decay with each individual TxConfirmStats and also keep a scale factor
|
||||
filein >> decay;
|
||||
filein >> Using<EncodedDoubleFormatter>(decay);
|
||||
if (decay <= 0 || decay >= 1) {
|
||||
throw std::runtime_error("Corrupt estimates file. Decay must be between 0 and 1 (non-inclusive)");
|
||||
}
|
||||
@ -380,15 +401,15 @@ void TxConfirmStats::Read(CAutoFile& filein, int nFileVersion, size_t numBuckets
|
||||
throw std::runtime_error("Corrupt estimates file. Scale must be non-zero");
|
||||
}
|
||||
|
||||
filein >> m_feerate_avg;
|
||||
filein >> Using<VectorFormatter<EncodedDoubleFormatter>>(m_feerate_avg);
|
||||
if (m_feerate_avg.size() != numBuckets) {
|
||||
throw std::runtime_error("Corrupt estimates file. Mismatch in feerate average bucket count");
|
||||
}
|
||||
filein >> txCtAvg;
|
||||
filein >> Using<VectorFormatter<EncodedDoubleFormatter>>(txCtAvg);
|
||||
if (txCtAvg.size() != numBuckets) {
|
||||
throw std::runtime_error("Corrupt estimates file. Mismatch in tx count bucket count");
|
||||
}
|
||||
filein >> confAvg;
|
||||
filein >> Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(confAvg);
|
||||
maxPeriods = confAvg.size();
|
||||
maxConfirms = scale * maxPeriods;
|
||||
|
||||
@ -401,7 +422,7 @@ void TxConfirmStats::Read(CAutoFile& filein, int nFileVersion, size_t numBuckets
|
||||
}
|
||||
}
|
||||
|
||||
filein >> failAvg;
|
||||
filein >> Using<VectorFormatter<VectorFormatter<EncodedDoubleFormatter>>>(failAvg);
|
||||
if (maxPeriods != failAvg.size()) {
|
||||
throw std::runtime_error("Corrupt estimates file. Mismatch in confirms tracked for failures");
|
||||
}
|
||||
@ -889,7 +910,7 @@ bool CBlockPolicyEstimator::Write(CAutoFile& fileout) const
|
||||
else {
|
||||
fileout << historicalFirst << historicalBest;
|
||||
}
|
||||
fileout << buckets;
|
||||
fileout << Using<VectorFormatter<EncodedDoubleFormatter>>(buckets);
|
||||
feeStats->Write(fileout);
|
||||
shortStats->Write(fileout);
|
||||
longStats->Write(fileout);
|
||||
@ -925,7 +946,7 @@ bool CBlockPolicyEstimator::Read(CAutoFile& filein)
|
||||
throw std::runtime_error("Corrupt estimates file. Historical block range for estimates is invalid");
|
||||
}
|
||||
std::vector<double> fileBuckets;
|
||||
filein >> fileBuckets;
|
||||
filein >> Using<VectorFormatter<EncodedDoubleFormatter>>(fileBuckets);
|
||||
size_t numBuckets = fileBuckets.size();
|
||||
if (numBuckets <= 1 || numBuckets > 1000) {
|
||||
throw std::runtime_error("Corrupt estimates file. Must have between 2 and 1000 feerate buckets");
|
||||
|
@ -126,34 +126,6 @@ template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
|
||||
s.read((char*)&obj, 8);
|
||||
return le64toh(obj);
|
||||
}
|
||||
inline uint64_t ser_double_to_uint64(double x)
|
||||
{
|
||||
uint64_t tmp;
|
||||
std::memcpy(&tmp, &x, sizeof(x));
|
||||
static_assert(sizeof(tmp) == sizeof(x), "double and uint64_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
inline uint32_t ser_float_to_uint32(float x)
|
||||
{
|
||||
uint32_t tmp;
|
||||
std::memcpy(&tmp, &x, sizeof(x));
|
||||
static_assert(sizeof(tmp) == sizeof(x), "float and uint32_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
inline double ser_uint64_to_double(uint64_t y)
|
||||
{
|
||||
double tmp;
|
||||
std::memcpy(&tmp, &y, sizeof(y));
|
||||
static_assert(sizeof(tmp) == sizeof(y), "double and uint64_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
inline float ser_uint32_to_float(uint32_t y)
|
||||
{
|
||||
float tmp;
|
||||
std::memcpy(&tmp, &y, sizeof(y));
|
||||
static_assert(sizeof(tmp) == sizeof(y), "float and uint32_t assumed to have the same size");
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
@ -238,8 +210,6 @@ template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_wri
|
||||
template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
|
||||
template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
|
||||
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
|
||||
template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); }
|
||||
template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
|
||||
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); }
|
||||
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); }
|
||||
template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
|
||||
@ -256,8 +226,6 @@ template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a =
|
||||
template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
|
||||
template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
|
||||
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
|
||||
template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); }
|
||||
template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
|
||||
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); }
|
||||
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }
|
||||
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); }
|
||||
|
@ -3,14 +3,14 @@
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <memusage.h>
|
||||
#include <serialize.h>
|
||||
#include <streams.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <util/serfloat.h>
|
||||
#include <version.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
FUZZ_TARGET(float)
|
||||
{
|
||||
@ -19,24 +19,17 @@ FUZZ_TARGET(float)
|
||||
{
|
||||
const double d = fuzzed_data_provider.ConsumeFloatingPoint<double>();
|
||||
(void)memusage::DynamicUsage(d);
|
||||
assert(ser_uint64_to_double(ser_double_to_uint64(d)) == d);
|
||||
|
||||
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
|
||||
stream << d;
|
||||
double d_deserialized;
|
||||
stream >> d_deserialized;
|
||||
assert(d == d_deserialized);
|
||||
}
|
||||
|
||||
{
|
||||
const float f = fuzzed_data_provider.ConsumeFloatingPoint<float>();
|
||||
(void)memusage::DynamicUsage(f);
|
||||
assert(ser_uint32_to_float(ser_float_to_uint32(f)) == f);
|
||||
|
||||
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
|
||||
stream << f;
|
||||
float f_deserialized;
|
||||
stream >> f_deserialized;
|
||||
assert(f == f_deserialized);
|
||||
uint64_t encoded = EncodeDouble(d);
|
||||
if constexpr (std::numeric_limits<double>::is_iec559) {
|
||||
if (!std::isnan(d)) {
|
||||
uint64_t encoded_in_memory;
|
||||
std::copy((const unsigned char*)&d, (const unsigned char*)(&d + 1), (unsigned char*)&encoded_in_memory);
|
||||
assert(encoded_in_memory == encoded);
|
||||
}
|
||||
}
|
||||
double d_deserialized = DecodeDouble(encoded);
|
||||
assert(std::isnan(d) == std::isnan(d_deserialized));
|
||||
assert(std::isnan(d) || d == d_deserialized);
|
||||
}
|
||||
}
|
||||
|
@ -117,10 +117,6 @@ FUZZ_TARGET_INIT(integer, initialize_integer)
|
||||
assert(dynamic_usage == incremental_dynamic_usage * i64s.size());
|
||||
}
|
||||
(void)MillisToTimeval(i64);
|
||||
const double d = ser_uint64_to_double(u64);
|
||||
assert(ser_double_to_uint64(d) == u64);
|
||||
const float f = ser_uint32_to_float(u32);
|
||||
assert(ser_float_to_uint32(f) == u32);
|
||||
(void)SighashToStr(uch);
|
||||
(void)SipHashUint256(u64, u64, u256);
|
||||
(void)SipHashUint256Extra(u64, u64, u256, u32);
|
||||
|
@ -507,8 +507,6 @@ void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noe
|
||||
WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),
|
||||
WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),
|
||||
WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),
|
||||
WRITE_TO_STREAM_CASE(float, fuzzed_data_provider.ConsumeFloatingPoint<float>()),
|
||||
WRITE_TO_STREAM_CASE(double, fuzzed_data_provider.ConsumeFloatingPoint<double>()),
|
||||
WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),
|
||||
WRITE_TO_STREAM_CASE(std::vector<char>, ConsumeRandomLengthIntegralVector<char>(fuzzed_data_provider)));
|
||||
} catch (const std::ios_base::failure&) {
|
||||
@ -539,8 +537,6 @@ void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) no
|
||||
READ_FROM_STREAM_CASE(uint32_t),
|
||||
READ_FROM_STREAM_CASE(int64_t),
|
||||
READ_FROM_STREAM_CASE(uint64_t),
|
||||
READ_FROM_STREAM_CASE(float),
|
||||
READ_FROM_STREAM_CASE(double),
|
||||
READ_FROM_STREAM_CASE(std::string),
|
||||
READ_FROM_STREAM_CASE(std::vector<char>));
|
||||
} catch (const std::ios_base::failure&) {
|
||||
|
129
src/test/serfloat_tests.cpp
Normal file
129
src/test/serfloat_tests.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
// Copyright (c) 2014-2020 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <hash.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <util/serfloat.h>
|
||||
#include <serialize.h>
|
||||
#include <streams.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(serfloat_tests, BasicTestingSetup)
|
||||
|
||||
namespace {
|
||||
|
||||
uint64_t TestDouble(double f) {
|
||||
uint64_t i = EncodeDouble(f);
|
||||
double f2 = DecodeDouble(i);
|
||||
if (std::isnan(f)) {
|
||||
// NaN is not guaranteed to round-trip exactly.
|
||||
BOOST_CHECK(std::isnan(f2));
|
||||
} else {
|
||||
// Everything else is.
|
||||
BOOST_CHECK(!std::isnan(f2));
|
||||
uint64_t i2 = EncodeDouble(f2);
|
||||
BOOST_CHECK_EQUAL(f, f2);
|
||||
BOOST_CHECK_EQUAL(i, i2);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
|
||||
BOOST_CHECK_EQUAL(TestDouble(0.0), 0);
|
||||
BOOST_CHECK_EQUAL(TestDouble(-0.0), 0x8000000000000000);
|
||||
BOOST_CHECK_EQUAL(TestDouble(std::numeric_limits<double>::infinity()), 0x7ff0000000000000);
|
||||
BOOST_CHECK_EQUAL(TestDouble(-std::numeric_limits<double>::infinity()), 0xfff0000000000000);
|
||||
BOOST_CHECK_EQUAL(TestDouble(0.5), 0x3fe0000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(TestDouble(1.0), 0x3ff0000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(TestDouble(2.0), 0x4000000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(TestDouble(4.0), 0x4010000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(TestDouble(785.066650390625), 0x4088888880000000ULL);
|
||||
|
||||
// Roundtrip test on IEC559-compatible systems
|
||||
if (std::numeric_limits<double>::is_iec559) {
|
||||
BOOST_CHECK_EQUAL(sizeof(double), 8);
|
||||
BOOST_CHECK_EQUAL(sizeof(uint64_t), 8);
|
||||
// Test extreme values
|
||||
TestDouble(std::numeric_limits<double>::min());
|
||||
TestDouble(-std::numeric_limits<double>::min());
|
||||
TestDouble(std::numeric_limits<double>::max());
|
||||
TestDouble(-std::numeric_limits<double>::max());
|
||||
TestDouble(std::numeric_limits<double>::lowest());
|
||||
TestDouble(-std::numeric_limits<double>::lowest());
|
||||
TestDouble(std::numeric_limits<double>::quiet_NaN());
|
||||
TestDouble(-std::numeric_limits<double>::quiet_NaN());
|
||||
TestDouble(std::numeric_limits<double>::signaling_NaN());
|
||||
TestDouble(-std::numeric_limits<double>::signaling_NaN());
|
||||
TestDouble(std::numeric_limits<double>::denorm_min());
|
||||
TestDouble(-std::numeric_limits<double>::denorm_min());
|
||||
// Test exact encoding: on currently supported platforms, EncodeDouble
|
||||
// should produce exactly the same as the in-memory representation for non-NaN.
|
||||
for (int j = 0; j < 1000; ++j) {
|
||||
// Iterate over 9 specific bits exhaustively; the others are chosen randomly.
|
||||
// These specific bits are the sign bit, and the 2 top and bottom bits of
|
||||
// exponent and mantissa in the IEEE754 binary64 format.
|
||||
for (int x = 0; x < 512; ++x) {
|
||||
uint64_t v = InsecureRandBits(64);
|
||||
v &= ~(uint64_t{1} << 0);
|
||||
if (x & 1) v |= (uint64_t{1} << 0);
|
||||
v &= ~(uint64_t{1} << 1);
|
||||
if (x & 2) v |= (uint64_t{1} << 1);
|
||||
v &= ~(uint64_t{1} << 50);
|
||||
if (x & 4) v |= (uint64_t{1} << 50);
|
||||
v &= ~(uint64_t{1} << 51);
|
||||
if (x & 8) v |= (uint64_t{1} << 51);
|
||||
v &= ~(uint64_t{1} << 52);
|
||||
if (x & 16) v |= (uint64_t{1} << 52);
|
||||
v &= ~(uint64_t{1} << 53);
|
||||
if (x & 32) v |= (uint64_t{1} << 53);
|
||||
v &= ~(uint64_t{1} << 61);
|
||||
if (x & 64) v |= (uint64_t{1} << 61);
|
||||
v &= ~(uint64_t{1} << 62);
|
||||
if (x & 128) v |= (uint64_t{1} << 62);
|
||||
v &= ~(uint64_t{1} << 63);
|
||||
if (x & 256) v |= (uint64_t{1} << 63);
|
||||
double f;
|
||||
memcpy(&f, &v, 8);
|
||||
uint64_t v2 = TestDouble(f);
|
||||
if (!std::isnan(f)) BOOST_CHECK_EQUAL(v, v2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Python code to generate the below hashes:
|
||||
|
||||
def reversed_hex(x):
|
||||
return binascii.hexlify(''.join(reversed(x)))
|
||||
def dsha256(x):
|
||||
return hashlib.sha256(hashlib.sha256(x).digest()).digest()
|
||||
|
||||
reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(doubles)
|
||||
{
|
||||
CDataStream ss(SER_DISK, 0);
|
||||
// encode
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ss << EncodeDouble(i);
|
||||
}
|
||||
BOOST_CHECK(Hash(ss) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96"));
|
||||
|
||||
// decode
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
uint64_t val;
|
||||
ss >> val;
|
||||
double j = DecodeDouble(val);
|
||||
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
@ -70,8 +70,6 @@ BOOST_AUTO_TEST_CASE(sizes)
|
||||
BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
|
||||
BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
|
||||
BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
|
||||
BOOST_CHECK_EQUAL(sizeof(float), GetSerializeSize(float(0), 0));
|
||||
BOOST_CHECK_EQUAL(sizeof(double), GetSerializeSize(double(0), 0));
|
||||
// Bool is serialized as char
|
||||
BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(bool(0), 0));
|
||||
|
||||
@ -85,93 +83,9 @@ BOOST_AUTO_TEST_CASE(sizes)
|
||||
BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0), 0), 4U);
|
||||
BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U);
|
||||
BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8U);
|
||||
BOOST_CHECK_EQUAL(GetSerializeSize(float(0), 0), 4U);
|
||||
BOOST_CHECK_EQUAL(GetSerializeSize(double(0), 0), 8U);
|
||||
BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(floats_conversion)
|
||||
{
|
||||
// Choose values that map unambiguously to binary floating point to avoid
|
||||
// rounding issues at the compiler side.
|
||||
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x00000000), 0.0F);
|
||||
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f000000), 0.5F);
|
||||
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f800000), 1.0F);
|
||||
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40000000), 2.0F);
|
||||
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40800000), 4.0F);
|
||||
BOOST_CHECK_EQUAL(ser_uint32_to_float(0x44444444), 785.066650390625F);
|
||||
|
||||
BOOST_CHECK_EQUAL(ser_float_to_uint32(0.0F), 0x00000000U);
|
||||
BOOST_CHECK_EQUAL(ser_float_to_uint32(0.5F), 0x3f000000U);
|
||||
BOOST_CHECK_EQUAL(ser_float_to_uint32(1.0F), 0x3f800000U);
|
||||
BOOST_CHECK_EQUAL(ser_float_to_uint32(2.0F), 0x40000000U);
|
||||
BOOST_CHECK_EQUAL(ser_float_to_uint32(4.0F), 0x40800000U);
|
||||
BOOST_CHECK_EQUAL(ser_float_to_uint32(785.066650390625F), 0x44444444U);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(doubles_conversion)
|
||||
{
|
||||
// Choose values that map unambiguously to binary floating point to avoid
|
||||
// rounding issues at the compiler side.
|
||||
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x0000000000000000ULL), 0.0);
|
||||
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3fe0000000000000ULL), 0.5);
|
||||
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3ff0000000000000ULL), 1.0);
|
||||
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4000000000000000ULL), 2.0);
|
||||
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4010000000000000ULL), 4.0);
|
||||
BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4088888880000000ULL), 785.066650390625);
|
||||
|
||||
BOOST_CHECK_EQUAL(ser_double_to_uint64(0.0), 0x0000000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(ser_double_to_uint64(0.5), 0x3fe0000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(ser_double_to_uint64(1.0), 0x3ff0000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(ser_double_to_uint64(2.0), 0x4000000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(ser_double_to_uint64(4.0), 0x4010000000000000ULL);
|
||||
BOOST_CHECK_EQUAL(ser_double_to_uint64(785.066650390625), 0x4088888880000000ULL);
|
||||
}
|
||||
/*
|
||||
Python code to generate the below hashes:
|
||||
|
||||
def reversed_hex(x):
|
||||
return binascii.hexlify(''.join(reversed(x)))
|
||||
def dsha256(x):
|
||||
return hashlib.sha256(hashlib.sha256(x).digest()).digest()
|
||||
|
||||
reversed_hex(dsha256(''.join(struct.pack('<f', x) for x in range(0,1000)))) == '8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c'
|
||||
reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
|
||||
*/
|
||||
BOOST_AUTO_TEST_CASE(floats)
|
||||
{
|
||||
CDataStream ss(SER_DISK, 0);
|
||||
// encode
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ss << float(i);
|
||||
}
|
||||
BOOST_CHECK(Hash(ss) == uint256S("8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c"));
|
||||
|
||||
// decode
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
float j;
|
||||
ss >> j;
|
||||
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(doubles)
|
||||
{
|
||||
CDataStream ss(SER_DISK, 0);
|
||||
// encode
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ss << double(i);
|
||||
}
|
||||
BOOST_CHECK(Hash(ss) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96"));
|
||||
|
||||
// decode
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
double j;
|
||||
ss >> j;
|
||||
BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(varints)
|
||||
{
|
||||
// encode
|
||||
|
Loading…
Reference in New Issue
Block a user