Skip to content

Commit

Permalink
feat: Added metric for concurrent keysigns (zeta-chain#1954)
Browse files Browse the repository at this point in the history
* initial commit

* added comments

* add changelog

* addressed comments

* rename constructor
  • Loading branch information
kevinssgh authored Mar 28, 2024
1 parent 0759af2 commit 1e0d859
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* [1815](https://github.com/zeta-chain/node/pull/1815) - add authority module for authorized actions
* [1884](https://github.com/zeta-chain/node/pull/1884) - added zetatool cmd, added subcommand to filter deposits
* [1935](https://github.com/zeta-chain/node/pull/1935) - add an operational authority group
* [1954](https://github.com/zeta-chain/node/pull/1954) - add metric for concurrent keysigns

### Tests

Expand Down
6 changes: 6 additions & 0 deletions zetaclient/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ var (
Name: "last_start_timestamp_seconds",
Help: "Start time in Unix time",
})

NumActiveMsgSigns = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: ZetaClientNamespace,
Name: "num_active_message_signs",
Help: "Number of concurrent key signs",
})
)

func NewMetrics() (*Metrics, error) {
Expand Down
49 changes: 49 additions & 0 deletions zetaclient/tss/tss_keysign_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tss

import (
"sync"

"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
)

// ConcurrentKeysignsTracker keeps track of concurrent keysigns performed by go-tss
type ConcurrentKeysignsTracker struct {
numActiveMsgSigns int64
mu sync.Mutex
Logger zerolog.Logger
}

// NewKeysignsTracker - constructor
func NewKeysignsTracker(logger zerolog.Logger) *ConcurrentKeysignsTracker {
return &ConcurrentKeysignsTracker{
numActiveMsgSigns: 0,
mu: sync.Mutex{},
Logger: logger.With().Str("submodule", "ConcurrentKeysignsTracker").Logger(),
}
}

// StartMsgSign is incrementing the number of active signing ceremonies as well as updating the prometheus metric
func (k *ConcurrentKeysignsTracker) StartMsgSign() {
k.mu.Lock()
defer k.mu.Unlock()
k.numActiveMsgSigns++
metrics.NumActiveMsgSigns.Inc()
k.Logger.Debug().Msgf("Start TSS message sign, numActiveMsgSigns: %d", k.numActiveMsgSigns)
}

// EndMsgSign is decrementing the number of active signing ceremonies as well as updating the prometheus metric
func (k *ConcurrentKeysignsTracker) EndMsgSign() {
k.mu.Lock()
defer k.mu.Unlock()
if k.numActiveMsgSigns > 0 {
k.numActiveMsgSigns--
metrics.NumActiveMsgSigns.Dec()
}
k.Logger.Debug().Msgf("End TSS message sign, numActiveMsgSigns: %d", k.numActiveMsgSigns)
}

// GetNumActiveMessageSigns gets the current number of active signing ceremonies
func (k *ConcurrentKeysignsTracker) GetNumActiveMessageSigns() int64 {
return k.numActiveMsgSigns
}
27 changes: 27 additions & 0 deletions zetaclient/tss/tss_keysign_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tss

import (
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

func TestKeySignManager_StartMsgSign(t *testing.T) {
ksman := NewKeysignsTracker(zerolog.Logger{})
ksman.StartMsgSign()
ksman.StartMsgSign()
ksman.StartMsgSign()
ksman.StartMsgSign()
require.Equal(t, int64(4), ksman.GetNumActiveMessageSigns())
}

func TestKeySignManager_EndMsgSign(t *testing.T) {
ksman := NewKeysignsTracker(zerolog.Logger{})
ksman.StartMsgSign()
ksman.StartMsgSign()
ksman.EndMsgSign()
ksman.EndMsgSign()
ksman.EndMsgSign()
require.Equal(t, int64(0), ksman.GetNumActiveMessageSigns())
}
35 changes: 23 additions & 12 deletions zetaclient/tss/tss_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ var _ interfaces.TSSSigner = (*TSS)(nil)

// TSS is a struct that holds the server and the keys for TSS
type TSS struct {
Server *tss.TssServer
Keys map[string]*Key // PubkeyInBech32 => TSSKey
CurrentPubkey string
logger zerolog.Logger
Signers []string
CoreBridge interfaces.ZetaCoreBridger
Server *tss.TssServer
Keys map[string]*Key // PubkeyInBech32 => TSSKey
CurrentPubkey string
logger zerolog.Logger
Signers []string
CoreBridge interfaces.ZetaCoreBridger
KeysignsTracker *ConcurrentKeysignsTracker

// TODO: support multiple Bitcoin network, not just one network
// https://github.com/zeta-chain/node/issues/1397
Expand All @@ -92,17 +93,19 @@ func NewTSS(
tssPassword string,
hotkeyPassword string,
) (*TSS, error) {
logger := log.With().Str("module", "tss_signer").Logger()
server, err := SetupTSSServer(peer, privkey, preParams, appContext.Config(), tssPassword)
if err != nil {
return nil, fmt.Errorf("SetupTSSServer error: %w", err)
}
newTss := TSS{
Server: server,
Keys: make(map[string]*Key),
CurrentPubkey: appContext.ZetaCoreContext().GetCurrentTssPubkey(),
logger: log.With().Str("module", "tss_signer").Logger(),
CoreBridge: bridge,
BitcoinChainID: bitcoinChainID,
Server: server,
Keys: make(map[string]*Key),
CurrentPubkey: appContext.ZetaCoreContext().GetCurrentTssPubkey(),
logger: logger,
CoreBridge: bridge,
KeysignsTracker: NewKeysignsTracker(logger),
BitcoinChainID: bitcoinChainID,
}

err = newTss.LoadTssFilesFromDirectory(appContext.Config().TssPath)
Expand All @@ -121,9 +124,13 @@ func NewTSS(
if err != nil {
return nil, err
}

// Initialize metrics
for _, key := range keygenRes.GranteePubkeys {
metrics.TssNodeBlamePerPubKey.WithLabelValues(key).Inc()
}
metrics.NumActiveMsgSigns.Set(0)

return &newTss, nil
}

Expand Down Expand Up @@ -203,7 +210,9 @@ func (tss *TSS) Sign(digest []byte, height uint64, nonce uint64, chain *chains.C
}
// #nosec G701 always in range
keysignReq := keysign.NewRequest(tssPubkey, []string{base64.StdEncoding.EncodeToString(H)}, int64(height), nil, "0.14.0")
tss.KeysignsTracker.StartMsgSign()
ksRes, err := tss.Server.KeySign(keysignReq)
tss.KeysignsTracker.EndMsgSign()
if err != nil {
log.Warn().Msg("keysign fail")
}
Expand Down Expand Up @@ -272,7 +281,9 @@ func (tss *TSS) SignBatch(digests [][]byte, height uint64, nonce uint64, chain *
// #nosec G701 always in range
keysignReq := keysign.NewRequest(tssPubkey, digestBase64, int64(height), nil, "0.14.0")

tss.KeysignsTracker.StartMsgSign()
ksRes, err := tss.Server.KeySign(keysignReq)
tss.KeysignsTracker.EndMsgSign()
if err != nil {
log.Warn().Err(err).Msg("keysign fail")
}
Expand Down

0 comments on commit 1e0d859

Please sign in to comment.