tutil: assert that Decimal precision is sufficient - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 24d47022b465705c710c5fa92f6e46d21d4b380c DIR parent b856336f8ce868717457676112a453b370eb9408 HTML Author: SomberNight <somber.night@protonmail.com> Date: Thu, 18 Feb 2021 02:40:21 +0100 util: assert that Decimal precision is sufficient Just for sanity... e.g. when importing electrum as a python library, tthe calling code could have changed the precision. related: #5223 Diffstat: M electrum/util.py | 7 +++++++ 1 file changed, 7 insertions(+), 0 deletions(-) --- DIR diff --git a/electrum/util.py b/electrum/util.py t@@ -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