-
Notifications
You must be signed in to change notification settings - Fork 765
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
Changes from 1 commit
c053761
ff46c55
dd228bc
608d804
17a33a8
72b5b78
8adcdf9
8987003
5f5e9f2
5351fb8
ce1dac1
526f986
75689e6
8923f71
6a2c193
381f5e1
7eecf80
395c6f8
be5c8d2
7fa486d
91d45d7
7e788c3
bc459e8
ea8a401
b694010
1f66378
9f9bab0
7ce9132
a5d3d14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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)) | ||
} | ||
|
@@ -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`) | ||
} | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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', | ||
) | ||
|
@@ -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', | ||
) | ||
|
@@ -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', | ||
) | ||
|
@@ -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', | ||
) | ||
|
@@ -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', | ||
|
@@ -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() | ||
}) | ||
|
There was a problem hiding this comment.
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 theBlock.fromBlockData()
variant?There was a problem hiding this comment.
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.