-
Notifications
You must be signed in to change notification settings - Fork 12
/
auto_update.ts
123 lines (118 loc) · 3.84 KB
/
auto_update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import Template from "https://deno.land/x/template@v0.1.0/mod.ts";
// date format: YYYYMMDD, e.g. 20231102
function getSnapshotURL(
network:
| "mainnet"
| "mainnet_prune"
| "mainnet_pbss"
| "testnet"
| "testnet_pbss",
date: Date
): string {
const dateString = date.toISOString().substr(0, 10).replace(/-/g, "");
if (network === "mainnet") {
return `https://opbnb-snapshot-mainnet.bnbchain.org/geth-${dateString}.tar.gz`;
} else if (network === "mainnet_prune") {
return `https://opbnb-snapshot-mainnet.bnbchain.org/geth-prune-${dateString}.tar.gz`;
} else if (network === "mainnet_pbss") {
return `https://opbnb-snapshot-mainnet.bnbchain.org/geth-pbss-${dateString}.tar.gz`;
} else if (network === "testnet_pbss") {
return `https://opbnb-snapshot-testnet.bnbchain.org/geth-pbss-${dateString}.tar.gz`;
} else if (network === "testnet") {
return `https://opbnb-snapshot-testnet.bnbchain.org/geth-${dateString}.tar.gz`;
} else {
throw new Error("invalid network");
}
}
async function getLatestSnapshot(
network:
| "mainnet"
| "mainnet_prune"
| "mainnet_pbss"
| "testnet"
| "testnet_pbss"
) {
const date = new Date();
let url = "";
let found = false;
const days = 7;
for (let i = 0; i < days; i++) {
url = getSnapshotURL(network, date);
const resp = await fetch(url);
console.log(url, resp.status);
if (resp.status === 200) {
found = true;
break;
}
date.setDate(date.getDate() - 1);
}
if (!found) {
throw new Error(`no snapshot found in latest ${days} days`);
}
// check latest url
const latestURL = getLatestSnapshotURL(network);
const latestURLRes = await fetch(latestURL);
const content = (await latestURLRes.text()).trim();
const urlPostfix = url.split("/").pop();
if (content !== urlPostfix) {
throw new Error(
`latest snapshot url is not the latest, latest: ${content}, expected: ${urlPostfix}`
);
}
// check sha256 checksum
const sha256URL = `${url}.sha256`;
const sha256Res = await fetch(sha256URL);
const sha256Text = await sha256Res.text();
const sha256 = sha256Text.split(" ")[0];
const data = {
url,
sha256,
};
console.dir({ network, data });
return data;
}
function getLatestSnapshotURL(
network:
| "mainnet"
| "mainnet_prune"
| "mainnet_pbss"
| "testnet"
| "testnet_pbss"
) {
if (network === "mainnet") {
return `https://opbnb-snapshot-mainnet.bnbchain.org/geth-latest`;
} else if (network === "mainnet_prune") {
return `https://opbnb-snapshot-mainnet.bnbchain.org/geth-prune-latest`;
} else if (network === "mainnet_pbss") {
return `https://opbnb-snapshot-mainnet.bnbchain.org/geth-pbss-latest`;
} else if (network === "testnet_pbss") {
return `https://opbnb-snapshot-testnet.bnbchain.org/geth-pbss-latest`;
} else if (network === "testnet") {
return `https://opbnb-snapshot-testnet.bnbchain.org/geth-latest`;
} else {
throw new Error("invalid network");
}
}
async function main() {
// const mainnetLatestSnapshotURL = await getLatestSnapshotURL("mainnet");
// const testnetLatestSnapshotURL = await getLatestSnapshot("testnet");
const testnetPbssLatestSnapshotURL = await getLatestSnapshot("testnet_pbss");
const mainnetPbssLatestSnapshotURL = await getLatestSnapshot("mainnet_pbss");
const mainnetPruneLatestSnapshotURL = await getLatestSnapshot(
"mainnet_prune"
);
const data = {
mainnetPrune: mainnetPruneLatestSnapshotURL,
mainnetPbss: mainnetPbssLatestSnapshotURL,
// testnet: testnetLatestSnapshotURL,
testnetPbss: testnetPbssLatestSnapshotURL,
updatedAt: new Date().toISOString(),
};
console.log(data);
// Render a template
const tpl = new Template();
const result = await tpl.renderFile("./README.md.template", data);
console.log(result);
await Deno.writeTextFile("./README.md", result);
}
await main();