Skip to content

Commit

Permalink
block: moved DAO extra data check logic to dedicated function, added …
Browse files Browse the repository at this point in the history
…two DAO HF extra data block creation tests
  • Loading branch information
holgerd77 committed Aug 27, 2020
1 parent 52a6017 commit e513ecd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
40 changes: 24 additions & 16 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import { Blockchain, BlockHeaderData, BufferLike, ChainOptions, PrefixedHexStrin
import { Buffer } from 'buffer'
import { Block } from './block'

const DAO_ExtraData = Buffer.from('64616f2d686172642d666f726b', 'hex')
const DAO_ForceExtraDataRange = 9 // force extra data be DAO_ExtraData for X blocks after DAO activation block (see: https://blog.slock.it/hard-fork-specification-24b889e70703)

/**
* An object that represents the block header
*/
Expand Down Expand Up @@ -136,19 +133,7 @@ export class BlockHeader {
]
defineProperties(this, fields, data)

if (this._common.hardforkIsActiveOnChain('dao')) {
// verify the extraData field.
const blockNumber = new BN(this.number)
const DAOActivationBlock = new BN(this._common.hardforkBlock('dao'))
if (blockNumber.gte(DAOActivationBlock)) {
const drift = blockNumber.sub(DAOActivationBlock)
if (drift.lten(DAO_ForceExtraDataRange)) {
if (!this.extraData.equals(DAO_ExtraData)) {
throw new Error("extraData should be 'dao-hard-fork'")
}
}
}
}
this._checkDAOExtraData()
}

/**
Expand Down Expand Up @@ -374,4 +359,27 @@ export class BlockHeader {
return undefined
}
}

/**
* Force extra data be DAO_ExtraData for DAO_ForceExtraDataRange blocks after DAO
* activation block (see: https://blog.slock.it/hard-fork-specification-24b889e70703)
*/
private _checkDAOExtraData() {
const DAO_ExtraData = Buffer.from('64616f2d686172642d666f726b', 'hex')
const DAO_ForceExtraDataRange = 9

if (this._common.hardforkIsActiveOnChain('dao')) {
// verify the extraData field.
const blockNumber = new BN(this.number)
const DAOActivationBlock = new BN(this._common.hardforkBlock('dao'))
if (blockNumber.gte(DAOActivationBlock)) {
const drift = blockNumber.sub(DAOActivationBlock)
if (drift.lten(DAO_ForceExtraDataRange)) {
if (!this.extraData.equals(DAO_ExtraData)) {
throw new Error("extraData should be 'dao-hard-fork'")
}
}
}
}
}
}
22 changes: 22 additions & 0 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,26 @@ tape('[Block]: block functions', function (t) {
st.equal(typeof block.toJSON(true), 'object')
st.end()
})

t.test('DAO hardfork', function (st) {
const blockData: any = rlp.decode(testData2.blocks[0].rlp)
// Set block number from test block to mainnet DAO fork block 1920000
blockData[0][8] = Buffer.from('1D4C00', 'hex')

const common = new Common('mainnet', 'dao')
st.throws(
function () {
new Block(blockData, { common: common })
},
/Error: extraData should be 'dao-hard-fork'$/,
'should throw on DAO HF block with wrong extra data',
) // eslint-disable-line

// Set extraData to dao-hard-fork
blockData[0][12] = Buffer.from('64616f2d686172642d666f726b', 'hex')
st.doesNotThrow(function () {
new Block(blockData, { common: common })
}, 'should not throw on DAO HF block with correct extra data')
st.end()
})
})

1 comment on commit e513ecd

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: e513ecd Previous: 9d1c766 Ratio
Block 9422905 1967 ops/sec (±2.76%) 1920 ops/sec (±3.31%) 0.98
Block 9422906 1946 ops/sec (±5.80%) 1748 ops/sec (±9.67%) 0.90
Block 9422907 1777 ops/sec (±9.29%) 1930 ops/sec (±1.19%) 1.09
Block 9422908 1980 ops/sec (±1.05%) 1672 ops/sec (±11.37%) 0.84
Block 9422909 1948 ops/sec (±1.11%) 1857 ops/sec (±1.49%) 0.95
Block 9422910 1903 ops/sec (±1.19%) 1832 ops/sec (±1.20%) 0.96
Block 9422911 1923 ops/sec (±1.03%) 1752 ops/sec (±5.02%) 0.91
Block 9422912 1371 ops/sec (±16.64%) 1340 ops/sec (±14.64%) 0.98
Block 9422913 1867 ops/sec (±1.01%) 1760 ops/sec (±0.79%) 0.94
Block 9422914 1850 ops/sec (±1.41%) 1728 ops/sec (±2.45%) 0.93

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.