diff --git a/node/pkg/p2p/p2p.go b/node/pkg/p2p/p2p.go index ff90ff954..899d40886 100644 --- a/node/pkg/p2p/p2p.go +++ b/node/pkg/p2p/p2p.go @@ -263,29 +263,15 @@ func Run( if gov != nil { gov.CollectMetrics(heartbeat, gossipSendC, gk, ourAddr) } - - b, err := proto.Marshal(heartbeat) - if err != nil { - panic(err) - } - DefaultRegistry.mu.Unlock() - // Sign the heartbeat using our node's guardian key. - digest := heartbeatDigest(b) - sig, err := ethcrypto.Sign(digest.Bytes(), gk) - if err != nil { - panic(err) + msg := gossipv1.GossipMessage{ + Message: &gossipv1.GossipMessage_SignedHeartbeat{ + SignedHeartbeat: createSignedHeartbeat(gk, heartbeat), + }, } - msg := gossipv1.GossipMessage{Message: &gossipv1.GossipMessage_SignedHeartbeat{ - SignedHeartbeat: &gossipv1.SignedHeartbeat{ - Heartbeat: b, - Signature: sig, - GuardianAddr: ourAddr.Bytes(), - }}} - - b, err = proto.Marshal(&msg) + b, err := proto.Marshal(&msg) if err != nil { panic(err) } @@ -472,6 +458,28 @@ func Run( } } +func createSignedHeartbeat(gk *ecdsa.PrivateKey, heartbeat *gossipv1.Heartbeat) *gossipv1.SignedHeartbeat { + ourAddr := ethcrypto.PubkeyToAddress(gk.PublicKey) + + b, err := proto.Marshal(heartbeat) + if err != nil { + panic(err) + } + + // Sign the heartbeat using our node's guardian key. + digest := heartbeatDigest(b) + sig, err := ethcrypto.Sign(digest.Bytes(), gk) + if err != nil { + panic(err) + } + + return &gossipv1.SignedHeartbeat{ + Heartbeat: b, + Signature: sig, + GuardianAddr: ourAddr.Bytes(), + } +} + func processSignedHeartbeat(from peer.ID, s *gossipv1.SignedHeartbeat, gs *node_common.GuardianSet, gst *node_common.GuardianSetState, disableVerify bool) (*gossipv1.Heartbeat, error) { envelopeAddr := common.BytesToAddress(s.GuardianAddr) idx, ok := gs.KeyIndex(envelopeAddr)