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

Block library refactoring #883

Merged
merged 29 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c053761
block -> refactor: reworked header class with static factory instanti…
holgerd77 Sep 21, 2020
ff46c55
block -> refactoring: added new static factory helpers to block class
holgerd77 Sep 21, 2020
dd228bc
block -> refactor: fix build errors, remove unused imports, unpad num…
jochem-brouwer Sep 24, 2020
608d804
block -> rename Header to BlockHeader
jochem-brouwer Sep 28, 2020
17a33a8
block/tx -> fix block tests
jochem-brouwer Sep 28, 2020
72b5b78
block -> enforce BNs on fields which are interpreted as numbers
jochem-brouwer Sep 28, 2020
8adcdf9
block -> edge case in toBN
jochem-brouwer Sep 29, 2020
8987003
ethash -> make ethash compatible with block
jochem-brouwer Sep 29, 2020
5f5e9f2
Merge branch 'master' into refactor-block-library
ryanio Oct 7, 2020
5351fb8
have validateTransactions return a string[] (https://github.com/ether…
ryanio Oct 7, 2020
ce1dac1
let => const
ryanio Oct 7, 2020
526f986
set default param to resolve js runtime check
ryanio Oct 7, 2020
75689e6
continue refactoring and simplifying methods
ryanio Oct 8, 2020
8923f71
api updates
ryanio Oct 8, 2020
6a2c193
continuing work
ryanio Oct 8, 2020
381f5e1
inline buffer validations. add checks for extraData, mixHash and nonce
ryanio Oct 8, 2020
7eecf80
various fixups
ryanio Oct 8, 2020
395c6f8
continuing various work
ryanio Oct 9, 2020
be5c8d2
continuing work and refactoring
ryanio Oct 9, 2020
7fa486d
Merge branch 'master' into refactor-block-library
ryanio Oct 9, 2020
91d45d7
re-add timestamp to genesis (for rinkeby)
ryanio Oct 9, 2020
7e788c3
last fixups
ryanio Oct 9, 2020
bc459e8
update readme, benchmarks
ryanio Oct 9, 2020
ea8a401
update vm readme, simplify validate
ryanio Oct 10, 2020
b694010
fix timestamp validation
ryanio Oct 10, 2020
1f66378
use native eq
ryanio Oct 10, 2020
9f9bab0
make blockchain optional in block.validate()
ryanio Oct 10, 2020
7ce9132
fixups
ryanio Oct 10, 2020
a5d3d14
remove BLOCK_difficulty_GivenAsList from skip list (https://github.co…
ryanio Oct 12, 2020
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
8 changes: 8 additions & 0 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ export class Block {
return new Block(header, transactions, uncleHeaders)
}

/**
* Alias for Block.fromBlockData() with initWithGenesisHeader set to true.
*/
public static genesis(blockData: BlockData = {}, opts: BlockOptions = {}) {
opts = { ...opts, initWithGenesisHeader: true }
return Block.fromBlockData(blockData, opts)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, thought we can really keep this as simple as you proposed (or more or less), so optimally Block.genesis(common), and everyone needing something adjusted can just use the Block.fromBlockData() variant?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I wanted to too but in practice (in our test suite) it was helpful to accept custom overrides as well.


/**
* This constructor takes the values, validates them, assigns them and freezes the object.
* Use the static factory methods to assist in creating a Block object from varying data types and options.
Expand Down
29 changes: 16 additions & 13 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ export class BlockHeader {
)
}

/**
* Alias for Header.fromHeaderData() with initWithGenesisHeader set to true.
*/
public static genesis(headerData: HeaderData = {}, opts: BlockOptions = {}) {
opts = { ...opts, initWithGenesisHeader: true }
return BlockHeader.fromHeaderData(headerData, opts)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Same here


/**
* This constructor takes the values, validates them, assigns them and freezes the object.
* Use the public static factory methods to assist in creating a Header object from
Expand Down Expand Up @@ -183,9 +191,6 @@ export class BlockHeader {
)
}
number = new BN(0)
if (timestamp.isZero()) {
timestamp = new BN(Date.now())
}
if (gasLimit.eq(DEFAULT_GAS_LIMIT)) {
gasLimit = new BN(toBuffer(this._common.genesis().gasLimit))
}
Expand Down Expand Up @@ -247,9 +252,6 @@ export class BlockHeader {
if (mixHash.length !== 32) {
throw new Error(`mixHash must be 32 bytes, received ${mixHash.length} bytes`)
}
if (extraData.length > 32) {
throw new Error(`extraData cannot exceed 32 bytes, received ${extraData.length} bytes`)
}
if (nonce.length !== 8) {
throw new Error(`nonce must be 8 bytes, received ${nonce.length} bytes`)
}
Expand Down Expand Up @@ -422,13 +424,6 @@ export class BlockHeader {
}
}

/**
* Returns the hash of the block header.
*/
hash(): Buffer {
return rlphash(this.raw())
}

/**
* Returns a Buffer Array of the raw Buffers in this header, in order.
*/
Expand All @@ -452,6 +447,14 @@ export class BlockHeader {
]
}

/**
* Returns the hash of the block header.
*/
hash(): Buffer {
return rlphash(this.raw())
}


/**
* Checks if the block header is a genesis header.
*/
Expand Down
30 changes: 15 additions & 15 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { Block } from '../src'
tape('[Block]: block functions', function (t) {
t.test('should test block initialization', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const block1 = Block.fromBlockData({}, { common: common, initWithGenesisHeader: true })
st.ok(block1.hash().toString('hex'), 'block should initialize')
const genesis = Block.genesis({}, { common })
Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, on the other hand (on the signature question): this also looks pretty simple already, so maybe not worth the further simplification, not sure.

st.ok(genesis.hash().toString('hex'), 'block should initialize')
st.end()
})

t.test('should initialize with undefined parameters without throwing', function (st) {
st.doesNotThrow(function () {
Block.fromBlockData()
st.end()
})
st.end()
})

t.test('should initialize with null parameters without throwing', function (st) {
Expand Down Expand Up @@ -76,11 +76,11 @@ tape('[Block]: block functions', function (t) {

const testDataGenesis = require('./testdata/genesishashestest.json').test
t.test('should test genesis hashes (mainnet default)', function (st) {
const genesisBlock = Block.fromBlockData({}, { initWithGenesisHeader: true })
const rlp = genesisBlock.serialize()
st.strictEqual(rlp.toString('hex'), testDataGenesis.genesis_rlp_hex, 'rlp hex match')
const genesis = Block.genesis()
const genesisRlp = genesis.serialize()
st.strictEqual(genesisRlp.toString('hex'), testDataGenesis.genesis_rlp_hex, 'rlp hex match')
st.strictEqual(
genesisBlock.hash().toString('hex'),
genesis.hash().toString('hex'),
testDataGenesis.genesis_hash,
'genesis hash match',
)
Expand All @@ -89,9 +89,9 @@ tape('[Block]: block functions', function (t) {

t.test('should test genesis hashes (ropsten)', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const genesisBlock = Block.fromBlockData({}, { common: common, initWithGenesisHeader: true })
const genesis = Block.genesis({}, { common })
st.strictEqual(
genesisBlock.hash().toString('hex'),
genesis.hash().toString('hex'),
common.genesis().hash.slice(2),
'genesis hash match',
)
Expand All @@ -100,9 +100,9 @@ tape('[Block]: block functions', function (t) {

t.test('should test genesis hashes (rinkeby)', function (st) {
const common = new Common({ chain: 'rinkeby', hardfork: 'chainstart' })
const genesisBlock = Block.fromBlockData({}, { common: common, initWithGenesisHeader: true })
const genesis = Block.genesis({}, { common })
st.strictEqual(
genesisBlock.hash().toString('hex'),
genesis.hash().toString('hex'),
common.genesis().hash.slice(2),
'genesis hash match',
)
Expand All @@ -111,10 +111,10 @@ tape('[Block]: block functions', function (t) {

t.test('should test genesis parameters (ropsten)', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const genesisBlock = Block.fromBlockData({}, { common, initWithGenesisHeader: true })
const genesis = Block.genesis({}, { common })
const ropstenStateRoot = '217b0bbcfb72e2d57e28f33cb361b9983513177755dc3f33ce3e7022ed62b77b'
st.strictEqual(
genesisBlock.header.stateRoot.toString('hex'),
genesis.header.stateRoot.toString('hex'),
ropstenStateRoot,
'genesis stateRoot match',
)
Expand All @@ -135,7 +135,7 @@ tape('[Block]: block functions', function (t) {
const common = new Common({ chain: 'mainnet', hardfork: 'dao' })
st.throws(
function () {
Block.fromValuesArray(blockData, { common: common })
Block.fromValuesArray(blockData, { common })
},
/Error: extraData should be 'dao-hard-fork'$/,
'should throw on DAO HF block with wrong extra data',
Expand All @@ -145,7 +145,7 @@ tape('[Block]: block functions', function (t) {
blockData[0][12] = Buffer.from('64616f2d686172642d666f726b', 'hex')

st.doesNotThrow(function () {
Block.fromValuesArray(blockData, { common: common })
Block.fromValuesArray(blockData, { common })
}, 'should not throw on DAO HF block with correct extra data')
st.end()
})
Expand Down
19 changes: 10 additions & 9 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ tape('[Block]: Header functions', function (t) {

t.test('should test header initialization', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const block1 = Block.fromBlockData(undefined, { common, initWithGenesisHeader: true })
st.ok(block1.hash().toString('hex'), 'block should initialize')
const header = BlockHeader.genesis(undefined, { common })
st.ok(header.hash().toString('hex'), 'block should initialize')
st.end()
})

Expand All @@ -56,16 +56,17 @@ tape('[Block]: Header functions', function (t) {
})

t.test('should test isGenesis', function (st) {
let header = BlockHeader.fromHeaderData({ number: 1 })
st.equal(header.isGenesis(), false)
header = BlockHeader.fromHeaderData({}, { initWithGenesisHeader: true })
st.equal(header.isGenesis(), true)
const header1 = BlockHeader.fromHeaderData({ number: 1 })
st.equal(header1.isGenesis(), false)

const header2 = BlockHeader.genesis()
st.equal(header2.isGenesis(), true)
st.end()
})

t.test('should test genesis hashes (mainnet default)', function (st) {
const testDataGenesis = require('./testdata/genesishashestest.json').test
const header = BlockHeader.fromHeaderData({}, { initWithGenesisHeader: true })
const header = BlockHeader.genesis()
st.strictEqual(
header.hash().toString('hex'),
testDataGenesis.genesis_hash,
Expand All @@ -76,10 +77,10 @@ tape('[Block]: Header functions', function (t) {

t.test('should test genesis parameters (ropsten)', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const genesisHeader = BlockHeader.fromHeaderData({}, { common, initWithGenesisHeader: true })
const genesis = BlockHeader.genesis({}, { common })
const ropstenStateRoot = '217b0bbcfb72e2d57e28f33cb361b9983513177755dc3f33ce3e7022ed62b77b'
st.strictEqual(
genesisHeader.stateRoot.toString('hex'),
genesis.stateRoot.toString('hex'),
ropstenStateRoot,
'genesis stateRoot match',
)
Expand Down
7 changes: 4 additions & 3 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ export default class Blockchain implements BlockchainInterface {
*/
async _setCanonicalGenesisBlock() {
const common = new Common({ chain: this._common.chainId(), hardfork: 'chainstart' })
const genesisBlock = Block.fromBlockData({}, { common, initWithGenesisHeader: true })
await this._putBlockOrHeader(genesisBlock, true)
const genesis = Block.genesis({}, { common })
await this._putBlockOrHeader(genesis, true)
}

/**
Expand Down Expand Up @@ -350,7 +350,8 @@ export default class Blockchain implements BlockchainInterface {

const hash = block.hash()
const { header } = block
const { number, difficulty: td } = header
const { number } = header
const td = header.difficulty.clone()
const currentTd = { header: new BN(0), block: new BN(0) }
const dbOps: DBOp[] = []

Expand Down
Loading