From 24d47022b465705c710c5fa92f6e46d21d4b380c Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 18 Feb 2021 02:40:21 +0100 Subject: [PATCH] util: assert that Decimal precision is sufficient Just for sanity... e.g. when importing electrum as a python library, the calling code could have changed the precision. related: #5223 --- electrum/util.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/electrum/util.py b/electrum/util.py index 8be83bafd..028d0e869 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -630,6 +630,13 @@ def format_satoshis_plain(x, *, decimal_point=8) -> str: return "{:.8f}".format(Decimal(x) / scale_factor).rstrip('0').rstrip('.') +# Check that Decimal precision is sufficient. +# We need at the very least ~20, as we deal with msat amounts, and +# log10(21_000_000 * 10**8 * 1000) ~= 18.3 +# decimal.DefaultContext.prec == 28 by default, but it is mutable. +# We enforce that we have at least that available. +assert decimal.getcontext().prec >= 28, f"PyDecimal precision too low: {decimal.getcontext().prec}" + DECIMAL_POINT = localeconv()['decimal_point'] # type: str