UNPKG

@ethereumjs/blockchain

Version:
61 lines 2.32 kB
import { Block } from '@ethereumjs/block'; import { RLP } from '@ethereumjs/rlp'; import { BIGINT_0 } from '@ethereumjs/util'; import { bytesBE8 } from "./constants.js"; import { DBOp, DBTarget } from "./operation.js"; /* * This extra helper file serves as an interface between the blockchain API functionality * and the DB operations from `db/operation.ts` and also handles the right encoding of the keys */ function DBSetTD(TD, blockNumber, blockHash) { return DBOp.set(DBTarget.TotalDifficulty, RLP.encode(TD), { blockNumber, blockHash, }); } /* * This method accepts either a BlockHeader or a Block and returns a list of DatabaseOperation instances * * - A "Set Header Operation" is always added * - A "Set Body Operation" is only added if the body is not empty (it has transactions/uncles) or if the block is the genesis block * (if there is a header but no block saved the DB will implicitly assume the block to be empty) */ function DBSetBlockOrHeader(blockBody) { const header = blockBody instanceof Block ? blockBody.header : blockBody; const dbOps = []; const blockNumber = header.number; const blockHash = header.hash(); const headerValue = header.serialize(); dbOps.push(DBOp.set(DBTarget.Header, headerValue, { blockNumber, blockHash, })); const isGenesis = header.number === BIGINT_0; if (isGenesis || blockBody instanceof Block) { const bodyValue = RLP.encode(blockBody.raw().slice(1)); dbOps.push(DBOp.set(DBTarget.Body, bodyValue, { blockNumber, blockHash, })); } return dbOps; } function DBSetHashToNumber(blockHash, blockNumber) { const blockNumber8Byte = bytesBE8(blockNumber); return DBOp.set(DBTarget.HashToNumber, blockNumber8Byte, { blockHash, }); } function DBSaveLookups(blockHash, blockNumber, skipNumIndex) { const ops = []; if (skipNumIndex !== true) { ops.push(DBOp.set(DBTarget.NumberToHash, blockHash, { blockNumber })); } const blockNumber8Bytes = bytesBE8(blockNumber); ops.push(DBOp.set(DBTarget.HashToNumber, blockNumber8Bytes, { blockHash, })); return ops; } export { DBOp, DBSaveLookups, DBSetBlockOrHeader, DBSetHashToNumber, DBSetTD }; //# sourceMappingURL=helpers.js.map