File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -8,3 +8,4 @@ pageviews.json
88pageviews_txt.json
99lnurlpay.json
1010addinvoice_payload.json
11+ data
Original file line number Diff line number Diff 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
3139Follow the code style of the file you are working in.
Original file line number Diff line number Diff line change 11import { 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+
24import { getBlocks } from "../blocks/index.ts" ;
35
46export 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+ } ;
Original file line number Diff line number Diff line change 11import { 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" ;
44import { LnurlPayRequest , LnurlPayRequestCallback } from "./donate.ts" ;
55import { GetBlockchainInfo , GetBlockCount , GetBlockHash } from "./misc.ts" ;
66
77const router = new Router ( ) ;
88
99router . get ( "/blocks" , GetBlocks ) ;
1010
11+ router . get ( "/blocks/:blocks" , GetBlocksPeriod ) ;
12+
1113router . get ( "/invoice" , LnurlPayRequest ) ;
1214
1315router . get ( "/invoice/callback" , LnurlPayRequestCallback ) ;
Original file line number Diff line number Diff 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 [ ] = [ ] ;
Original file line number Diff line number Diff line change 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 ) ) ;
You can’t perform that action at this time.
0 commit comments