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.
3751type 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 {
0 commit comments