Skip to content

Commit e59d4e7

Browse files
acolytec3ScottyPoi
authored andcommitted
Blockchain, Client, VM: Head function rename rebase (#1822)
* blockchain/tests: remove tests for getHead() * vm: change blockchain.getHead() to blockchain.getIteratorHead() * vm: remove tests for blockchain.getHead() * client: change blockchain.getHead() to blockchain.getIteratorHead() * blockchain: remove setHead() for setIteratorHead() * blockchain: edit text of setIteratorHead() test * blockchain: rename getLatestHeader() to getCanonicalHeadHeader() * blockchain/tests: rename getLatestHeader() to getCanonicalHeadHeader() * client: rename blockchain.getLatestHeader() to blockchain.getCanonicalHeadHeader() * client/tests: rename blockchain.getLatestHeader() to blockchain.getCanonicalHeadHeader() * blockchain/tests: rename getLatestBlock() to getCanonicalHeadBlock() * client: rename blockchain.getLatestBlock to blockchain.getCanonicalBlockHeader() * client/tests: rename blockchain.getLatestBlock to blockchain.getCanonicalBlockHeader() * blockchain: remove get meta() * VM: Remove blockchain.meta from tests * Blockchain: Remove blockchain.meta from tests * blockchain: fix linting errors * BN to BigInt misses from rebase * BN to BigInt fixes + lint * Fix missing name replacement * lint * Add back blockchain testrunner last header check * Add test for call with no gas * Add back correct renamed functions * lint fixes Co-authored-by: ScottyPoi <scott.simpson@ethereum.org>
1 parent 7ab0e85 commit e59d4e7

30 files changed

+92
-176
lines changed

packages/blockchain/src/index.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,6 @@ export default class Blockchain implements BlockchainInterface {
297297
}
298298
}
299299

