Last active
May 17, 2021 04:27
-
-
Save bluepnume/d74116db61f3db3e0d9e02e59eea59fe to your computer and use it in GitHub Desktop.
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
const createBlock = async (publicKey : string, transactions : Array<string>) : Promise<?string> => { | |
const headBlock = root.getLongestBranchNode().getValue(); | |
const newTransactions = await asyncFilter(transactions, verifyPackedSignature); | |
const newDifficulty = (headBlock.elapsed) > BLOCK_TIME | |
? headBlock.difficulty - 1 | |
: headBlock.difficulty + 1; | |
const newReward = divisibleBy(headBlock.index, REWARD_HALVING_SCHEDULE) | |
? Math.floor(headBlock.reward / 2) | |
: headBlock.reward; | |
const newBlock = { | |
miner: publicKey, | |
parentid: headBlock.id, | |
index: headBlock.index + 1, | |
id: uniqueID(), | |
time: now(), | |
elapsed: now() - headBlock.time, | |
transactions: newTransactions, | |
difficulty: newDifficulty, | |
reward: newReward | |
}; | |
const hashedBlock = await hashAndPack(newBlock); | |
const hash = unpackHash(hashedBlock); | |
if (divisibleBy(hash, headBlock.difficulty)) { | |
return hashedBlock; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment