Skip to content

Commit

Permalink
Added more telemetry messages in grandpa client
Browse files Browse the repository at this point in the history
Added telemetry messges of types `afg.finalized_blocks_up_to` and
`afg.authority_set`

Closes #1841
Closes #1842
  • Loading branch information
kishansagathiya committed Nov 15, 2021
1 parent dde989d commit ee9ec60
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 6 deletions.
44 changes: 44 additions & 0 deletions dot/telemetry/afg_authority_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

package telemetry

// afgAuthoritySetTM is a telemetry message of type `afg.authority_set` which is
// meant to be sent when authority set changes (generally when a round is initiated)
type afgAuthoritySetTM struct {
// finalized hash and finalized number are sent by substrate, but not ready by
// substrate telemetry
// FinalizedHash *common.Hash `json:"hash"`
// FinalizedNumber string `json:"number"`
AuthorityID string `json:"authority_id"`
AuthoritySetID string `json:"authority_set_id"`
// Substrate creates an array of string of authority IDs. It JSON-serializes
// that array and send that as a string.
Authorities string `json:"authorities"`
}

// NewAfgAuthoritySetTM creates a new afgAuthoritySetTM struct.
func NewAfgAuthoritySetTM(authorityID, authoritySetID string, authorities string) Message {
return &afgAuthoritySetTM{
AuthorityID: authorityID,
AuthoritySetID: authoritySetID,
Authorities: authorities,
}
}

func (afgAuthoritySetTM) messageType() string {
return afgAuthoritySetMsg
}
27 changes: 27 additions & 0 deletions dot/telemetry/afg_finalized.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

package telemetry

import (
"github.com/ChainSafe/gossamer/lib/common"
)

// TODO: Why is this not being sent by substrate?
type afgFinalizedTM struct {
FinalizedHash *common.Hash `json:"finalized_hash"`
FinalizedNumber string `json:"finalized_number"`
}
40 changes: 40 additions & 0 deletions dot/telemetry/afg_finalized_blocks_up_to.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.

package telemetry

import (
"github.com/ChainSafe/gossamer/lib/common"
)

// afgFinalizedBlocksUpToTM holds telemetry message of type `afg.finalized_blocks_up_to`,
// which is supposed to be send GRANDPA client finalizes new blocks.
type afgFinalizedBlocksUpToTM struct {
Hash common.Hash `json:"hash"`
Number string `json:"number"`
}

// NewAfgFinalizedBlocksUpToTM creates a new afgFinalizedBlocksUpToTM struct.
func NewAfgFinalizedBlocksUpToTM(hash common.Hash, number string) Message {
return &afgFinalizedBlocksUpToTM{
Hash: hash,
Number: number,
}
}

func (afgFinalizedBlocksUpToTM) messageType() string {
return afgFinalizedBlocksUpToMsg
}
2 changes: 1 addition & 1 deletion dot/telemetry/notify_finalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type notifyFinalizedTM struct {
Height string `json:"height"`
}

// NewNotifyFinalizedTM gets a new NotifyFinalizedTM struct.
// NewNotifyFinalizedTM gets a new notifyFinalizedTM struct.
func NewNotifyFinalizedTM(best common.Hash, height string) Message {
return &notifyFinalizedTM{
Best: best,
Expand Down
12 changes: 7 additions & 5 deletions dot/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import (

// telemetry message types
const (
notifyFinalizedMsg = "notify.finalized"
blockImportMsg = "block.import"
systemNetworkStateMsg = "system.network_state"
systemConnectedMsg = "system.connected"
systemIntervalMsg = "system.interval"
notifyFinalizedMsg = "notify.finalized"
blockImportMsg = "block.import"
systemNetworkStateMsg = "system.network_state"
systemConnectedMsg = "system.connected"
systemIntervalMsg = "system.interval"
afgFinalizedBlocksUpToMsg = "afg.finalized_blocks_up_to"
afgAuthoritySetMsg = "afg.authority_set"
)

type telemetryConnection struct {
Expand Down
34 changes: 34 additions & 0 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ package grandpa
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"sync"
"sync/atomic"
"time"

"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/blocktree"
Expand Down Expand Up @@ -263,6 +265,29 @@ func (s *Service) updateAuthorities() error {
s.state.voters = nextAuthorities
s.state.setID = currSetID
s.state.round = 0 // round resets to 1 after a set ID change, setting to 0 before incrementing indicates the setID has been increased

authorityID := s.keypair.Public().Hex()
authorities := make([]string, len(s.state.voters))
for i, voter := range s.state.voters {
authorities[i] = fmt.Sprintf("%d", voter.ID)
}

authoritiesBytes, err := json.Marshal(authorities)
if err != nil {
return err
}

err = telemetry.GetInstance().SendMessage(
telemetry.NewAfgAuthoritySetTM(
authorityID,
fmt.Sprintf("%d", s.state.setID),
string(authoritiesBytes),
),
)
if err != nil {
logger.Debugf("problem sending afg.authority_set telemetry message: %s", err)
}

return nil
}

Expand Down Expand Up @@ -612,6 +637,15 @@ func (s *Service) attemptToFinalize() error {

logger.Debugf("sending CommitMessage: %v", cm)
s.network.GossipMessage(msg)

err = telemetry.GetInstance().SendMessage(telemetry.NewAfgFinalizedBlocksUpToTM(
s.head.Hash(),
s.head.Number.String(),
))
if err != nil {
logger.Debugf("problem sending `afg.finalized_blocks_up_to` telemetry message: %s", err)
}

return nil
}
}
Expand Down

0 comments on commit ee9ec60

Please sign in to comment.