Skip to content

Rethink eth_chainId calls #2266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion web3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"HTTPProvider",
"IPCProvider",
"WebsocketProvider",
"TestRPCProvider",
"EthereumTesterProvider",
"Account",
"AsyncHTTPProvider",
Expand Down
2 changes: 1 addition & 1 deletion web3/_utils/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
web3.eth.max_priority_fee + (2 * web3.eth.get_block('latest')['baseFeePerGas'])
),
'maxPriorityFeePerGas': lambda web3, tx: web3.eth.max_priority_fee,
'chainId': lambda web3, tx: web3.eth.chain_id,
'chainId': lambda web3, tx: web3.provider.internal_chain_id,
}

if TYPE_CHECKING:
Expand Down
4 changes: 2 additions & 2 deletions web3/middleware/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

@curry
def validate_chain_id(web3: "Web3", chain_id: int) -> int:
if to_integer_if_hex(chain_id) == web3.eth.chain_id:
if to_integer_if_hex(chain_id) == web3.provider.internal_chain_id:
return chain_id
else:
raise ValidationError(
Expand Down Expand Up @@ -88,7 +88,7 @@ def transaction_param_validator(web3: "Web3") -> Callable[..., Any]:
transactions_params_validators = {
"chainId": apply_formatter_if(
# Bypass `validate_chain_id` if chainId can't be determined
lambda _: is_not_null(web3.eth.chain_id),
lambda _: is_not_null(web3.provider.internal_chain_id),
validate_chain_id(web3),
),
}
Expand Down
14 changes: 14 additions & 0 deletions web3/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from web3._utils.encoding import (
FriendlyJsonSerde,
)
from web3._utils.method_formatters import (
to_integer_if_hex,
)
from web3.middleware import (
combine_middlewares,
)
Expand All @@ -34,6 +37,7 @@ class BaseProvider:
_middlewares: Tuple[Middleware, ...] = ()
# a tuple of (all_middlewares, request_func)
_request_func_cache: Tuple[Tuple[Middleware, ...], Callable[..., RPCResponse]] = (None, None)
internal_chain_id: int = None

@property
def middlewares(self) -> Tuple[Middleware, ...]:
Expand Down Expand Up @@ -84,6 +88,16 @@ class JSONBaseProvider(BaseProvider):
def __init__(self) -> None:
self.request_counter = itertools.count()

# Set an `internal_chain_id` since the `chain_id` value should not change after a provider
# is instantiated.
try:
self.internal_chain_id = to_integer_if_hex(
self.make_request(RPCEndpoint('eth_chainId'), [])
['result']
)
except Exception:
self.internal_chain_id = None

def decode_rpc_response(self, raw_response: bytes) -> RPCResponse:
text_response = to_text(raw_response)
return cast(RPCResponse, FriendlyJsonSerde().json_decode(text_response))
Expand Down