Published
Edited
Sep 20, 2019
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function searchForBlock(transactionRoot, previousBlockHash, difficulty, nonce){
let block = {txRoot: transactionRoot,
previousBlockHash: previousBlockHash,
nonce: nonce}
// create the hash of the block
let blockHash = sha256(JSON.stringify(block))
// use the number of leading repeating 0s as a measure of difficulty
if(blockHash.startsWith("0".repeat(difficulty))) {
// if the block meets the difficulty requirement, we are done
return block
} else {
// if the block doesn't meet the difficulty requirement, change the nonce and try again
return searchForBlock(transactionRoot, previousBlockHash, difficulty, nonce + 1)
}
}
Insert cell
block1 = searchForBlock(
"717B51070AA71E451BAA2D6B173219F66B95B7C63D956CA9BDDFEFE589B68ECD", // this is a random hash value, but in a real blockchain this would be the Merkle root of the transactions tree
sha256(JSON.stringify(genesisBlock)), // previous block is the genesis block
3, // lets keep the difficulty super low so we don't break the browser ;)
0) // start the nonce at 0, this can be any number
Insert cell
Insert cell
Insert cell
block2 = searchForBlock("7B9C45BEC3B15B063BD2B88C7F8CE61EDA062FE37A6686CCDE81345DF3B64D9A", sha256(JSON.stringify(block1)), 3, 0)
Insert cell
Insert cell
block2hash = sha256(JSON.stringify(block2))
Insert cell
Insert cell
block3 = searchForBlock("464FE2FFCC89795D396D235A1B09C1CDF60C5FA40467C42DE6953CC55E0A8DE6", sha256(JSON.stringify(block2)), 2, 0)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more