Skip to content

Instantly share code, notes, and snippets.

@bluepnume
Last active May 17, 2021 04:27
Show Gist options
  • Save bluepnume/d74116db61f3db3e0d9e02e59eea59fe to your computer and use it in GitHub Desktop.
Save bluepnume/d74116db61f3db3e0d9e02e59eea59fe to your computer and use it in GitHub Desktop.
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