Skip to content
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

core: prevent block level tracers from crashing the node #30831

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
bc.engine.VerifyHeader(bc, bc.CurrentHeader())

if bc.logger != nil && bc.logger.OnBlockchainInit != nil {
bc.logger.OnBlockchainInit(chainConfig)
bc.guardedOnBlockchainInit(chainConfig)
}
if bc.logger != nil && bc.logger.OnGenesisBlock != nil {
if block := bc.CurrentBlock(); block.Number.Uint64() == 0 {
Expand All @@ -423,7 +423,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
if alloc == nil {
return nil, errors.New("live blockchain tracer requires genesis alloc to be set")
}
bc.logger.OnGenesisBlock(bc.genesisBlock, alloc)
bc.guardedOnGenesisBlock(bc.genesisBlock, alloc)
}
}

Expand Down Expand Up @@ -1155,7 +1155,7 @@ func (bc *BlockChain) Stop() {
}
// Allow tracers to clean-up and release resources.
if bc.logger != nil && bc.logger.OnClose != nil {
bc.logger.OnClose()
bc.guardedOnClose()
}
// Close the trie database, release all the held resources as the last step.
if err := bc.triedb.Close(); err != nil {
Expand Down Expand Up @@ -1752,7 +1752,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
}
stats.processed++
if bc.logger != nil && bc.logger.OnSkippedBlock != nil {
bc.logger.OnSkippedBlock(tracing.BlockEvent{
bc.guardedOnSkippedBlock(tracing.BlockEvent{
Block: block,
TD: bc.GetTd(block.ParentHash(), block.NumberU64()-1),
Finalized: bc.CurrentFinalBlock(),
Expand Down Expand Up @@ -1879,18 +1879,15 @@ type blockProcessingResult struct {
// it writes the block and associated state to database.
func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, start time.Time, setHead bool) (_ *blockProcessingResult, blockEndErr error) {
if bc.logger != nil && bc.logger.OnBlockStart != nil {
td := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
bc.logger.OnBlockStart(tracing.BlockEvent{
bc.guardedOnBlockStart(tracing.BlockEvent{
Block: block,
TD: td,
TD: bc.GetTd(block.ParentHash(), block.NumberU64()-1),
Finalized: bc.CurrentFinalBlock(),
Safe: bc.CurrentSafeBlock(),
})
}
if bc.logger != nil && bc.logger.OnBlockEnd != nil {
defer func() {
bc.logger.OnBlockEnd(blockEndErr)
}()
defer bc.guardedOnBlockEnd(blockEndErr)
}

// Process block using the parent state as reference point
Expand Down
97 changes: 97 additions & 0 deletions core/blockchain_tracing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Package core implements the Ethereum consensus protocol.
package core

import (
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)

// guardedOnGenesisBlock wraps the tracer OnGenesisBlock method with 'recover'. If
// a panic occurs, the hook will be disabled.
func (bc *BlockChain) guardedOnGenesisBlock(genesis *types.Block, alloc types.GenesisAlloc) {
defer func() {
if err := recover(); err != nil {
log.Warn("Tracer OnGenesisBlock exited with panic, disabling tracer", "err", err)
bc.logger.OnGenesisBlock = nil
}
}()
bc.logger.OnGenesisBlock(genesis, alloc)
}

// guardedOnBlockchainInit wraps the tracer OnBlockchainInit method with 'recover'. If
// a panic occurs, the hook will be disabled.
func (bc *BlockChain) guardedOnBlockchainInit(chainConfig *params.ChainConfig) {
defer func() {
if err := recover(); err != nil {
log.Warn("Tracer OnBlockchainInit exited with panic, disabling tracer", "err", err)
bc.logger.OnBlockchainInit = nil
}
}()
bc.logger.OnBlockchainInit(chainConfig)
}

// guardedOnBlockStart wraps the tracer OnBlockStart method with 'recover'. If
// a panic occurs, the hook will be disabled.
func (bc *BlockChain) guardedOnBlockStart(blockEvent tracing.BlockEvent) {
defer func() {
if err := recover(); err != nil {
log.Warn("Tracer OnBlockStart exited with panic, disabling tracer", "err", err)
bc.logger.OnBlockStart = nil
}
}()
bc.logger.OnBlockStart(blockEvent)
}

// guardedOnBlockEnd wraps the tracer OnBlockEnd method with 'recover'. If
// a panic occurs, the hook will be disabled.
func (bc *BlockChain) guardedOnBlockEnd(err error) {
defer func() {
if err := recover(); err != nil {
log.Warn("Tracer OnBlockEnd exited with panic, disabling tracer", "err", err)
bc.logger.OnBlockEnd = nil
}
}()
bc.logger.OnBlockEnd(err)
}

// guardedOnClose wraps the tracer OnClose method with 'recover'. If
// a panic occurs, the hook will be disabled.
func (bc *BlockChain) guardedOnClose() {
defer func() {
if err := recover(); err != nil {
log.Warn("Tracer OnClose exited with panic, disabling tracer", "err", err)
bc.logger.OnClose = nil
}
}()
bc.logger.OnClose()
}

// guardedOnSkippedBlock wraps the tracer OnSkippedBlock method with 'recover'. If
// a panic occurs, the hook will be disabled.
func (bc *BlockChain) guardedOnSkippedBlock(event tracing.BlockEvent) {
defer func() {
if err := recover(); err != nil {
log.Warn("Tracer OnSkippedBlock exited with panic, disabling tracer", "err", err)
bc.logger.OnSkippedBlock = nil
}
}()
bc.logger.OnSkippedBlock(event)
}
129 changes: 129 additions & 0 deletions eth/tracers/live/crash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package live

import (
"encoding/json"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/params"
)

func init() {
tracers.LiveDirectory.Register("crash", newCrashTracer)
}

// crash is a no-op live tracer. It's there to
// catch changes in the tracing interface, as well as
// for testing live tracing performance. Can be removed
// as soon as we have a real live tracer.
type crash struct{}

func newCrashTracer(_ json.RawMessage) (*tracing.Hooks, error) {
t := &crash{}
return &tracing.Hooks{
OnTxStart: t.OnTxStart,
OnTxEnd: t.OnTxEnd,
OnEnter: t.OnEnter,
OnExit: t.OnExit,
OnOpcode: t.OnOpcode,
OnFault: t.OnFault,
OnGasChange: t.OnGasChange,
OnBlockchainInit: t.OnBlockchainInit,
OnBlockStart: t.OnBlockStart,
OnBlockEnd: t.OnBlockEnd,
OnSkippedBlock: t.OnSkippedBlock,
OnGenesisBlock: t.OnGenesisBlock,
OnBalanceChange: t.OnBalanceChange,
OnNonceChange: t.OnNonceChange,
OnCodeChange: t.OnCodeChange,
OnStorageChange: t.OnStorageChange,
OnLog: t.OnLog,
}, nil
}

func (t *crash) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
panic("OnOpcode")
}

func (t *crash) OnFault(pc uint64, op byte, gas, cost uint64, _ tracing.OpContext, depth int, err error) {
panic("OnFault")
}

func (t *crash) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
panic("OnEnter")
}

func (t *crash) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) {
panic("OnExit")
}

func (t *crash) OnTxStart(vm *tracing.VMContext, tx *types.Transaction, from common.Address) {
panic("OnTxStart")
}

func (t *crash) OnTxEnd(receipt *types.Receipt, err error) {
panic("OnTxEnd")
}

func (t *crash) OnBlockStart(ev tracing.BlockEvent) {
panic("OnBlockStart")
}

func (t *crash) OnBlockEnd(err error) {
panic("OnBlockEnd")
}

func (t *crash) OnSkippedBlock(ev tracing.BlockEvent) {
panic("OnSkippedBlock")
}

func (t *crash) OnBlockchainInit(chainConfig *params.ChainConfig) {
panic("OnBlockchainInit")
}

func (t *crash) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) {
panic("OnGenesisBlock")
}

func (t *crash) OnBalanceChange(a common.Address, prev, new *big.Int, reason tracing.BalanceChangeReason) {
panic("OnBalanceChange")
}

func (t *crash) OnNonceChange(a common.Address, prev, new uint64) {
panic("OnNonceChange")
}

func (t *crash) OnCodeChange(a common.Address, prevCodeHash common.Hash, prev []byte, codeHash common.Hash, code []byte) {
panic("OnCodeChange")
}

func (t *crash) OnStorageChange(a common.Address, k, prev, new common.Hash) {
panic("OnStorageChange")
}

func (t *crash) OnLog(l *types.Log) {
panic("OnLog")
}

func (t *crash) OnGasChange(old, new uint64, reason tracing.GasChangeReason) {
panic("OnGasChange")
}