Ejemplo n.º 1
0
def create_header_from_parent(difficulty_fn: Callable[[BlockHeaderAPI, int],
                                                      int],
                              parent_header: Optional[BlockHeaderAPI],
                              **header_params: Any) -> BlockHeaderAPI:

    if 'gas_limit' not in header_params:
        if parent_header is not None and not hasattr(parent_header,
                                                     'base_fee_per_gas'):
            # If the previous block was not a London block,
            #   double the gas limit, so the new target is the old gas limit
            header_params[
                'gas_limit'] = parent_header.gas_limit * ELASTICITY_MULTIPLIER
        else:
            # frontier rules
            header_params['gas_limit'] = compute_gas_limit(
                parent_header,
                genesis_gas_limit=GENESIS_GAS_LIMIT,
            )

    # byzantium
    if 'timestamp' not in header_params:
        header_params['timestamp'] = new_timestamp_from_parent(parent_header)

    if 'difficulty' not in header_params:
        if parent_header is None:
            raise ValueError(
                "Must set difficulty when creating a new genesis header (no parent)."
                " Consider 1 for easy mining or eth.constants.GENESIS_DIFFICULTY for consistency."
            )
        else:
            header_params['difficulty'] = difficulty_fn(
                parent_header,
                header_params['timestamp'],
            )

    # The general fill function doesn't recognize this custom field, so remove it
    configured_fee_per_gas = header_params.pop('base_fee_per_gas', None)

    all_fields = fill_header_params_from_parent(parent_header, **header_params)

    calculated_fee_per_gas = calculate_expected_base_fee_per_gas(parent_header)
    if configured_fee_per_gas is None:
        all_fields['base_fee_per_gas'] = calculated_fee_per_gas
    else:
        # Must not configure an invalid base fee. So verify that either:
        #   1. This is the genesis header, or
        #   2. The configured value matches the calculated value from the parent
        if parent_header is None or configured_fee_per_gas == calculated_fee_per_gas:
            all_fields['base_fee_per_gas'] = configured_fee_per_gas
        else:
            raise ValidationError(
                f"Cannot select an invalid base_fee_per_gas of:"
                f" {configured_fee_per_gas}, expected: {calculated_fee_per_gas}"
            )

    new_header = LondonBlockHeader(**all_fields)  # type:ignore
    return new_header
Ejemplo n.º 2
0
def create_frontier_header_from_parent(parent_header: BlockHeader,
                                       **header_params: Any) -> BlockHeader:
    if 'difficulty' not in header_params:
        # Use setdefault to ensure the new header has the same timestamp we use to calculate its
        # difficulty.
        header_params.setdefault('timestamp', parent_header.timestamp + 1)
        header_params['difficulty'] = compute_frontier_difficulty(
            parent_header,
            header_params['timestamp'],
        )
    if 'gas_limit' not in header_params:
        header_params['gas_limit'] = compute_gas_limit(
            parent_header,
            gas_limit_floor=GENESIS_GAS_LIMIT,
        )

    header = BlockHeader.from_parent(parent=parent_header, **header_params)

    return header
Ejemplo n.º 3
0
def create_frontier_header_from_parent(parent_header: BlockHeaderAPI,
                                       **header_params: Any) -> BlockHeader:
    if 'timestamp' not in header_params:
        header_params['timestamp'] = new_timestamp_from_parent(parent_header)

    if 'difficulty' not in header_params:
        # Use setdefault to ensure the new header has the same timestamp we use to calculate its
        # difficulty.
        header_params['difficulty'] = compute_frontier_difficulty(
            parent_header,
            header_params['timestamp'],
        )
    if 'gas_limit' not in header_params:
        header_params['gas_limit'] = compute_gas_limit(
            parent_header,
            genesis_gas_limit=GENESIS_GAS_LIMIT,
        )

    all_fields = fill_header_params_from_parent(parent_header, **header_params)
    return BlockHeader(**all_fields)