UNPKG

deth

Version:

Ethereum node focused on Developer Experience

38 lines (37 loc) 1.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ethereumjs_block_1 = __importDefault(require("ethereumjs-block")); const bn_js_1 = __importDefault(require("bn.js")); const ethereumjs_util_1 = require("ethereumjs-util"); const getLatestBlock_1 = require("./getLatestBlock"); async function getNextBlock(vm, transactions, options, clockSkew) { const block = await getEmptyNextBlock(vm, options, clockSkew); await addTransactionsToBlock(block, transactions); return block; } exports.getNextBlock = getNextBlock; async function getEmptyNextBlock(vm, options, clockSkew) { const latestBlock = await getLatestBlock_1.getLatestBlock(vm); const header = { gasLimit: options.blockGasLimit, nonce: 42, timestamp: Math.floor(Date.now() / 1000) + clockSkew, number: new bn_js_1.default(latestBlock.header.number).addn(1), parentHash: latestBlock.hash(), coinbase: options.coinbaseAddress, }; const block = new ethereumjs_block_1.default({ header }, { common: vm._common }); block.validate = (blockchain, cb) => cb(null); block.header.difficulty = ethereumjs_util_1.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; }