Skip to content

Commit abe91ca

Browse files
committed
Generate specific periods
1 parent ae8f50e commit abe91ca

7 files changed

Lines changed: 61 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pageviews.json
88
pageviews_txt.json
99
lnurlpay.json
1010
addinvoice_payload.json
11+
data

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ Set `mode: "fake"` or `mode: "fake-backend"` in your `config/config.ts`.
2626

2727
`fake-frontend`: Frontend will generate fake blocks, this mode works in conjuction with `./frontend-dev-server.sh`
2828

29+
## Generating block data from a specific difficulty period
30+
31+
Difficulty period is calculated as `floor([any block in the period])`.
32+
33+
`deno run --allow-net --allow-read --allow-write generate/index.ts [period number]`
34+
35+
Once generated, the data is available via API call `/blocks/[epoch number]` or `[project root]/data/periods/[epoch].json`.
36+
2937
## Commit and Code-Style
3038

3139
Follow the code style of the file you are working in.

backend/api/blocks.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
import { RouterMiddleware } from "https://deno.land/x/oak@v7.5.0/mod.ts";
2+
import { exists } from "https://deno.land/std@0.98.0/fs/mod.ts";
3+
24
import { getBlocks } from "../blocks/index.ts";
35

46
export const GetBlocks: RouterMiddleware = (context) => {
57
const blocks = getBlocks();
68
context.response.body = blocks;
79
};
10+
export const GetBlocksPeriod: RouterMiddleware = async (context) => {
11+
try {
12+
const period = Number.parseInt(context.params.blocks || "");
13+
if (Number.isNaN(period)) {
14+
context.response.status = 404;
15+
context.response.body = "404 File Not Found";
16+
return;
17+
}
18+
19+
const path = Deno.cwd() + `/data/periods/${context.params.blocks}.json`;
20+
if (!(await exists(path))) {
21+
context.response.status = 404;
22+
context.response.body = "404 File Not Found";
23+
return;
24+
}
25+
26+
const blocks = await Deno.readFile(path);
27+
context.response.body = blocks;
28+
} catch (error) {
29+
console.log(error);
30+
context.response.status = 500;
31+
context.response.body = "Unknown error";
32+
}
33+
};

backend/api/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Router } from "https://deno.land/x/oak@v7.5.0/mod.ts";
22

3-
import { GetBlocks } from "./blocks.ts";
3+
import { GetBlocks, GetBlocksPeriod } from "./blocks.ts";
44
import { LnurlPayRequest, LnurlPayRequestCallback } from "./donate.ts";
55
import { GetBlockchainInfo, GetBlockCount, GetBlockHash } from "./misc.ts";
66

77
const router = new Router();
88

99
router.get("/blocks", GetBlocks);
1010

11+
router.get("/blocks/:blocks", GetBlocksPeriod);
12+
1113
router.get("/invoice", LnurlPayRequest);
1214

1315
router.get("/invoice/callback", LnurlPayRequestCallback);

backend/blocks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function createRealBlock(height: number): Promise<IBlock> {
4040
};
4141
}
4242

43-
async function setupPeriod(blockCount: number, startHeight: number, endHeight: number): Promise<IBlock[]> {
43+
export async function setupPeriod(blockCount: number, startHeight: number, endHeight: number): Promise<IBlock[]> {
4444
const createBlock = config.mode === "real" ? createRealBlock : createFakeBlock;
4545

4646
const blocks: IBlock[] = [];

data/periods/.gitkeep

Whitespace-only changes.

generate/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { setupPeriod } from "../backend/blocks/index.ts";
2+
3+
const periodString = Deno.args[0];
4+
5+
if (!periodString) {
6+
console.log("Usage: [period]");
7+
Deno.exit(0);
8+
}
9+
10+
const period = Number.parseInt(periodString);
11+
12+
if (Number.isNaN(period)) {
13+
console.log("Error: Invalid period: " + periodString + " (evaluated as " + period + ")");
14+
Deno.exit(1);
15+
}
16+
17+
const difficultyPeriodStartHeight = period * 2016;
18+
const difficultyPeriodEndHeight = difficultyPeriodStartHeight + 2016;
19+
20+
const blocks = await setupPeriod(period * 2016 + 0, difficultyPeriodStartHeight, difficultyPeriodEndHeight);
21+
console.log(Deno.cwd());
22+
await Deno.writeTextFile(Deno.cwd() + `/data/periods/${period}.json`, JSON.stringify(blocks));

0 commit comments

Comments
 (0)