Skip to content

Commit 2165d23

Browse files
ryanioholgerd77
authored andcommitted
common/blockchain genesis refactor (#1916)
1 parent c91b566 commit 2165d23

File tree

87 files changed

+655
-1191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+655
-1191
lines changed

.github/workflows/e2e-hardhat.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
branches: [master]
55
tags: ['*']
66
pull_request:
7+
branches: [master]
78
types: [opened, reopened, synchronize]
89

910
env:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Detailed version can be seen on [Codecov.io][coverage-link]
4848
trie --> client
4949
trie --> vm
5050
trie --> block
51+
trie --> blockchain
5152
common --> block
5253
common --> blockchain
5354
common --> tx

packages/block/src/block.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,6 @@ export class Block {
128128
return new Block(header, transactions, uncleHeaders, opts)
129129
}
130130

131-
/**
132-
* Alias for {@link Block.fromBlockData} with {@link BlockOptions.initWithGenesisHeader} set to true.
133-
*/
134-
public static genesis(blockData: BlockData = {}, opts?: BlockOptions) {
135-
opts = { ...opts, initWithGenesisHeader: true }
136-
return Block.fromBlockData(blockData, opts)
137-
}
138-
139131
/**
140132
* This constructor takes the values, validates them, assigns them and freezes the object.
141133
* Use the static factory methods to assist in creating a Block object from varying data types and options.
@@ -185,7 +177,7 @@ export class Block {
185177
}
186178

187179
/**
188-
* Produces a hash the RLP of the block.
180+
* Returns the hash of the block.
189181
*/
190182
hash(): Buffer {
191183
return this.header.hash()

packages/block/src/header.ts

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
intToBuffer,
2020
KECCAK256_RLP_ARRAY,
2121
KECCAK256_RLP,
22-
toBuffer,
2322
toType,
2423
TypeOutput,
2524
zeros,
@@ -163,15 +162,6 @@ export class BlockHeader {
163162
opts
164163
)
165164
}
166-
167-
/**
168-
* Alias for {@link BlockHeader.fromHeaderData} with {@link BlockOptions.initWithGenesisHeader} set to true.
169-
*/
170-
public static genesis(headerData: HeaderData = {}, opts?: BlockOptions) {
171-
opts = { ...opts, initWithGenesisHeader: true }
172-
return BlockHeader.fromHeaderData(headerData, opts)
173-
}
174-
175165
/**
176166
* This constructor takes the values, validates them, assigns them and freezes the object.
177167
*
@@ -186,9 +176,6 @@ export class BlockHeader {
186176
this._common = new Common({
187177
chain: Chain.Mainnet, // default
188178
})
189-
if (options.initWithGenesisHeader) {
190-
this._common.setHardforkByBlockNumber(0)
191-
}
192179
}
193180

194181
if (options.hardforkByBlockNumber !== undefined && options.hardforkByTD !== undefined) {
@@ -221,19 +208,19 @@ export class BlockHeader {
221208
const coinbase = headerData.coinbase
222209
? new Address(toType(headerData.coinbase, TypeOutput.Buffer))
223210
: defaults.coinbase
224-
let stateRoot = toType(headerData.stateRoot, TypeOutput.Buffer) ?? defaults.stateRoot
211+
const stateRoot = toType(headerData.stateRoot, TypeOutput.Buffer) ?? defaults.stateRoot
225212
const transactionsTrie =
226213
toType(headerData.transactionsTrie, TypeOutput.Buffer) ?? defaults.transactionsTrie
227214
const receiptTrie = toType(headerData.receiptTrie, TypeOutput.Buffer) ?? defaults.receiptTrie
228215
const logsBloom = toType(headerData.logsBloom, TypeOutput.Buffer) ?? defaults.logsBloom
229-
let difficulty = toType(headerData.difficulty, TypeOutput.BigInt) ?? defaults.difficulty
230-
let number = toType(headerData.number, TypeOutput.BigInt) ?? defaults.number
231-
let gasLimit = toType(headerData.gasLimit, TypeOutput.BigInt) ?? defaults.gasLimit
216+
const difficulty = toType(headerData.difficulty, TypeOutput.BigInt) ?? defaults.difficulty
217+
const number = toType(headerData.number, TypeOutput.BigInt) ?? defaults.number
218+
const gasLimit = toType(headerData.gasLimit, TypeOutput.BigInt) ?? defaults.gasLimit
232219
const gasUsed = toType(headerData.gasUsed, TypeOutput.BigInt) ?? defaults.gasUsed
233-
let timestamp = toType(headerData.timestamp, TypeOutput.BigInt) ?? defaults.timestamp
234-
let extraData = toType(headerData.extraData, TypeOutput.Buffer) ?? defaults.extraData
220+
const timestamp = toType(headerData.timestamp, TypeOutput.BigInt) ?? defaults.timestamp
221+
const extraData = toType(headerData.extraData, TypeOutput.Buffer) ?? defaults.extraData
235222
const mixHash = toType(headerData.mixHash, TypeOutput.Buffer) ?? defaults.mixHash
236-
let nonce = toType(headerData.nonce, TypeOutput.Buffer) ?? defaults.nonce
223+
const nonce = toType(headerData.nonce, TypeOutput.Buffer) ?? defaults.nonce
237224
let baseFeePerGas =
238225
toType(headerData.baseFeePerGas, TypeOutput.BigInt) ?? defaults.baseFeePerGas
239226

@@ -260,34 +247,6 @@ export class BlockHeader {
260247
}
261248
}
262249

263-
if (options.initWithGenesisHeader) {
264-
number = BigInt(0)
265-
if (gasLimit === DEFAULT_GAS_LIMIT) {
266-
gasLimit = BigInt(this._common.genesis().gasLimit)
267-
}
268-
if (timestamp === BigInt(0)) {
269-
timestamp = BigInt(this._common.genesis().timestamp ?? 0)
270-
}
271-
if (difficulty === BigInt(0)) {
272-
difficulty = BigInt(this._common.genesis().difficulty)
273-
}
274-
if (extraData.length === 0) {
275-
extraData = toBuffer(this._common.genesis().extraData)
276-
}
277-
if (nonce.equals(zeros(8))) {
278-
nonce = toBuffer(this._common.genesis().nonce)
279-
}
280-
if (stateRoot.equals(zeros(32))) {
281-
stateRoot = toBuffer(this._common.genesis().stateRoot)
282-
}
283-
if (
284-
this._common.gteHardfork(Hardfork.London) &&
285-
this._common.genesis().baseFeePerGas !== undefined
286-
) {
287-
baseFeePerGas = BigInt(this._common.genesis().baseFeePerGas!)
288-
}
289-
}
290-
291250
this.parentHash = parentHash
292251
this.uncleHash = uncleHash
293252
this.coinbase = coinbase

packages/block/src/types.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,6 @@ export interface BlockOptions {
3939
* pointing to a Shanghai block: this will lead to set the HF as Shanghai and not the Merge).
4040
*/
4141
hardforkByTD?: BigIntLike
42-
/**
43-
* Turns the block header into the canonical genesis block header
44-
*
45-
* If set to `true` all other header data is ignored.
46-
*
47-
* If a {@link Common} instance is passed the instance need to be set to `chainstart` as a HF,
48-
* otherwise usage of this option will throw
49-
*
50-
* Default: `false`
51-
*/
52-
initWithGenesisHeader?: boolean
53-
5442
/**
5543
* If a preceding {@link BlockHeader} (usually the parent header) is given the preceding
5644
* header will be used to calculate the difficulty for this block and the calculated

packages/block/test/block.spec.ts

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import util from 'util'
2121
tape('[Block]: block functions', function (t) {
2222
t.test('should test block initialization', function (st) {
2323
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
24-
const genesis = Block.genesis({}, { common })
24+
const genesis = Block.fromBlockData({}, { common })
2525
st.ok(genesis.hash().toString('hex'), 'block should initialize')
2626

2727
// test default freeze values
@@ -181,7 +181,7 @@ tape('[Block]: block functions', function (t) {
181181
const blockchain = new Mockchain()
182182
const block = blockFromRpc(testDataFromRpcGoerli, [], { common })
183183

184-
const genesis = Block.genesis({}, { common })
184+
const genesis = Block.fromBlockData({}, { common })
185185
await blockchain.putBlock(genesis)
186186

187187
const parentBlock = Block.fromBlockData(
@@ -262,7 +262,7 @@ tape('[Block]: block functions', function (t) {
262262
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
263263
const blockchain = new Mockchain()
264264

265-
const genesis = Block.genesis({})
265+
const genesis = Block.fromBlockData({})
266266
await blockchain.putBlock(genesis)
267267

268268
const uncleBlock1 = createBlock(genesis, 'uncle', [], common)
@@ -286,7 +286,7 @@ tape('[Block]: block functions', function (t) {
286286
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
287287
const blockchain = new Mockchain()
288288

289-
const genesis = Block.genesis({})
289+
const genesis = Block.fromBlockData({})
290290
await blockchain.putBlock(genesis)
291291

292292
const uncleBlock = createBlock(genesis, 'uncle', [], common)
@@ -319,7 +319,7 @@ tape('[Block]: block functions', function (t) {
319319
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
320320
const blockchain = new Mockchain()
321321

322-
const genesis = Block.genesis({})
322+
const genesis = Block.fromBlockData({})
323323
await blockchain.putBlock(genesis)
324324

325325
const emptyBlock = Block.fromBlockData({ header: { number: BigInt(1) } }, { common })
@@ -354,7 +354,7 @@ tape('[Block]: block functions', function (t) {
354354
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
355355
const blockchain = new Mockchain()
356356

357-
const genesis = Block.genesis({})
357+
const genesis = Block.fromBlockData({})
358358
await blockchain.putBlock(genesis)
359359

360360
const uncleBlock = createBlock(genesis, 'uncle', [], common)
@@ -385,7 +385,7 @@ tape('[Block]: block functions', function (t) {
385385
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
386386
const blockchain = new Mockchain()
387387

388-
const genesis = Block.genesis({})
388+
const genesis = Block.fromBlockData({})
389389
await blockchain.putBlock(genesis)
390390

391391
const uncleBlock = createBlock(genesis, 'uncle', [], common)
@@ -406,7 +406,7 @@ tape('[Block]: block functions', function (t) {
406406
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
407407
const blockchain = new Mockchain()
408408

409-
const genesis = Block.genesis({})
409+
const genesis = Block.fromBlockData({})
410410
await blockchain.putBlock(genesis)
411411

412412
const uncleBlock = Block.fromBlockData(
@@ -441,7 +441,7 @@ tape('[Block]: block functions', function (t) {
441441
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
442442
const blockchain = new Mockchain()
443443

444-
const genesis = Block.genesis({})
444+
const genesis = Block.fromBlockData({})
445445
await blockchain.putBlock(genesis)
446446

447447
const uncleBlock1 = createBlock(genesis, 'uncle1', [], common)
@@ -482,7 +482,7 @@ tape('[Block]: block functions', function (t) {
482482
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
483483
const blockchain = new Mockchain()
484484

485-
const genesis = Block.genesis({})
485+
const genesis = Block.fromBlockData({})
486486
await blockchain.putBlock(genesis)
487487

488488
const block1 = createBlock(genesis, 'block1', [], common)
@@ -503,7 +503,7 @@ tape('[Block]: block functions', function (t) {
503503
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
504504
const blockchain = new Mockchain()
505505

506-
const genesis = Block.genesis({})
506+
const genesis = Block.fromBlockData({})
507507
await blockchain.putBlock(genesis)
508508

509509
const uncleBlock = createBlock(genesis, 'uncle', [], common)
@@ -658,41 +658,11 @@ tape('[Block]: block functions', function (t) {
658658
})
659659

660660
t.test('should test genesis hashes (mainnet default)', function (st) {
661-
const genesis = Block.genesis()
662-
const genesisRlp = genesis.serialize()
663-
st.ok(
664-
genesisRlp.equals(Buffer.from(testDataGenesis.test.genesis_rlp_hex, 'hex')),
665-
'rlp hex match'
666-
)
667-
st.ok(
668-
genesis.hash().equals(Buffer.from(testDataGenesis.test.genesis_hash, 'hex')),
669-
'genesis hash match'
670-
)
671-
st.end()
672-
})
673-
674-
t.test('should test genesis hashes (ropsten)', function (st) {
675661
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Chainstart })
676-
const genesis = Block.genesis({}, { common })
677-
st.ok(genesis.hash().equals(toBuffer(common.genesis().hash)), 'genesis hash match')
678-
st.end()
679-
})
680-
681-
t.test('should test genesis hashes (rinkeby)', function (st) {
682-
const common = new Common({ chain: Chain.Rinkeby, hardfork: Hardfork.Chainstart })
683-
const genesis = Block.genesis({}, { common })
684-
st.ok(genesis.hash().equals(toBuffer(common.genesis().hash)), 'genesis hash match')
685-
st.end()
686-
})
687-
688-
t.test('should test genesis parameters (ropsten)', function (st) {
689-
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Chainstart })
690-
const genesis = Block.genesis({}, { common })
691-
const ropstenStateRoot = Buffer.from(
692-
'217b0bbcfb72e2d57e28f33cb361b9983513177755dc3f33ce3e7022ed62b77b',
693-
'hex'
694-
)
695-
st.ok(genesis.header.stateRoot.equals(ropstenStateRoot), 'genesis stateRoot match')
662+
const rlp = Buffer.from(testDataGenesis.test.genesis_rlp_hex, 'hex')
663+
const hash = Buffer.from(testDataGenesis.test.genesis_hash, 'hex')
664+
const block = Block.fromRLPSerializedBlock(rlp, { common })
665+
st.ok(block.hash().equals(hash), 'genesis hash match')
696666
st.end()
697667
})
698668

@@ -746,7 +716,7 @@ tape('[Block]: block functions', function (t) {
746716
'should set canonical difficulty if I provide a calcDifficultyFromHeader header',
747717
function (st) {
748718
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
749-
const genesis = Block.genesis({}, { common })
719+
const genesis = Block.fromBlockData({}, { common })
750720

751721
const nextBlockHeaderData = {
752722
number: genesis.header.number + BigInt(1),

packages/block/test/clique.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ tape('[Header]: Clique PoA Functionality', function (t) {
1212
header.cliqueIsEpochTransition()
1313
}, 'cliqueIsEpochTransition() -> should throw on PoW networks')
1414

15-
header = BlockHeader.genesis({}, { common })
15+
header = BlockHeader.fromHeaderData({}, { common })
1616
st.ok(
1717
header.cliqueIsEpochTransition(),
1818
'cliqueIsEpochTransition() -> should indicate an epoch transition for the genesis block'

0 commit comments

Comments
 (0)