Skip to content
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

use eth_chainId instead of net_version #3804

Merged
merged 3 commits into from
Jun 27, 2022
Merged
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
47 changes: 30 additions & 17 deletions beacon_chain/eth1/eth1_monitor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,7 @@ proc detectPrimaryProviderComingOnline(m: Eth1Monitor) {.async.} =
# Use one of the get/request-type methods from
# https://github.com/ethereum/execution-apis/blob/v1.0.0-alpha.9/src/engine/specification.md#underlying-protocol
# which does nit take parameters and returns a small structure, to ensure
# this works with engine API endpoints. Either eth_chainId or eth_syncing
# works for this purpose.
# this works with engine API endpoints.
let testRequest = tempProvider.web3.provider.eth_syncing()

yield testRequest or sleepAsync(web3Timeouts)
Expand Down Expand Up @@ -1343,18 +1342,28 @@ proc startEth1Syncing(m: Eth1Monitor, delayBeforeStart: Duration) {.async.} =
contract = $m.depositContractAddress

if isFirstRun and m.eth1Network.isSome:
let
providerNetwork = awaitWithRetries m.dataProvider.web3.provider.net_version()
expectedNetwork = case m.eth1Network.get
of mainnet: "1"
of ropsten: "3"
of rinkeby: "4"
of goerli: "5"
of sepolia: "11155111"
if expectedNetwork != providerNetwork:
fatal "The specified web3 provider serves data for a different network",
expectedNetwork, providerNetwork
quit 1
try:
let
providerChain =
awaitWithRetries m.dataProvider.web3.provider.eth_chainId()

# https://eips.ethereum.org/EIPS/eip-155#list-of-chain-ids
expectedChain = case m.eth1Network.get
of mainnet: 1.Quantity
of ropsten: 3.Quantity
of rinkeby: 4.Quantity
of goerli: 5.Quantity
of sepolia: 11155111.Quantity # https://chainid.network/
if expectedChain != providerChain:
fatal "The specified Web3 provider serves data for a different chain",
expectedChain = distinctBase(expectedChain),
providerChain = distinctBase(providerChain)
quit 1
except CatchableError as exc:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't DataProviderFailure be enough here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. Didn't want to risk it, because debugging that in an async context is problematic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, {.push raises: [Defect].} effectively doesn't apply to {.async.} functions, which means that even if currently, DataProviderFailure might be enough, changes in the library implementation could undermine that with no compiler-time warnings or errors.

# Typically because it's not synced through EIP-155, assuming this Web3
# endpoint has been otherwise working.
debug "startEth1Syncing: eth_chainId failed: ",
error = exc.msg

var mustUsePolling = m.forcePolling or
web3Url.startsWith("http://") or
Expand Down Expand Up @@ -1526,8 +1535,6 @@ proc testWeb3Provider*(web3Url: Uri,
web3 = mustSucceed "connect to web3 provider":
await newWeb3(
$web3Url, getJsonRpcRequestHeaders(jwtSecret))
networkVersion = mustSucceed "get network version":
awaitWithRetries web3.provider.net_version()
latestBlock = mustSucceed "get latest block":
awaitWithRetries web3.provider.eth_getBlockByNumber(blockId("latest"), false)
syncStatus = mustSucceed "get sync status":
Expand All @@ -1543,13 +1550,19 @@ proc testWeb3Provider*(web3Url: Uri,
awaitWithRetries web3.provider.eth_mining()

echo "Client Version: ", clientVersion
echo "Network Version: ", networkVersion
echo "Network Peers: ", peers
echo "Syncing: ", syncStatus
echo "Latest block: ", latestBlock.number.uint64
echo "Last Known Nonce: ", web3.lastKnownNonce
echo "Mining: ", mining

try:
let chainId = awaitWithRetries web3.provider.eth_chainId()
echo "Chain ID: ", chainId.uint64
except DataProviderFailure as exc:
# Typically because it's not synced through EIP-155.
echo "Web3 provider does not provide chain ID: " & exc.msg

let ns = web3.contractSender(DepositContract, depositContractAddress)
try:
let depositRoot = awaitWithRetries(
Expand Down