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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions dot/network/block_announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 0 additions & 10 deletions dot/network/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
16 changes: 0 additions & 16 deletions dot/network/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions dot/network/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
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
25 changes: 12 additions & 13 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 = 5 * time.Minute
)

// Handshake is an alias for network.Handshake
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
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