Skip to content

Commit

Permalink
feature(dot/telemetry): Implement prepared_block_for_proposing Teleme…
Browse files Browse the repository at this point in the history
…try Interface (#1906)
  • Loading branch information
kishansagathiya committed Oct 25, 2021
1 parent 2186caf commit bb4a529
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
43 changes: 43 additions & 0 deletions dot/telemetry/prepared_block_for_proposing.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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
}
11 changes: 6 additions & 5 deletions dot/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions lib/babe/babe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bb4a529

Please sign in to comment.