Skip to content

Commit

Permalink
Avoid JsonRpc retry in 1s, use staticNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzdogan committed Jul 4, 2023
1 parent 44925c9 commit d4397c7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
43 changes: 29 additions & 14 deletions packages/lib-sourcify/src/lib/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@ethereum-sourcify/bytecode-utils';
import {
JsonRpcProvider,
Network,
TransactionResponse,
WebSocketProvider,
getAddress,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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),
Expand All @@ -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;
}
Expand Down
12 changes: 10 additions & 2 deletions packages/lib-sourcify/test/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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();
});

Expand Down
11 changes: 9 additions & 2 deletions test/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
id: keccak256str,
JsonRpcProvider,
getCreateAddress,
Network,
} = require("ethers");
const { EventEmitter } = require("stream");
const { LOCAL_CHAINS } = require("../dist/sourcify-chains");
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 10 additions & 4 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit d4397c7

Please sign in to comment.