Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carver/low precision decimal test #27

Merged
merged 3 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions eth_abi/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
NonEmptyPaddingBytes,
)
from eth_abi.utils.numeric import (
abi_decimal_context,
big_endian_to_int,
quantize_value,
ceil32,
Expand Down Expand Up @@ -386,9 +387,10 @@ class UnsignedRealDecoder(BaseRealDecoder):
@classmethod
def decoder_fn(cls, data):
value = big_endian_to_int(data)
decimal_value = decimal.Decimal(value)
raw_real_value = decimal_value / 2 ** cls.low_bit_size
real_value = quantize_value(raw_real_value, cls.low_bit_size)
with decimal.localcontext(abi_decimal_context):
decimal_value = decimal.Decimal(value)
raw_real_value = decimal_value / 2 ** cls.low_bit_size
real_value = quantize_value(raw_real_value, cls.low_bit_size)
return real_value


Expand Down
14 changes: 8 additions & 6 deletions eth_abi/utils/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def big_endian_to_int(value):
return int.from_bytes(value, byteorder='big')


abi_decimal_context = decimal.Context(prec=999)


def ceil32(x):
return x if x % 32 == 0 else x + 32 - (x % 32)

Expand Down Expand Up @@ -86,11 +89,10 @@ def compute_signed_real_bounds(num_high_bits, num_low_bits):

def quantize_value(value, decimal_bit_size):
num_decimals = int(math.ceil(math.log10(2 ** decimal_bit_size)))
with decimal.localcontext() as ctx:
ctx.prec = 999
with decimal.localcontext(abi_decimal_context):
if num_decimals == 0:
quantize_value = decimal.Decimal('1', context=ctx)
quantize_value = decimal.Decimal('1')
else:
quantize_value = decimal.Decimal('1.{0}'.format(''.zfill(num_decimals)), context=ctx)
decimal_value = decimal.Decimal(value, context=ctx)
return decimal_value.quantize(quantize_value, context=ctx)
quantize_value = decimal.Decimal('1.{0}'.format(''.zfill(num_decimals)))
decimal_value = decimal.Decimal(value)
return decimal_value.quantize(quantize_value)
10 changes: 8 additions & 2 deletions tests/decoding/test_single_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
zpad32,
)
from eth_abi.utils.numeric import (
abi_decimal_context,
big_endian_to_int,
int_to_big_endian,
compute_signed_integer_bounds,
Expand Down Expand Up @@ -416,7 +417,10 @@ def test_decode_unsigned_real(high_bit_size,
decoded_value = decoder(stream)

unsigned_integer_value = big_endian_to_int(stream_bytes[:data_byte_size])
raw_real_value = decimal.Decimal(unsigned_integer_value) / 2 ** low_bit_size

with decimal.localcontext(abi_decimal_context):
raw_real_value = decimal.Decimal(unsigned_integer_value) / 2 ** low_bit_size

actual_value = quantize_value(raw_real_value, low_bit_size)

assert decoded_value == actual_value
Expand Down Expand Up @@ -483,7 +487,9 @@ def test_decode_signed_real(high_bit_size,
else:
signed_integer_value = unsigned_integer_value

raw_actual_value = decimal.Decimal(signed_integer_value) / 2 ** low_bit_size
with decimal.localcontext(abi_decimal_context):
raw_actual_value = decimal.Decimal(signed_integer_value) / 2 ** low_bit_size

actual_value = quantize_value(raw_actual_value, low_bit_size)

assert decoded_value == actual_value
17 changes: 15 additions & 2 deletions tests/encoding/test_single_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
)

from eth_abi.utils.numeric import (
abi_decimal_context,
int_to_big_endian,
compute_unsigned_integer_bounds,
compute_signed_integer_bounds,
Expand Down Expand Up @@ -287,6 +288,13 @@ def test_encode_string(string_value):
value_bit_size=st.integers(min_value=1, max_value=32).map(lambda v: v * 8),
data_byte_size=st.integers(min_value=1, max_value=32),
)
@example(
base_integer_value=384,
value_bit_size=232,
high_bit_size=64,
low_bit_size=168,
data_byte_size=29,
)
def test_encode_unsigned_real(base_integer_value,
value_bit_size,
high_bit_size,
Expand Down Expand Up @@ -324,7 +332,9 @@ def test_encode_unsigned_real(base_integer_value,
assert 'UnsignedReal' in str(exception_info.value)
return

real_value = decimal.Decimal(base_integer_value) / 2 ** low_bit_size
with decimal.localcontext(abi_decimal_context):
real_value = decimal.Decimal(base_integer_value) / 2 ** low_bit_size

lower_bound, upper_bound = compute_unsigned_real_bounds(
high_bit_size,
low_bit_size,
Expand Down Expand Up @@ -387,7 +397,10 @@ def test_encode_signed_real(base_integer_value,
return

unsigned_integer_value = base_integer_value % 2**(high_bit_size + low_bit_size)
real_value = decimal.Decimal(unsigned_integer_value) / 2 ** low_bit_size

with decimal.localcontext(abi_decimal_context):
real_value = decimal.Decimal(unsigned_integer_value) / 2 ** low_bit_size

lower_bound, upper_bound = compute_signed_real_bounds(
high_bit_size,
low_bit_size,
Expand Down