diff --git a/packages/backend/src/Chain.ts b/packages/backend/src/Chain.ts index c764313..2eed69e 100644 --- a/packages/backend/src/Chain.ts +++ b/packages/backend/src/Chain.ts @@ -16,7 +16,8 @@ export default class Chain { public height = 0 as Types.BlockNumber; public blockTimestamp = 0 as Types.Timestamp; - private blockTimes: Array = new Array(BLOCK_TIME_HISTORY); + private blockTimes: Array = new Array(BLOCK_TIME_HISTORY).fill(0); + private averageBlockTime: Maybe = null; constructor(label: Types.ChainLabel) { this.label = label; @@ -82,7 +83,7 @@ export default class Chain { const { height, blockTimestamp } = node; if (this.blockTimestamp) { - this.blockTimes[height % BLOCK_TIME_HISTORY] = blockTimestamp - this.blockTimestamp; + this.updateAverageBlockTime(height, blockTimestamp); } this.height = height; @@ -101,21 +102,20 @@ export default class Chain { console.log(`[${this.label}] ${node.name} imported ${node.height}, block time: ${node.blockTime / 1000}s, average: ${node.average / 1000}s | latency ${node.latency}`); } - private get averageBlockTime(): Maybe { + private updateAverageBlockTime(height: Types.BlockNumber, now: Types.Timestamp) { + this.blockTimes[height % BLOCK_TIME_HISTORY] = now - this.blockTimestamp; + let sum = 0; let count = 0; for (const time of this.blockTimes) { - if (time != null) { + if (time) { sum += time; count += 1; } } - if (count === 0) { - return null; - } - - return (sum / count) as Types.Milliseconds; + // We are guaranteed that count > 0 + this.averageBlockTime = (sum / count) as Types.Milliseconds; } } diff --git a/packages/frontend/src/components/Chain.css b/packages/frontend/src/components/Chain.css index e9ef150..e9a564b 100644 --- a/packages/frontend/src/components/Chain.css +++ b/packages/frontend/src/components/Chain.css @@ -6,13 +6,17 @@ color: #000; } -.Chain-content { +.Chain-content-container { position: absolute; left: 0; /*80px;*/ right: 0; bottom: 0; top: 148px; - min-height: 50vh; +} + +.Chain-content { + width: 100%; + min-height: 100%; background: #222; color: #fff; box-shadow: rgba(0,0,0,0.5) 0 3px 30px; diff --git a/packages/frontend/src/components/Chain.tsx b/packages/frontend/src/components/Chain.tsx index 9158ee4..20ef9c3 100644 --- a/packages/frontend/src/components/Chain.tsx +++ b/packages/frontend/src/components/Chain.tsx @@ -46,27 +46,29 @@ export function Chain(props: Chain.Props) { { blockAverage == null ? '-' : (blockAverage / 1000).toFixed(3) + 's' } -
- - - - - - - - - - - - - - - - { - nodes.map((node) => ) - } - -
+
+
+ + + + + + + + + + + + + + + + { + nodes.map((node) => ) + } + +
+
);