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

Fix panic on nil block in ethstats #1360

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
4 changes: 4 additions & 0 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,10 @@ func (s *Service) assembleBlockStats(block *types.Block) *blockStats {
}
}

if block == nil {
return nil
}

header = block.Header()
td = fullBackend.GetTd(context.Background(), header.Hash())

Expand Down
69 changes: 69 additions & 0 deletions ethstats/ethstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@
package ethstats

import (
"context"
"math/big"
"strconv"
"testing"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/rpc"
)

func TestParseEthstatsURL(t *testing.T) {
Expand Down Expand Up @@ -83,3 +92,63 @@ func TestParseEthstatsURL(t *testing.T) {
}
}
}

// MockBackend is a mock implementation of the backend interface
type MockFullNodeBackend struct{}

func (m *MockFullNodeBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription {
return nil
}

func (m *MockFullNodeBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
return nil
}

func (m *MockFullNodeBackend) CurrentHeader() *types.Header {
return &types.Header{}
}

func (m *MockFullNodeBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
return nil, nil
}

func (m *MockFullNodeBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
return big.NewInt(0)
}

func (m *MockFullNodeBackend) Stats() (pending int, queued int) {
return 0, 0
}

func (m *MockFullNodeBackend) SyncProgress() ethereum.SyncProgress {
return ethereum.SyncProgress{}
}

func (m *MockFullNodeBackend) SubscribeChain2HeadEvent(ch chan<- core.Chain2HeadEvent) event.Subscription {
return nil
}

func (m *MockFullNodeBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
return nil, nil
}

func (m *MockFullNodeBackend) CurrentBlock() *types.Header {
return &types.Header{Number: big.NewInt(1)}
}

func (m *MockFullNodeBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
return big.NewInt(0), nil
}

func TestAssembleBlockStats_NilBlock(t *testing.T) {
mockBackend := &MockFullNodeBackend{}
service := &Service{
backend: mockBackend,
}

result := service.assembleBlockStats(nil)

if result != nil {
t.Errorf("Expected nil, got %v", result)
}
}
Loading