JSFiddle - React, Tailwind, and code Playground
https://jsfiddle.net/jspafford/mejhn90a/15/#
by jspafford
HTML
<!DOCTYPE html>
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
<body>
<div>
<label>Target mining time (seconds):</label>
<select id="target" >
<option>15</option>
<option>50</option>
<option>60</option>
<option>120</option>
</select>
</div>
<div>
<label>Bucket size (seconds)?</label>
<input id="bucket" value="30" type="number" min="1" max="100"></input>
</div>
<div>
<label>Time around target mining time (seconds)?</label>
<input id="seconds" value="30" type="number" min="10" max="1000"></input>
</div>
<canvas id="chart1" style="width:100%;max-width:1600px"></canvas>
<canvas id="chart2" style="width:100%;max-width:1600px"></canvas>
</body>
</html>
JavaScript
const MIN_DIFFICULTY = BigInt(131072)
const DEFAULT_TARGET = BigInt(883423532389192164791648750371459257913741948437809479060803100646309888)
const targetInput = document.getElementById('target');
const bucketInput = document.getElementById('bucket');
const secondsInput= document.getElementById('seconds');
targetInput.value = 60
bucketInput.value = 10
secondsInput.value = 100
const chart1 = makeChart('chart1', 'Difficulty by seconds after last block (JS)')
const chart2 = makeChart('chart2', 'Difficulty by seconds after last block (RUST)')
function calcDifficulty(targetMedian, bucketSize, previousBlockDifficulty, diffInSeconds) {
const difficulty_denominator = 2048
const increment_divisor = 10
const increment_min = -99
const threshold = 1
const halfBucket = Math.floor(bucketSize / 2)
const bucket = Math.floor((diffInSeconds - targetMedian + halfBucket) / bucketSize) + 1
const max_change = BigInt(previousBlockDifficulty / BigInt(difficulty_denominator))
let target = null
if (bucket <= threshold) {
// Scale up
const multiplier = threshold - bucket
target = previousBlockDifficulty + BigInt(max_change) * BigInt(multiplier)
} else {
// Scale down
const multiplier = min(bucket - threshold, 99)
target = previousBlockDifficulty - BigInt(max_change) * BigInt(multiplier)
}
return max(MIN_DIFFICULTY, target)
}
function calcDifficultyRust(targetMedian, bucketSize, previousBlockDifficulty, diffInSeconds) {
let threshold = 1
let increment_divisor = 10
let difficulty_bound_divisor = 2048
let diff_inc = Math.floor(diffInSeconds / increment_divisor);
let max_change = BigInt((previousBlockDifficulty / BigInt(difficulty_bound_divisor)))
let target = null
if (diff_inc <= threshold) {
// Scale up
let multiplier = threshold - diff_inc
let change = max_change * multiplier
target = previousBlockDifficulty + change
} else {
// Scale down
let multiplier = diff_inc - threshold
let...