Browse Source

Fix block polling

refactor
Hampus Sjöberg 4 years ago
parent
commit
ac76de43a1
  1. 46
      blocks.ts
  2. 4
      frontend/pages/index.tsx

46
blocks.ts

@ -8,18 +8,11 @@ export interface IBlock {
signals: boolean | undefined; signals: boolean | undefined;
} }
const blocks: IBlock[] = []; let blocks: IBlock[] = [];
export async function bootstrapBlocks() { async function setupPeriod(blockCount: number, startHeight: number, endHeight: number): Promise<IBlock[]> {
console.log("Bootstrapping block data..."); const blocks: IBlock[] = [];
for (let i = startHeight; i < endHeight; i++) {
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++) {
if (i > blockCount) { if (i > blockCount) {
blocks.push({ blocks.push({
height: i, 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 () => { setInterval(async () => {
console.log("Checking for new blocks");
const newBlockCount = await getblockcount(); const newBlockCount = await getblockcount();
if (newBlockCount > blockCount) { if (newBlockCount > blockCount) {
for (let i = blockCount; i < newBlockCount; i++) { console.log("Found new block");
const blockHash = await getblockhash(i + 1); 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); const blockheader = await getblockheader(blockHash);
blocks[i % 2016].signals = (blockheader.version & (config.fork.versionBit + 1)) === config.fork.versionBit + 1; blocks[i % 2016].signals = (blockheader.version & (config.fork.versionBit + 1)) === config.fork.versionBit + 1;
console.log(`Block ${i % 2016} set`);
} }
blockCount = newBlockCount;
} }
}, 10 * 1000); }, 10 * 1000);

4
frontend/pages/index.tsx

@ -39,14 +39,14 @@ const CurrentPeriod = styled.h2`
font-size: 24px; font-size: 24px;
margin-bottom: 10px; margin-bottom: 10px;
color: #ff9b20; color: #ff9b20;
text-shadow: #000 3px 3px 0px; text-shadow: #000 2px 2px 0px;
`; `;
const LockinInfo = styled.h2` const LockinInfo = styled.h2`
font-size: 16px; font-size: 16px;
margin-bottom: 10px; margin-bottom: 10px;
color: #ff9b20; color: #ff9b20;
text-shadow: #000 3px 3px 0px; text-shadow: #000 2px 2px 0px;
`; `;
export default function Blocks() { export default function Blocks() {

Loading…
Cancel
Save