Skip to content

Commit fdc315d

Browse files
committed
fix: nil checks
1 parent 230d6b7 commit fdc315d

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

cmd/p2p/sensor/api.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/0xPolygon/polygon-cli/p2p"
11+
"github.com/ethereum/go-ethereum/core/types"
1112
"github.com/ethereum/go-ethereum/eth/protocols/eth"
1213
ethp2p "github.com/ethereum/go-ethereum/p2p"
1314
"github.com/prometheus/client_golang/prometheus"
@@ -33,14 +34,27 @@ type blockInfo struct {
3334
Number uint64 `json:"number"`
3435
}
3536

37+
// newBlockInfo creates a blockInfo from a types.Header.
38+
// Returns nil if the header is nil.
39+
func newBlockInfo(header *types.Header) *blockInfo {
40+
if header == nil {
41+
return nil
42+
}
43+
44+
return &blockInfo{
45+
Hash: header.Hash().Hex(),
46+
Number: header.Number.Uint64(),
47+
}
48+
}
49+
3650
// apiData represents all sensor information including node info and peer data.
3751
type apiData struct {
3852
ENR string `json:"enr"`
3953
URL string `json:"enode"`
4054
PeerCount int `json:"peer_count"`
4155
Peers map[string]peerData `json:"peers"`
42-
Head blockInfo `json:"head_block"`
43-
Oldest blockInfo `json:"oldest_block"`
56+
Head *blockInfo `json:"head_block"`
57+
Oldest *blockInfo `json:"oldest_block"`
4458
}
4559

4660
// handleAPI sets up the API for interacting with the sensor. All endpoints
@@ -82,19 +96,18 @@ func handleAPI(server *ethp2p.Server, counter *prometheus.CounterVec, conns *p2p
8296
head := conns.HeadBlock()
8397
oldest := conns.OldestBlock()
8498

99+
var headHeader *types.Header
100+
if head.Block != nil {
101+
headHeader = head.Block.Header()
102+
}
103+
85104
data := apiData{
86105
ENR: server.NodeInfo().ENR,
87106
URL: server.Self().URLv4(),
88107
PeerCount: len(peers),
89108
Peers: peers,
90-
Head: blockInfo{
91-
Hash: head.Block.Hash().Hex(),
92-
Number: head.Block.NumberU64(),
93-
},
94-
Oldest: blockInfo{
95-
Hash: oldest.Hash().Hex(),
96-
Number: oldest.Number.Uint64(),
97-
},
109+
Head: newBlockInfo(headHeader),
110+
Oldest: newBlockInfo(oldest),
98111
}
99112

100113
if err := json.NewEncoder(w).Encode(data); err != nil {

p2p/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewBlockMetrics(block *types.Block) *BlockMetrics {
5858

5959
// Update updates all block metrics.
6060
func (m *BlockMetrics) Update(block *types.Block, oldest *types.Header) {
61-
if m == nil {
61+
if m == nil || block == nil || oldest == nil {
6262
return
6363
}
6464

p2p/protocol.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ func (c *conn) getParentBlock(ctx context.Context, header *types.Header) error {
311311
}
312312

313313
oldestBlock := c.conns.OldestBlock()
314+
if oldestBlock == nil {
315+
return nil
316+
}
314317

315318
// Check cache first before querying the database
316319
cache, ok := c.conns.Blocks().Peek(header.ParentHash)

0 commit comments

Comments
 (0)