300-
/**
301-
* Returns an object with metadata about the Blockchain. It's defined for
302-
* backwards compatibility.
303-
*/
304-
get meta() {
305-
return {
306-
rawHead: this._headHeaderHash,
307-
heads: this._heads,
308-
genesis: this._genesis,
309-
}
310-
}
311-
312300
/**
313301
* Returns a deep copy of this {@link Blockchain} instance.
314302
*
@@ -795,7 +783,7 @@ export default class Blockchain implements BlockchainInterface {
795783
/**
796784
* Returns the latest header in the canonical chain.
797785
*/
798-
async getLatestHeader(): Promise<BlockHeader> {
786+
async getCanonicalHeadHeader(): Promise<BlockHeader> {
799787
return await this.runWithLock<BlockHeader>(async () => {
800788
if (!this._headHeaderHash) {
801789
throw new Error('No head header set')
@@ -808,7 +796,7 @@ export default class Blockchain implements BlockchainInterface {
808796
/**
809797
* Returns the latest full block in the canonical chain.
810798
*/
811-
async getLatestBlock(): Promise<Block> {
799+
async getCanonicalHeadBlock(): Promise<Block> {
812800
return this.runWithLock<Block>(async () => {
813801
if (!this._headBlockHash) {
814802
throw new Error('No head block set')
@@ -1311,18 +1299,6 @@ export default class Blockchain implements BlockchainInterface {
13111299
* @param headHash - The head hash to save
13121300
*/
13131301
async setIteratorHead(tag: string, headHash: Buffer) {
1314-
return await this.setHead(tag, headHash)
1315-
}
1316-
1317-
/**
1318-
* Set header hash of a certain `tag`.
1319-
* When calling the iterator, the iterator will start running the first child block after the header hash currently stored.
1320-
* @param tag - The tag to save the headHash to
1321-
* @param headHash - The head hash to save
1322-
*
1323-
* @deprecated use {@link Blockchain.setIteratorHead()} instead
1324-
*/
1325-
async setHead(tag: string, headHash: Buffer) {
13261302
await this.runWithLock<void>(async () => {
13271303
this._heads[tag] = headHash
13281304
await this._saveHeads()
@@ -1592,8 +1568,7 @@ export default class Blockchain implements BlockchainInterface {
15921568
if (signerIndex === -1) {
15931569
throw new Error('Signer not found')
15941570
}
1595-
const { number } = await this.getLatestHeader()
1596-
//eslint-disable-next-line
1571+
const { number } = await this.getCanonicalHeadHeader()
15971572
return (number + BigInt(1)) % BigInt(signers.length) === BigInt(signerIndex)
15981573
}
15991574
}

packages/blockchain/test/clique.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ tape('Clique: Initialization', (t) => {
1010
const common = new Common({ chain: Chain.Rinkeby, hardfork: Hardfork.Chainstart })
1111
const blockchain = await Blockchain.create({ common })
1212

13-
const head = await blockchain.getHead()
14-
st.equal(head.hash().toString('hex'), common.genesis().hash.slice(2), 'correct genesis hash')
13+
const head = await blockchain.getIteratorHead()
14+
st.equals(head.hash().toString('hex'), common.genesis().hash.slice(2), 'correct genesis hash')
1515

1616
st.deepEquals(
1717
blockchain.cliqueActiveSigners(),

packages/blockchain/test/index.spec.ts

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,17 @@ tape('blockchain test', (t) => {
1414
validateBlocks: true,
1515
validateConsensus: false,
1616
})
17-
await blockchain.getHead()
17+
await blockchain.getIteratorHead()
1818
st.end()
1919
})
2020

2121
t.test('should initialize correctly', async (st) => {
2222
const common = new Common({ chain: Chain.Ropsten })
2323
let blockchain = await Blockchain.create({ common })
2424

25-
const head = await blockchain.getHead()
2625
const iteratorHead = await blockchain.getIteratorHead()
2726

28-
st.equal(
29-
head.hash().toString('hex'),
30-
common.genesis().hash.slice(2),
31-
'correct genesis hash (getHead())'
32-
)
33-
st.equal(
27+
st.equals(
3428
iteratorHead.hash().toString('hex'),
3529
common.genesis().hash.slice(2),
3630
'correct genesis hash (getIteratorHead())'
@@ -52,10 +46,8 @@ tape('blockchain test', (t) => {
5246
validateConsensus: false,
5347
common,
5448
})
55-
56-
const head = await blockchain.getHead()
57-
58-
st.equal(head.header.number, BigInt(5), 'correct block number')
49+
const head = await blockchain.getIteratorHead()
50+
st.equals(head.header.number, BigInt(0), 'correct block number')
5951
st.end()
6052
})
6153

@@ -81,7 +73,7 @@ tape('blockchain test', (t) => {
8173
genesisBlock,
8274
})
8375
st.ok(
84-
genesisBlock.hash().equals(blockchain.meta.genesis!),
76+
genesisBlock.hash().equals((await blockchain.getCanonicalHeadHeader()).hash()),
8577
'genesis block hash should be correct'
8678
)
8779
st.end()
@@ -450,15 +442,15 @@ tape('blockchain test', (t) => {
450442
// Note: if st.end() is not called (Promise did not throw), then this test fails, as it does not end.
451443
})
452444

453-
t.test('should test setHead (@deprecated)/setIteratorHead method', async (st) => {
445+
t.test('should test setIteratorHead method', async (st) => {
454446
const { blockchain, blocks, error } = await generateBlockchain(25)
455447
st.error(error, 'no error')
456448

457449
const headBlockIndex = 5
458450

459451
const headHash = blocks[headBlockIndex].hash()
460452
await blockchain.setIteratorHead('myHead', headHash)
461-
const currentHeadBlock = await blockchain.getHead('myHead')
453+
const currentHeadBlock = await blockchain.getIteratorHead('myHead')
462454

463455
st.ok(headHash.equals(currentHeadBlock.hash()), 'head hash equals the provided head hash')
464456

@@ -507,21 +499,6 @@ tape('blockchain test', (t) => {
507499
st.end()
508500
})
509501

510-
t.test('should get meta.genesis', async (st) => {
511-
const { blockchain, blocks, error } = await generateBlockchain(25)
512-
st.error(error, 'no error')
513-
st.ok(blockchain.meta.rawHead.equals(blocks[24].hash()), 'should get meta.rawHead')
514-
st.ok(blockchain.meta.genesis.equals(blocks[0].hash()), 'should get meta.genesis')
515-
let i = 0
516-
await blockchain.iterator('test', (block: Block) => {
517-
if (block.hash().equals(blocks[i + 1].hash())) {
518-
i++
519-
}
520-
})
521-
st.ok(blockchain.meta.heads['test'], 'should get meta.heads')
522-
st.end()
523-
})
524-
525502
t.test('should add fork header and reset stale heads', async (st) => {
526503
const { blockchain, blocks, error } = await generateBlockchain(15)
527504
st.error(error, 'no error')
@@ -634,7 +611,7 @@ tape('blockchain test', (t) => {
634611
t.test('should get heads', async (st) => {
635612
const [db, genesis] = await createTestDB()
636613
const blockchain = await Blockchain.create({ db: db })
637-
const head = await blockchain.getHead()
614+
const head = await blockchain.getIteratorHead()
638615
if (genesis) {
639616
st.ok(head.hash().equals(genesis.hash()), 'should get head')
640617
st.equal(
@@ -736,10 +713,10 @@ tape('blockchain test', (t) => {
736713
genesisBlock,
737714
})
738715

739-
const latestHeader = await blockchain.getLatestHeader()
716+
const latestHeader = await blockchain.getCanonicalHeadHeader()
740717
st.ok(latestHeader.hash().equals(header.hash()), 'should save headHeader')
741718

742-
const latestBlock = await blockchain.getLatestBlock()
719+
const latestBlock = await blockchain.getCanonicalHeadBlock()
743720
st.ok(latestBlock.hash().equals(genesisBlock.hash()), 'should save headBlock')
744721
st.end()
745722
})
@@ -789,18 +766,18 @@ tape('blockchain test', (t) => {
789766

790767
await blockchain.putHeaders(headers)
791768

792-
const latestHeader = await blockchain.getLatestHeader()
769+
const latestHeader = await blockchain.getCanonicalHeadHeader()
793770
st.ok(latestHeader.hash().equals(headers[1].hash()), 'should update latest header')
794771

795-
const latestBlock = await blockchain.getLatestBlock()
772+
const latestBlock = await blockchain.getCanonicalHeadBlock()
796773
st.ok(latestBlock.hash().equals(genesisBlock.hash()), 'should not change latest block')
797774

798775
await blockchain.putBlock(block)
799776

800-
const latestHeader2 = await blockchain.getLatestHeader()
777+
const latestHeader2 = await blockchain.getCanonicalHeadHeader()
801778
st.ok(latestHeader2.hash().equals(headers[1].hash()), 'should not change latest header')
802779

803-
const getBlock = await blockchain.getLatestBlock()
780+
const getBlock = await blockchain.getCanonicalHeadBlock()
804781
st.ok(getBlock!.hash().equals(block.hash()), 'should update latest block')
805782
st.end()
806783
})
@@ -868,7 +845,7 @@ tape('initialization tests', (t) => {
868845
const blockchain = await Blockchain.create({ common })
869846

870847
st.ok(
871-
(await blockchain.getHead()).hash().equals(genesisHash),
848+
(await blockchain.getIteratorHead()).hash().equals(genesisHash),
872849
'head hash should equal expected ropsten genesis hash'
873850
)
874851

@@ -877,7 +854,7 @@ tape('initialization tests', (t) => {
877854
const newBlockchain = await Blockchain.create({ db, common })
878855

879856
st.ok(
880-
(await newBlockchain.getHead()).hash().equals(genesisHash),
857+
(await newBlockchain.getIteratorHead()).hash().equals(genesisHash),
881858
'head hash should be read from the provided db'
882859
)
883860
st.end()
@@ -894,13 +871,13 @@ tape('initialization tests', (t) => {
894871
const db = blockchain.db
895872

896873
st.ok(
897-
(await blockchain.getHead()).hash().equals(hash),
874+
(await blockchain.getIteratorHead()).hash().equals(hash),
898875
'blockchain should put custom genesis block'
899876
)
900877

901878
const newBlockchain = await Blockchain.create({ db, genesisBlock })
902879
st.ok(
903-
(await newBlockchain.getHead()).hash().equals(hash),
880+
(await newBlockchain.getIteratorHead()).hash().equals(hash),
904881
'head hash should be read from the provided db'
905882
)
906883
st.end()

packages/blockchain/test/pos.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ tape('Proof of Stake - inserting blocks into blockchain', async (t) => {
6161
common: s.common,
6262
hardforkByHeadBlockNumber: true,
6363
})
64-
const genesisHeader = await blockchain.getLatestHeader()
64+
const genesisHeader = await blockchain.getCanonicalHeadHeader()
6565

6666
t.equal(
6767
genesisHeader.hash().toString('hex'),
@@ -70,7 +70,7 @@ tape('Proof of Stake - inserting blocks into blockchain', async (t) => {
7070
)
7171
await buildChain(blockchain, s.common, 15)
7272

73-
const latestHeader = await blockchain.getLatestHeader()
73+
const latestHeader = await blockchain.getCanonicalHeadHeader()
7474
t.equal(latestHeader.number, BigInt(15), 'blockchain is at correct height')
7575

7676
t.equal(

packages/blockchain/test/reorg.spec.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ tape('reorg tests', (t) => {
2121
},
2222
{ common }
2323
)
24-
const blockchain = await Blockchain.create({
25-
validateBlocks: true,
26-
validateConsensus: false,
27-
common,
28-
genesisBlock: genesis,
29-
})
3024

3125
const blocks_lowTD: Block[] = []
3226
const blocks_highTD: Block[] = []
@@ -64,23 +58,6 @@ tape('reorg tests', (t) => {
6458
'low TD block should have a higher number than high TD block'
6559
)
6660

67-
await blockchain.putBlocks(blocks_lowTD)
68-
69-
const head_lowTD = await blockchain.getHead()
70-
71-
await blockchain.putBlocks(blocks_highTD)
72-
73-
const head_highTD = await blockchain.getHead()
74-
75-
t.ok(
76-
head_lowTD.hash().equals(lowTDBlock.hash()),
77-
'head on the low TD chain should equal the low TD block'
78-
)
79-
t.ok(
80-
head_highTD.hash().equals(highTDBlock.hash()),
81-
'head on the high TD chain should equal the high TD block'
82-
)
83-
8461
st.end()
8562
}
8663
)
@@ -170,19 +147,8 @@ tape('reorg tests', (t) => {
170147
)
171148

172149
await blockchain.putBlocks([block1_low, block2_low])
173-
const head_low = await blockchain.getHead()
174150

175151
await blockchain.putBlocks([block1_high, block2_high, block3_high])
176-
const head_high = await blockchain.getHead()
177-
178-
t.ok(
179-
head_low.hash().equals(block2_low.hash()),
180-
'head on the low chain should equal the low block'
181-
)
182-
t.ok(
183-
head_high.hash().equals(block3_high.hash()),
184-
'head on the high chain should equal the high block'
185-
)
186152

187153
let signerStates = (blockchain as any)._cliqueLatestSignerStates
188154
t.ok(

packages/client/bin/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ async function startBlock(client: EthereumClient) {
329329
if (!args.startBlock) return
330330
const startBlock = BigInt(args.startBlock)
331331
const { blockchain } = client.chain
332-
const height = (await blockchain.getLatestHeader()).number
332+
const height = (await blockchain.getCanonicalHeadHeader()).number
333333
if (height === startBlock) return
334334
if (height < startBlock) {
335335
logger.error(`Cannot start chain higher than current height ${height}`)

packages/client/lib/blockchain/chain.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class Chain {
186186
const block = this.config.chainCommon.hardforkBlock()
187187
this.config.logger.info(`New hardfork reached 🪢 ! hardfork=${hardfork} block=${block}`)
188188
} else {
189-
const block = await this.getLatestBlock()
189+
const block = await this.getCanonicalHeadBlock()
190190
const num = block.header.number
191191
const td = await this.blockchain.getTotalDifficulty(block.hash(), num)
192192
this.config.logger.info(`Merge hardfork reached 🐼 👉 👈 🐼 ! block=${num} td=${td}`)
@@ -225,8 +225,8 @@ export class Chain {
225225
height: BigInt(0),
226226
}
227227

228-
headers.latest = await this.getLatestHeader()
229-
blocks.latest = await this.getLatestBlock()
228+
headers.latest = await this.getCanonicalHeadHeader()
229+
blocks.latest = await this.getCanonicalHeadBlock()
230230

231231
headers.height = headers.latest.number
232232
blocks.height = blocks.latest.header.number
@@ -350,17 +350,17 @@ export class Chain {
350350
/**
351351
* Gets the latest header in the canonical chain
352352
*/
353-
async getLatestHeader(): Promise<BlockHeader> {
353+
async getCanonicalHeadHeader(): Promise<BlockHeader> {
354354
if (!this.opened) throw new Error('Chain closed')
355-
return this.blockchain.getLatestHeader()
355+
return this.blockchain.getCanonicalHeadHeader()
356356
}
357357

358358
/**
359359
* Gets the latest block in the canonical chain
360360
*/
361-
async getLatestBlock(): Promise<Block> {
361+
async getCanonicalHeadBlock(): Promise<Block> {
362362
if (!this.opened) throw new Error('Chain closed')
363-
return this.blockchain.getLatestBlock()
363+
return this.blockchain.getCanonicalHeadBlock()
364364
}
365365

366366
/**

packages/client/lib/execution/vmexecution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class VMExecution extends Execution {
139139

140140
const { blockchain } = this.vm
141141
let startHeadBlock = await blockchain.getIteratorHead()
142-
let canonicalHead = await blockchain.getLatestBlock()
142+
let canonicalHead = await blockchain.getCanonicalHeadBlock()
143143

144144
let headBlock: Block | undefined
145145
let parentState: Buffer | undefined
@@ -271,7 +271,7 @@ export class VMExecution extends Execution {
271271
)
272272
}
273273
startHeadBlock = endHeadBlock
274-
canonicalHead = await this.vm.blockchain.getLatestBlock()
274+
canonicalHead = await this.vm.blockchain.getCanonicalHeadBlock()
275275
}
276276
this.running = false
277277
return numExecuted ?? 0

packages/client/lib/rpc/modules/admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Admin {
4242

4343
// TODO version not present in reference..
4444
// const ethVersion = Math.max.apply(Math, this._ethProtocol.versions)
45-
const latestHeader = await this._chain.getLatestHeader()
45+
const latestHeader = await this._chain.getCanonicalHeadHeader()
4646
const difficulty = latestHeader.difficulty.toString()
4747
const genesis = bufferToHex(this._chain.genesis.hash)
4848
const head = bufferToHex(latestHeader.mixHash)

0 commit comments

Comments
 (0)