From bb4a5296b71b98b0ebabda64951a24a5ace0c3bf Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Mon, 25 Oct 2021 15:02:50 +0530 Subject: [PATCH] feature(dot/telemetry): Implement prepared_block_for_proposing Telemetry Interface (#1906) --- dot/telemetry/prepared_block_for_proposing.go | 43 +++++++++++++++++++ dot/telemetry/telemetry_test.go | 11 ++--- lib/babe/babe.go | 11 +++++ 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 dot/telemetry/prepared_block_for_proposing.go diff --git a/dot/telemetry/prepared_block_for_proposing.go b/dot/telemetry/prepared_block_for_proposing.go new file mode 100644 index 0000000000..c74cd00932 --- /dev/null +++ b/dot/telemetry/prepared_block_for_proposing.go @@ -0,0 +1,43 @@ +// 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 . + +package telemetry + +import ( + "github.com/ChainSafe/gossamer/lib/common" +) + +// preparedBlockForProposingTM holds a 'prepared_block_for_proposing' telemetry +// message, which is supposed to be sent when a new block is built. +type preparedBlockForProposingTM struct { + Hash common.Hash `json:"hash"` + // Height of the chain, Block.Header.Number + Number string `json:"number"` + Msg string `json:"msg"` +} + +// NewPreparedBlockForProposingTM gets a new PreparedBlockForProposingTM struct. +func NewPreparedBlockForProposingTM(hash common.Hash, number string) Message { + return &preparedBlockForProposingTM{ + Hash: hash, + Number: number, + Msg: "prepared_block_for_proposing", + } +} + +func (tm *preparedBlockForProposingTM) messageType() string { + return tm.Msg +} diff --git a/dot/telemetry/telemetry_test.go b/dot/telemetry/telemetry_test.go index 7e89665149..61c3283477 100644 --- a/dot/telemetry/telemetry_test.go +++ b/dot/telemetry/telemetry_test.go @@ -84,8 +84,9 @@ func TestHandler_SendMulti(t *testing.T) { expected2 := []byte(`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","height":2,"msg":"block.import","origin":"NetworkInitialSync","ts":`) expected3 := []byte(`{"bandwidth_download":2,"bandwidth_upload":3,"msg":"system.interval","peers":1,"ts":`) expected4 := []byte(`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","finalized_hash":"0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2","finalized_height":32256,"height":32375,"msg":"system.interval","ts":`) // nolint + expected6 := []byte(`{"hash":"0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c","msg":"prepared_block_for_proposing","number":"1","ts":`) - expected := [][]byte{expected1, expected3, expected4, expected2} + expected := [][]byte{expected1, expected3, expected4, expected2, expected6} var actual [][]byte for data := range resultCh { @@ -98,10 +99,10 @@ func TestHandler_SendMulti(t *testing.T) { sort.Slice(actual, func(i, j int) bool { return bytes.Compare(actual[i], actual[j]) < 0 }) - require.Contains(t, string(actual[0]), string(expected[0])) - require.Contains(t, string(actual[1]), string(expected[1])) - require.Contains(t, string(actual[2]), string(expected[2])) - require.Contains(t, string(actual[3]), string(expected[3])) + + for i := range actual { + require.Contains(t, string(actual[i]), string(expected[i])) + } } func TestListenerConcurrency(t *testing.T) { diff --git a/lib/babe/babe.go b/lib/babe/babe.go index c0e13592e7..bc09fe3ca9 100644 --- a/lib/babe/babe.go +++ b/lib/babe/babe.go @@ -25,6 +25,7 @@ import ( "sync" "time" + "github.com/ChainSafe/gossamer/dot/telemetry" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" "github.com/ChainSafe/gossamer/lib/runtime" @@ -572,6 +573,16 @@ func (b *Service) handleSlot(epoch, slotNum uint64) error { "parent", parent.Hash(), ) + err = telemetry.GetInstance().SendMessage( + telemetry.NewPreparedBlockForProposingTM( + block.Header.Hash(), + block.Header.Number.String(), + ), + ) + if err != nil { + logger.Debug("problem sending 'prepared_block_for_proposing' telemetry message", "error", err) + } + if err := b.blockImportHandler.HandleBlockProduced(block, ts); err != nil { logger.Warn("failed to import built block", "error", err) return err