Published
Edited
Oct 22, 2019
3 forks
12 stars
Insert cell
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))
console.log("blockHash:" + blockHash + " nonce:" + nonce) // open the javascript console to see the hashes
// 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
2, // lets keep the difficulty super low so we don't break the browser ;)
0) // start the nonce at 0, but this can be any number
Insert cell
Insert cell
Insert cell
block2 = searchForBlock("7B9C45BEC3B15B063BD2B88C7F8CE61EDA062FE37A6686CCDE81345DF3B64D9A", // this is a random hash value, but in a real blockchain this would be the Merkle root of the transactions tree
sha256(JSON.stringify(block1)),
2,
0)
Insert cell
Insert cell
block2hash = sha256(JSON.stringify(block2))
Insert cell
Insert cell
block3 = searchForBlock("464FE2FFCC89795D396D235A1B09C1CDF60C5FA40467C42DE6953CC55E0A8DE6", // this is a random hash value, but in a real blockchain this would be the Merkle root of the transactions tree
sha256(JSON.stringify(block2)),
2,
0)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more