From 7c870a4400b6aa8e78ed47dd64961a438dfedf11 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Wed, 1 Dec 2021 04:31:35 +0530 Subject: [PATCH] feat(dot/telemetry): Added more telemetry messages in grandpa client (#2043) * Added more telemetry messages in grandpa client Added telemetry messges of types `afg.finalized_blocks_up_to` and `afg.authority_set` Closes #1841 Closes #1842 * fixed lint * Added tests * Send afg.authority_set once every grandpa round * Addressed some reviews * fix linting * addressed reviews * change ordering * addressed reviews --- dot/telemetry/afg_authority_set.go | 27 ++++++++++++++ dot/telemetry/afg_finalized_blocks_up_to.go | 27 ++++++++++++++ dot/telemetry/notify_finalized.go | 2 +- dot/telemetry/telemetry.go | 25 ++++++++----- dot/telemetry/telemetry_test.go | 5 +++ lib/grandpa/grandpa.go | 39 +++++++++++++++++++++ 6 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 dot/telemetry/afg_authority_set.go create mode 100644 dot/telemetry/afg_finalized_blocks_up_to.go diff --git a/dot/telemetry/afg_authority_set.go b/dot/telemetry/afg_authority_set.go new file mode 100644 index 0000000000..233df980cc --- /dev/null +++ b/dot/telemetry/afg_authority_set.go @@ -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 +} diff --git a/dot/telemetry/afg_finalized_blocks_up_to.go b/dot/telemetry/afg_finalized_blocks_up_to.go new file mode 100644 index 0000000000..0b63c8559d --- /dev/null +++ b/dot/telemetry/afg_finalized_blocks_up_to.go @@ -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 +} diff --git a/dot/telemetry/notify_finalized.go b/dot/telemetry/notify_finalized.go index 8323f3280d..75501d37de 100644 --- a/dot/telemetry/notify_finalized.go +++ b/dot/telemetry/notify_finalized.go @@ -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 ¬ifyFinalizedTM{ Best: best, diff --git a/dot/telemetry/telemetry.go b/dot/telemetry/telemetry.go index c5285494c0..9c2f121bea 100644 --- a/dot/telemetry/telemetry.go +++ b/dot/telemetry/telemetry.go @@ -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 { diff --git a/dot/telemetry/telemetry_test.go b/dot/telemetry/telemetry_test.go index eef1c59805..98410ce662 100644 --- a/dot/telemetry/telemetry_test.go +++ b/dot/telemetry/telemetry_test.go @@ -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 @@ -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{}), diff --git a/lib/grandpa/grandpa.go b/lib/grandpa/grandpa.go index 0b1aeab9ae..0955dcbb7f 100644 --- a/lib/grandpa/grandpa.go +++ b/lib/grandpa/grandpa.go @@ -6,6 +6,7 @@ package grandpa import ( "bytes" "context" + "encoding/json" "errors" "fmt" "math/big" @@ -13,6 +14,7 @@ import ( "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" @@ -258,6 +260,9 @@ func (s *Service) updateAuthorities() error { // setting to 0 before incrementing indicates // the setID has been increased s.state.round = 0 + + s.sendTelemetryAuthoritySet() + return nil } @@ -265,6 +270,31 @@ 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() @@ -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 } }