Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Granular validation #121

Merged
merged 6 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
53 changes: 45 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,26 @@ export interface BlockchainOptions {
db?: any

/**
* This the flag indicates if blocks should be validated (e.g. Proof-of-Work), latest HF rules
* supported: `Petersburg`.
* This the flag indicates if blocks and Proof-of-Work should be validated.
* This option can't be used in conjunction with `validatePow` nor `validateBlocks`.
*
* @deprecated
*/
validate?: boolean

/**
* This flags indicates if Proof-of-work should be validated. If `validate` is provided, this
* option takes its value. If neither `validate` nor this option are provided, it defaults to
* `true`.
*/
validatePow?: boolean

/**
* This flags indicates if blocks should be validated. See Block#validate for details. If
* `validate` is provided, this option takes its value. If neither `validate` nor this option are
* provided, it defaults to `true`.
*/
validateBlocks?: boolean
alcuadrado marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -158,9 +174,14 @@ export default class Blockchain implements BlockchainInterface {
ethash: any

/**
* A flag indicating if this Blockchain validates blocks or not.
* This field is always `true`. It's here only for backwards compatibility.
*
* @deprecated
*/
validate: boolean
public readonly validate: boolean = true

private readonly _validatePow: boolean
private readonly _validateBlocks: boolean

/**
* Creates new Blockchain object
Expand All @@ -179,11 +200,27 @@ export default class Blockchain implements BlockchainInterface {
this._common = new Common(chain, hardfork)
}

if (opts.validate !== undefined) {
if (opts.validatePow !== undefined || opts.validateBlocks !== undefined) {
throw new Error(
"opts.validate can't be used at the same time than opts.validatePow nor opts.validateBlocks",
)
}
}

// defaults

if (opts.validate !== undefined) {
this._validatePow = opts.validate
this._validateBlocks = opts.validate
} else {
this._validatePow = opts.validatePow !== undefined ? opts.validatePow : true
this._validateBlocks = opts.validateBlocks !== undefined ? opts.validateBlocks : true
}

this.db = opts.db ? opts.db : level()
this.dbManager = new DBManager(this.db, this._common)
this.validate = opts.validate === undefined ? true : opts.validate
this.ethash = this.validate ? new Ethash(this.db) : null
this.ethash = this._validatePow ? new Ethash(this.db) : null
this._heads = {}
this._genesis = null
this._headHeader = null
Expand Down Expand Up @@ -444,7 +481,7 @@ export default class Blockchain implements BlockchainInterface {
)

function verify(next: any) {
if (!self.validate) {
if (!self._validateBlocks) {
return next()
}

Expand All @@ -456,7 +493,7 @@ export default class Blockchain implements BlockchainInterface {
}

function verifyPOW(next: any) {
if (!self.validate) {
if (!self._validatePow) {
return next()
}

Expand Down
12 changes: 5 additions & 7 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ const testData = require('./testdata.json')

test('blockchain test', function(t) {
t.plan(70)
const blockchain = new Blockchain()
const blockchain = new Blockchain({ validate: false })
let genesisBlock: any
const blocks: any[] = []
let forkHeader: any
blockchain.validate = false
async.series(
[
function(done) {
Expand Down Expand Up @@ -74,14 +73,14 @@ test('blockchain test', function(t) {
})
},
function invalidGenesis(done) {
const localBlockchain = new Blockchain({ validate: true })
const badBlock = new Block()
badBlock.header.number = Buffer.from([])
blockchain.validate = true
blockchain.putBlock(

localBlockchain.putBlock(
badBlock,
function(err?: any) {
t.ok(err, 'should not validate a block incorrectly flagged as genesis')
blockchain.validate = false
done()
},
false,
Expand Down Expand Up @@ -314,8 +313,7 @@ test('blockchain test', function(t) {
)
},
function iterateEmpty(done) {
const blockchain = new Blockchain()
blockchain.validate = false
const blockchain = new Blockchain({ validate: false })
blockchain.iterator(
'test',
function() {
Expand Down