Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blockchain: internal refactor #895

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export interface BlockchainOptions {
* provided, it defaults to `true`.
*/
validateBlocks?: boolean

/**
* If a genesis block is present in the provided `db`, then this genesis block will be used.
Copy link
Member

@holgerd77 holgerd77 Oct 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "this" reads a bit confusing here, I would just repeat "the genesis block from db will be used" (especially since there is another "this" below referencing genesisBlock, I actually would remove the "this" there as well for readability)

* If there is no genesis block in the `db`, use this `genesisBlock`: if none is provided, use the standard genesis block.
*/
genesisBlock?: Block
}

/**
Expand Down Expand Up @@ -141,7 +147,7 @@ export default class Blockchain implements BlockchainInterface {
this._initDone = false

// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._initPromise = this._init()
this._initPromise = this._init(opts.genesisBlock)
}

/**
Expand All @@ -162,15 +168,19 @@ export default class Blockchain implements BlockchainInterface {
*
* @hidden
*/
private async _init(): Promise<void> {
private async _init(genesisBlock?: Block): Promise<void> {
let genesisHash
try {
genesisHash = await this.dbManager.numberToHash(new BN(0))
} catch (error) {
if (error.type !== 'NotFoundError') {
throw error
}
await this._setCanonicalGenesisBlock()
if (genesisBlock) {
await this._putBlockOrHeader(genesisBlock)
} else {
await this._setCanonicalGenesisBlock()
}
genesisHash = this._genesis
}

Expand Down Expand Up @@ -358,6 +368,12 @@ export default class Blockchain implements BlockchainInterface {
const block = item instanceof BlockHeader ? new Block(item) : item
const isGenesis = block.isGenesis()

// we cannot overwrite the Genesis block after initializing the Blockchain

if (this._initDone && isGenesis) {
throw new Error('Cannot put a new Genesis block: create a new Blockchain')
}

const { header } = block
const blockHash = header.hash()
const blockNumber = header.number
Expand Down
25 changes: 12 additions & 13 deletions packages/vm/tests/BlockchainTestsRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@ export default async function runBlockchainTest(options: any, testData: any, t:
const { common } = options
common.setHardforkByBlockNumber(0)

// create and add genesis block
const header = formatBlockHeader(testData.genesisBlockHeader)
const blockData = { header }
const genesisBlock = Block.fromBlockData(blockData, { common })

if (testData.genesisRLP) {
const rlp = toBuffer(testData.genesisRLP)
t.ok(genesisBlock.serialize().equals(rlp), 'correct genesis RLP')
}

const blockchain = new Blockchain({
db: blockchainDB,
common,
validateBlocks: true,
validatePow,
genesisBlock,
})

if (validatePow) {
Expand All @@ -58,22 +69,10 @@ export default async function runBlockchainTest(options: any, testData: any, t:
common,
})

// create and add genesis block
const header = formatBlockHeader(testData.genesisBlockHeader)
const blockData = { header }
const genesis = Block.fromBlockData(blockData, { common })

// set up pre-state
await setupPreConditions(vm.stateManager._trie, testData)

t.ok(vm.stateManager._trie.root.equals(genesis.header.stateRoot), 'correct pre stateRoot')

if (testData.genesisRLP) {
const rlp = toBuffer(testData.genesisRLP)
t.ok(genesis.serialize().equals(rlp), 'correct genesis RLP')
}

await blockchain.putGenesis(genesis)
t.ok(vm.stateManager._trie.root.equals(genesisBlock.header.stateRoot), 'correct pre stateRoot')

async function handleError(error: string | undefined, expectException: string) {
if (expectException) {
Expand Down