From c3d4d6b2da6850698b9979795944526bf908c90a Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Fri, 6 Nov 2020 19:41:21 +0100 Subject: [PATCH] blockchain: make all private fns private blockchain: fix rebase, fix docs vm: fix example blockchain: fix tests --- packages/blockchain/src/db/helpers.ts | 2 +- packages/blockchain/src/db/manager.ts | 6 ++-- packages/blockchain/src/index.ts | 24 +++++++++----- packages/blockchain/test/index.ts | 35 ++++++++++---------- packages/vm/examples/run-blockchain/index.ts | 9 +++-- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/packages/blockchain/src/db/helpers.ts b/packages/blockchain/src/db/helpers.ts index 73c4876179..48e2ec90ae 100644 --- a/packages/blockchain/src/db/helpers.ts +++ b/packages/blockchain/src/db/helpers.ts @@ -5,7 +5,7 @@ import { Block, BlockHeader } from '@ethereumjs/block' import { bufBE8 } from './constants' /* - This extra helper file is an interface between blockchain / databaseOperation.ts + This extra helper file is an interface between blockchain / operation.ts It also handles the right encoding of the keys, so this does not have to happen in index.ts anymore. */ diff --git a/packages/blockchain/src/db/manager.ts b/packages/blockchain/src/db/manager.ts index 4c1f7d7bb0..5979cf3e30 100644 --- a/packages/blockchain/src/db/manager.ts +++ b/packages/blockchain/src/db/manager.ts @@ -32,9 +32,9 @@ export type CacheMap = { [key: string]: Cache } * @hidden */ export class DBManager { - _cache: CacheMap - _common: Common - _db: LevelUp + private _cache: CacheMap + private _common: Common + private _db: LevelUp constructor(db: LevelUp, common: Common) { this._db = db diff --git a/packages/blockchain/src/index.ts b/packages/blockchain/src/index.ts index 0c4d1faebc..f3cd3cdf9b 100644 --- a/packages/blockchain/src/index.ts +++ b/packages/blockchain/src/index.ts @@ -75,8 +75,9 @@ export interface BlockchainOptions { validateBlocks?: boolean /** - * If a genesis block is present in the provided `db`, then this genesis block will be used. - * If there is no genesis block in the `db`, use this `genesisBlock`: if none is provided, use the standard genesis block. + * The blockchain only initializes succesfully if it has a genesis block. + * If there is no block available in the DB and a `genesisBlock` is provided, then the provided `genesisBlock` will be used as genesis + * If no block is present in the DB and no block is provided, then the genesis block as provided from the `common` will be used */ genesisBlock?: Block } @@ -386,7 +387,7 @@ export default class Blockchain implements BlockchainInterface { * This thus rolls back these headers so that these can be updated to the "new" canonical header using the iterator method. * @hidden */ - async _putBlockOrHeader(item: Block | BlockHeader) { + private async _putBlockOrHeader(item: Block | BlockHeader) { await this.runWithLock(async () => { const block = item instanceof BlockHeader ? new Block(item) : item const isGenesis = block.isGenesis() @@ -486,7 +487,7 @@ export default class Blockchain implements BlockchainInterface { /** * @hidden */ - async _getBlock(blockId: Buffer | number | BN) { + private async _getBlock(blockId: Buffer | number | BN) { return this.dbManager.getBlock(blockId) } @@ -587,7 +588,7 @@ export default class Blockchain implements BlockchainInterface { /** * @hidden */ - async _delBlock(blockHash: Buffer) { + private async _delBlock(blockHash: Buffer) { const dbOps: DBOp[] = [] // get header @@ -623,7 +624,12 @@ export default class Blockchain implements BlockchainInterface { * @param ops - the `DatabaseOperation` list to add the delete operations to * @hidden */ - async _delChild(blockHash: Buffer, blockNumber: BN, headHash: Buffer | null, ops: DBOp[]) { + private async _delChild( + blockHash: Buffer, + blockNumber: BN, + headHash: Buffer | null, + ops: DBOp[] + ) { // delete header, body, hash to number mapping and td ops.push(DBOp.del(DBTarget.Header, { blockHash, blockNumber })) ops.push(DBOp.del(DBTarget.Body, { blockHash, blockNumber })) @@ -843,7 +849,7 @@ export default class Blockchain implements BlockchainInterface { * * @hidden */ - async _getHeader(hash: Buffer, number?: BN) { + private async _getHeader(hash: Buffer, number?: BN) { if (!number) { number = await this.dbManager.hashToNumber(hash) } @@ -855,7 +861,7 @@ export default class Blockchain implements BlockchainInterface { * * @hidden */ - async _getCanonicalHeader(number: BN) { + private async _getCanonicalHeader(number: BN) { const hash = await this.dbManager.numberToHash(number) return this._getHeader(hash, number) } @@ -865,7 +871,7 @@ export default class Blockchain implements BlockchainInterface { * * @hidden */ - async _getTd(hash: Buffer, number?: BN) { + private async _getTd(hash: Buffer, number?: BN) { if (!number) { number = await this.dbManager.hashToNumber(hash) } diff --git a/packages/blockchain/test/index.ts b/packages/blockchain/test/index.ts index 24da0e8600..26546fae3d 100644 --- a/packages/blockchain/test/index.ts +++ b/packages/blockchain/test/index.ts @@ -122,13 +122,13 @@ tape('blockchain test', (t) => { const blockData = { header: { number: 1, - parentHash: genesis.hash(), - timestamp: genesis.header.timestamp.addn(1), + parentHash: genesisBlock.hash(), + timestamp: genesisBlock.header.timestamp.addn(1), gasLimit, }, } const block = Block.fromBlockData(blockData, { - calcDifficultyFromHeader: genesis.header, + calcDifficultyFromHeader: genesisBlock.header, }) blocks.push(block) await blockchain.putBlock(block) @@ -545,7 +545,8 @@ tape('blockchain test', (t) => { const hash = await blockchain.dbManager.numberToHash(new BN(0)) st.ok(genesis.hash().equals(hash), 'should perform _numberToHash correctly') - const td = await blockchain._getTd(genesis.hash(), new BN(0)) + // cast the blockchain as in order to get access to the private _getTd + const td = await (blockchain)._getTd(genesis.hash(), new BN(0)) st.ok(td.eq(genesis.header.difficulty), 'should perform _getTd correctly') st.end() }) @@ -564,12 +565,12 @@ tape('blockchain test', (t) => { const headerData = { number: 1, - parentHash: genesis.hash(), + parentHash: genesisBlock.hash(), gasLimit, timestamp: genesisBlock.header.timestamp.addn(1), } const header = BlockHeader.fromHeaderData(headerData, { - calcDifficultyFromHeader: genesis.header, + calcDifficultyFromHeader: genesisBlock.header, }) await blockchain.putHeader(header) @@ -603,21 +604,21 @@ tape('blockchain test', (t) => { const blockData = { header: { number: 1, - parentHash: genesis.hash(), - timestamp: genesis.header.timestamp.addn(3), + parentHash: genesisBlock.hash(), + timestamp: genesisBlock.header.timestamp.addn(3), gasLimit, }, } - opts.calcDifficultyFromHeader = genesis.header + opts.calcDifficultyFromHeader = genesisBlock.header const block = Block.fromBlockData(blockData, opts) const headerData1 = { number: 1, - parentHash: genesis.hash(), - timestamp: genesis.header.timestamp.addn(1), + parentHash: genesisBlock.hash(), + timestamp: genesisBlock.header.timestamp.addn(1), gasLimit, } - opts.calcDifficultyFromHeader = genesis.header + opts.calcDifficultyFromHeader = genesisBlock.header const header1 = BlockHeader.fromHeaderData(headerData1, opts) const headers = [header1] @@ -659,8 +660,8 @@ tape('blockchain test', (t) => { const blockData1 = { header: { number: 1, - parentHash: genesis.hash(), - timestamp: genesis.header.timestamp.addn(1), + parentHash: genesisBlock.hash(), + timestamp: genesisBlock.header.timestamp.addn(1), gasLimit, }, } @@ -671,11 +672,11 @@ tape('blockchain test', (t) => { } const blocks = [ - genesis, - Block.fromBlockData(blockData1, { common, calcDifficultyFromHeader: genesis.header }), + genesisBlock, + Block.fromBlockData(blockData1, { common, calcDifficultyFromHeader: genesisBlock.header }), Block.fromBlockData(blockData2, { common: new Common({ chain: 'ropsten', hardfork: 'chainstart' }), - calcDifficultyFromHeader: genesis.header, + calcDifficultyFromHeader: genesisBlock.header, }), ] diff --git a/packages/vm/examples/run-blockchain/index.ts b/packages/vm/examples/run-blockchain/index.ts index a98cae8fd8..1b5e60e3bb 100644 --- a/packages/vm/examples/run-blockchain/index.ts +++ b/packages/vm/examples/run-blockchain/index.ts @@ -12,10 +12,11 @@ async function main() { const validatePow = true const validateBlocks = true - const blockchain = new Blockchain({ + const blockchain = await Blockchain.create({ common, validatePow, validateBlocks, + genesisBlock: getGenesisBlock(common) }) // When verifying PoW, setting this cache improves the @@ -28,8 +29,6 @@ async function main() { await setupPreConditions(vm, testData) - await setGenesisBlock(blockchain, common) - await putBlocks(blockchain, common, testData) await vm.runBlockchain(blockchain) @@ -66,10 +65,10 @@ async function setupPreConditions(vm: VM, testData: any) { await vm.stateManager.commit() } -async function setGenesisBlock(blockchain: any, common: Common) { +function getGenesisBlock(common: Common) { const header = testData.genesisBlockHeader const genesis = Block.genesis({ header }, { common }) - await blockchain.putGenesis(genesis) + return genesis } async function putBlocks(blockchain: any, common: Common, testData: any) {