Skip to content

Commit

Permalink
Merge pull request #89 from skalenetwork/update-check-url-func
Browse files Browse the repository at this point in the history
Update check url function in SNB
  • Loading branch information
dmytrotkk authored Feb 8, 2024
2 parents 989ed90 + 04ad70e commit 8ce465e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
8 changes: 4 additions & 4 deletions network-browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
getMainnetManagerAbi,
getMainnetProvider
} from './src/contracts'
import { delay, getLoggerConfig, pingUrl, withTimeout } from './src/tools'
import { delay, getLoggerConfig, checkEndpoint, withTimeout } from './src/tools'
import { BrowserTimeoutError } from './src/errors'
import { browse } from './src/browser'
import {
Expand Down Expand Up @@ -54,9 +54,9 @@ async function safeNetworkBrowserLoop() {
log.info(`NETWORK_BROWSER_DELAY: ${NETWORK_BROWSER_DELAY}`)

log.info(`Trying to connect to the sChain RPC: ${SCHAIN_RPC_URL}`)
await pingUrl(SCHAIN_RPC_URL)
log.info(`Trying to connect to the mainnet RPC: ${MAINNET_RPC_URL}`)
await pingUrl(MAINNET_RPC_URL)
await checkEndpoint(SCHAIN_RPC_URL)
log.info(`Trying to connect to the mainnet RPC`)
await checkEndpoint(MAINNET_RPC_URL)

const provider = await getMainnetProvider(MAINNET_RPC_URL, MULTICALL)
const managerAbi = getMainnetManagerAbi()
Expand Down
19 changes: 10 additions & 9 deletions network-browser/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @copyright SKALE Labs 2023-Present
*/

import { id, toBeHex } from 'ethers'
import { JsonRpcProvider, id, toBeHex } from 'ethers'
import { Logger, type ILogObj } from 'tslog'

import { readFileSync, writeFileSync, renameSync } from 'fs'
Expand Down Expand Up @@ -97,32 +97,33 @@ function moveFile(source: string, destination: string): void {
log.info(`Successfully moved the file from ${source} to ${destination}`)
}

export async function pingUrl(
export async function checkEndpoint(
url: string,
maxAttempts: number = DEFAULT_PING_ITERATIONS,
delay: number = DEFAULT_PING_DELAY
): Promise<void> {
let attempt = 0
const provider = new JsonRpcProvider(url)
while (attempt < maxAttempts) {
try {
const response = await fetch(url)
if (response.ok) {
log.info(`URL is available: ${url}`)
const bn = await provider.getBlockNumber()
if (bn > 0) {
log.info(`URL is available, block number: ${bn}`)
return
} else {
log.info(`Attempt ${attempt + 1} failed with status: ${response.status}`)
log.error(`Attempt ${attempt + 1} to connect failed`)
}
} catch (error) {
log.info(
`${url} connection failed - ${attempt + 1}/${maxAttempts}, retrying in ${
log.error(
`Connection failed - ${attempt + 1}/${maxAttempts}, retrying in ${
delay / 1000
} seconds...`
)
}
await new Promise((resolve) => setTimeout(resolve, delay))
attempt++
}
log.info('Max attempts reached, URL is not available.')
log.error('Max attempts reached, URL is not available.')
}

export function chainIdHex(schainName: string): string {
Expand Down

0 comments on commit 8ce465e

Please sign in to comment.