diff --git a/packages/lib-sourcify/src/lib/verification.ts b/packages/lib-sourcify/src/lib/verification.ts index 7ad766d63..1074e254f 100644 --- a/packages/lib-sourcify/src/lib/verification.ts +++ b/packages/lib-sourcify/src/lib/verification.ts @@ -15,6 +15,7 @@ import { } from '@ethereum-sourcify/bytecode-utils'; import { JsonRpcProvider, + Network, TransactionResponse, WebSocketProvider, getAddress, @@ -490,25 +491,31 @@ export async function getBytecode( // Request sequentially. Custom node is always before ALCHEMY so we don't waste resources if succeeds. for (const rpcURL of sourcifyChain.rpc) { - try { - const provider = rpcURL.startsWith('http') - ? new JsonRpcProvider(rpcURL) - : new WebSocketProvider(rpcURL); + const ethersNetwork = new Network( + sourcifyChain.name, + sourcifyChain.chainId + ); + + const provider = rpcURL.startsWith('http') + ? // Use staticNetwork to avoid seding unnecessary eth_chainId requests + new JsonRpcProvider(rpcURL, ethersNetwork, { + staticNetwork: ethersNetwork, + }) + : new WebSocketProvider(rpcURL); + try { // Race the RPC call with a timeout const bytecode = await Promise.race([ provider.getCode(address), rejectInMs(RPC_TIMEOUT, rpcURL), ]); - return bytecode; } catch (err) { - // Catch to try the next RPC if (err instanceof Error) { logWarn( `Can't fetch bytecode from RPC ${rpcURL} and chain ${sourcifyChain.chainId}` ); - console.log(err.message); + continue; } else { throw err; } @@ -520,12 +527,21 @@ export async function getBytecode( async function getTx(creatorTxHash: string, sourcifyChain: SourcifyChain) { if (!sourcifyChain?.rpc.length) throw new Error('No RPC provider was given for this chain.'); + for (const rpcURL of sourcifyChain.rpc) { - try { - const provider = rpcURL.startsWith('http') - ? new JsonRpcProvider(rpcURL) - : new WebSocketProvider(rpcURL); + const ethersNetwork = new Network( + sourcifyChain.name, + sourcifyChain.chainId + ); + + const provider = rpcURL.startsWith('http') + ? // Use staticNetwork to avoid seding unnecessary eth_chainId requests + new JsonRpcProvider(rpcURL, ethersNetwork, { + staticNetwork: ethersNetwork, + }) + : new WebSocketProvider(rpcURL); + try { // Race the RPC call with a timeout const tx = await Promise.race([ provider.getTransaction(creatorTxHash), @@ -542,12 +558,11 @@ async function getTx(creatorTxHash: string, sourcifyChain: SourcifyChain) { ); } } catch (err) { - // Catch to try the next RPC if (err instanceof Error) { logWarn( - `Can't fetch from RPC ${rpcURL} and chain ${sourcifyChain.chainId}` + `Can't fetch bytecode from RPC ${rpcURL} and chain ${sourcifyChain.chainId}` ); - logWarn(err.message); + continue; } else { throw err; } diff --git a/packages/lib-sourcify/test/verification.spec.ts b/packages/lib-sourcify/test/verification.spec.ts index 4cc9245a7..8ac17b46c 100644 --- a/packages/lib-sourcify/test/verification.spec.ts +++ b/packages/lib-sourcify/test/verification.spec.ts @@ -24,7 +24,7 @@ import { verifyDeployed, } from '../src'; import fs from 'fs'; -import { JsonRpcProvider, JsonRpcSigner } from 'ethers'; +import { JsonRpcProvider, JsonRpcSigner, Network } from 'ethers'; // import { Match } from '@ethereum-sourcify/lib-sourcify'; const ganacheServer = Ganache.server({ @@ -56,7 +56,15 @@ let signer: JsonRpcSigner; describe('lib-sourcify tests', () => { before(async () => { await ganacheServer.listen(GANACHE_PORT); - localProvider = new JsonRpcProvider(`http://localhost:${GANACHE_PORT}`); + const ethersNetwork = new Network( + sourcifyChainGanache.rpc[0], + sourcifyChainGanache.chainId + ); + localProvider = new JsonRpcProvider( + `http://localhost:${GANACHE_PORT}`, + ethersNetwork, + { staticNetwork: ethersNetwork } + ); signer = await localProvider.getSigner(); }); diff --git a/test/monitor.js b/test/monitor.js index a5e80b5ab..421da6f60 100644 --- a/test/monitor.js +++ b/test/monitor.js @@ -15,6 +15,7 @@ const { id: keccak256str, JsonRpcProvider, getCreateAddress, + Network, } = require("ethers"); const { EventEmitter } = require("stream"); const { LOCAL_CHAINS } = require("../dist/sourcify-chains"); @@ -166,9 +167,15 @@ describe("Monitor", function () { }); await ganacheServer.listen(GANACHE_PORT); console.log("Started ganache local server at port " + GANACHE_PORT); - + const sourcifyChainGanache = LOCAL_CHAINS[0]; + const ethersNetwork = new Network( + sourcifyChainGanache.rpc[0], + sourcifyChainGanache.chainId + ); signer = await new JsonRpcProvider( - `http://localhost:${GANACHE_PORT}` + `http://localhost:${GANACHE_PORT}`, + ethersNetwork, + { staticNetwork: ethersNetwork } ).getSigner(); account = await signer.getAddress(); diff --git a/test/server.js b/test/server.js index 985dfcb26..1586fab2c 100644 --- a/test/server.js +++ b/test/server.js @@ -36,7 +36,8 @@ const { deployFromAbiAndBytecodeForCreatorTxHash, } = require("./helpers/helpers"); const { deployFromAbiAndBytecode } = require("./helpers/helpers"); -const { JsonRpcProvider } = require("ethers"); +const { JsonRpcProvider, Network } = require("ethers"); +const { LOCAL_CHAINS } = require("../dist/sourcify-chains"); chai.use(chaiHttp); const EXTENDED_TIME = 20000; // 20 seconds @@ -76,11 +77,16 @@ describe("Server", function () { // const ipfs = await IPFS.create(); // const httpGateway = new HttpGateway(ipfs); // await httpGateway.start(); - + const sourcifyChainGanache = LOCAL_CHAINS[0]; console.log("Started ganache local server on port " + GANACHE_PORT); - + const ethersNetwork = new Network( + sourcifyChainGanache.rpc[0], + sourcifyChainGanache.chainId + ); localSigner = await new JsonRpcProvider( - `http://localhost:${GANACHE_PORT}` + `http://localhost:${GANACHE_PORT}`, + ethersNetwork, + { staticNetwork: ethersNetwork } ).getSigner(); console.log("Initialized Provider");