UNPKG

deth

Version:

Ethereum node focused on Developer Experience

32 lines (31 loc) 1.29 kB
import Block from 'ethereumjs-block'; import BN from 'bn.js'; import { toBuffer } from 'ethereumjs-util'; import { getLatestBlock } from './getLatestBlock'; export async function getNextBlock(vm, transactions, options, clockSkew) { const block = await getEmptyNextBlock(vm, options, clockSkew); await addTransactionsToBlock(block, transactions); return block; } async function getEmptyNextBlock(vm, options, clockSkew) { const latestBlock = await getLatestBlock(vm); const header = { gasLimit: options.blockGasLimit, nonce: 42, timestamp: Math.floor(Date.now() / 1000) + clockSkew, number: new BN(latestBlock.header.number).addn(1), parentHash: latestBlock.hash(), coinbase: options.coinbaseAddress, }; const block = new Block({ header }, { common: vm._common }); block.validate = (blockchain, cb) => cb(null); block.header.difficulty = toBuffer(block.header.canonicalDifficulty(latestBlock)); return block; } async function addTransactionsToBlock(block, transactions) { block.transactions.push(...transactions); await new Promise((resolve, reject) => { block.genTxTrie(err => err != null ? reject(err) : resolve()); }); block.header.transactionsTrie = block.txTrie.root; }