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
type MemPool = Array<{| | |
fee : number, | |
signedTransaction : string | |
|}>; | |
type BitcoinNodeType = {| | |
getPublicKey : () => Promise<string>, | |
run : () => Promise<void>, | |
send : (receiver : string, amount : number, fee : number) => Promise<void>, | |
getBlockChain : () => BlockChainType, |
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 getBalances = async () : Promise<Counter> => { | |
const balances = new Counter(); | |
for (let { miner, reward, transactions } of root.getLongestChainAsValues()) { | |
balances.add(miner, reward); | |
for (let { receiver, amount, fee, sender } of transactions) { | |
balances.add(miner, fee); | |
balances.add(receiver, amount); | |
balances.subtract(sender, amount); |
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 addBlock = async (hashedBlock : string) => { | |
const verifiedBlock = await verifyHashAndUnpack(hashedBlock); | |
const verifiedTransactions = await asyncMap(verifiedBlock.transactions, verifySignatureAndUnpack); | |
const fullyVerifiedBlock = { | |
...verifiedBlock, | |
transactions: verifiedTransactions | |
}; | |
const headNode = root.findValueByID(fullyVerifiedBlock.parentid); |
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 doesHashPassDifficulty = (hash : string, difficulty : number) : boolean => { | |
return (parseInt(hash, 36) % difficulty) === 0; | |
} |
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 calculateNewReward = (headBlock : BlockType) => { | |
return Math.floor(INITIAL_REWARD / (2 ** Math.floor(headBlock.index / REWARD_HALVING_SCHEDULE))); | |
}; |
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 calculateNewDifficulty = (headBlock : BlockType, previousHeadBlock : ?BlockType) : number => { | |
if (!previousHeadBlock) { | |
return headBlock.difficulty; | |
} | |
return (headBlock.time - previousHeadBlock.time) > BLOCK_TIME | |
? headBlock.difficulty - 1 | |
: headBlock.difficulty + 1; | |
}; |
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) |
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
export const BLOCK_TIME = 1000; | |
export const INITIAL_REWARD = 1024; | |
export const REWARD_HALVING_SCHEDULE = 20; | |
export const BLOCK_SIZE_LIMIT = 10; | |
export const GENESIS_BLOCK : BlockType = { | |
id: 'GENESIS', |
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 root = TreeNode(GENESIS_BLOCK); |
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
export type BlockChainType = {| | |
addBlock : (hashedBlock : string) => Promise<void>, | |
createBlock : (publicKey : string, transactions : Array<string>) => Promise<?string>, | |
getBalances : () => Promise<Counter> | |
|}; | |
export function BlockChain() : BlockChainType { | |
const createBlock = async (publicKey, transactions) : Promise<?string> => { | |
}; |