From ac76de43a1e409c94f990a0178e1189ba3e9e3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20Sj=C3=B6berg?= Date: Tue, 27 Apr 2021 18:07:07 +0200 Subject: [PATCH] Fix block polling --- blocks.ts | 46 ++++++++++++++++++++++++++++------------ frontend/pages/index.tsx | 4 ++-- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/blocks.ts b/blocks.ts index 759cc1a..f34cd29 100644 --- a/blocks.ts +++ b/blocks.ts @@ -8,18 +8,11 @@ export interface IBlock { signals: boolean | undefined; } -const blocks: IBlock[] = []; +let blocks: IBlock[] = []; -export async function bootstrapBlocks() { - console.log("Bootstrapping block data..."); - - const blockCount = await getblockcount(); - const difficultyPeriodStartHeight = blockCount - (blockCount % 2016); - const difficultyPeriodEndHeight = difficultyPeriodStartHeight + 2016; - - console.log(`Current block height is ${blockCount}`); - - for (let i = difficultyPeriodStartHeight; i < difficultyPeriodEndHeight; i++) { +async function setupPeriod(blockCount: number, startHeight: number, endHeight: number): Promise { + const blocks: IBlock[] = []; + for (let i = startHeight; i < endHeight; i++) { if (i > blockCount) { blocks.push({ height: i, @@ -41,15 +34,40 @@ export async function bootstrapBlocks() { } } + return blocks; +} + +export async function bootstrapBlocks() { + console.log("Bootstrapping block data..."); + + let blockCount = await getblockcount(); + const difficultyPeriodStartHeight = blockCount - (blockCount % 2016); + const difficultyPeriodEndHeight = difficultyPeriodStartHeight + 2016; + console.log(`Current block height is ${blockCount}`); + blocks = await setupPeriod(blockCount, difficultyPeriodStartHeight, difficultyPeriodEndHeight); + setInterval(async () => { - console.log("Checking for new blocks"); const newBlockCount = await getblockcount(); if (newBlockCount > blockCount) { - for (let i = blockCount; i < newBlockCount; i++) { - const blockHash = await getblockhash(i + 1); + console.log("Found new block"); + if (newBlockCount % 2016 === 0) { + blockCount = newBlockCount; + console.log("New block period!"); + const difficultyPeriodStartHeight = blockCount - (blockCount % 2016); + const difficultyPeriodEndHeight = difficultyPeriodStartHeight + 2016; + + console.log(`Current block height is ${blockCount}`); + blocks = await setupPeriod(blockCount, difficultyPeriodStartHeight, difficultyPeriodEndHeight); + return; + } + + for (let i = blockCount + 1; i <= newBlockCount; i++) { + const blockHash = await getblockhash(i); const blockheader = await getblockheader(blockHash); blocks[i % 2016].signals = (blockheader.version & (config.fork.versionBit + 1)) === config.fork.versionBit + 1; + console.log(`Block ${i % 2016} set`); } + blockCount = newBlockCount; } }, 10 * 1000); diff --git a/frontend/pages/index.tsx b/frontend/pages/index.tsx index 0a8450d..c86167e 100644 --- a/frontend/pages/index.tsx +++ b/frontend/pages/index.tsx @@ -39,14 +39,14 @@ const CurrentPeriod = styled.h2` font-size: 24px; margin-bottom: 10px; color: #ff9b20; - text-shadow: #000 3px 3px 0px; + text-shadow: #000 2px 2px 0px; `; const LockinInfo = styled.h2` font-size: 16px; margin-bottom: 10px; color: #ff9b20; - text-shadow: #000 3px 3px 0px; + text-shadow: #000 2px 2px 0px; `; export default function Blocks() {