deth
Version:
Ethereum node focused on Developer Experience
33 lines (32 loc) • 926 B
JavaScript
import { bufferToHash, quantityToNumber } from '../../primitives';
/**
* Simple, in memory, copyable blockchain
*/
export class DethBlockchain {
constructor(blocks = []) {
this.blocks = blocks;
}
getBlockByNumber(blockNumber) {
return this.blocks[quantityToNumber(blockNumber)];
}
getBlockByHash(hash) {
return this.blocks.filter(b => hash === 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]);
}
}