diff --git a/internal/client/network-gossip/bridge.go b/internal/client/network-gossip/bridge.go index 7c951d4d1c..7504a72ab6 100644 --- a/internal/client/network-gossip/bridge.go +++ b/internal/client/network-gossip/bridge.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package gossip import ( @@ -19,8 +22,8 @@ var logger = log.NewFromGlobal(log.AddContext("pkg", "client/network-gossip")) // In the scenario where messages have been received from the network but a subscribed message sink is not ready to // receiver, we delay 10 ms and will remove the channel from the sinks if the message is not consumed by the end of // the delay. To model this process a gossip engine can be in two forwarding states: idle, and busy. - -// GossipEngine utilizes and implementation of [Network] and provides gossiping capabilities on +// +// GossipEngine utilises and implementation of [Network] and provides gossiping capabilities on // top of it. type GossipEngine[H runtime.Hash, N runtime.Number, Hasher runtime.Hasher[H]] struct { stateMachine consensusGossip[H, Hasher] @@ -65,7 +68,12 @@ func NewGossipEngine[H runtime.Hash, N runtime.Number, Hasher runtime.Hasher[H]] validator Validator[H], ) GossipEngine[H, N, Hasher] { ge := newGossipEngine[H, N, Hasher](network, sync, notificationService, protocol, validator) - go ge.poll() + go func() { + err := ge.poll() + if err != nil { + panic(err) + } + }() return ge } @@ -163,7 +171,7 @@ func (ge *GossipEngine[H, N, Hasher]) Announce(block H, associatedData []byte) { ge.sync.AnnounceBlock(block, associatedData) } -func (ge *GossipEngine[H, N, Hasher]) poll() error { +func (ge *GossipEngine[H, N, Hasher]) poll() error { //nolint: gocyclo var nextNotificationEvent <-chan service.NotificationEvent // outer: for { @@ -197,7 +205,8 @@ func (ge *GossipEngine[H, N, Hasher]) poll() error { case service.NotificationEventNotificationStreamClosed: ge.stateMachine.PeerDisconnected(ge.notificationService, event.Peer) case service.NotificationEventNotificationReceived: - toForward := ge.stateMachine.OnIncoming(ge.network, ge.notificationService, event.Peer, [][]byte{event.Notification}) + toForward := ge.stateMachine.OnIncoming( + ge.network, ge.notificationService, event.Peer, [][]byte{event.Notification}) ge.forwardingState = forwardingStateBusy[H](toForward) default: panic("unreachable") diff --git a/internal/client/network-gossip/bridge_test.go b/internal/client/network-gossip/bridge_test.go index 86e68e1451..635e3bf554 100644 --- a/internal/client/network-gossip/bridge_test.go +++ b/internal/client/network-gossip/bridge_test.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package gossip import ( @@ -144,7 +147,9 @@ func (ao TestValidator) NewPeer(context ValidatorContext[hash.H256], who peerid. } func (ao TestValidator) PeerDisconnected(context ValidatorContext[hash.H256], who peerid.PeerID) { } -func (ao TestValidator) Validate(context ValidatorContext[hash.H256], sender peerid.PeerID, data []byte) ValidationResult { +func (ao TestValidator) Validate( + context ValidatorContext[hash.H256], sender peerid.PeerID, data []byte, +) ValidationResult { return ValidationResultProcessAndKeep[hash.H256]{ Hash: hash.H256(data[0:32]), } @@ -154,7 +159,9 @@ func (ao TestValidator) MessageExpired() func(topic hash.H256, message []byte) b return false } } -func (ao TestValidator) MessageAllowed() func(who peerid.PeerID, intent MessageIntent, topic hash.H256, data []byte) bool { +func (ao TestValidator) MessageAllowed() func( + who peerid.PeerID, intent MessageIntent, topic hash.H256, data []byte, +) bool { return func(who peerid.PeerID, intent MessageIntent, topic hash.H256, data []byte) bool { return true } @@ -296,7 +303,8 @@ func TestGossipEngine(t *testing.T) { if !ok { gossipEngine.messageSinks[topicChan.Topic] = make([]chan TopicNotification, 0) } - gossipEngine.messageSinks[topicChan.Topic] = append(gossipEngine.messageSinks[topicChan.Topic], topicChan.Chan) + gossipEngine.messageSinks[topicChan.Topic] = append( + gossipEngine.messageSinks[topicChan.Topic], topicChan.Chan) } // Register the remote peer. diff --git a/internal/client/network-gossip/network_gossip.go b/internal/client/network-gossip/network_gossip.go index ea0c48134c..d550bdf0e2 100644 --- a/internal/client/network-gossip/network_gossip.go +++ b/internal/client/network-gossip/network_gossip.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package gossip import ( diff --git a/internal/client/network-gossip/state_machine.go b/internal/client/network-gossip/state_machine.go index 092c596e4f..08d2e935db 100644 --- a/internal/client/network-gossip/state_machine.go +++ b/internal/client/network-gossip/state_machine.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package gossip import ( @@ -137,7 +140,10 @@ func (h hasher[K]) Hash(key K) uint32 { } // Create a new instance using the given validator. -func newConsensusGossip[H runtime.Hash, Hasher runtime.Hasher[H]](validator Validator[H], protocol network.ProtocolName) consensusGossip[H, Hasher] { +func newConsensusGossip[H runtime.Hash, Hasher runtime.Hasher[H]]( + validator Validator[H], + protocol network.ProtocolName, +) consensusGossip[H, Hasher] { h := hasher[H]{maphash.NewHasher[H]()} knownMessages, err := freelru.New[H, any](knownMessageCacheSize, h.Hash) if err != nil { @@ -154,7 +160,11 @@ func newConsensusGossip[H runtime.Hash, Hasher runtime.Hasher[H]](validator Vali } // Handle new connected peer. -func (cg *consensusGossip[H, Hasher]) NewPeer(notificationService service.NotificationService, who peerid.PeerID, role role.ObservedRole) { +func (cg *consensusGossip[H, Hasher]) NewPeer( + notificationService service.NotificationService, + who peerid.PeerID, + role role.ObservedRole, +) { cg.peers[who] = peerConsensus[H]{knownMessages: make(map[H]any)} validator := cg.validator @@ -162,7 +172,9 @@ func (cg *consensusGossip[H, Hasher]) NewPeer(notificationService service.Notifi validator.NewPeer(context, who, role) } -func (cg *consensusGossip[H, Hasher]) registerMessageHashed(messageHash H, topic H, message []byte, sender *peerid.PeerID) { +func (cg *consensusGossip[H, Hasher]) registerMessageHashed( + messageHash H, topic H, message []byte, sender *peerid.PeerID, +) { cg.knownMessages.Add(messageHash, nil) cg.messages = append(cg.messages, messageEntry[H]{ messageHash: messageHash, @@ -182,7 +194,9 @@ func (cg *consensusGossip[H, Hasher]) RegisterMessage(topic H, message []byte) { } // Call when a peer has been disconnected to stop tracking gossip status. -func (cg *consensusGossip[H, Hasher]) PeerDisconnected(notificationService service.NotificationService, who peerid.PeerID) { +func (cg *consensusGossip[H, Hasher]) PeerDisconnected( + notificationService service.NotificationService, who peerid.PeerID, +) { validator := cg.validator context := newtorkContext[H, Hasher]{gossip: cg, notificationService: notificationService} validator.PeerDisconnected(context, who) @@ -205,7 +219,9 @@ func (cg *consensusGossip[H, Hasher]) rebroadcast(notificationService service.No } // Broadcast all messages with given topic. -func (cg *consensusGossip[H, Hasher]) BroadcastTopic(notificationService service.NotificationService, topic H, force bool) { +func (cg *consensusGossip[H, Hasher]) BroadcastTopic( + notificationService service.NotificationService, topic H, force bool, +) { var messages []messageEntry[H] for _, entry := range cg.messages { if entry.topic == topic { @@ -240,7 +256,7 @@ func (cg *consensusGossip[H, Hasher]) CollectGarbage() { // TODO: expired messages metric for id, peer := range cg.peers { - for h, _ := range peer.knownMessages { + for h := range peer.knownMessages { if _, ok := knownMessages.Get(h); !ok { delete(peer.knownMessages, h) } @@ -335,7 +351,9 @@ func (cg *consensusGossip[H, Hasher]) OnIncoming( } // Send all messages with given topic to a peer. -func (cg *consensusGossip[H, Hasher]) SendTopic(notificationService service.NotificationService, who peerid.PeerID, topic H, force bool) { +func (cg *consensusGossip[H, Hasher]) SendTopic( + notificationService service.NotificationService, who peerid.PeerID, topic H, force bool, +) { messageAllowed := cg.validator.MessageAllowed() if peer, ok := cg.peers[who]; ok { @@ -363,7 +381,9 @@ func (cg *consensusGossip[H, Hasher]) SendTopic(notificationService service.Noti } // Multicast a message to all peers. -func (cg *consensusGossip[H, Hasher]) Multicast(notificationService service.NotificationService, topic H, message []byte, force bool) { +func (cg *consensusGossip[H, Hasher]) Multicast( + notificationService service.NotificationService, topic H, message []byte, force bool, +) { messageHash := (*new(Hasher)).Hash(message) cg.registerMessageHashed(messageHash, topic, message, nil) var intent MessageIntent = MessageIntentBroadcast @@ -382,7 +402,9 @@ func (cg *consensusGossip[H, Hasher]) Multicast(notificationService service.Noti } // Send addressed message to a peer. The message is not kept or multicast later on. -func (cg *consensusGossip[H, Hasher]) SendMessage(notificationService service.NotificationService, who peerid.PeerID, message []byte) { +func (cg *consensusGossip[H, Hasher]) SendMessage( + notificationService service.NotificationService, who peerid.PeerID, message []byte, +) { peer, ok := cg.peers[who] if !ok { return diff --git a/internal/client/network-gossip/state_machine_test.go b/internal/client/network-gossip/state_machine_test.go index d204affb07..6dd27152d1 100644 --- a/internal/client/network-gossip/state_machine_test.go +++ b/internal/client/network-gossip/state_machine_test.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package gossip import ( @@ -191,7 +194,9 @@ func (NoOpNotificationService) MessageSink(peer peerid.PeerID) service.MessageSi var _ service.NotificationService = NoOpNotificationService{} -func pushMessage(consensus *consensusGossip[hash.H256, runtime.BlakeTwo256], topic hash.H256, h hash.H256, message []byte) { +func pushMessage( + consensus *consensusGossip[hash.H256, runtime.BlakeTwo256], topic hash.H256, h hash.H256, message []byte, +) { consensus.knownMessages.Add(h, nil) consensus.messages = append(consensus.messages, messageEntry[hash.H256]{ messageHash: h, diff --git a/internal/client/network-gossip/validator.go b/internal/client/network-gossip/validator.go index 9c4eecf670..b1e03c7b1a 100644 --- a/internal/client/network-gossip/validator.go +++ b/internal/client/network-gossip/validator.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package gossip import ( diff --git a/internal/client/network/config/config.go b/internal/client/network/config/config.go index f7d6be95bc..137c3ef373 100644 --- a/internal/client/network/config/config.go +++ b/internal/client/network/config/config.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package config import ( diff --git a/internal/client/network/event/event.go b/internal/client/network/event/event.go index a4516f7d39..0e0dd0dea8 100644 --- a/internal/client/network/event/event.go +++ b/internal/client/network/event/event.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package event import ( @@ -26,7 +29,7 @@ type DHTEventValuePut kad.Key // DHTEventValuePutFailed means an error has occurred while putting a record into the DHT. type DHTEventValuePutFailed kad.Key -// DHTEventStartProvidingFailed means an error occured while registering as a content provider on the DHT. +// DHTEventStartProvidingFailed means an error occurred while registering as a content provider on the DHT. type DHTEventStartProvidingFailed kad.Key // DHTEventPutRecordRequest means the DHT received a put record request. diff --git a/internal/client/network/network.go b/internal/client/network/network.go index 80b2bee261..7396219b43 100644 --- a/internal/client/network/network.go +++ b/internal/client/network/network.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package network // The protocol name transmitted on the wire. diff --git a/internal/client/network/role/role.go b/internal/client/network/role/role.go index 8c74ac9bbb..0ebeb95145 100644 --- a/internal/client/network/role/role.go +++ b/internal/client/network/role/role.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package role // Role that the peer sent to us during the handshake, with the addition of what our local node knows about that peer. @@ -25,7 +28,7 @@ const ( RoleAuthority ) -// Roles are a bitmask of the roles that a node fulfills. +// Roles are a bitmask of the roles that a node fulfils. type Roles uint8 const ( diff --git a/internal/client/network/service/service.go b/internal/client/network/service/service.go index 6093d7dbe9..41c8b1172a 100644 --- a/internal/client/network/service/service.go +++ b/internal/client/network/service/service.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package service import ( @@ -15,7 +18,7 @@ type NetworkSyncForkRequest[BlockHash, BlockNumber any] interface { // // If the given slice of peers is empty then the underlying implementation // should make a best effort to fetch the block from any peers it is - // connected to (NOTE: this assumption will change in the future #3629). + // connected to. SetSyncForkRequest(peers []peerid.PeerID, hash BlockHash, number BlockNumber) } @@ -201,7 +204,7 @@ type NotificationEventNotificationReceived struct { func (NotificationEventNotificationReceived) isNotificationEvent() {} -// NotificationService is the notification service. It defines behaviors that both the protocol implementations and +// NotificationService is the notification service. It defines behaviours that both the protocol implementations and // NotificationService can expect from each other. // // NotificationService can send two different kinds of information to protocol: @@ -214,7 +217,7 @@ func (NotificationEventNotificationReceived) isNotificationEvent() {} // [ValidationResultAccept] or [ValidationResultReject]. // // After the validation result has been received by NotificationService, it prepares the substream for communication by -// initializing the necessary sinks and emits [NotificationEventNotificationStreamOpened] which informs the protocol +// initialising the necessary sinks and emits [NotificationEventNotificationStreamOpened] which informs the protocol // that the remote peer is ready to receive notifications. // // Both local and remote peer can close the substream at any time. Local peer can do so by calling CloseSubstream which @@ -269,7 +272,7 @@ type NotificationService interface { // notifications to the remote peer. // // Use of this API is discouraged as it's not as performant as sending notifications through [NotificationService] due -// to synchronization required to keep the underlying notification sink up to date with possible sink replacement +// to synchronisation required to keep the underlying notification sink up to date with possible sink replacement // events. type MessageSink interface { // Send synchronous notification to the peer associated with this MessageSink. diff --git a/internal/client/network/sync/sync.go b/internal/client/network/sync/sync.go index 6c7ab59304..ea001c9f7f 100644 --- a/internal/client/network/sync/sync.go +++ b/internal/client/network/sync/sync.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package sync import ( diff --git a/internal/client/network/types/kad/kad.go b/internal/client/network/types/kad/kad.go index e983cfb025..32472002aa 100644 --- a/internal/client/network/types/kad/kad.go +++ b/internal/client/network/types/kad/kad.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package kad import ( diff --git a/internal/client/network/types/multiaddr/multiaddr.go b/internal/client/network/types/multiaddr/multiaddr.go index 639a3136a8..aa8b800114 100644 --- a/internal/client/network/types/multiaddr/multiaddr.go +++ b/internal/client/network/types/multiaddr/multiaddr.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package multiaddr import ( diff --git a/internal/client/network/types/multihash/multihash.go b/internal/client/network/types/multihash/multihash.go index 06d49c2c45..b1328dfa80 100644 --- a/internal/client/network/types/multihash/multihash.go +++ b/internal/client/network/types/multihash/multihash.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package multihash import "github.com/multiformats/go-multihash" diff --git a/internal/client/network/types/peer-id/peer_id.go b/internal/client/network/types/peer-id/peer_id.go index 34a4aca092..55330a2c9c 100644 --- a/internal/client/network/types/peer-id/peer_id.go +++ b/internal/client/network/types/peer-id/peer_id.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package peerid import ( @@ -80,7 +83,7 @@ func NewRandomPeerID() PeerID { } } -// Create a [PeerID] parsed from bytes. +// NewPeerID creates a [PeerID] parsed from bytes. func NewPeerID(data []byte) (PeerID, error) { peerID, err := peer.IDFromBytes(data) if err != nil { diff --git a/internal/client/network/types/peer-id/peer_id_test.go b/internal/client/network/types/peer-id/peer_id_test.go index 1702562bfb..e2d6fa7d5f 100644 --- a/internal/client/network/types/peer-id/peer_id_test.go +++ b/internal/client/network/types/peer-id/peer_id_test.go @@ -1,3 +1,6 @@ +// Copyright 2025 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package peerid import (