Skip to content

Commit

Permalink
upstream: more code review fix (#33)
Browse files Browse the repository at this point in the history
* add GenesisHeader to ChainHeaderReader

* fix check cancun header for parlia

* misc
  • Loading branch information
buddh0 committed Mar 1, 2024
1 parent ba58f57 commit 93d652b
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 18 deletions.
1 change: 0 additions & 1 deletion cmd/evm/t8n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ func TestT8n(t *testing.T) {
output: t8nOutput{alloc: true, result: true},
expOut: "exp.json",
},
// TODO(Nathan): Cancun not ready
{ // Cancun tests
base: "./testdata/28",
input: t8nInput{
Expand Down
3 changes: 3 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type ChainHeaderReader interface {
// Config retrieves the blockchain's chain configuration.
Config() *params.ChainConfig

// GenesisHeader retrieves the chain's genesis block header.
GenesisHeader() *types.Header

// CurrentHeader retrieves the current header from the local chain.
CurrentHeader() *types.Header

Expand Down
24 changes: 19 additions & 5 deletions consensus/parlia/parlia.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,24 @@ func (p *Parlia) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
if header.WithdrawalsHash != nil {
return fmt.Errorf("invalid withdrawalsHash: have %x, expected nil", header.WithdrawalsHash)
}
// Verify the existence / non-existence of excessBlobGas
// Verify the existence / non-existence of cancun-specific header fields
if header.ParentBeaconRoot != nil {
return fmt.Errorf("invalid parentBeaconRoot, have %#x, expected nil", header.ParentBeaconRoot)
}
cancun := chain.Config().IsCancun(header.Number, header.Time)
if !cancun {
switch {
case header.ExcessBlobGas != nil:
return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas)
case header.BlobGasUsed != nil:
return fmt.Errorf("invalid blobGasUsed: have %d, expected nil", header.BlobGasUsed)
}
} else {
if err := eip4844.VerifyEIP4844Header(parent, header); err != nil {
return err
}
}

if !cancun && header.ExcessBlobGas != nil {
return fmt.Errorf("invalid excessBlobGas: have %d, expected nil", header.ExcessBlobGas)
}
Expand Down Expand Up @@ -975,8 +991,7 @@ func (p *Parlia) Prepare(chain consensus.ChainHeaderReader, header *types.Header
}

header.Extra = header.Extra[:extraVanity-nextForkHashSize]
genesisTime := chain.GetHeader(p.genesisHash, 0).Time
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, genesisTime, number, header.Time)
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, chain.GenesisHeader().Time, number, header.Time)
header.Extra = append(header.Extra, nextForkHash[:]...)

if err := p.prepareValidators(header); err != nil {
Expand Down Expand Up @@ -1117,8 +1132,7 @@ func (p *Parlia) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
if err != nil {
return err
}
genesisTime := chain.GetHeader(p.genesisHash, 0).Time
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, genesisTime, number, header.Time)
nextForkHash := forkid.NextForkHash(p.chainConfig, p.genesisHash, chain.GenesisHeader().Time, number, header.Time)
if !snap.isMajorityFork(hex.EncodeToString(nextForkHash[:])) {
log.Debug("there is a possible fork, and your client is not the majority. Please check...", "nextForkHash", hex.EncodeToString(nextForkHash[:]))
}
Expand Down
8 changes: 4 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
// Make sure the state associated with the block is available, or log out
// if there is no available state, waiting for state sync.
head := bc.CurrentBlock()
if !bc.NoTries() && !bc.HasState(head.Root) {
if !bc.HasState(head.Root) {
if head.Number.Uint64() == 0 {
// The genesis state is missing, which is only possible in the path-based
// scheme. This situation occurs when the initial state sync is not finished
Expand Down Expand Up @@ -883,7 +883,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
}
}
if beyondRoot || newHeadBlock.NumberU64() == 0 {
if !bc.NoTries() && !bc.HasState(newHeadBlock.Root()) && bc.stateRecoverable(newHeadBlock.Root()) {
if !bc.HasState(newHeadBlock.Root()) && bc.stateRecoverable(newHeadBlock.Root()) {
// Rewind to a block with recoverable state. If the state is
// missing, run the state recovery here.
if err := bc.triedb.Recover(newHeadBlock.Root()); err != nil {
Expand Down Expand Up @@ -914,7 +914,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
// the pivot point. In this scenario, there is no possible recovery
// approach except for rerunning a snap sync. Do nothing here until the
// state syncer picks it up.
if !bc.NoTries() && !bc.HasState(newHeadBlock.Root()) {
if !bc.HasState(newHeadBlock.Root()) {
log.Info("Chain is stateless, wait state sync", "number", newHeadBlock.Number(), "hash", newHeadBlock.Hash())
}
}
Expand Down Expand Up @@ -1216,7 +1216,7 @@ func (bc *BlockChain) Stop() {

if snapBase != (common.Hash{}) {
log.Info("Writing snapshot state to disk", "root", snapBase)
if err := bc.triedb.Commit(snapBase, true); err != nil {
if err := triedb.Commit(snapBase, true); err != nil {
log.Error("Failed to commit recent state trie", "err", err)
} else {
rawdb.WriteSafePointBlockNumber(bc.db, bc.CurrentBlock().Number.Uint64())
Expand Down
7 changes: 6 additions & 1 deletion core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int {

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

// GenesisHeader retrieves the chain's genesis block header.
func (bc *BlockChain) GenesisHeader() *types.Header {
return bc.genesisBlock.Header()
}

// TxIndexProgress returns the transaction indexing progress.
func (bc *BlockChain) TxIndexProgress() (TxIndexProgress, error) {
if bc.txIndexer == nil {
Expand Down
4 changes: 4 additions & 0 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ func (cm *chainMaker) GetHeaderByHash(hash common.Hash) *types.Header {
return b.Header()
}

func (cm *chainMaker) GenesisHeader() *types.Header {
panic("not supported")
}

func (cm *chainMaker) GetHeader(hash common.Hash, number uint64) *types.Header {
return cm.GetHeaderByNumber(number)
}
Expand Down
4 changes: 4 additions & 0 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func (hc *HeaderChain) getFinalizedNumber(header *types.Header) uint64 {
return 0
}

func (hc *HeaderChain) GenesisHeader() *types.Header {
panic("not supported")
}

// GetBlockNumber retrieves the block number belonging to the given hash
// from the cache or database
func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {
Expand Down
1 change: 0 additions & 1 deletion core/rawdb/ancient_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func inspectFreezers(db ethdb.Database) ([]freezerInfo, error) {
if err != nil {
return nil, err
}
// TODO, offset=0?
f, err := NewStateFreezer(datadir, true, 0)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions eth/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ type BlockChain interface {
type DownloadOption func(downloader *Downloader) *Downloader

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

// TODO how to use success
go dl.stateFetcher()
return dl
}
Expand Down
2 changes: 1 addition & 1 deletion eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func newHandler(config *handlerConfig) (*handler, error) {
}
h.snapSync.Store(true)
log.Warn("Switch sync mode from full sync to snap sync", "reason", "snap sync incomplete")
} else if !h.chain.NoTries() && !h.chain.HasState(fullBlock.Root) {
} else if !h.chain.HasState(fullBlock.Root) {
h.snapSync.Store(true)
log.Warn("Switch sync mode from full sync to snap sync", "reason", "head state missing")
}
Expand Down
2 changes: 1 addition & 1 deletion eth/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (cs *chainSyncer) modeAndLocalHead() (downloader.SyncMode, *big.Int) {
// We are in a full sync, but the associated head state is missing. To complete
// the head state, forcefully rerun the snap sync. Note it doesn't mean the
// persistent state is corrupted, just mismatch with the head block.
if !cs.handler.chain.NoTries() && !cs.handler.chain.HasState(head.Root) {
if !cs.handler.chain.HasState(head.Root) {
block := cs.handler.chain.CurrentSnapBlock()
td := cs.handler.chain.GetTd(block.Hash(), block.Number.Uint64())
log.Info("Reenabled snap sync as chain is stateless")
Expand Down
2 changes: 1 addition & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,8 @@ type generateParams struct {
random common.Hash // The randomness generated by beacon chain, empty before the merge
withdrawals types.Withdrawals // List of withdrawals to include in block.
prevWork *environment
noTxs bool // Flag whether an empty block without any transaction is expected
beaconRoot *common.Hash // The beacon root (cancun field).
noTxs bool // Flag whether an empty block without any transaction is expected
}

// prepareWork constructs the sealing task according to the given parameters,
Expand Down
1 change: 0 additions & 1 deletion trie/triedb/pathdb/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ func TestTruncateOutOfRange(t *testing.T) {

// openFreezer initializes the freezer instance for storing state histories.
func openFreezer(datadir string, readOnly bool) (*rawdb.ResettableFreezer, error) {
// TODO offset=0?
return rawdb.NewStateFreezer(datadir, readOnly, 0)
}

Expand Down

3 comments on commit 93d652b

@Sadikaslan1
Copy link

Choose a reason for hiding this comment

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

Trust my team we make party

@Sadikaslan1
Copy link

Choose a reason for hiding this comment

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

Thank

@Sadikaslan1
Copy link

Choose a reason for hiding this comment

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

Run

Please sign in to comment.