Skip to content

Commit 93d652b

Browse files
authored
upstream: more code review fix (#33)
* add GenesisHeader to ChainHeaderReader * fix check cancun header for parlia * misc
1 parent ba58f57 commit 93d652b

13 files changed

Lines changed: 44 additions & 18 deletions

File tree

cmd/evm/t8n_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ func TestT8n(t *testing.T) {
263263
output: t8nOutput{alloc: true, result: true},
264264
expOut: "exp.json",
265265
},
266-
// TODO(Nathan): Cancun not ready
267266
{ // Cancun tests
268267
base: "./testdata/28",
269268
input: t8nInput{

consensus/consensus.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type ChainHeaderReader interface {
3838
// Config retrieves the blockchain's chain configuration.
3939
Config() *params.ChainConfig
4040

41+
// GenesisHeader retrieves the chain's genesis block header.
42+
GenesisHeader() *types.Header
43+
4144
// CurrentHeader retrieves the current header from the local chain.
4245
CurrentHeader() *types.Header
4346

consensus/parlia/parlia.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,24 @@ func (p *Parlia) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
587587
if header.WithdrawalsHash != nil {
588588
return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash)
589589
}
590-
// Verify the existence / non-existence of excessBlobGas
590+
// Verify the existence / non-existence of cancun-specific header fields
591+
if header.ParentBeaconRoot != nil {
592+
return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", header.ParentBeaconRoot)
593+
}
591594
cancun := chain.Config().IsCancun(header.Number, header.Time)
595+
if !cancun {
596+
switch {
597+
case header.ExcessBlobGas != nil:
598+
return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas)
599+
case header.BlobGasUsed != nil:
600+
return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed)
601+
}
602+
} else {
603+
if err := eip4844.VerifyEIP4844Header(parent, header); err != nil {
604+
return err
605+
}
606+
}
607+
592608
if !cancun && header.ExcessBlobGas != nil {
593609
return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas)
594610
}
@@ -975,8 +991,7 @@ func (p *Parlia) Prepare(chain consensus.ChainHeaderReader, header *types.Header
975991
}
976992

977993
header.Extra = header.Extra[:extraVanity-nextForkHashSize]
978-
genesisTime := chain.GetHeader(p.genesisHash, 0).Time
979-
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, genesisTime, number, header.Time)
994+
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, chain.GenesisHeader().Time, number, header.Time)
980995
header.Extra = append(header.Extra, nextForkHash[:]...)
981996

982997
if err := p.prepareValidators(header); err != nil {
@@ -1117,8 +1132,7 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
11171132
if err != nil {
11181133
return err
11191134
}
1120-
genesisTime := chain.GetHeader(p.genesisHash, 0).Time
1121-
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, genesisTime, number, header.Time)
1135+
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, chain.GenesisHeader().Time, number, header.Time)
11221136
if !snap.isMajorityFork(hex.EncodeToString(nextForkHash[:])) {
11231137
log.Debug("there is a possible fork, and your client is not the majority. Please check...", "nextForkHash", hex.EncodeToString(nextForkHash[:]))
11241138
}

