Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 8d4cf53

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into develop
2 parents c8041a7 + 5687f29 commit 8d4cf53

File tree

8 files changed

+117
-34
lines changed

8 files changed

+117
-34
lines changed

ethereum/abi.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ethereum import utils
55
from rlp.utils import decode_hex, encode_hex
66
from ethereum.utils import encode_int, zpad, big_endian_to_int, is_numeric, is_string, ceil32
7-
from ethereum.utils import isnumeric
7+
from ethereum.utils import isnumeric, TT256, TT255
88
import ast
99

1010

@@ -145,35 +145,38 @@ class ValueOutOfBounds(EncodingError):
145145
pass
146146

147147

148-
# Decode an integer
149-
def decint(n):
148+
# Decode an unsigned/signed integer
149+
def decint(n, signed=False):
150150
if isinstance(n, str):
151151
n = utils.to_string(n)
152-
if is_numeric(n) and n < 2**256 and n >= -2**255:
152+
153+
if is_numeric(n):
154+
min, max = (-TT255,TT255-1) if signed else (0,TT256-1)
155+
if n > max or n < min:
156+
raise EncodingError("Number out of range: %r" % n)
153157
return n
154-
elif is_numeric(n):
155-
raise EncodingError("Number out of range: %r" % n)
156-
elif is_string(n) and len(n) == 40:
157-
return big_endian_to_int(decode_hex(n))
158-
elif is_string(n) and len(n) <= 32:
159-
return big_endian_to_int(n)
160-
elif is_string(n) and len(n) > 32:
161-
raise EncodingError("String too long: %r" % n)
158+
elif is_string(n):
159+
if len(n) == 40:
160+
n = decode_hex(n)
161+
if len(n) > 32:
162+
raise EncodingError("String too long: %r" % n)
163+
164+
i = big_endian_to_int(n)
165+
return (i - TT256) if signed and i >= TT255 else i
162166
elif n is True:
163167
return 1
164168
elif n is False or n is None:
165169
return 0
166170
else:
167171
raise EncodingError("Cannot encode integer: %r" % n)
168172

169-
170173
# Encodes a base datum
171174
def encode_single(typ, arg):
172175
base, sub, _ = typ
173176
# Unsigned integers: uint<sz>
174177
if base == 'uint':
175178
sub = int(sub)
176-
i = decint(arg)
179+
i = decint(arg, False)
177180

178181
if not 0 <= i < 2**sub:
179182
raise ValueOutOfBounds(repr(arg))
@@ -185,8 +188,8 @@ def encode_single(typ, arg):
185188
# Signed integers: int<sz>
186189
elif base == 'int':
187190
sub = int(sub)
188-
i = decint(arg)
189-
if not -2**(sub - 1) <= i < 2**sub:
191+
i = decint(arg, True)
192+
if not -2**(sub - 1) <= i < 2**(sub - 1):
190193
raise ValueOutOfBounds(repr(arg))
191194
return zpad(encode_int(i % 2**sub), 32)
192195
# Unsigned reals: ureal<high>x<low>

ethereum/tests/test_abi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def test_abi_encode_signed_int():
2222

2323
def test_abi_encode_single_int():
2424
assert abi.encode_single(['int', '256', []], -2**255) == (b'\x80'+b'\x00'*31)
25+
assert abi.encode_single(['int', '256', []], (b'\x80'+b'\x00'*31)) == (b'\x80'+b'\x00'*31)
2526

2627
assert abi.encode_single(['int', '8', []], -128) == zpad(b'\x80', 32)
2728
with pytest.raises(abi.ValueOutOfBounds):

ethereum/tests/test_contracts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ def test_types():
11891189

11901190

11911191
ecrecover_code = """
1192-
def test_ecrecover(h, v, r, s):
1192+
def test_ecrecover(h:uint256, v:uint256, r:uint256, s:uint256):
11931193
return(ecrecover(h, v, r, s))
11941194
"""
11951195

