Skip to content

Commit

Permalink
Sepolia resolvers with analytics (#30)
Browse files Browse the repository at this point in the history
* WIP

* Fix errors

* Ignore favicon

* Update bun.lockb

* Fix lint error

* Update gateway url

* Add console.log

* Changed endpoint

* Update apiEndpoint

* Add sender and calldata to props

* Move Tracker to evm-gateway

* Revert "Move Tracker to evm-gateway"

This reverts commit 5d3ba37.

* WIP

* Downgrade to 4.20231121.0

* Bump it to the latest

* Add type

* Replace tracker with @ensdomains/server-analytics

* Pass custom apiEndpoint and props

* Add GATEWAY_DOMAIN and ENDPOINT_URL

* Point to correct branch

* Fix CORS problem

* Add gateway log tracker to OP

* Fix lint error

* Update readme
  • Loading branch information
makoto authored Feb 2, 2024
1 parent 7cc97a6 commit 5f93840
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 30 deletions.
7 changes: 4 additions & 3 deletions arb-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ cd arb-gateway
npm install -g wrangler
wrngler login
wrangler secret put L1_PROVIDER_URL
wrangler secret put L2_PROVIDER_URL
wrangler secret put L2_ROLLUP
wrangler secret put L1_PROVIDER_URL --env sepolia
wrangler secret put L2_PROVIDER_URL --env sepolia
wrangler secret put L2_ROLLUP --env sepolia
wrangler secret put ENDPOINT_URL --env sepolia
yarn deploy --env sepolia
```

Expand Down
3 changes: 2 additions & 1 deletion arb-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@
},
"dependencies": {
"@chainlink/ccip-read-server": "^0.2.1",
"@cloudflare/workers-types": "^4.20240117.0",
"@ensdomains/evm-gateway": "^0.1.0",
"@ensdomains/server-analytics": "0.0.1-alpha.2",
"@ethereumjs/block": "^5.0.0",
"@nomicfoundation/ethereumjs-block": "^5.0.2",
"commander": "^11.0.0",
"ethers": "^6.7.1"
},
"devDependencies": {
"@commander-js/extra-typings": "^11.0.0"

}
}
79 changes: 73 additions & 6 deletions arb-gateway/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
import { Request as CFWRequest } from '@cloudflare/workers-types';
import { Server } from '@ensdomains/ccip-read-cf-worker';
import type { Router } from '@ensdomains/evm-gateway';
import { InMemoryBlockCache } from './blockCache/InMemoryBlockCache.js';
import { Tracker } from '@ensdomains/server-analytics';
interface Env {
L1_PROVIDER_URL: string;
L2_PROVIDER_URL: string;
L2_ROLLUP: string;
GATEWAY_DOMAIN: string;
ENDPOINT_URL: string;
}
interface LogResult {
(request: CFWRequest, result: Response): Promise<Response>;
}

let app: Router, logResult: LogResult;

const decodeUrl = (url: string) => {
const trackingData = url.match(
/\/0x[a-fA-F0-9]{40}\/0x[a-fA-F0-9]{1,}\.json/
);
if (trackingData) {
return {
sender: trackingData[0].slice(1, 42),
calldata: trackingData[0].slice(44).replace('.json', ''),
};
} else {
return {};
}
};

async function fetch(request: CFWRequest, env: Env) {
// Set PROVIDER_URL under .dev.vars locally. Set the key as secret remotely with `wrangler secret put WORKER_PROVIDER_URL`
const {
L1_PROVIDER_URL,
L2_PROVIDER_URL,
L2_ROLLUP,
GATEWAY_DOMAIN,
ENDPOINT_URL,
} = env;
const tracker = new Tracker(GATEWAY_DOMAIN, {
apiEndpoint: ENDPOINT_URL,
enableLogging: true,
});

let app: Router;
async function fetch(request: Request, env: Env) {
// Loading libraries dynamically as a temp work around.
// Otherwise, deployment thorws "Error: Script startup exceeded CPU time limit." error
if (!app) {
Expand All @@ -17,13 +52,37 @@ async function fetch(request: Request, env: Env) {
const EVMGateway = (await import('@ensdomains/evm-gateway')).EVMGateway;
const ArbProofService = (await import('./ArbProofService.js'))
.ArbProofService;
// Set PROVIDER_URL under .dev.vars locally. Set the key as secret remotely with `wrangler secret put WORKER_PROVIDER_URL`
const { L1_PROVIDER_URL, L2_PROVIDER_URL, L2_ROLLUP } = env;

const l1Provider = new ethers.JsonRpcProvider(L1_PROVIDER_URL);
const l2Provider = new ethers.JsonRpcProvider(L2_PROVIDER_URL);

console.log({ L1_PROVIDER_URL, L2_PROVIDER_URL });
logResult = async (
request: CFWRequest,
result: Response
): Promise<Response> => {
if (request.url.match(/favicon/)) {
return result;
}
if (!result.body) {
return result;
}
const [streamForLog, streamForResult] = result.body.tee();
const logResultData = (
await new Response(streamForLog).json()
).data.substring(0, 200);
const props = decodeUrl(request.url);
await tracker.trackEvent(
request,
'result',
{ props: { ...props, result: logResultData } },
true
);
const myHeaders = new Headers();
myHeaders.set('Access-Control-Allow-Origin', '*');
myHeaders.set('Access-Control-Allow-Methods', 'GET,HEAD,POST,OPTIONS');
myHeaders.set('Access-Control-Max-Age', '86400');
return new Response(streamForResult, { ...result, headers: myHeaders });
};
const gateway = new EVMGateway(
new ArbProofService(
l1Provider,
Expand All @@ -37,7 +96,15 @@ async function fetch(request: Request, env: Env) {
gateway.add(server);
app = server.makeApp('/');
}
return app.handle(request);

const props = decodeUrl(request.url);
await tracker.trackEvent(
request,
'request',
{ props: { ...props, ...{} } },
true
);
return app.handle(request).then(logResult.bind(null, request));
}

export default {
Expand Down
4 changes: 3 additions & 1 deletion arb-gateway/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "arb-gateway-worker"
account_id = "15dcc9085cb794bb4f29d3e8177ac880"
main = "./src/worker.ts"
node_compat = true
compatibility_date = "2023-10-13"
compatibility_date = "2024-01-25"


[dev]
Expand All @@ -13,3 +13,5 @@ command = "yarn build"

[env.sepolia]
name = "arb-sepolia-gateway-worker"
[env.sepolia.vars]
GATEWAY_DOMAIN="arb-sepolia-gateway-worker.ens-cf.workers.dev"
2 changes: 2 additions & 0 deletions arb-verifier/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const config: HardhatUserConfig = {
url: L1_PROVIDER_URL,
accounts: [DEPLOYER_PRIVATE_KEY],
deploy: ['deploy_l1/'],
chainId:11155111,
companionNetworks: {
l2: 'arbitrumSepolia',
},
Expand All @@ -52,6 +53,7 @@ const config: HardhatUserConfig = {
deploy: ['deploy_l2/'],
},
arbitrumSepolia: {
chainId: 421614,
url: 'https://sepolia-rollup.arbitrum.io/rpc',
accounts: [DEPLOYER_PRIVATE_KEY],
deploy: [ "deploy_l2/" ],
Expand Down
8 changes: 5 additions & 3 deletions arb-verifier/test/testArbVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ describe('ArbVerifier', () => {
const rollupAddress = process.env.ROLLUP_ADDRESS;
// When testing against Goerli, replace with this address
// const rollupAddress = '0x45e5cAea8768F42B385A366D3551Ad1e0cbFAb17';

const chainId = hre.network.config.chainId
console.log(hre.network.config)
console.log({chainId})
const gateway = await makeArbGateway(
(hre.network.config as any).url,
(hre.network.config as any).url,
(hre.config.networks[hre.network.companionNetworks.l2] as any).url,
rollupAddress,
5,
chainId,
);
const server = new Server()
gateway.add(server)
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion crosschain-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"dependencies": {
"@ensdomains/address-encoder": "^0.2.22",
"@ensdomains/ens-contracts": "ensdomains/ens-contracts#l2-deployment",
"@ensdomains/ens-contracts": "ensdomains/ens-contracts#feature/crosschain-resolver-with-reverse-registrar",
"@ensdomains/evm-verifier": "^0.1.0",
"@eth-optimism/contracts": "^0.6.0"
}
Expand Down
2 changes: 1 addition & 1 deletion crosschain-reverse-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@ensdomains/ens-contracts": "ensdomains/ens-contracts#l2-deployment",
"@ensdomains/ens-contracts": "ensdomains/ens-contracts#feature/crosschain-resolver-with-reverse-registrar",
"@ensdomains/evm-verifier": "^0.1.0",
"@eth-optimism/contracts": "^0.6.0"
}
Expand Down
1 change: 1 addition & 0 deletions evm-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module": "./_esm/index.js",
"dependencies": {
"@chainlink/ccip-read-server": "^0.2.1",
"@cloudflare/workers-types": "^4.20240117.0",
"@ensdomains/ccip-read-cf-worker": "^0.0.1",
"ethers": "^6.7.1"
},
Expand Down
4 changes: 3 additions & 1 deletion evm-gateway/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Request as CFWRequest } from '@cloudflare/workers-types';

export interface Router {
handle: (request: Request) => void;
handle: (request: CFWRequest) => Promise<Response>;
}
3 changes: 2 additions & 1 deletion l1-gateway/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Request as CFWRequest } from '@cloudflare/workers-types';
import { Server } from '@ensdomains/ccip-read-cf-worker';
import type { Router } from '@ensdomains/evm-gateway';
interface Env {
WORKER_PROVIDER_URL: string;
}
let app: Router;
async function fetch(request: Request, env: Env) {
async function fetch(request: CFWRequest, env: Env) {
// Loading libraries dynamically as a temp work around.
// Otherwise, deployment thorws "Error: Script startup exceeded CPU time limit." error
if (!app) {
Expand Down
11 changes: 6 additions & 5 deletions op-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ yarn dev
cd op-gateway
npm install -g wrangler
wrngler login
wrangler secret put L1_PROVIDER_URL
wrangler secret put L2_PROVIDER_URL
wrangler secret put L2_OUTPUT_ORACLE
wrangler secret put DELAY
yarn deploy --env op-sepolia // or --env base-sepolia for base deployment
wrangler secret put L1_PROVIDER_URL --env op-sepolia|base-sepolia
wrangler secret put L2_PROVIDER_URL --env op-sepolia|base-sepolia
wrangler secret put L2_OUTPUT_ORACLE --env op-sepolia|base-sepolia
wrangler secret put DELAY --env op-sepolia|base-sepolia
wrangler secret put ENDPOINT_URL --env op-sepolia|base-sepolia
yarn deploy --env op-sepolia|base-sepolia
```

## How to test
Expand Down
82 changes: 77 additions & 5 deletions op-gateway/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,88 @@
import { Request as CFWRequest } from '@cloudflare/workers-types';
import { Server } from '@ensdomains/ccip-read-cf-worker';
import type { Router } from '@ensdomains/evm-gateway';
import { Tracker } from '@ensdomains/server-analytics';

interface Env {
L1_PROVIDER_URL: string;
L2_PROVIDER_URL: string;
L2_OUTPUT_ORACLE: string;
DELAY: number;
GATEWAY_DOMAIN: string;
ENDPOINT_URL: string;
}
interface LogResult {
(request: CFWRequest, result: Response): Promise<Response>;
}
let app: Router;
async function fetch(request: Request, env: Env) {

let app: Router, logResult: LogResult;
const decodeUrl = (url: string) => {
const trackingData = url.match(
/\/0x[a-fA-F0-9]{40}\/0x[a-fA-F0-9]{1,}\.json/
);
if (trackingData) {
return {
sender: trackingData[0].slice(1, 42),
calldata: trackingData[0].slice(44).replace('.json', ''),
};
} else {
return {};
}
};

async function fetch(request: CFWRequest, env: Env) {
// Set PROVIDER_URL under .dev.vars locally. Set the key as secret remotely with `wrangler secret put WORKER_PROVIDER_URL`
const {
L1_PROVIDER_URL,
L2_PROVIDER_URL,
L2_OUTPUT_ORACLE,
DELAY,
GATEWAY_DOMAIN,
ENDPOINT_URL,
} = env;

const tracker = new Tracker(GATEWAY_DOMAIN, {
apiEndpoint: ENDPOINT_URL,
enableLogging: true,
});

// Loading libraries dynamically as a temp work around.
// Otherwise, deployment thorws "Error: Script startup exceeded CPU time limit." error
if (!app) {
const ethers = await import('ethers');
const EVMGateway = (await import('@ensdomains/evm-gateway')).EVMGateway;
const OPProofService = (await import('./OPProofService.js')).OPProofService;
// Set PROVIDER_URL under .dev.vars locally. Set the key as secret remotely with `wrangler secret put WORKER_PROVIDER_URL`
const { L1_PROVIDER_URL, L2_PROVIDER_URL, L2_OUTPUT_ORACLE, DELAY } = env;
const l1Provider = new ethers.JsonRpcProvider(L1_PROVIDER_URL);
const l2Provider = new ethers.JsonRpcProvider(L2_PROVIDER_URL);

logResult = async (
request: CFWRequest,
result: Response
): Promise<Response> => {
if (request.url.match(/favicon/)) {
return result;
}
if (!result.body) {
return result;
}
const [streamForLog, streamForResult] = result.body.tee();
const logResultData = (
await new Response(streamForLog).json()
).data.substring(0, 200);
const props = decodeUrl(request.url);
await tracker.trackEvent(
request,
'result',
{ props: { ...props, result: logResultData } },
true
);
const myHeaders = new Headers();
myHeaders.set('Access-Control-Allow-Origin', '*');
myHeaders.set('Access-Control-Allow-Methods', 'GET,HEAD,POST,OPTIONS');
myHeaders.set('Access-Control-Max-Age', '86400');
return new Response(streamForResult, { ...result, headers: myHeaders });
};

console.log({ L1_PROVIDER_URL, L2_PROVIDER_URL, DELAY });
const gateway = new EVMGateway(
new OPProofService(
Expand All @@ -33,7 +97,15 @@ async function fetch(request: Request, env: Env) {
gateway.add(server);
app = server.makeApp('/');
}
return app.handle(request);
const props = decodeUrl(request.url);
await tracker.trackEvent(
request,
'request',
{ props: { ...props, ...{} } },
true
);

return app.handle(request).then(logResult.bind(null, request));
}

export default {
Expand Down
5 changes: 4 additions & 1 deletion op-gateway/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ command = "yarn build"

[env.base-sepolia]
name = "base-sepolia-gateway-worker"

[env.base-sepolia.vars]
GATEWAY_DOMAIN="base-sepolia-gateway-worker.ens-cf.workers.dev"
[env.op-sepolia]
name = "op-sepolia-gateway-worker"
[env.op-sepolia.vars]
GATEWAY_DOMAIN="op-sepolia-gateway-worker.ens-cf.workers.dev"
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"ES2022", // By using ES2022 we get access to the `.cause` property on `Error` instances.
"DOM" // For `btoa` and `atob`.
],

// Skip type checking for node modules
"skipLibCheck": true
}
Expand Down

0 comments on commit 5f93840

Please sign in to comment.