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

Commit 16a3085

Browse files
committed
Merge pull request #335 from RomanZacharia/develop
Fix failing tests, add difficulty tests. Update to latest fixtures submodule commit.
2 parents 72a7e2c + 4e2eba3 commit 16a3085

File tree

3 files changed

+92
-13
lines changed

3 files changed

+92
-13
lines changed

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: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)