Skip to content

Commit

Permalink
refactored the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmpatel1997 committed Nov 16, 2023
1 parent 9c21684 commit acb592b
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 1,085 deletions.
4 changes: 4 additions & 0 deletions cf/latest-snapshot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This worker acts on two endpoints:
- `https://forest-archive.chainsafe.dev/latest/calibnet/`
- `https://forest-archive.chainsafe.dev/latest/mainnet/`

- `https://forest-archive.chainsafe.dev/archive/calibnet/*`
- `https://forest-archive.chainsafe.dev/archive/mainnet/*`
- `https://forest-archive.chainsafe.dev/archive/historical/*`

These links will download the latest available snapshot for calibnet and mainnet, respectively.

# Local deployment
Expand Down
101 changes: 72 additions & 29 deletions cf/latest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import parseRange from 'range-parser';
import {R2ObjectBody} from "@cloudflare/workers-types";

interface Env {
FOREST_ARCHIVE: R2Bucket;
Expand All @@ -8,24 +9,53 @@ function basename(path: string) {
return path.split('/').reverse()[0];
}

enum SnapshotType {
latest = 'latest',
archive = 'archive',
}

// Directly fetch the data for the latest snapshot of a given chain (eg. calibnet or mainnet)
async function get_latest(req_headers: Headers, env: Env, chain: string): Promise<Response> {
const listed = await env.FOREST_ARCHIVE.list({ prefix: chain + '/latest/' });
const latest = listed.objects.at(-1);
if (latest == null) {
return new Response(`No latest snapshot found ${chain}`, {
status: 404,
});
} else {
const object = await env.FOREST_ARCHIVE.get(latest.key, {
range: req_headers,
onlyIf: req_headers,
});
async function get_latest(req_headers: Headers, env: Env, path: string, type: SnapshotType): Promise<Response> {
let object: R2ObjectBody|null = null;

if (object === null) {
return new Response('No latest snapshot found', {
status: 404,
});
switch (type) {
case SnapshotType.latest: {
const listed = await env.FOREST_ARCHIVE.list({prefix: path + '/latest/'});
const latest = listed.objects.at(-1);
if (latest == undefined) {
return new Response(`No latest snapshot found ${path}`, {
status: 404,
});
}

object = await env.FOREST_ARCHIVE.get(latest.key, {
range: req_headers,
onlyIf: req_headers,
});
if (object === null) {
return new Response('No latest snapshot found', {
status: 404,
});
}
break;
}
case SnapshotType.archive: {
object = await env.FOREST_ARCHIVE.get(path, {
range: req_headers,
onlyIf: req_headers,
});
if (object === null) {
return new Response(`No archive snapshot found ${path}`, {
status: 404,
});
}
break;
}
default: {
return new Response('Invalid Snapshot Type. Only archive OR latest is supported', {
status: 400,
});
}
}

const headers = new Headers();
Expand Down Expand Up @@ -73,27 +103,40 @@ async function get_latest(req_headers: Headers, env: Env, chain: string): Promis
status,
});
}
}
}

export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
const chain = (url.pathname.match(/\/latest\/(\w*)/) || ['undefined'])[1];

// Disallow any other request method except HEAD and GET, they are not sensible in the context
// of fetching a snapshot.
switch (request.method) {
case 'HEAD':
case 'GET':
return await get_latest(request.headers, env, chain);
default:
return new Response('Method not allowed', {
status: 405,
headers: {
Allow: 'GET, HEAD',
},
case 'HEAD':{
break;
}
case 'GET':{
const url = new URL(request.url);
const path = url.pathname.match(/\/archive\/(\S*)/);
if (path != null && path.length > 1) {
return await get_latest(request.headers, env, path[1], SnapshotType.archive);
}

const chain = url.pathname.match(/\/latest\/(\w*)/);
if (chain != null && chain.length > 1) {
return await get_latest(request.headers, env, chain[1], SnapshotType.latest);
}

return new Response('path not found', {
status: 404,
});
}
default: {
return new Response('Method not allowed', {
status: 405,
headers: {
Allow: 'GET, HEAD',
},
});
}
}
},
};
6 changes: 6 additions & 0 deletions cf/latest-snapshot/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ main = "./src/index.ts"

compatibility_date = "2022-06-30"
logpush = true
node_compat = true

routes = [
{ pattern = "forest-archive.chainsafe.dev/latest/calibnet", zone_name = "chainsafe.dev" },
{ pattern = "forest-archive.chainsafe.dev/latest/mainnet", zone_name = "chainsafe.dev" },
{ pattern = "forest-archive.chainsafe.dev/latest/calibnet/", zone_name = "chainsafe.dev" },
{ pattern = "forest-archive.chainsafe.dev/latest/mainnet/", zone_name = "chainsafe.dev" },

# below endpoints are for the archive
{ pattern = "forest-archive.chainsafe.dev/archive/calibnet/*", zone_name = "chainsafe.dev" },
{ pattern = "forest-archive.chainsafe.dev/archive/mainnet/*", zone_name = "chainsafe.dev" },
{ pattern = "forest-archive.chainsafe.dev/archive/historical/*", zone_name = "chainsafe.dev" }
]

[[r2_buckets]]
Expand Down
13 changes: 0 additions & 13 deletions cf/r2-snapshot/.editorconfig

This file was deleted.

1 change: 0 additions & 1 deletion cf/r2-snapshot/.gitignore

This file was deleted.

19 changes: 0 additions & 19 deletions cf/r2-snapshot/.prettierrc

This file was deleted.

17 changes: 0 additions & 17 deletions cf/r2-snapshot/README.md

This file was deleted.

Loading

0 comments on commit acb592b

Please sign in to comment.