UNPKG

deth

Version:

Ethereum node focused on Developer Experience

36 lines (35 loc) 1.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const primitives_1 = require("../../primitives"); /** * Simple, in memory, copyable blockchain */ class DethBlockchain { constructor(blocks = []) { this.blocks = blocks; } getBlockByNumber(blockNumber) { return this.blocks[primitives_1.quantityToNumber(blockNumber)]; } getBlockByHash(hash) { return this.blocks.filter(b => hash === primitives_1.bufferToHash(b.hash()))[0]; } putBlock(block) { this.blocks.push(block); return block; } putGenesis(block) { if (this.blocks.length !== 0) { throw new Error(`Trying to put a genesis block on not empty chain! Chain has ${this.blocks.length} blocks already`); } this.blocks.push(block); return block; } getLatestBlock() { return this.blocks[this.blocks.length - 1]; } copy() { return new DethBlockchain([...this.blocks]); } } exports.DethBlockchain = DethBlockchain;