Skip to content

Commit

Permalink
feat(dot/telemetry): implement notify.finalized telemetry interface (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kishansagathiya committed Oct 27, 2021
1 parent b53627a commit de1a60d
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 145 deletions.
19 changes: 18 additions & 1 deletion dot/state/block_finalisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"math/big"

"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
)
Expand Down Expand Up @@ -126,7 +127,7 @@ func (bs *BlockState) GetHighestFinalisedHeader() (*types.Header, error) {
return header, nil
}

// SetFinalisedHash sets the latest finalised block header
// SetFinalisedHash sets the latest finalised block hash
func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) error {
bs.Lock()
defer bs.Unlock()
Expand Down Expand Up @@ -178,6 +179,22 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er
}
}

header, err := bs.GetHeader(hash)
if err != nil {
return fmt.Errorf("failed to get finalised header, hash: %s, error: %s", hash, err)
}

err = telemetry.GetInstance().SendMessage(
telemetry.NewNotifyFinalizedTM(
header.Hash(),
header.Number.String(),
),
)
if err != nil {
return fmt.Errorf("could not send 'notify.finalized' telemetry message, error: %s", err)
}

// return bs.setHighestRoundAndSetID(round, setID)
bs.lastFinalised = hash
return nil
}
Expand Down
43 changes: 43 additions & 0 deletions dot/telemetry/block_import.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 (
"math/big"

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

// blockImportTM struct to hold block import telemetry messages
type blockImportTM struct {
BestHash *common.Hash `json:"best"`
Height *big.Int `json:"height"`
Origin string `json:"origin"`
}

// NewBlockImportTM function to create new Block Import Telemetry Message
func NewBlockImportTM(bestHash *common.Hash, height *big.Int, origin string) Message {
return &blockImportTM{
BestHash: bestHash,
Height: height,
Origin: origin,
}
}

func (blockImportTM) messageType() string {
return blockImportMsg
}
66 changes: 66 additions & 0 deletions dot/telemetry/network_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 (
"fmt"

"github.com/ChainSafe/gossamer/lib/common"
libp2phost "github.com/libp2p/go-libp2p-core/host"
)

// networkStateTM struct to hold network state telemetry messages
type networkStateTM struct {
State map[string]interface{} `json:"state"`
}

// NewNetworkStateTM function to create new Network State Telemetry Message
func NewNetworkStateTM(host libp2phost.Host, peerInfos []common.PeerInfo) Message {
netState := make(map[string]interface{})
netState["peerId"] = host.ID()
hostAddrs := make([]string, 0, len(host.Addrs()))
for _, v := range host.Addrs() {
hostAddrs = append(hostAddrs, v.String())
}
netState["externalAddressess"] = hostAddrs

netListAddrs := host.Network().ListenAddresses()
listAddrs := make([]string, 0, len(netListAddrs))
for _, v := range netListAddrs {
listAddrs = append(listAddrs, fmt.Sprintf("%s/p2p/%s", v, host.ID()))
}
netState["listenedAddressess"] = listAddrs

peers := make(map[string]interface{})
for _, v := range peerInfos {
p := &peerInfo{
Roles: v.Roles,
BestHash: v.BestHash.String(),
BestNumber: v.BestNumber,
}
peers[v.PeerID] = *p
}
netState["connectedPeers"] = peers

return &networkStateTM{
State: netState,
}
}

func (networkStateTM) messageType() string {
return systemNetworkStateMsg
}
42 changes: 42 additions & 0 deletions dot/telemetry/notify_finalized.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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"
)

//nolint
// notifyFinalizedTM holds `notify.finalized` telemetry message, which is
// supposed to be send when a new block gets finalized.
type notifyFinalizedTM struct {
Best common.Hash `json:"best"`
// Height is same as block.Header.Number
Height string `json:"height"`
}

// NewNotifyFinalizedTM gets a new NotifyFinalizedTM struct.
func NewNotifyFinalizedTM(best common.Hash, height string) Message {
return &notifyFinalizedTM{
Best: best,
Height: height,
}
}

func (notifyFinalizedTM) messageType() string {
return notifyFinalizedMsg
}
50 changes: 50 additions & 0 deletions dot/telemetry/system_connected.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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"

// systemConnectedTM struct to hold system connected telemetry messages
type systemConnectedTM struct {
Authority bool `json:"authority"`
Chain string `json:"chain"`
GenesisHash *common.Hash `json:"genesis_hash"`
Implementation string `json:"implementation"`
Name string `json:"name"`
NetworkID string `json:"network_id"`
StartupTime string `json:"startup_time"`
Version string `json:"version"`
}

// NewSystemConnectedTM function to create new System Connected Telemetry Message
func NewSystemConnectedTM(authority bool, chain string, genesisHash *common.Hash,
implementation, name, networkID, startupTime, version string) Message {
return &systemConnectedTM{
Authority: authority,
Chain: chain,
GenesisHash: genesisHash,
Implementation: implementation,
Name: name,
NetworkID: networkID,
StartupTime: startupTime,
Version: version,
}
}

func (systemConnectedTM) messageType() string {
return systemConnectedMsg
}
62 changes: 62 additions & 0 deletions dot/telemetry/system_interval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 (
"math/big"

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

// systemIntervalTM struct to hold system interval telemetry messages
type systemIntervalTM struct {
BandwidthDownload float64 `json:"bandwidth_download,omitempty"`
BandwidthUpload float64 `json:"bandwidth_upload,omitempty"`
Peers int `json:"peers,omitempty"`
BestHash *common.Hash `json:"best,omitempty"`
BestHeight *big.Int `json:"height,omitempty"`
FinalisedHash *common.Hash `json:"finalized_hash,omitempty"` // nolint
FinalisedHeight *big.Int `json:"finalized_height,omitempty"` // nolint
TxCount *big.Int `json:"txcount,omitempty"`
UsedStateCacheSize *big.Int `json:"used_state_cache_size,omitempty"`
}

// NewBandwidthTM function to create new Bandwidth Telemetry Message
func NewBandwidthTM(bandwidthDownload, bandwidthUpload float64, peers int) Message {
return &systemIntervalTM{
BandwidthDownload: bandwidthDownload,
BandwidthUpload: bandwidthUpload,
Peers: peers,
}
}

// NewBlockIntervalTM function to create new Block Interval Telemetry Message
func NewBlockIntervalTM(beshHash *common.Hash, bestHeight *big.Int, finalisedHash *common.Hash,
finalisedHeight, txCount, usedStateCacheSize *big.Int) Message {
return &systemIntervalTM{
BestHash: beshHash,
BestHeight: bestHeight,
FinalisedHash: finalisedHash,
FinalisedHeight: finalisedHeight,
TxCount: txCount,
UsedStateCacheSize: usedStateCacheSize,
}
}

func (systemIntervalTM) messageType() string {
return systemIntervalMsg
}
Loading

0 comments on commit de1a60d

Please sign in to comment.