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

Commit 885d251

Browse files
authored
Merge pull request #121 from ethereumjs/granular-validation
Granular validation
2 parents 3c1445f + 7ae5db9 commit 885d251

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

src/index.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,26 @@ export interface BlockchainOptions {
9393
db?: any
9494

9595
/**
96-
* This the flag indicates if blocks should be validated (e.g. Proof-of-Work), latest HF rules
97-
* supported: `Petersburg`.
96+
* This the flag indicates if blocks and Proof-of-Work should be validated.
97+
* This option can't be used in conjunction with `validatePow` nor `validateBlocks`.
98+
*
99+
* @deprecated
98100
*/
99101
validate?: boolean
102+
103+
/**
104+
* This flags indicates if Proof-of-work should be validated. If `validate` is provided, this
105+
* option takes its value. If neither `validate` nor this option are provided, it defaults to
106+
* `true`.
107+
*/
108+
validatePow?: boolean
109+
110+
/**
111+
* This flags indicates if blocks should be validated. See Block#validate for details. If
112+
* `validate` is provided, this option takes its value. If neither `validate` nor this option are
113+
* provided, it defaults to `true`.
114+
*/
115+
validateBlocks?: boolean
100116
}
101117

102118
/**
@@ -158,9 +174,14 @@ export default class Blockchain implements BlockchainInterface {
158174
ethash: any
159175

160176
/**
161-
* A flag indicating if this Blockchain validates blocks or not.
177+
* This field is always `true`. It's here only for backwards compatibility.
178+
*
179+
* @deprecated
162180
*/
163-
validate: boolean
181+
public readonly validate: boolean = true
182+
183+
private readonly _validatePow: boolean
184+
private readonly _validateBlocks: boolean
164185

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

203+
if (opts.validate !== undefined) {
204+
if (opts.validatePow !== undefined || opts.validateBlocks !== undefined) {
205+
throw new Error(
206+
"opts.validate can't be used at the same time than opts.validatePow nor opts.validateBlocks",
207+
)
208+
}
209+
}
210+
182211
// defaults
212+
213+
if (opts.validate !== undefined) {
214+
this._validatePow = opts.validate
215+
this._validateBlocks = opts.validate
216+
} else {
217+
this._validatePow = opts.validatePow !== undefined ? opts.validatePow : true
218+
this._validateBlocks = opts.validateBlocks !== undefined ? opts.validateBlocks : true
219+
}
220+
183221
this.db = opts.db ? opts.db : level()
184222
this.dbManager = new DBManager(this.db, this._common)
185-
this.validate = opts.validate === undefined ? true : opts.validate
186-
this.ethash = this.validate ? new Ethash(this.db) : null
223+
this.ethash = this._validatePow ? new Ethash(this.db) : null
187224
this._heads = {}
188225
this._genesis = null
189226
this._headHeader = null
@@ -444,7 +481,7 @@ export default class Blockchain implements BlockchainInterface {
444481
)
445482

446483
function verify(next: any) {
447-
if (!self.validate) {
484+
if (!self._validateBlocks) {
448485
return next()
449486
}
450487

@@ -456,7 +493,7 @@ export default class Blockchain implements BlockchainInterface {
456493
}
457494

458495
function verifyPOW(next: any) {
459-
if (!self.validate) {
496+
if (!self._validatePow) {
460497
return next()
461498
}
462499

test/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ const testData = require('./testdata.json')
1212

1313
test('blockchain test', function(t) {
1414
t.plan(70)
15-
const blockchain = new Blockchain()
15+
const blockchain = new Blockchain({ validate: false })
1616
let genesisBlock: any
1717
const blocks: any[] = []
1818
let forkHeader: any
19-
blockchain.validate = false
2019
async.series(
2120
[
2221
function(done) {
@@ -74,14 +73,14 @@ test('blockchain test', function(t) {
7473
})
7574
},
7675
function invalidGenesis(done) {
76+
const localBlockchain = new Blockchain({ validate: true })
7777
const badBlock = new Block()
7878
badBlock.header.number = Buffer.from([])
79-
blockchain.validate = true
80-
blockchain.putBlock(
79+
80+
localBlockchain.putBlock(
8181
badBlock,
8282
function(err?: any) {
8383
t.ok(err, 'should not validate a block incorrectly flagged as genesis')
84-
blockchain.validate = false
8584
done()
8685
},
8786
false,
@@ -314,8 +313,7 @@ test('blockchain test', function(t) {
314313
)
315314
},
316315
function iterateEmpty(done) {
317-
const blockchain = new Blockchain()
318-
blockchain.validate = false
316+
const blockchain = new Blockchain({ validate: false })
319317
blockchain.iterator(
320318
'test',
321319
function() {

0 commit comments

Comments
 (0)