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

fix(lib/grandpa): update grandpa protocol ID #2678

Merged
merged 7 commits into from
Jul 26, 2022
Merged
2 changes: 1 addition & 1 deletion lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (s *Service) Start() error {
}
}()

go s.sendNeighbourMessage()
go s.sendNeighbourMessage(neighbourMessageInterval)

return nil
}
Expand Down
23 changes: 14 additions & 9 deletions lib/grandpa/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package grandpa

import (
"fmt"
"strings"
"time"

"github.com/ChainSafe/gossamer/dot/network"
Expand All @@ -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 = time.Minute * 5
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
)

// Handshake is an alias for network.Handshake
Expand All @@ -39,8 +39,9 @@ type GrandpaHandshake struct { //nolint:revive
}

// SubProtocol returns the grandpa sub-protocol
// TODO: should we remove the SubProtocol method from the `Message` interface?
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
func (*GrandpaHandshake) SubProtocol() string {
return string(grandpaID)
return ""
}

// String formats a BlockAnnounceHandshake as a string
Expand Down Expand Up @@ -74,9 +75,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,
Expand Down Expand Up @@ -175,8 +180,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 {
Expand Down
6 changes: 1 addition & 5 deletions lib/grandpa/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down