diff --git a/.gitignore b/.gitignore index fd08eaf32b..ab94c73406 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,6 @@ tests/fixtures tests/t8n_testdata fixtures + +# test artifacts +logs/ diff --git a/src/ethereum/forks/homestead/fork.py b/src/ethereum/forks/homestead/fork.py index 6d16c81936..b89f62575f 100644 --- a/src/ethereum/forks/homestead/fork.py +++ b/src/ethereum/forks/homestead/fork.py @@ -740,59 +740,25 @@ def calculate_block_difficulty( parent_timestamp: U256, parent_difficulty: Uint, ) -> Uint: - """ - Computes difficulty of a block using its header and parent header. - - The difficulty is determined by the time the block was created after its - parent. The ``offset`` is calculated using the parent block's difficulty, - ``parent_difficulty``, and the timestamp between blocks. This offset is - then added to the parent difficulty and is stored as the ``difficulty`` - variable. If the time between the block and its parent is too short, the - offset will result in a positive number thus making the sum of - ``parent_difficulty`` and ``offset`` to be a greater value in order to - avoid mass forking. But, if the time is long enough, then the offset - results in a negative value making the block less difficult than - its parent. - - The base standard for a block's difficulty is the predefined value - set for the genesis block since it has no parent. So, a block - can't be less difficult than the genesis block, therefore each block's - difficulty is set to the maximum value between the calculated - difficulty and the ``GENESIS_DIFFICULTY``. + """Homestead difficulty: same behavior, fewer int() casts.""" + # Convert once to signed ints for the parts that need negatives. + pd_int = int(parent_difficulty) + dt_int = int(block_timestamp) - int(parent_timestamp) - Parameters - ---------- - block_number : - Block number of the block. - block_timestamp : - Timestamp of the block. - parent_timestamp : - Timestamp of the parent block. - parent_difficulty : - difficulty of the parent block. + # Base adjustment and signed offset (can be negative). + base = pd_int // 2048 + offset = base * max(1 - (dt_int // 10), -99) - Returns - ------- - difficulty : `ethereum.base_types.Uint` - Computed difficulty for a block. + # Apply offset. + diff_int = pd_int + offset - """ - offset = ( - int(parent_difficulty) - // 2048 - * max(1 - int(block_timestamp - parent_timestamp) // 10, -99) - ) - difficulty = int(parent_difficulty) + offset - # Historical Note: The difficulty bomb was not present in Ethereum at the - # start of Frontier, but was added shortly after launch. However since the - # bomb has no effect prior to block 200000 we pretend it existed from - # genesis. - # See https://github.com/ethereum/go-ethereum/pull/1588 + # Difficulty bomb. num_bomb_periods = (int(block_number) // 100000) - 2 if num_bomb_periods >= 0: - difficulty += 2**num_bomb_periods + diff_int += 1 << num_bomb_periods - # Some clients raise the difficulty to `MINIMUM_DIFFICULTY` prior to adding - # the bomb. This bug does not matter because the difficulty is always much - # greater than `MINIMUM_DIFFICULTY` on Mainnet. - return Uint(max(difficulty, int(MINIMUM_DIFFICULTY))) + # Clamp to minimum and return typed. + min_diff = int(MINIMUM_DIFFICULTY) + if diff_int < min_diff: + diff_int = min_diff + return Uint(diff_int)