Skip to content

Commit

Permalink
chore(dot/telemetry): Return concrete types (#2142)
Browse files Browse the repository at this point in the history
* chore(dot/telemetry): Return concrete types

Instead of returning Message interface, return telemetry message structs

* chore: add interface assertion

* Update dot/telemetry/afg_received.go

Co-authored-by: Quentin McGaw <[email protected]>

* chore: use hex instead of byte array

Co-authored-by: Eclésio Júnior <[email protected]>
Co-authored-by: Quentin McGaw <[email protected]>
  • Loading branch information
3 people authored Jan 6, 2022
1 parent 71b2cf7 commit ccf12de
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 64 deletions.
14 changes: 8 additions & 6 deletions dot/telemetry/afg_authority_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@

package telemetry

// afgAuthoritySetTM is a telemetry message of type `afg.authority_set` which is
var _ Message = (*AfgAuthoritySetTM)(nil)

// 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 {
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{
// NewAfgAuthoritySetTM creates a new AfgAuthoritySetTM struct.
func NewAfgAuthoritySetTM(authorityID, authoritySetID, authorities string) *AfgAuthoritySetTM {
return &AfgAuthoritySetTM{
AuthorityID: authorityID,
AuthoritySetID: authoritySetID,
Authorities: authorities,
}
}

func (afgAuthoritySetTM) messageType() string {
func (AfgAuthoritySetTM) messageType() string {
return afgAuthoritySetMsg
}
14 changes: 8 additions & 6 deletions dot/telemetry/afg_finalized_blocks_up_to.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
)

// afgFinalizedBlocksUpToTM holds telemetry message of type `afg.finalized_blocks_up_to`,
var _ Message = (*AfgFinalizedBlocksUpToTM)(nil)

// 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 {
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{
// NewAfgFinalizedBlocksUpToTM creates a new AfgFinalizedBlocksUpToTM struct.
func NewAfgFinalizedBlocksUpToTM(hash common.Hash, number string) *AfgFinalizedBlocksUpToTM {
return &AfgFinalizedBlocksUpToTM{
Hash: hash,
Number: number,
}
}

func (afgFinalizedBlocksUpToTM) messageType() string {
func (AfgFinalizedBlocksUpToTM) messageType() string {
return afgFinalizedBlocksUpToMsg
}
43 changes: 25 additions & 18 deletions dot/telemetry/afg_received.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,70 @@ import "github.com/ChainSafe/gossamer/lib/common"

// AfG ("Al's Finality Gadget") is synonymous with GRANDPA.

var (
_ Message = (*AfgReceivedPrecommitTM)(nil)
_ Message = (*AfgReceivedPrevoteTM)(nil)
_ Message = (*AfgReceivedCommitTM)(nil)
)

type afgReceivedTM struct {
TargetHash common.Hash `json:"target_hash"`
TargetNumber string `json:"target_number"`
Voter string `json:"voter"`
}

// afgReceivedPrecommitTM holds `afg.received_precommit` telemetry message which is
// AfgReceivedPrecommitTM holds `afg.received_precommit` telemetry message which is
// supposed to be sent when grandpa client receives a precommit.
type afgReceivedPrecommitTM afgReceivedTM
type AfgReceivedPrecommitTM afgReceivedTM

// NewAfgReceivedPrecommitTM gets a new afgReceivedPrecommitTM struct.
func NewAfgReceivedPrecommitTM(targetHash common.Hash, targetNumber, voter string) Message {
return &afgReceivedPrecommitTM{
// NewAfgReceivedPrecommitTM gets a new AfgReceivedPrecommitTM struct.
func NewAfgReceivedPrecommitTM(targetHash common.Hash, targetNumber, voter string) *AfgReceivedPrecommitTM {
return &AfgReceivedPrecommitTM{
TargetHash: targetHash,
TargetNumber: targetNumber,
Voter: voter,
}
}

func (afgReceivedPrecommitTM) messageType() string {
func (AfgReceivedPrecommitTM) messageType() string {
return afgReceivedPrecommitMsg
}

// afgReceivedPrevoteTM holds `afg.received_prevote` telemetry message which is
// AfgReceivedPrevoteTM holds `afg.received_prevote` telemetry message which is
// supposed to be sent when grandpa client receives a prevote.
type afgReceivedPrevoteTM afgReceivedTM
type AfgReceivedPrevoteTM afgReceivedTM

// NewAfgReceivedPrevoteTM gets a new afgReceivedPrevoteTM struct.
func NewAfgReceivedPrevoteTM(targetHash common.Hash, targetNumber, voter string) Message {
return &afgReceivedPrevoteTM{
// NewAfgReceivedPrevoteTM gets a new AfgReceivedPrevoteTM struct.
func NewAfgReceivedPrevoteTM(targetHash common.Hash, targetNumber, voter string) *AfgReceivedPrevoteTM {
return &AfgReceivedPrevoteTM{
TargetHash: targetHash,
TargetNumber: targetNumber,
Voter: voter,
}
}

func (afgReceivedPrevoteTM) messageType() string {
func (AfgReceivedPrevoteTM) messageType() string {
return afgReceivedPrevoteMsg
}

// afgReceivedCommitTM holds `afg.received_commit` telemetry message which is
// AfgReceivedCommitTM holds `afg.received_commit` telemetry message which is
// supposed to be sent when grandpa client receives a commit.
type afgReceivedCommitTM struct {
type AfgReceivedCommitTM struct {
TargetHash common.Hash `json:"target_hash"`
TargetNumber string `json:"target_number"`
ContainsPrecommitsSignedBy []string `json:"contains_precommits_signed_by"`
}

// NewAfgReceivedCommitTM gets a new afgReceivedCommitTM struct.
func NewAfgReceivedCommitTM(targetHash common.Hash, targetNumber string, containsPrecommitsSignedBy []string) Message {
return &afgReceivedCommitTM{
// NewAfgReceivedCommitTM gets a new AfgReceivedCommitTM struct.
func NewAfgReceivedCommitTM(targetHash common.Hash, targetNumber string,
containsPrecommitsSignedBy []string) *AfgReceivedCommitTM {
return &AfgReceivedCommitTM{
TargetHash: targetHash,
TargetNumber: targetNumber,
ContainsPrecommitsSignedBy: containsPrecommitsSignedBy,
}
}

func (afgReceivedCommitTM) messageType() string {
func (AfgReceivedCommitTM) messageType() string {
return afgReceivedCommitMsg
}
12 changes: 7 additions & 5 deletions dot/telemetry/block_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
)

// blockImportTM struct to hold block import telemetry messages
type blockImportTM struct {
var _ Message = (*BlockImportTM)(nil)

// 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{
func NewBlockImportTM(bestHash *common.Hash, height *big.Int, origin string) *BlockImportTM {
return &BlockImportTM{
BestHash: bestHash,
Height: height,
Origin: origin,
}
}

func (blockImportTM) messageType() string {
func (BlockImportTM) messageType() string {
return blockImportMsg
}
14 changes: 8 additions & 6 deletions dot/telemetry/notify_finalized.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
)

// notifyFinalizedTM holds `notify.finalized` telemetry message, which is
var _ Message = (*NotifyFinalizedTM)(nil)

// NotifyFinalizedTM holds `notify.finalized` telemetry message, which is
// supposed to be send when a new block gets finalised.
type notifyFinalizedTM struct {
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{
// NewNotifyFinalizedTM gets a new NotifyFinalizedTM struct.
func NewNotifyFinalizedTM(best common.Hash, height string) *NotifyFinalizedTM {
return &NotifyFinalizedTM{
Best: best,
Height: height,
}
}

func (notifyFinalizedTM) messageType() string {
func (NotifyFinalizedTM) messageType() string {
return notifyFinalizedMsg
}
12 changes: 7 additions & 5 deletions dot/telemetry/prepared_block_for_proposing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
)

// preparedBlockForProposingTM holds a 'prepared_block_for_proposing' telemetry
var _ Message = (*PreparedBlockForProposingTM)(nil)

// PreparedBlockForProposingTM holds a 'prepared_block_for_proposing' telemetry
// message, which is supposed to be sent when a new block is built.
type preparedBlockForProposingTM struct {
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{
func NewPreparedBlockForProposingTM(hash common.Hash, number string) *PreparedBlockForProposingTM {
return &PreparedBlockForProposingTM{
Hash: hash,
Number: number,
}
}

func (preparedBlockForProposingTM) messageType() string {
func (PreparedBlockForProposingTM) messageType() string {
return preparedBlockForProposingMsg
}
12 changes: 7 additions & 5 deletions dot/telemetry/system_connected.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package telemetry

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

// systemConnectedTM struct to hold system connected telemetry messages
type systemConnectedTM struct {
var _ Message = (*SystemConnectedTM)(nil)

// 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"`
Expand All @@ -19,8 +21,8 @@ type systemConnectedTM struct {

// 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{
implementation, name, networkID, startupTime, version string) *SystemConnectedTM {
return &SystemConnectedTM{
Authority: authority,
Chain: chain,
GenesisHash: genesisHash,
Expand All @@ -32,6 +34,6 @@ func NewSystemConnectedTM(authority bool, chain string, genesisHash *common.Hash
}
}

func (systemConnectedTM) messageType() string {
func (SystemConnectedTM) messageType() string {
return systemConnectedMsg
}
16 changes: 9 additions & 7 deletions dot/telemetry/system_interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
)

// systemIntervalTM struct to hold system interval telemetry messages
type systemIntervalTM struct {
var _ Message = (*SystemIntervalTM)(nil)

// 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"`
Expand All @@ -23,8 +25,8 @@ type systemIntervalTM struct {
}

// NewBandwidthTM function to create new Bandwidth Telemetry Message
func NewBandwidthTM(bandwidthDownload, bandwidthUpload float64, peers int) Message {
return &systemIntervalTM{
func NewBandwidthTM(bandwidthDownload, bandwidthUpload float64, peers int) *SystemIntervalTM {
return &SystemIntervalTM{
BandwidthDownload: bandwidthDownload,
BandwidthUpload: bandwidthUpload,
Peers: peers,
Expand All @@ -33,8 +35,8 @@ func NewBandwidthTM(bandwidthDownload, bandwidthUpload float64, peers int) Messa

// 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{
finalisedHeight, txCount, usedStateCacheSize *big.Int) *SystemIntervalTM {
return &SystemIntervalTM{
BestHash: beshHash,
BestHeight: bestHeight,
FinalisedHash: finalisedHash,
Expand All @@ -44,6 +46,6 @@ func NewBlockIntervalTM(beshHash *common.Hash, bestHeight *big.Int, finalisedHas
}
}

func (systemIntervalTM) messageType() string {
func (SystemIntervalTM) messageType() string {
return systemIntervalMsg
}
14 changes: 8 additions & 6 deletions dot/telemetry/txpool_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@

package telemetry

// txpoolImportTM holds `txpool.import` telemetry message, which is supposed to be
var _ Message = (*TxpoolImportTM)(nil)

// TxpoolImportTM holds `txpool.import` telemetry message, which is supposed to be
// sent when a new transaction gets imported in the transaction pool.
type txpoolImportTM struct {
type TxpoolImportTM struct {
Ready uint `json:"ready"`
Future uint `json:"future"`
}

// NewTxpoolImportTM creates a new txpoolImportTM struct
func NewTxpoolImportTM(ready, future uint) Message {
return &txpoolImportTM{
// NewTxpoolImportTM creates a new TxpoolImportTM struct
func NewTxpoolImportTM(ready, future uint) *TxpoolImportTM {
return &TxpoolImportTM{
Ready: ready,
Future: future,
}
}

func (txpoolImportTM) messageType() string {
func (TxpoolImportTM) messageType() string {
return txPoolImportMsg
}

0 comments on commit ccf12de

Please sign in to comment.