Skip to content

Commit

Permalink
(dot/parachain): handle view update message network bridge
Browse files Browse the repository at this point in the history
Over the network through collation and validation protocol, as part of WireMessage we could get a PeerMessage or a ViewUpdate.

ViewUpdate tells us about latest blocks (heads) and finalized number with our peers.

We are supposed to be update our peer data with these views.

This commit, reads ViewUpdate from both collation and validation protocol and handles them inside network bridge by updating our peer data. Later, it sends that view to other subsystems so that each subsystem can update their view too.

Fixes #3864
  • Loading branch information
kishansagathiya committed Oct 14, 2024
1 parent cf75d6e commit 6f4fb63
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
14 changes: 14 additions & 0 deletions dot/parachain/network-bridge/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ type View struct {
FinalizedNumber uint32
}

func (v View) Equals(v2 View) bool {
if v.FinalizedNumber != v.FinalizedNumber {

Check failure on line 64 in dot/parachain/network-bridge/events/events.go

View workflow job for this annotation

GitHub Actions / linting

SA4000: identical expressions on the left and right side of the '!=' operator (staticcheck)
return false
}

for i, head := range v.Heads {
if head != v2.Heads[i] {
return false
}
}

return true
}

type OurViewChange struct {
View View
}
Expand Down
45 changes: 42 additions & 3 deletions dot/parachain/network-bridge/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
events "github.com/ChainSafe/gossamer/dot/parachain/network-bridge/events"
networkbridgemessages "github.com/ChainSafe/gossamer/dot/parachain/network-bridge/messages"
validationprotocol "github.com/ChainSafe/gossamer/dot/parachain/validation-protocol"
"github.com/ChainSafe/gossamer/dot/peerset"

parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"

Expand All @@ -28,6 +29,8 @@ import (

var logger = log.NewFromGlobal(log.AddContext("pkg", "network-bridge"))

const newMaxHeads = 5

var (
ErrFinalizedNumber = errors.New("finalized number is greater than or equal to the block number")
ErrInvalidStringFormat = errors.New("invalid string format for fetched collation info")
Expand Down Expand Up @@ -274,7 +277,7 @@ func (nbr *NetworkBridgeReceiver) handleCollationMessage(
"failed to cast into view update, expected: *ViewUpdate, got: %T",
value)
}
nbr.handleViewUpdate(*viewUpdate)
nbr.handleViewUpdate(sender, *viewUpdate)

Check failure on line 280 in dot/parachain/network-bridge/receiver.go

View workflow job for this annotation

GitHub Actions / linting

Error return value of `nbr.handleViewUpdate` is not checked (errcheck)

}

Expand Down Expand Up @@ -320,14 +323,50 @@ func (nbr *NetworkBridgeReceiver) handleValidationMessage(
"failed to cast into view update, expected: *ViewUpdate, got: %T",
value)
}
nbr.handleViewUpdate(*viewUpdate)
nbr.handleViewUpdate(sender, *viewUpdate)

Check failure on line 326 in dot/parachain/network-bridge/receiver.go

View workflow job for this annotation

GitHub Actions / linting

Error return value of `nbr.handleViewUpdate` is not checked (errcheck)
}

return propagate, nil
}

func (nbr *NetworkBridgeReceiver) handleViewUpdate(view ViewUpdate) {
func (nbr *NetworkBridgeReceiver) handleViewUpdate(peer peer.ID, view ViewUpdate) error {

peerData, ok := nbr.peerData[peer]
if !ok {
return errors.New("peer not found")
}
if len(view.Heads) > newMaxHeads || view.FinalizedNumber < peerData.view.FinalizedNumber {
nbr.net.ReportPeer(peerset.ReputationChange{
Value: peerset.CostMajor,
Reason: "malformed view",
}, peer)
} else if len(view.Heads) == 0 {
nbr.net.ReportPeer(peerset.ReputationChange{
Value: peerset.CostMinor,
Reason: "peer sent us empty view",
}, peer)
} else if events.View(view).Equals(events.View(peerData.view)) {
// nothing
} else {
peerData.view = View(view)
nbr.peerData[peer] = peerData

nbr.SubsystemsToOverseer <- events.Event[collatorprotocolmessages.CollationProtocol]{
Inner: events.PeerViewChange{
PeerID: peer,
View: events.View(view),
},
}

nbr.SubsystemsToOverseer <- events.Event[validationprotocol.ValidationProtocol]{
Inner: events.PeerViewChange{
PeerID: peer,
View: events.View(view),
},
}
}

return nil
}

func (nbr *NetworkBridgeReceiver) ProcessBlockFinalizedSignal(signal parachaintypes.BlockFinalizedSignal) error {
Expand Down

0 comments on commit 6f4fb63

Please sign in to comment.