@@ -3,11 +3,11 @@ package sensor
33import (
44 "context"
55 "crypto/ecdsa"
6+ _ "embed"
67 "errors"
78 "fmt"
89 "os"
910 "os/signal"
10- "sync"
1111 "syscall"
1212 "time"
1313
@@ -17,6 +17,7 @@ import (
1717 "github.com/ethereum/go-ethereum/common"
1818 "github.com/ethereum/go-ethereum/core/forkid"
1919 "github.com/ethereum/go-ethereum/crypto"
20+ "github.com/ethereum/go-ethereum/eth/protocols/eth"
2021 ethp2p "github.com/ethereum/go-ethereum/p2p"
2122 "github.com/ethereum/go-ethereum/p2p/dnsdisc"
2223 "github.com/ethereum/go-ethereum/p2p/enode"
8283)
8384
8485var (
86+ //go:embed usage.md
87+ sensorUsage string
8588 inputSensorParams sensorParams
8689)
8790
9093var SensorCmd = & cobra.Command {
9194 Use : "sensor [nodes file]" ,
9295 Short : "Start a devp2p sensor that discovers other peers and will receive blocks and transactions." ,
93- Long : "If no nodes.json file exists, it will be created." ,
96+ Long : sensorUsage ,
9497 Args : cobra .MinimumNArgs (1 ),
9598 PreRunE : func (cmd * cobra.Command , args []string ) (err error ) {
9699 inputSensorParams .NodesFile = args [0 ]
@@ -170,14 +173,14 @@ var SensorCmd = &cobra.Command{
170173 // Fetch the latest block which will be used later when crafting the status
171174 // message. This call will only be made once and stored in the head field
172175 // until the sensor receives a new block it can overwrite it with.
173- block , err := getLatestBlock (inputSensorParams .RPC )
176+ rpcBlock , err := getLatestBlock (inputSensorParams .RPC )
174177 if err != nil {
175178 return err
176179 }
177- head := p2p. HeadBlock {
178- Hash : block . Hash . ToHash (),
179- TotalDifficulty : block . TotalDifficulty . ToBigInt (),
180- Number : block . Number . ToUint64 (),
180+
181+ head := eth. NewBlockPacket {
182+ Block : rpcBlock . ToBlock (),
183+ TD : rpcBlock . TotalDifficulty . ToBigInt (),
181184 }
182185
183186 peersGauge := promauto .NewGauge (prometheus.GaugeOpts {
@@ -192,10 +195,13 @@ var SensorCmd = &cobra.Command{
192195 Help : "The number and type of messages the sensor has sent and received" ,
193196 }, []string {"message" , "url" , "name" , "direction" })
194197
198+ metrics := p2p .NewBlockMetrics (head .Block )
199+
195200 // Create peer connection manager for broadcasting transactions
196201 // and managing the global blocks cache
197202 conns := p2p .NewConns (p2p.ConnsOptions {
198203 BlocksCache : inputSensorParams .BlocksCache ,
204+ Head : head ,
199205 })
200206
201207 opts := p2p.EthProtocolOptions {
@@ -206,8 +212,6 @@ var SensorCmd = &cobra.Command{
206212 SensorID : inputSensorParams .SensorID ,
207213 NetworkID : inputSensorParams .NetworkID ,
208214 Conns : conns ,
209- Head : & head ,
210- HeadMutex : & sync.RWMutex {},
211215 ForkID : forkid.ID {Hash : [4 ]byte (inputSensorParams .ForkID )},
212216 MsgCounter : msgCounter ,
213217 RequestsCache : inputSensorParams .RequestsCache ,
@@ -280,6 +284,8 @@ var SensorCmd = &cobra.Command{
280284 peersGauge .Set (float64 (server .PeerCount ()))
281285 db .WritePeers (cmd .Context (), server .Peers (), time .Now ())
282286
287+ metrics .Update (conns .HeadBlock ().Block , conns .OldestBlock ())
288+
283289 urls := []string {}
284290 for _ , peer := range server .Peers () {
285291 urls = append (urls , peer .Node ().URLv4 ())
0 commit comments