-
Notifications
You must be signed in to change notification settings - Fork 511
Added components for difficulty adjustment #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Randy808
wants to merge
1
commit into
Blockstream:master
Choose a base branch
from
Randy808:difficulty-adjustment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const activeNetwork = process.env.MENU_ACTIVE || ""; | ||
|
|
||
| export const isBitcoinNetwork = activeNetwork | ||
| .toLowerCase() | ||
| .startsWith("bitcoin"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| import { ArrowsInSimpleIcon } from "../components/icons"; | ||
| import { InfoCard } from "../components/info-card"; | ||
|
|
||
| const DIFFICULTY_PERIOD = 2016; | ||
| const TARGET_BLOCK_SECONDS = 10 * 60; | ||
| const HASHES_PER_DIFFICULTY = 2 ** 32; | ||
|
|
||
| const HASHRATE_UNITS = [ | ||
| [1e24, "YH/s", "Yottahashes per second"], | ||
| [1e21, "ZH/s", "Zettahashes per second"], | ||
| [1e18, "EH/s", "Exahashes per second"], | ||
| [1e15, "PH/s", "Petahashes per second"], | ||
| [1e12, "TH/s", "Terahashes per second"], | ||
| [1e9, "GH/s", "Gigahashes per second"], | ||
| [1e6, "MH/s", "Megahashes per second"], | ||
| [1e3, "kH/s", "Kilohashes per second"], | ||
| [1, "H/s", "Hashes per second"], | ||
| ]; | ||
|
|
||
| const DIFFICULTY_UNITS = [ | ||
| [1e15, "Q"], | ||
| [1e12, "T"], | ||
| [1e9, "B"], | ||
| [1e6, "M"], | ||
| [1e3, "K"], | ||
| [1, ""], | ||
| ]; | ||
|
|
||
| const formatAdjustment = (value) => { | ||
| if (!Number.isFinite(value)) return "N/A"; | ||
| if (value === 0) return "0.00%"; | ||
|
|
||
| return `${value > 0 ? "+" : ""}${value.toFixed(2)}%`; | ||
| }; | ||
|
|
||
| const adjustmentClass = (value) => | ||
| value > 0 ? "success" : value < 0 ? "danger" : ""; | ||
|
|
||
| const getEpochTiming = (latestBlock, epochStartBlock) => { | ||
| const epochStartHeight = | ||
| latestBlock && Number.isFinite(latestBlock.height) | ||
| ? latestBlock.height - (latestBlock.height % DIFFICULTY_PERIOD) | ||
| : null; | ||
|
|
||
| if ( | ||
| !latestBlock || | ||
| !epochStartBlock || | ||
| epochStartBlock.requestedHeight !== epochStartHeight || | ||
| !Number.isFinite(epochStartBlock.height) || | ||
| !Number.isFinite(latestBlock.timestamp) || | ||
| !Number.isFinite(epochStartBlock.timestamp) | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| const blocksMined = latestBlock.height - epochStartBlock.height; | ||
| const actualSeconds = latestBlock.timestamp - epochStartBlock.timestamp; | ||
|
|
||
| if (blocksMined < 0 || actualSeconds < 0) return null; | ||
|
|
||
| const averageBlockSeconds = blocksMined | ||
| ? Math.max(actualSeconds, 1) / blocksMined | ||
| : TARGET_BLOCK_SECONDS; | ||
| const blocksUntilAdjustment = | ||
| DIFFICULTY_PERIOD - (latestBlock.height % DIFFICULTY_PERIOD); | ||
| const secondsUntilAdjustment = | ||
| blocksUntilAdjustment * averageBlockSeconds; | ||
|
|
||
| return { | ||
| averageBlockSeconds, | ||
| estimatedAdjustmentTimestamp: | ||
| latestBlock.timestamp + secondsUntilAdjustment, | ||
| }; | ||
| }; | ||
|
|
||
| const expectedAdjustment = (epochTiming) => | ||
| epochTiming | ||
| ? (TARGET_BLOCK_SECONDS / epochTiming.averageBlockSeconds - 1) * 100 | ||
| : null; | ||
|
|
||
| const previousAdjustment = (latestBlock, previousBlock) => { | ||
| if ( | ||
| !latestBlock || | ||
| !previousBlock || | ||
| previousBlock.requestedHeight !== latestBlock.height - DIFFICULTY_PERIOD || | ||
| !Number.isFinite(latestBlock.difficulty) || | ||
| !Number.isFinite(previousBlock.difficulty) || | ||
| previousBlock.difficulty === 0 | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| return (latestBlock.difficulty / previousBlock.difficulty - 1) * 100; | ||
| }; | ||
|
|
||
| const formatHashrate = (difficulty, averageBlockSeconds) => { | ||
| if ( | ||
| !Number.isFinite(difficulty) || | ||
| difficulty < 0 || | ||
| !Number.isFinite(averageBlockSeconds) || | ||
| averageBlockSeconds <= 0 | ||
| ) { | ||
| return { value: "N/A", footer: "Hashes per second" }; | ||
| } | ||
|
|
||
| const hashrate = (difficulty * HASHES_PER_DIFFICULTY) / averageBlockSeconds; | ||
| const unit = | ||
| HASHRATE_UNITS.find(([threshold]) => hashrate >= threshold) || | ||
| HASHRATE_UNITS[HASHRATE_UNITS.length - 1]; | ||
| const [divisor, symbol, footer] = unit; | ||
| const value = (hashrate / divisor).toLocaleString("en-US", { | ||
| maximumSignificantDigits: 3, | ||
| }); | ||
|
|
||
| return { value: `${value} ${symbol}`, footer }; | ||
| }; | ||
|
|
||
| const formatDifficulty = (difficulty) => { | ||
| if (!Number.isFinite(difficulty) || difficulty < 0) return "N/A"; | ||
|
|
||
| const unit = | ||
| DIFFICULTY_UNITS.find(([threshold]) => difficulty >= threshold) || | ||
| DIFFICULTY_UNITS[DIFFICULTY_UNITS.length - 1]; | ||
| const [divisor, suffix] = unit; | ||
| const scaledDifficulty = difficulty / divisor; | ||
|
|
||
| if (scaledDifficulty < 0.01 && scaledDifficulty !== 0) { | ||
| return scaledDifficulty.toLocaleString("en-US", { | ||
| maximumSignificantDigits: 3, | ||
| }); | ||
| } | ||
|
|
||
| return `${scaledDifficulty.toLocaleString("en-US", { | ||
| minimumFractionDigits: 2, | ||
| maximumFractionDigits: 2, | ||
| })}${suffix}`; | ||
| }; | ||
|
|
||
| const formatAdjustmentDate = (timestamp) => { | ||
| if (!Number.isFinite(timestamp)) return "N/A"; | ||
|
|
||
| const date = new Date(timestamp * 1000); | ||
| const month = date.toLocaleString("en-US", { month: "long" }); | ||
| const day = date.getDate(); | ||
| const minute = String(date.getMinutes()).padStart(2, "0"); | ||
| const period = date.getHours() >= 12 ? "pm" : "am"; | ||
| const hour = String(date.getHours() % 12 || 12).padStart(2, "0"); | ||
|
|
||
| return `${month} ${day} - ${hour}:${minute} ${period}`; | ||
| }; | ||
|
|
||
| const formatTimeUntil = (timestamp) => { | ||
| if (!Number.isFinite(timestamp)) return "N/A"; | ||
|
|
||
| const totalMinutes = Math.max( | ||
| 0, | ||
| Math.floor((timestamp * 1000 - Date.now()) / (60 * 1000)), | ||
| ); | ||
| const days = Math.floor(totalMinutes / (24 * 60)); | ||
| const hours = Math.floor((totalMinutes % (24 * 60)) / 60); | ||
| const minutes = totalMinutes % 60; | ||
|
|
||
| if (days >= 14) { | ||
| const weeks = Math.floor(days / 7); | ||
| const remainingDays = days % 7; | ||
| return remainingDays ? `${weeks}w ${remainingDays}d` : `${weeks}w`; | ||
| } | ||
|
|
||
| if (days) return `${days}d ${hours}h`; | ||
| if (hours) return `${hours}h ${minutes}m`; | ||
|
|
||
| return totalMinutes ? `${totalMinutes}m` : "< 1m"; | ||
| }; | ||
|
|
||
| const adjustmentStat = (title, value, className = "") => ( | ||
| <div className="difficulty-adjustment-stat"> | ||
| <p className="difficulty-adjustment-stat-title">{title}</p> | ||
| <p className={`difficulty-adjustment-stat-value ${className}`}>{value}</p> | ||
| </div> | ||
| ); | ||
|
|
||
| const statDivider = () => ( | ||
| <div className="difficulty-adjustment-stat-divider"></div> | ||
| ); | ||
|
|
||
| export default ({ | ||
| blocks, | ||
| dashboardEpochStartBlock, | ||
| dashboardPreviousDifficultyBlock, | ||
| }) => { | ||
| const latestBlock = blocks && blocks[0]; | ||
| const epochTiming = getEpochTiming(latestBlock, dashboardEpochStartBlock); | ||
| const expected = expectedAdjustment(epochTiming); | ||
| const previous = previousAdjustment( | ||
| latestBlock, | ||
| dashboardPreviousDifficultyBlock, | ||
| ); | ||
| const hashrate = formatHashrate( | ||
| latestBlock && latestBlock.difficulty, | ||
| epochTiming && epochTiming.averageBlockSeconds, | ||
| ); | ||
| const estimatedAdjustmentTimestamp = | ||
| epochTiming && epochTiming.estimatedAdjustmentTimestamp; | ||
| const nextAdjustment = formatTimeUntil(estimatedAdjustmentTimestamp); | ||
| const nextAdjustmentFooter = Number.isFinite(estimatedAdjustmentTimestamp) | ||
| ? `Next adj. in ${nextAdjustment}` | ||
| : "Next adjustment unavailable"; | ||
|
|
||
| return ( | ||
| <div className="difficulty-adjustment-section"> | ||
| <div className="difficulty-adjustment-panel"> | ||
| <div className="table-header"> | ||
| <div className="table-header-icon-container"> | ||
| <ArrowsInSimpleIcon /> | ||
| </div> | ||
| <h1 className="table-header-title">Difficulty Adjustment</h1> | ||
| </div> | ||
| <div className="difficulty-adjustment-stats"> | ||
| {adjustmentStat("AVERAGE BLOCK TIME", "~10 minutes")} | ||
|
|
||
| {statDivider()} | ||
| {adjustmentStat( | ||
| "EXPECTED", | ||
| formatAdjustment(expected), | ||
| adjustmentClass(expected), | ||
| )} | ||
|
|
||
| {statDivider()} | ||
| {adjustmentStat( | ||
| "PREVIOUS", | ||
| formatAdjustment(previous), | ||
| adjustmentClass(previous), | ||
| )} | ||
|
|
||
| {statDivider()} | ||
| {adjustmentStat( | ||
| "EXPECTED DATE", | ||
| formatAdjustmentDate(estimatedAdjustmentTimestamp), | ||
| )} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="difficulty-adjustment-metrics"> | ||
| <InfoCard | ||
| className="difficulty-adjustment-metric-card" | ||
| title="Hashrate" | ||
| value={hashrate.value} | ||
| footer={hashrate.footer} | ||
| /> | ||
|
|
||
| <InfoCard | ||
| className="difficulty-adjustment-metric-card" | ||
| title="Difficulty" | ||
| value={formatDifficulty(latestBlock && latestBlock.difficulty)} | ||
| footer={nextAdjustmentFooter} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't match the final design in Figma but I wanted to propose this alternative