|
| 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() |
0 commit comments