|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * 1. Verifies src/assets/snapshots.json still matches the `snapshots` arrays in |
| 4 | + * src/components/claim-modal.js, and fails the build if they have drifted. |
| 5 | + * 2. Copies the manifest to public/snapshots.json so the standalone |
| 6 | + * staking-rewards.html page can read it same-origin (files under public/ are |
| 7 | + * copied verbatim by the build and cannot import from src/). |
| 8 | + * |
| 9 | + * The arrays in claim-modal.js are duplicated here on purpose, and only |
| 10 | + * temporarily: raw.githubusercontent.com/kleros/court/master/src/components/claim-modal.js |
| 11 | + * is scraped by klerosboard and proof-of-humanity-v2-web to locate the monthly |
| 12 | + * reward snapshots. Once those repos point at snapshots.json instead, delete the |
| 13 | + * arrays from claim-modal.js, have it import this manifest, and drop assertParity(). |
| 14 | + * |
| 15 | + * The array index of each snapshot is the on-chain `week` argument to |
| 16 | + * MerkleRedeem.claimWeek, so order and length must never change. |
| 17 | + */ |
| 18 | +const fs = require("fs"); |
| 19 | +const path = require("path"); |
| 20 | + |
| 21 | +const root = path.join(__dirname, ".."); |
| 22 | +const source = path.join(root, "src", "assets", "snapshots.json"); |
| 23 | +const claimModal = path.join(root, "src", "components", "claim-modal.js"); |
| 24 | +const destination = path.join(root, "public", "snapshots.json"); |
| 25 | + |
| 26 | +// Every chain the reward snapshots cover, declared once. To support a new chain, add one entry |
| 27 | +// here (with its filename prefix); CHAIN_IDS and the prefix lookup derive from it, so the set of |
| 28 | +// chains and their prefixes can never drift apart. |
| 29 | +const CHAINS = { |
| 30 | + 1: { filenamePrefix: "snapshot-" }, |
| 31 | + 100: { filenamePrefix: "xdai-snapshot-" }, |
| 32 | +}; |
| 33 | +const CHAIN_IDS = Object.keys(CHAINS); |
| 34 | +const ENTRY = /^[A-Za-z0-9]+\/[A-Za-z0-9._-]+\.json$/; |
| 35 | + |
| 36 | +// rewards.js derives the IPFS url, the month and the chain from these strings alone, so a |
| 37 | +// malformed entry there is a wrong reward figure at runtime rather than an error. |
| 38 | +function assertEntriesAreWellFormed(entries, chainId) { |
| 39 | + for (const entry of entries) { |
| 40 | + if (!ENTRY.test(entry)) { |
| 41 | + throw new Error(`snapshots.json: chain ${chainId} entry "${entry}" is not of the form "<cid>/<filename>.json"`); |
| 42 | + } |
| 43 | + const filename = entry.split("/")[1]; |
| 44 | + const { filenamePrefix } = CHAINS[chainId]; |
| 45 | + if (!filename.startsWith(filenamePrefix)) { |
| 46 | + throw new Error( |
| 47 | + `snapshots.json: chain ${chainId} entry "${entry}" must start with "${filenamePrefix}" ` + |
| 48 | + `— rewards.js tells the chains apart by that prefix.` |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// A month published for one chain but not the other would make getSnapshotRewardAndSupply() |
| 55 | +// sum an incomplete reward set. Snapshots are appended, so only the newest month can be partial. |
| 56 | +function assertNewestMonthIsOnEveryChain(snapshots) { |
| 57 | + const newestMonth = (chainId) => { |
| 58 | + const entries = snapshots[chainId]; |
| 59 | + const newest = entries[entries.length - 1]; |
| 60 | + const month = (newest.match(/(\d{4}-\d{2})/) || [])[1]; |
| 61 | + if (!month) { |
| 62 | + throw new Error( |
| 63 | + `snapshots.json: chain ${chainId}'s newest entry "${newest}" has no YYYY-MM, ` + |
| 64 | + `so it can't be checked against the other chains.` |
| 65 | + ); |
| 66 | + } |
| 67 | + return month; |
| 68 | + }; |
| 69 | + const [referenceChain, ...otherChains] = CHAIN_IDS; |
| 70 | + const referenceMonth = newestMonth(referenceChain); |
| 71 | + for (const chainId of otherChains) { |
| 72 | + const month = newestMonth(chainId); |
| 73 | + if (month !== referenceMonth) { |
| 74 | + throw new Error( |
| 75 | + `snapshots.json: newest month differs per chain ` + |
| 76 | + `(chain ${referenceChain} ${referenceMonth}, chain ${chainId} ${month}). ` + |
| 77 | + `Add the missing month, or the monthly reward total will be missing a chain.` |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Read the `snapshots: [...]` array literal for a chain out of claim-modal.js. This is coupled to |
| 84 | +// that file's current indentation; reformatting it will throw here rather than pass silently. |
| 85 | +function readClaimModalSnapshots(code, chainId) { |
| 86 | + const block = code.match(new RegExp(`\\n ${chainId}: \\{[\\s\\S]*?snapshots: \\[([\\s\\S]*?)\\n \\]`)); |
| 87 | + if (!block) throw new Error(`claim-modal.js: could not find the snapshots array for chain ${chainId}`); |
| 88 | + return Array.from(block[1].matchAll(/"([^"]+)"/g), (match) => match[1]); |
| 89 | +} |
| 90 | + |
| 91 | +function assertParity(snapshots) { |
| 92 | + const code = fs.readFileSync(claimModal, "utf8"); |
| 93 | + for (const chainId of CHAIN_IDS) { |
| 94 | + const expected = readClaimModalSnapshots(code, chainId); |
| 95 | + const actual = snapshots[chainId]; |
| 96 | + const drifted = expected.length !== actual.length || expected.some((entry, i) => entry !== actual[i]); |
| 97 | + if (drifted) { |
| 98 | + throw new Error( |
| 99 | + `snapshots.json and claim-modal.js disagree for chain ${chainId} ` + |
| 100 | + `(${actual.length} vs ${expected.length} entries). Update both, keeping the order identical.` |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +const snapshots = JSON.parse(fs.readFileSync(source, "utf8")); |
| 107 | + |
| 108 | +// Everything below is verified per chain in CHAIN_IDS, but the whole object is written out. Reject |
| 109 | +// any other chain so nothing reaches public/snapshots.json without being validated first. |
| 110 | +const unverifiedChains = Object.keys(snapshots).filter((chainId) => !CHAIN_IDS.includes(chainId)); |
| 111 | +if (unverifiedChains.length > 0) { |
| 112 | + throw new Error( |
| 113 | + `snapshots.json: chain(s) ${unverifiedChains.join(", ")} are not verified. Add them to CHAINS ` + |
| 114 | + `(and claim-modal.js) so they are validated before being published.` |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +for (const chainId of CHAIN_IDS) { |
| 119 | + if (!Array.isArray(snapshots[chainId]) || snapshots[chainId].length === 0) { |
| 120 | + throw new Error(`snapshots.json is missing snapshots for chain ${chainId}`); |
| 121 | + } |
| 122 | + assertEntriesAreWellFormed(snapshots[chainId], chainId); |
| 123 | +} |
| 124 | +assertNewestMonthIsOnEveryChain(snapshots); |
| 125 | +assertParity(snapshots); |
| 126 | + |
| 127 | +fs.writeFileSync(destination, JSON.stringify(snapshots) + "\n"); |
0 commit comments