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

Commit b48514f

Browse files
NerdRomanZacharia
authored andcommitted
Added difficulty tests.
1 parent 17e17a9 commit b48514f

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

ethereum/tests/test_difficulty.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
parent_bh = blocks.BlockHeader(timestamp=parent_timestamp,
29+
difficulty=parent_difficulty,
30+
number=parent_blk_number)
31+
block = blocks.Block(parent_bh, [], env=tester.state().env,
32+
making=True)
33+
34+
calculated_dif = blocks.calc_difficulty(block, cur_blk_timestamp)
35+
36+
print calculated_dif
37+
print reference_dif
38+
assert calculated_dif == reference_dif
39+
40+
41+
#def pytest_generate_tests(metafunc):
42+
# testutils.generate_test_params('BasicTests', metafunc)
43+
44+
def not_a_difficulty_test(filename, testname, testdata):
45+
if 'difficulty' in filename:
46+
return False
47+
if 'difficulty.json' in filename:
48+
return False
49+
if 'difficultyFrontier' in filename:
50+
return False
51+
return True
52+
53+
54+
def pytest_generate_tests(metafunc):
55+
testutils.generate_test_params('DifficultyTests', metafunc, exclude_func=not_a_difficulty_test)
56+
57+
58+
def main():
59+
import pdb; pdb.set_trace()
60+
if len(sys.argv) == 1:
61+
# read fixture from stdin
62+
fixtures = {'stdin': json.load(sys.stdin)}
63+
else:
64+
# load fixtures from specified file or dir
65+
fixtures = testutils.get_tests_from_file_or_dir(sys.argv[1])
66+
for filename, tests in list(fixtures.items()):
67+
for testname, testdata in list(tests.items()):
68+
if len(sys.argv) < 3 or testname == sys.argv[2]:
69+
print("Testing: %s %s" % (filename, testname))
70+
# testutils.check_state_test(testdata)
71+
test_difficulty(filename, testname, testdata)
72+
73+
74+
if __name__ == '__main__':
75+
main()

0 commit comments

Comments
 (0)