core/blockchain.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
406406
// Make sure the state associated with the block is available, or log out
407407
// if there is no available state, waiting for state sync.
408408
head := bc.CurrentBlock()
409-
if !bc.NoTries() && !bc.HasState(head.Root) {
409+
if !bc.HasState(head.Root) {
410410
if head.Number.Uint64() == 0 {
411411
// The genesis state is missing, which is only possible in the path-based
412412
// scheme. This situation occurs when the initial state sync is not finished
@@ -883,7 +883,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
883883
}
884884
}
885885
if beyondRoot || newHeadBlock.NumberU64() == 0 {
886-
if !bc.NoTries() && !bc.HasState(newHeadBlock.Root()) && bc.stateRecoverable(newHeadBlock.Root()) {
886+
if !bc.HasState(newHeadBlock.Root()) && bc.stateRecoverable(newHeadBlock.Root()) {
887887
// Rewind to a block with recoverable state. If the state is
888888
// missing, run the state recovery here.
889889
if err := bc.triedb.Recover(newHeadBlock.Root()); err != nil {
@@ -914,7 +914,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
914914
// the pivot point. In this scenario, there is no possible recovery
915915
// approach except for rerunning a snap sync. Do nothing here until the
916916
// state syncer picks it up.
917-
if !bc.NoTries() && !bc.HasState(newHeadBlock.Root()) {
917+
if !bc.HasState(newHeadBlock.Root()) {
918918
log.Info("Chain is stateless, wait state sync", "number", newHeadBlock.Number(), "hash", newHeadBlock.Hash())
919919
}
920920
}
@@ -1216,7 +1216,7 @@ func (bc *BlockChain) Stop() {
12161216

12171217
if snapBase != (common.Hash{}) {
12181218
log.Info("Writing snapshot state to disk", "root", snapBase)
1219-
if err := bc.triedb.Commit(snapBase, true); err != nil {
1219+
if err := triedb.Commit(snapBase, true); err != nil {
12201220
log.Error("Failed to commit recent state trie", "err", err)
12211221
} else {
12221222
rawdb.WriteSafePointBlockNumber(bc.db, bc.CurrentBlock().Number.Uint64())

core/blockchain_reader.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {
322322

323323
// HasState checks if state trie is fully present in the database or not.
324324
func (bc *BlockChain) HasState(hash common.Hash) bool {
325-
if bc.stateCache.NoTries() {
325+
if bc.NoTries() {
326326
return bc.snaps != nil && bc.snaps.Snapshot(hash) != nil
327327
}
328328
if bc.pipeCommit && bc.snaps != nil {
@@ -418,6 +418,11 @@ func (bc *BlockChain) Genesis() *types.Block {
418418
return bc.genesisBlock
419419
}
420420

421+
// GenesisHeader retrieves the chain's genesis block header.
422+
func (bc *BlockChain) GenesisHeader() *types.Header {
423+
return bc.genesisBlock.Header()
424+
}
425+
421426
// TxIndexProgress returns the transaction indexing progress.
422427
func (bc *BlockChain) TxIndexProgress() (TxIndexProgress, error) {
423428
if bc.txIndexer == nil {

core/chain_makers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ func (cm *chainMaker) GetHeaderByHash(hash common.Hash) *types.Header {
569569
return b.Header()
570570
}
571571

572+
func (cm *chainMaker) GenesisHeader() *types.Header {
573+
panic("not supported")
574+
}
575+
572576
func (cm *chainMaker) GetHeader(hash common.Hash, number uint64) *types.Header {
573577
return cm.GetHeaderByNumber(number)
574578
}

core/headerchain.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ func (hc *HeaderChain) getFinalizedNumber(header *types.Header) uint64 {
134134
return 0
135135
}
136136

137+
func (hc *HeaderChain) GenesisHeader() *types.Header {
138+
panic("not supported")
139+
}
140+
137141
// GetBlockNumber retrieves the block number belonging to the given hash
138142
// from the cache or database
139143
func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {

core/rawdb/ancient_utils.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func inspectFreezers(db ethdb.Database) ([]freezerInfo, error) {
9898
if err != nil {
9999
return nil, err
100100
}
101-
// TODO, offset=0?
102101
f, err := NewStateFreezer(datadir, true, 0)
103102
if err != nil {
104103
return nil, err

eth/downloader/downloader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ type BlockChain interface {
211211
type DownloadOption func(downloader *Downloader) *Downloader
212212

213213
// New creates a new downloader to fetch hashes and blocks from remote peers.
214-
func New(stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn, success func()) *Downloader {
214+
func New(stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, lightchain LightChain, dropPeer peerDropFn, _ func()) *Downloader {
215215
if lightchain == nil {
216216
lightchain = chain
217217
}
@@ -230,7 +230,6 @@ func New(stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, lightchai
230230
syncStartBlock: chain.CurrentSnapBlock().Number.Uint64(),
231231
}
232232

233-
// TODO how to use success
234233
go dl.stateFetcher()
235234
return dl
236235
}

eth/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func newHandler(config *handlerConfig) (*handler, error) {
219219
}
220220
h.snapSync.Store(true)
221221
log.Warn("Switch sync mode from full sync to snap sync", "reason", "snap sync incomplete")
222-
} else if !h.chain.NoTries() && !h.chain.HasState(fullBlock.Root) {
222+
} else if !h.chain.HasState(fullBlock.Root) {
223223
h.snapSync.Store(true)
224224
log.Warn("Switch sync mode from full sync to snap sync", "reason", "head state missing")
225225
}

0 commit comments

Comments
 (0)