Skip to content

Commit

Permalink
blockchain: make all private fns private
Browse files Browse the repository at this point in the history
blockchain: fix rebase, fix docs
vm: fix example

blockchain: fix tests
  • Loading branch information
jochem-brouwer committed Nov 6, 2020
1 parent 5bd731a commit 4757523
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/blockchain/src/db/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down
24 changes: 15 additions & 9 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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<void>(async () => {
const block = item instanceof BlockHeader ? new Block(item) : item
const isGenesis = block.isGenesis()
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }))
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
35 changes: 18 additions & 17 deletions packages/blockchain/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <any> in order to get access to the private _getTd
const td = await (<any>blockchain)._getTd(genesis.hash(), new BN(0))
st.ok(td.eq(genesis.header.difficulty), 'should perform _getTd correctly')
st.end()
})
Expand All @@ -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)

Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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,
}),
]

Expand Down
9 changes: 4 additions & 5 deletions packages/vm/examples/run-blockchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,8 +29,6 @@ async function main() {

await setupPreConditions(vm, testData)

await setGenesisBlock(blockchain, common)

await putBlocks(blockchain, common, testData)

await vm.runBlockchain(blockchain)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4757523

Please sign in to comment.