ethereum/tests/test_difficulty.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from ethereum import tester, blocks
2+
import ethereum.utils as utils
3+
import rlp
4+
import ethereum.testutils as testutils
5+
from ethereum.testutils import fixture_to_bytes
6+
import ethereum.config as config
7+
import sys
8+
import os
9+
import json
10+
11+
from ethereum.slogging import get_logger
12+
logger = get_logger()
13+
# customize VM log output to your needs
14+
# hint: use 'py.test' with the '-s' option to dump logs to the console
15+
# configure_logging(':trace')
16+
17+
18+
def test_difficulty(filename, testname, testdata):
19+
testdata = fixture_to_bytes(testdata)
20+
21+
parent_timestamp=int(testdata["parentTimestamp"], 10 if testdata["parentTimestamp"].isdigit() else 16)
22+
parent_difficulty=int(testdata["parentDifficulty"], 10 if testdata["parentDifficulty"].isdigit() else 16)
23+
parent_blk_number=int(testdata["currentBlockNumber"], 10 if testdata["currentBlockNumber"].isdigit() else 16)-1
24+
cur_blk_timestamp=int(testdata["currentTimestamp"], 10 if testdata["currentTimestamp"].isdigit() else 16)
25+
reference_dif = int(testdata["currentDifficulty"], 10 if testdata["currentDifficulty"].isdigit() else 16)
26+
27+
28+
env = tester.state().env
29+
if 'Homestead' in filename:
30+
env.config['HOMESTEAD_FORK_BLKNUM'] = 0
31+
if 'difficultyMorden' in filename:
32+
env.config['HOMESTEAD_FORK_BLKNUM'] = 494000
33+
34+
parent_bh = blocks.BlockHeader(timestamp=parent_timestamp,
35+
difficulty=parent_difficulty,
36+
number=parent_blk_number)
37+
block = blocks.Block(parent_bh, [], env=env,
38+
making=True)
39+
40+
41+
calculated_dif = blocks.calc_difficulty(block, cur_blk_timestamp)
42+
43+
print calculated_dif
44+
print reference_dif
45+
assert calculated_dif == reference_dif
46+
47+
48+
def not_a_difficulty_test(filename, testname, testdata):
49+
if 'difficultyOlimpic.json' in filename:
50+
return True
51+
if 'difficulty' in filename:
52+
return False
53+
54+
return True
55+
56+
57+
def pytest_generate_tests(metafunc):
58+
testutils.generate_test_params('BasicTests', metafunc, exclude_func=not_a_difficulty_test)
59+
60+
61+
def main():
62+
import pdb; pdb.set_trace()
63+
if len(sys.argv) == 1:
64+
# read fixture from stdin
65+
fixtures = {'stdin': json.load(sys.stdin)}
66+
else:
67+
# load fixtures from specified file or dir
68+
fixtures = testutils.get_tests_from_file_or_dir(sys.argv[1])
69+
for filename, tests in list(fixtures.items()):
70+
for testname, testdata in list(tests.items()):
71+
if len(sys.argv) < 3 or testname == sys.argv[2]:
72+
print("Testing: %s %s" % (filename, testname))
73+
# testutils.check_state_test(testdata)
74+
test_difficulty(filename, testname, testdata)
75+
76+
77+
if __name__ == '__main__':
78+
main()

ethereum/testutils.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def blkhash(n):
362362
success, output = pb.apply_transaction(blk, tx)
363363
blk.commit_state()
364364
print 'success', blk.get_receipts()[-1].gas_used
365-
except pb.InvalidTransaction:
365+
except InvalidTransaction:
366366
success, output = False, b''
367367
blk.commit_state()
368368
pass
@@ -476,7 +476,7 @@ def run_abi_test(params, mode):
476476
def run_genesis_test(params, mode):
477477
params = copy.deepcopy(params)
478478
if 'difficulty' not in params:
479-
params['difficulty'] = int_to_hex(2**34)
479+
params['difficulty'] = int_to_hex(2 ** 34)
480480
if 'mixhash' not in params:
481481
params['mixhash'] = '0x' + '0' * 64
482482
if 'nonce' not in params:
@@ -576,18 +576,19 @@ def generate_test_params(testsource, metafunc, skip_func=None, exclude_func=None
576576
base_dir = os.path.dirname(os.path.dirname(__file__))
577577
params = []
578578
for filename, tests in fixtures.items():
579-
filename = os.path.relpath(filename, base_dir)
580-
for testname, testdata in tests.items():
581-
if exclude_func and exclude_func(filename, testname, testdata):
582-
continue
583-
if skip_func:
584-
skipif = pytest.mark.skipif(
585-
skip_func(filename, testname, testdata),
586-
reason="Excluded"
587-
)
588-
params.append(skipif((filename, testname, testdata)))
589-
else:
590-
params.append((filename, testname, testdata))
579+
if isinstance(tests, dict):
580+
filename = os.path.relpath(filename, base_dir)
581+
for testname, testdata in tests.items():
582+
if exclude_func and exclude_func(filename, testname, testdata):
583+
continue
584+
if skip_func:
585+
skipif = pytest.mark.skipif(
586+
skip_func(filename, testname, testdata),
587+
reason="Excluded"
588+
)
589+
params.append(skipif((filename, testname, testdata)))
590+
else:
591+
params.append((filename, testname, testdata))
591592

592593
metafunc.parametrize(
593594
('filename', 'testname', 'testdata'),

fixtures

Submodule fixtures updated 33 files

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.2.1
2+
current_version = 1.2.0
33
commit = True
44
tag = True
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# *IMPORTANT*: Don't manually change the version here. Use the 'bumpversion' utility.
2222
# see: https://github.com/ethereum/pyethapp/wiki/Development:-Versions-and-Releases
23-
version = '1.2.1'
23+
version = '1.2.0'
2424

2525
setup(
2626
name="ethereum",

0 commit comments

Comments
 (0)