From 3be75b24e85db3a939af33bb1cf2ee23f22cf320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Junior?= Date: Tue, 26 Jul 2022 14:16:32 -0400 Subject: [PATCH] fix(lib/grandpa): update grandpa protocol ID (#2678) * feat: update grandpa protocol ID --- dot/network/block_announce.go | 10 ---------- dot/network/light.go | 10 ---------- dot/network/message.go | 16 ---------------- dot/network/transaction.go | 10 ---------- lib/grandpa/grandpa.go | 2 +- lib/grandpa/network.go | 25 ++++++++++++------------- lib/grandpa/network_test.go | 6 +----- 7 files changed, 14 insertions(+), 65 deletions(-) diff --git a/dot/network/block_announce.go b/dot/network/block_announce.go index 8c5771b67e..033ded1e4b 100644 --- a/dot/network/block_announce.go +++ b/dot/network/block_announce.go @@ -30,11 +30,6 @@ type BlockAnnounceMessage struct { BestBlock bool } -// SubProtocol returns the block-announces sub-protocol -func (*BlockAnnounceMessage) SubProtocol() string { - return blockAnnounceID -} - // Type returns BlockAnnounceMsgType func (*BlockAnnounceMessage) Type() byte { return BlockAnnounceMsgType @@ -114,11 +109,6 @@ type BlockAnnounceHandshake struct { GenesisHash common.Hash } -// SubProtocol returns the block-announces sub-protocol -func (*BlockAnnounceHandshake) SubProtocol() string { - return blockAnnounceID -} - // String formats a BlockAnnounceHandshake as a string func (hs *BlockAnnounceHandshake) String() string { return fmt.Sprintf("BlockAnnounceHandshake Roles=%d BestBlockNumber=%d BestBlockHash=%s GenesisHash=%s", diff --git a/dot/network/light.go b/dot/network/light.go index 15a009194d..e1f0839fc7 100644 --- a/dot/network/light.go +++ b/dot/network/light.go @@ -125,11 +125,6 @@ func newRequest() *request { } } -// SubProtocol returns the light sub-protocol -func (l *LightRequest) SubProtocol() string { - return lightID -} - // Encode encodes a LightRequest message using SCALE and appends the type byte to the start func (l *LightRequest) Encode() ([]byte, error) { req := request{ @@ -206,11 +201,6 @@ func newResponse() *response { } } -// SubProtocol returns the light sub-protocol -func (l *LightResponse) SubProtocol() string { - return lightID -} - // Encode encodes a LightResponse message using SCALE and appends the type byte to the start func (l *LightResponse) Encode() ([]byte, error) { resp := response{ diff --git a/dot/network/message.go b/dot/network/message.go index 8c26974138..2a9db0d4f9 100644 --- a/dot/network/message.go +++ b/dot/network/message.go @@ -26,7 +26,6 @@ const ( // Message must be implemented by all network messages type Message interface { - SubProtocol() string Encode() ([]byte, error) Decode([]byte) error String() string @@ -82,11 +81,6 @@ type BlockRequestMessage struct { Max *uint32 } -// SubProtocol returns the sync sub-protocol -func (bm *BlockRequestMessage) SubProtocol() string { - return syncID -} - // String formats a BlockRequestMessage as a string func (bm *BlockRequestMessage) String() string { hash := common.Hash{} @@ -207,11 +201,6 @@ type BlockResponseMessage struct { BlockData []*types.BlockData } -// SubProtocol returns the sync sub-protocol -func (bm *BlockResponseMessage) SubProtocol() string { - return syncID -} - // String formats a BlockResponseMessage as a string func (bm *BlockResponseMessage) String() string { if bm == nil { @@ -362,11 +351,6 @@ type ConsensusMessage struct { Data []byte } -// SubProtocol returns the empty, since consensus message sub-protocol is determined by the package using it -func (cm *ConsensusMessage) SubProtocol() string { - return "" -} - // Type returns ConsensusMsgType func (cm *ConsensusMessage) Type() byte { return ConsensusMsgType diff --git a/dot/network/transaction.go b/dot/network/transaction.go index e8a41087d4..e22e70344b 100644 --- a/dot/network/transaction.go +++ b/dot/network/transaction.go @@ -28,11 +28,6 @@ type TransactionMessage struct { Extrinsics []types.Extrinsic } -// SubProtocol returns the transactions sub-protocol -func (*TransactionMessage) SubProtocol() string { - return transactionsID -} - // Type returns TransactionMsgType func (*TransactionMessage) Type() byte { return TransactionMsgType @@ -69,11 +64,6 @@ func (*TransactionMessage) IsHandshake() bool { type transactionHandshake struct{} -// SubProtocol returns the transactions sub-protocol -func (*transactionHandshake) SubProtocol() string { - return transactionsID -} - // String formats a transactionHandshake as a string func (*transactionHandshake) String() string { return "transactionHandshake" diff --git a/lib/grandpa/grandpa.go b/lib/grandpa/grandpa.go index 23e7dd5876..4860343441 100644 --- a/lib/grandpa/grandpa.go +++ b/lib/grandpa/grandpa.go @@ -200,7 +200,7 @@ func (s *Service) Start() error { } }() - go s.sendNeighbourMessage() + go s.sendNeighbourMessage(neighbourMessageInterval) return nil } diff --git a/lib/grandpa/network.go b/lib/grandpa/network.go index 50fffe956e..84d40e2df1 100644 --- a/lib/grandpa/network.go +++ b/lib/grandpa/network.go @@ -5,6 +5,7 @@ package grandpa import ( "fmt" + "strings" "time" "github.com/ChainSafe/gossamer/dot/network" @@ -15,10 +16,9 @@ import ( "github.com/libp2p/go-libp2p-core/protocol" ) -var ( - grandpaID protocol.ID = "/paritytech/grandpa/1" - messageID = network.ConsensusMsgType - neighbourMessageInterval = time.Minute * 5 +const ( + grandpaID1 = "grandpa/1" + neighbourMessageInterval = 5 * time.Minute ) // Handshake is an alias for network.Handshake @@ -38,11 +38,6 @@ type GrandpaHandshake struct { //nolint:revive Roles byte } -// SubProtocol returns the grandpa sub-protocol -func (*GrandpaHandshake) SubProtocol() string { - return string(grandpaID) -} - // String formats a BlockAnnounceHandshake as a string func (hs *GrandpaHandshake) String() string { return fmt.Sprintf("GrandpaHandshake Roles=%d", hs.Roles) @@ -74,9 +69,13 @@ func (*GrandpaHandshake) IsHandshake() bool { } func (s *Service) registerProtocol() error { + genesisHash := s.blockState.GenesisHash().String() + genesisHash = strings.TrimPrefix(genesisHash, "0x") + grandpaProtocolID := fmt.Sprintf("/%s/%s", genesisHash, grandpaID1) + return s.network.RegisterNotificationsProtocol( - grandpaID, - messageID, + protocol.ID(grandpaProtocolID), + network.ConsensusMsgType, s.getHandshake, s.decodeHandshake, s.validateHandshake, @@ -175,8 +174,8 @@ func (s *Service) sendMessage(msg GrandpaMessage) error { return nil } -func (s *Service) sendNeighbourMessage() { - t := time.NewTicker(neighbourMessageInterval) +func (s *Service) sendNeighbourMessage(interval time.Duration) { + t := time.NewTicker(interval) defer t.Stop() for { select { diff --git a/lib/grandpa/network_test.go b/lib/grandpa/network_test.go index 92e8c4cc1f..4684cbf6d8 100644 --- a/lib/grandpa/network_test.go +++ b/lib/grandpa/network_test.go @@ -80,11 +80,7 @@ func TestHandleNetworkMessage(t *testing.T) { func TestSendNeighbourMessage(t *testing.T) { gs, st := newTestService(t) - neighbourMessageInterval = time.Second - defer func() { - neighbourMessageInterval = time.Minute * 5 - }() - go gs.sendNeighbourMessage() + go gs.sendNeighbourMessage(time.Second) digest := types.NewDigest() prd, err := types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()