From 854e7563e74e32950cdfb1e1c21819e94dcd9b9d Mon Sep 17 00:00:00 2001 From: kishansagathiya Date: Thu, 19 Sep 2024 11:05:30 +0530 Subject: [PATCH 1/3] decode messages to wire message collation protocol and validation protocol uses messages wrapped as wiremessage. So we had to adjust our decoder and handler functions according to that. Apart from this I have also added message type to wire message struct. Since wire message could be collation message or validation message, I have chose to manually set this as a value instead of returning a hardcoded value. --- .../collation_protocol_message.go | 18 ++++++++++++++---- dot/parachain/network-bridge/receiver.go | 4 +--- dot/parachain/network-bridge/sender.go | 5 +++++ .../validation_protocol_message.go | 17 +++++++++++++++-- dot/parachain/network-bridge/wire_message.go | 11 ++++++++--- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/dot/parachain/network-bridge/collation_protocol_message.go b/dot/parachain/network-bridge/collation_protocol_message.go index b6bd4169d7..896c846e67 100644 --- a/dot/parachain/network-bridge/collation_protocol_message.go +++ b/dot/parachain/network-bridge/collation_protocol_message.go @@ -10,14 +10,24 @@ import ( ) func decodeCollationMessage(in []byte) (network.NotificationsMessage, error) { - // TODO : Decode a wire message instead - collationMessage := collatorprotocolmessages.CollationProtocol{} - - err := scale.Unmarshal(in, &collationMessage) + wireMessage := WireMessage{} + err := wireMessage.SetValue(collatorprotocolmessages.CollationProtocol{}) + if err != nil { + return nil, fmt.Errorf("cannot set collation protocol message: %w", err) + } + err = scale.Unmarshal(in, &wireMessage) if err != nil { return nil, fmt.Errorf("cannot decode message: %w", err) } + collationMessageV, err := wireMessage.Value() + if err != nil { + return nil, fmt.Errorf("failed to get collation protocol message value: %w", err) + } + collationMessage, ok := collationMessageV.(collatorprotocolmessages.CollationProtocol) + if !ok { + return nil, fmt.Errorf("failed to cast to collation protocol message") + } return &collationMessage, nil } diff --git a/dot/parachain/network-bridge/receiver.go b/dot/parachain/network-bridge/receiver.go index af5181fb19..2ffac461e7 100644 --- a/dot/parachain/network-bridge/receiver.go +++ b/dot/parachain/network-bridge/receiver.go @@ -430,9 +430,7 @@ func signingKeyAndIndex(validators []parachaintypes.ValidatorID, ks keystore.Key func (nbr *NetworkBridgeReceiver) handleCollationMessage( sender peer.ID, msg network.NotificationsMessage) (bool, error) { - - // TODO: this notification has to be a WireMessage. Check if it is a WireMessage - // TODO: Handle ViewUpdate message. ViewUpdate happens on both protocols. + // TODO: Handle ViewUpdate message. ViewUpdate happens on both protocols. #4156 #4155 // we don't propagate collation messages, so it will always be false propagate := false diff --git a/dot/parachain/network-bridge/sender.go b/dot/parachain/network-bridge/sender.go index ab6c2ba1a3..e1a2df06e8 100644 --- a/dot/parachain/network-bridge/sender.go +++ b/dot/parachain/network-bridge/sender.go @@ -7,6 +7,7 @@ import ( "context" "fmt" + "github.com/ChainSafe/gossamer/dot/network" networkbridgemessages "github.com/ChainSafe/gossamer/dot/parachain/network-bridge/messages" parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types" ) @@ -67,6 +68,8 @@ func (nbs *NetworkBridgeSender) processMessage(msg any) error { return fmt.Errorf("setting wire message: %w", err) } + wireMessage.SetType(network.CollationMsgType) + for _, to := range msg.To { err = nbs.net.SendMessage(to, wireMessage) if err != nil { @@ -81,6 +84,8 @@ func (nbs *NetworkBridgeSender) processMessage(msg any) error { return fmt.Errorf("setting wire message: %w", err) } + wireMessage.SetType(network.ValidationMsgType) + for _, to := range msg.To { err = nbs.net.SendMessage(to, wireMessage) if err != nil { diff --git a/dot/parachain/network-bridge/validation_protocol_message.go b/dot/parachain/network-bridge/validation_protocol_message.go index 556b85a3ad..a1f1ee3e3b 100644 --- a/dot/parachain/network-bridge/validation_protocol_message.go +++ b/dot/parachain/network-bridge/validation_protocol_message.go @@ -12,13 +12,26 @@ import ( const MaxValidationMessageSize uint64 = 100 * 1024 func decodeValidationMessage(in []byte) (network.NotificationsMessage, error) { - validationMessage := validationprotocol.ValidationProtocol{} + wireMessage := WireMessage{} + err := wireMessage.SetValue(validationprotocol.ValidationProtocol{}) + if err != nil { + return nil, fmt.Errorf("cannot set validation protocol message: %w", err) + } - err := scale.Unmarshal(in, &validationMessage) + err = scale.Unmarshal(in, &wireMessage) if err != nil { return nil, fmt.Errorf("cannot decode message: %w", err) } + validationMessageV, err := wireMessage.Value() + if err != nil { + return nil, fmt.Errorf("failed to get validation protocol message value: %w", err) + } + validationMessage, ok := validationMessageV.(validationprotocol.ValidationProtocol) + if !ok { + return nil, fmt.Errorf("failed to cast to validation protocol message") + } + return &validationMessage, nil } diff --git a/dot/parachain/network-bridge/wire_message.go b/dot/parachain/network-bridge/wire_message.go index a4f76c71e5..82c7f472fd 100644 --- a/dot/parachain/network-bridge/wire_message.go +++ b/dot/parachain/network-bridge/wire_message.go @@ -8,16 +8,17 @@ import ( "reflect" "sort" - "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/ChainSafe/gossamer/dot/network" collatorprotocolmessages "github.com/ChainSafe/gossamer/dot/parachain/collator-protocol/messages" validationprotocol "github.com/ChainSafe/gossamer/dot/parachain/validation-protocol" ) type WireMessage struct { - inner any + inner any + messageType network.MessageType } type WireMessageValues interface { @@ -67,9 +68,13 @@ func (mvdt WireMessage) ValueAt(index uint) (value any, err error) { return nil, scale.ErrUnknownVaryingDataTypeValue } +func (w *WireMessage) SetType(messageType network.MessageType) { + w.messageType = messageType +} + func (w WireMessage) Type() network.MessageType { // TODO: create a wire message type and return that #4108 - return network.CollationMsgType + return w.messageType } func (w WireMessage) Hash() (common.Hash, error) { From 0c1f269337d3eeabb713893ac7bf325873970c9e Mon Sep 17 00:00:00 2001 From: kishansagathiya Date: Tue, 1 Oct 2024 20:58:09 +0530 Subject: [PATCH 2/3] addressed some reviews --- .../network-bridge/collation_protocol_message.go | 8 ++++---- .../network-bridge/validation_protocol_message.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dot/parachain/network-bridge/collation_protocol_message.go b/dot/parachain/network-bridge/collation_protocol_message.go index 896c846e67..40a0f5c0ad 100644 --- a/dot/parachain/network-bridge/collation_protocol_message.go +++ b/dot/parachain/network-bridge/collation_protocol_message.go @@ -13,20 +13,20 @@ func decodeCollationMessage(in []byte) (network.NotificationsMessage, error) { wireMessage := WireMessage{} err := wireMessage.SetValue(collatorprotocolmessages.CollationProtocol{}) if err != nil { - return nil, fmt.Errorf("cannot set collation protocol message: %w", err) + return nil, fmt.Errorf("setting collation protocol message: %w", err) } err = scale.Unmarshal(in, &wireMessage) if err != nil { - return nil, fmt.Errorf("cannot decode message: %w", err) + return nil, fmt.Errorf("decoding message: %w", err) } collationMessageV, err := wireMessage.Value() if err != nil { - return nil, fmt.Errorf("failed to get collation protocol message value: %w", err) + return nil, fmt.Errorf("getting collation protocol message value: %w", err) } collationMessage, ok := collationMessageV.(collatorprotocolmessages.CollationProtocol) if !ok { - return nil, fmt.Errorf("failed to cast to collation protocol message") + return nil, fmt.Errorf("casting to collation protocol message") } return &collationMessage, nil } diff --git a/dot/parachain/network-bridge/validation_protocol_message.go b/dot/parachain/network-bridge/validation_protocol_message.go index a1f1ee3e3b..661afef1d6 100644 --- a/dot/parachain/network-bridge/validation_protocol_message.go +++ b/dot/parachain/network-bridge/validation_protocol_message.go @@ -15,21 +15,21 @@ func decodeValidationMessage(in []byte) (network.NotificationsMessage, error) { wireMessage := WireMessage{} err := wireMessage.SetValue(validationprotocol.ValidationProtocol{}) if err != nil { - return nil, fmt.Errorf("cannot set validation protocol message: %w", err) + return nil, fmt.Errorf("setting validation protocol message: %w", err) } err = scale.Unmarshal(in, &wireMessage) if err != nil { - return nil, fmt.Errorf("cannot decode message: %w", err) + return nil, fmt.Errorf("decoding message: %w", err) } validationMessageV, err := wireMessage.Value() if err != nil { - return nil, fmt.Errorf("failed to get validation protocol message value: %w", err) + return nil, fmt.Errorf("getting validation protocol message value: %w", err) } validationMessage, ok := validationMessageV.(validationprotocol.ValidationProtocol) if !ok { - return nil, fmt.Errorf("failed to cast to validation protocol message") + return nil, fmt.Errorf("casting to validation protocol message") } return &validationMessage, nil From 2ca483937b35d6520ca0e269881b3c2bc6ecb539 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 2 Oct 2024 11:39:47 +0530 Subject: [PATCH 3/3] Update dot/parachain/network-bridge/wire_message.go --- dot/parachain/network-bridge/wire_message.go | 1 - 1 file changed, 1 deletion(-) diff --git a/dot/parachain/network-bridge/wire_message.go b/dot/parachain/network-bridge/wire_message.go index 82c7f472fd..ed00432cfb 100644 --- a/dot/parachain/network-bridge/wire_message.go +++ b/dot/parachain/network-bridge/wire_message.go @@ -73,7 +73,6 @@ func (w *WireMessage) SetType(messageType network.MessageType) { } func (w WireMessage) Type() network.MessageType { - // TODO: create a wire message type and return that #4108 return w.messageType }