Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dot/parachain): handle view update message for receiver side of the network bridge #4249

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions dot/parachain/network-bridge/collation_protocol_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@ import (
"fmt"

"github.com/ChainSafe/gossamer/dot/network"
collatorprotocolmessages "github.com/ChainSafe/gossamer/dot/parachain/collator-protocol/messages"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/libp2p/go-libp2p/core/peer"
)

func decodeCollationMessage(in []byte) (network.NotificationsMessage, error) {
wireMessage := WireMessage{}
err := wireMessage.SetValue(collatorprotocolmessages.CollationProtocol{})
if err != nil {
return nil, fmt.Errorf("setting collation protocol message: %w", err)
}
err = scale.Unmarshal(in, &wireMessage)
// err := wireMessage.SetValue(collatorprotocolmessages.CollationProtocol{})
// if err != nil {
// return nil, fmt.Errorf("setting collation protocol message: %w", err)
// }
err := scale.Unmarshal(in, &wireMessage)
if err != nil {
return nil, fmt.Errorf("decoding message: %w", err)
}

collationMessageV, err := wireMessage.Value()
if err != nil {
return nil, fmt.Errorf("getting collation protocol message value: %w", err)
}
collationMessage, ok := collationMessageV.(collatorprotocolmessages.CollationProtocol)
if !ok {
return nil, fmt.Errorf("casting to collation protocol message")
}
return &collationMessage, nil
wireMessage.SetType(network.CollationMsgType)
// collationMessageV, err := wireMessage.Value()
// if err != nil {
// return nil, fmt.Errorf("getting collation protocol message value: %w", err)
// }
// collationMessage, ok := collationMessageV.(collatorprotocolmessages.CollationProtocol)
// if !ok {
// return nil, fmt.Errorf("casting to collation protocol message")
// }
// return &collationMessage, nil
return wireMessage, nil
}

func getCollatorHandshake() (network.Handshake, error) {
Expand Down
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 @@
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
119 changes: 104 additions & 15 deletions dot/parachain/network-bridge/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 @@

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 All @@ -53,6 +56,11 @@
networkEventInfoChan chan *network.NetworkEventInfo

authorityDiscoveryService AuthorityDiscoveryService

peerData map[peer.ID]struct {
view View
protocolVersion uint32
}
}

type CollationStatus int
Expand Down Expand Up @@ -240,46 +248,127 @@
network.CollationMsgType, msg.Type())
}

collatorProtocol, ok := msg.(*collatorprotocolmessages.CollationProtocol)
wireMessage, ok := msg.(*WireMessage)
if !ok {
return propagate, fmt.Errorf(
"failed to cast into collator protocol message, expected: *CollationProtocol, got: %T",
msg)
return propagate, fmt.Errorf("failed to cast into wire message, expected: *WireMessage, got: %T", msg)
}

index, value, err := wireMessage.IndexValue()
if err != nil {
return propagate, fmt.Errorf("getting index value: %w", err)
}

nbr.SubsystemsToOverseer <- events.PeerMessage[collatorprotocolmessages.CollationProtocol]{
PeerID: sender,
Message: *collatorProtocol,
switch index {
case 1:
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
collatorProtocol, ok := value.(*collatorprotocolmessages.CollationProtocol)
if !ok {
return propagate, fmt.Errorf(
"failed to cast into collator protocol message, expected: *CollationProtocol, got: %T",
value)
}
nbr.SubsystemsToOverseer <- events.PeerMessage[collatorprotocolmessages.CollationProtocol]{
PeerID: sender,
Message: *collatorProtocol,
}
case 2:
viewUpdate, ok := value.(*ViewUpdate)
if !ok {
return propagate, fmt.Errorf(
"failed to cast into view update, expected: *ViewUpdate, got: %T",
value)
}
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)

}

return propagate, nil
}

func (nbr *NetworkBridgeReceiver) handleValidationMessage(
sender peer.ID, msg network.NotificationsMessage) (bool, error) {
// we don't propagate collation messages, so it will always be false

propagate := false

if msg.Type() != network.ValidationMsgType {
return propagate, fmt.Errorf("%w, expected: %d, found:%d", ErrUnexpectedMessageOnValidationProtocol,
network.ValidationMsgType, msg.Type())
}

validationProtocol, ok := msg.(*validationprotocol.ValidationProtocol)
wireMessage, ok := msg.(*WireMessage)
if !ok {
return propagate, fmt.Errorf(
"failed to cast into collator protocol message, expected: *CollationProtocol, got: %T",
msg)
return propagate, fmt.Errorf("failed to cast into wire message, expected: *WireMessage, got: %T", msg)
}

nbr.SubsystemsToOverseer <- events.PeerMessage[validationprotocol.ValidationProtocol]{
PeerID: sender,
Message: *validationProtocol,
index, value, err := wireMessage.IndexValue()
if err != nil {
return propagate, fmt.Errorf("getting index value: %w", err)
}

switch index {
case 1:
axaysagathiya marked this conversation as resolved.
Show resolved Hide resolved
validationProtocol, ok := msg.(*validationprotocol.ValidationProtocol)
if !ok {
return propagate, fmt.Errorf(
"failed to cast into validation protocol message, expected: *ValidationProtocol, got: %T",
value)
}
nbr.SubsystemsToOverseer <- events.PeerMessage[validationprotocol.ValidationProtocol]{
PeerID: sender,
Message: *validationProtocol,
}
case 2:
viewUpdate, ok := value.(*ViewUpdate)
if !ok {
return propagate, fmt.Errorf(
"failed to cast into view update, expected: *ViewUpdate, got: %T",
value)
}
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(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)) {
kishansagathiya marked this conversation as resolved.
Show resolved Hide resolved
// 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 {
if nbr.finalizedNumber >= signal.BlockNumber {
return ErrFinalizedNumber
Expand Down
1 change: 1 addition & 0 deletions dot/parachain/network-bridge/wire_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (mvdt WireMessage) ValueAt(index uint) (value any, err error) {
}

func (w *WireMessage) SetType(messageType network.MessageType) {
// NOTE: We need a message type only to know where to send it
w.messageType = messageType
}

Expand Down
Loading