Skip to content

Commit 3be75b2

Browse files
fix(lib/grandpa): update grandpa protocol ID (#2678)
* feat: update grandpa protocol ID
1 parent 4cfcb42 commit 3be75b2

File tree

7 files changed

+14
-65
lines changed

7 files changed

+14
-65
lines changed

dot/network/block_announce.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ type BlockAnnounceMessage struct {
3030
BestBlock bool
3131
}
3232

33-
// SubProtocol returns the block-announces sub-protocol
34-
func (*BlockAnnounceMessage) SubProtocol() string {
35-
return blockAnnounceID
36-
}
37-
3833
// Type returns BlockAnnounceMsgType
3934
func (*BlockAnnounceMessage) Type() byte {
4035
return BlockAnnounceMsgType
@@ -114,11 +109,6 @@ type BlockAnnounceHandshake struct {
114109
GenesisHash common.Hash
115110
}
116111

117-
// SubProtocol returns the block-announces sub-protocol
118-
func (*BlockAnnounceHandshake) SubProtocol() string {
119-
return blockAnnounceID
120-
}
121-
122112
// String formats a BlockAnnounceHandshake as a string
123113
func (hs *BlockAnnounceHandshake) String() string {
124114
return fmt.Sprintf("BlockAnnounceHandshake Roles=%d BestBlockNumber=%d BestBlockHash=%s GenesisHash=%s",

dot/network/light.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ func newRequest() *request {
125125
}
126126
}
127127

128-
// SubProtocol returns the light sub-protocol
129-
func (l *LightRequest) SubProtocol() string {
130-
return lightID
131-
}
132-
133128
// Encode encodes a LightRequest message using SCALE and appends the type byte to the start
134129
func (l *LightRequest) Encode() ([]byte, error) {
135130
req := request{
@@ -206,11 +201,6 @@ func newResponse() *response {
206201
}
207202
}
208203

209-
// SubProtocol returns the light sub-protocol
210-
func (l *LightResponse) SubProtocol() string {
211-
return lightID
212-
}
213-
214204
// Encode encodes a LightResponse message using SCALE and appends the type byte to the start
215205
func (l *LightResponse) Encode() ([]byte, error) {
216206
resp := response{

dot/network/message.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const (
2626

2727
// Message must be implemented by all network messages
2828
type Message interface {
29-
SubProtocol() string
3029
Encode() ([]byte, error)
3130
Decode([]byte) error
3231
String() string
@@ -82,11 +81,6 @@ type BlockRequestMessage struct {
8281
Max *uint32
8382
}
8483

85-
// SubProtocol returns the sync sub-protocol
86-
func (bm *BlockRequestMessage) SubProtocol() string {
87-
return syncID
88-
}
89-
9084
// String formats a BlockRequestMessage as a string
9185
func (bm *BlockRequestMessage) String() string {
9286
hash := common.Hash{}
@@ -207,11 +201,6 @@ type BlockResponseMessage struct {
207201
BlockData []*types.BlockData
208202
}
209203

210-
// SubProtocol returns the sync sub-protocol
211-
func (bm *BlockResponseMessage) SubProtocol() string {
212-
return syncID
213-
}
214-
215204
// String formats a BlockResponseMessage as a string
216205
func (bm *BlockResponseMessage) String() string {
217206
if bm == nil {
@@ -362,11 +351,6 @@ type ConsensusMessage struct {
362351
Data []byte
363352
}
364353

365-
// SubProtocol returns the empty, since consensus message sub-protocol is determined by the package using it
366-
func (cm *ConsensusMessage) SubProtocol() string {
367-
return ""
368-
}
369-
370354
// Type returns ConsensusMsgType
371355
func (cm *ConsensusMessage) Type() byte {
372356
return ConsensusMsgType

dot/network/transaction.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ type TransactionMessage struct {
2828
Extrinsics []types.Extrinsic
2929
}
3030

31-
// SubProtocol returns the transactions sub-protocol
32-
func (*TransactionMessage) SubProtocol() string {
33-
return transactionsID
34-
}
35-
3631
// Type returns TransactionMsgType
3732
func (*TransactionMessage) Type() byte {
3833
return TransactionMsgType
@@ -69,11 +64,6 @@ func (*TransactionMessage) IsHandshake() bool {
6964

7065
type transactionHandshake struct{}
7166

72-
// SubProtocol returns the transactions sub-protocol
73-
func (*transactionHandshake) SubProtocol() string {
74-
return transactionsID
75-
}
76-
7767
// String formats a transactionHandshake as a string
7868
func (*transactionHandshake) String() string {
7969
return "transactionHandshake"

lib/grandpa/grandpa.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (s *Service) Start() error {
200200
}
201201
}()
202202

203-
go s.sendNeighbourMessage()
203+
go s.sendNeighbourMessage(neighbourMessageInterval)
204204

205205
return nil
206206
}

lib/grandpa/network.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package grandpa
55

66
import (
77
"fmt"
8+
"strings"
89
"time"
910

1011
"github.com/ChainSafe/gossamer/dot/network"
@@ -15,10 +16,9 @@ import (
1516
"github.com/libp2p/go-libp2p-core/protocol"
1617
)
1718

18-
var (
19-
grandpaID protocol.ID = "/paritytech/grandpa/1"
20-
messageID = network.ConsensusMsgType
21-
neighbourMessageInterval = time.Minute * 5
19+
const (
20+
grandpaID1 = "grandpa/1"
21+
neighbourMessageInterval = 5 * time.Minute
2222
)
2323

2424
// Handshake is an alias for network.Handshake
@@ -38,11 +38,6 @@ type GrandpaHandshake struct { //nolint:revive
3838
Roles byte
3939
}
4040

41-
// SubProtocol returns the grandpa sub-protocol
42-
func (*GrandpaHandshake) SubProtocol() string {
43-
return string(grandpaID)
44-
}
45-
4641
// String formats a BlockAnnounceHandshake as a string
4742
func (hs *GrandpaHandshake) String() string {
4843
return fmt.Sprintf("GrandpaHandshake Roles=%d", hs.Roles)
@@ -74,9 +69,13 @@ func (*GrandpaHandshake) IsHandshake() bool {
7469
}
7570

7671
func (s *Service) registerProtocol() error {
72+
genesisHash := s.blockState.GenesisHash().String()
73+
genesisHash = strings.TrimPrefix(genesisHash, "0x")
74+
grandpaProtocolID := fmt.Sprintf("/%s/%s", genesisHash, grandpaID1)
75+
7776
return s.network.RegisterNotificationsProtocol(
78-
grandpaID,
79-
messageID,
77+
protocol.ID(grandpaProtocolID),
78+
network.ConsensusMsgType,
8079
s.getHandshake,
8180
s.decodeHandshake,
8281
s.validateHandshake,
@@ -175,8 +174,8 @@ func (s *Service) sendMessage(msg GrandpaMessage) error {
175174
return nil
176175
}
177176

178-
func (s *Service) sendNeighbourMessage() {
179-
t := time.NewTicker(neighbourMessageInterval)
177+
func (s *Service) sendNeighbourMessage(interval time.Duration) {
178+
t := time.NewTicker(interval)
180179
defer t.Stop()
181180
for {
182181
select {

lib/grandpa/network_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ func TestHandleNetworkMessage(t *testing.T) {
8080

8181
func TestSendNeighbourMessage(t *testing.T) {
8282
gs, st := newTestService(t)
83-
neighbourMessageInterval = time.Second
84-
defer func() {
85-
neighbourMessageInterval = time.Minute * 5
86-
}()
87-
go gs.sendNeighbourMessage()
83+
go gs.sendNeighbourMessage(time.Second)
8884

8985
digest := types.NewDigest()
9086
prd, err := types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()

0 commit comments

Comments
 (0)