Skip to content

Commit

Permalink
feat(dot/telemetry): Added more telemetry messages in grandpa client (C…
Browse files Browse the repository at this point in the history
…hainSafe#2043)

* Added more telemetry messages in grandpa client

Added telemetry messges of types `afg.finalized_blocks_up_to` and
`afg.authority_set`

Closes ChainSafe#1841
Closes ChainSafe#1842

* fixed lint

* Added tests

* Send afg.authority_set once every grandpa round

* Addressed some reviews

* fix linting

* addressed reviews

* change ordering

* addressed reviews
  • Loading branch information
kishansagathiya authored and timwu20 committed Dec 6, 2021
1 parent 8ea3d9f commit 7c870a4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 10 deletions.
27 changes: 27 additions & 0 deletions dot/telemetry/afg_authority_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

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 {
AuthorityID string `json:"authority_id"`
AuthoritySetID string `json:"authority_set_id"`
// Substrate creates an array of string of authority IDs. It JSON-serialises
// that array and send that as a string.
Authorities string `json:"authorities"`
}

// NewAfgAuthoritySetTM creates a new afgAuthoritySetTM struct.
func NewAfgAuthoritySetTM(authorityID, authoritySetID, 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_blocks_up_to.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

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 sent when GRANDPA client finalises 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 @@ -15,7 +15,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
25 changes: 16 additions & 9 deletions dot/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ import (

// telemetry message types
const (
notifyFinalizedMsg = "notify.finalized"
blockImportMsg = "block.import"
systemNetworkStateMsg = "system.network_state"
systemConnectedMsg = "system.connected"
systemIntervalMsg = "system.interval"
afgReceivedPrecommitMsg = "afg.received_precommit"
afgReceivedPrevoteMsg = "afg.received_prevote"
afgReceivedCommitMsg = "afg.received_commit"
txPoolImportMsg = "txpool.import"
afgAuthoritySetMsg = "afg.authority_set"
afgFinalizedBlocksUpToMsg = "afg.finalized_blocks_up_to"
afgReceivedCommitMsg = "afg.received_commit"
afgReceivedPrecommitMsg = "afg.received_precommit"
afgReceivedPrevoteMsg = "afg.received_prevote"

blockImportMsg = "block.import"

notifyFinalizedMsg = "notify.finalized"

preparedBlockForProposingMsg = "prepared_block_for_proposing"

systemConnectedMsg = "system.connected"
systemIntervalMsg = "system.interval"
systemNetworkStateMsg = "system.network_state"

txPoolImportMsg = "txpool.import"
)

type telemetryConnection struct {
Expand Down
5 changes: 5 additions & 0 deletions dot/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestHandler_SendMulti(t *testing.T) {
[]byte(`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","height":"32375","msg":"notify.finalized","ts":`), //nolint:lll
[]byte(`{"hash":"0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c","msg":"prepared_block_for_proposing","number":"1","ts":`), //nolint:lll
[]byte(`{"future":2,"msg":"txpool.import","ready":1,"ts":`),
[]byte(`{"authorities":"json-stringified-ids-of-authorities","authority_id":"authority_id","authority_set_id":"authority_set_id","msg":"afg.authority_set","ts`), //nolint:lll
[]byte(`{"hash":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","msg":"afg.finalized_blocks_up_to","number":"1","ts":`), //nolint:lll
[]byte(`{"contains_precommits_signed_by":[],"msg":"afg.received_commit","target_hash":"0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c","target_number":"1","ts":`), //nolint:lll
[]byte(`{"msg":"afg.received_precommit","target_hash":"0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c","target_number":"1","ts":`), //nolint:lll
[]byte(`{"msg":"afg.received_prevote","target_hash":"0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c","target_number":"1","ts":`), //nolint:lll
Expand All @@ -79,6 +81,9 @@ func TestHandler_SendMulti(t *testing.T) {
common.MustHexToHash("0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2"),
),

NewAfgAuthoritySetTM("authority_id", "authority_set_id", "json-stringified-ids-of-authorities"),
NewAfgFinalizedBlocksUpToTM(
common.MustHexToHash("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6"), "1"),
NewAfgReceivedCommitTM(
common.MustHexToHash("0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c"),
"1", []string{}),
Expand Down
39 changes: 39 additions & 0 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,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 @@ -258,13 +260,41 @@ func (s *Service) updateAuthorities() error {
// setting to 0 before incrementing indicates
// the setID has been increased
s.state.round = 0

s.sendTelemetryAuthoritySet()

return nil
}

func (s *Service) publicKeyBytes() ed25519.PublicKeyBytes {
return s.keypair.Public().(*ed25519.PublicKey).AsBytes()
}

func (s *Service) sendTelemetryAuthoritySet() {
authorityID := s.keypair.Public().Hex()
authorities := make([]string, len(s.state.voters))
for i, voter := range s.state.voters {
authorities[i] = fmt.Sprint(voter.ID)
}

authoritiesBytes, err := json.Marshal(authorities)
if err != nil {
logger.Warnf("could not marshal authorities: %s", err)
return
}

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

func (s *Service) initiateRound() error {
// if there is an authority change, execute it
err := s.updateAuthorities()
Expand Down Expand Up @@ -609,6 +639,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 7c870a4

Please sign in to comment.