Skip to content

Commit 98ad1d6

Browse files
Merge pull request #34 from babylonchain/release/v0.1.3
Release v0.1.3
2 parents f2796b8 + a94125d commit 98ad1d6

File tree

7 files changed

+190
-16
lines changed

7 files changed

+190
-16
lines changed

config/config.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ network = "regtest"
1919

2020
[remote-signer-config]
2121
# The list of signer urls in the format http://covenant_pk@signer_host:port
22-
urls = ["http://02f071ad9a03c3c13c0ce0107c89269edd9d3ed16c42aeaf72572e1135af34b81f@http://127.0.0.1:9791", ]
22+
urls = ["http://03d4625ae15c25c380e131a0a95e23612805f0f314ef5a39ff6cba2a7e3c06f5d9@http://127.0.0.1:9791", ]
2323
# The timeout of each request to the remote signing server
2424
timeout_seconds = 2
25+
26+
[metrics-config]
27+
# Enable reporting metrics
28+
enabled = false
29+
# IP of the Prometheus server
30+
host = "127.0.0.1"
31+
# Port of the Prometheus server
32+
port = 2112

internal/config/config.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ import (
1212
)
1313

1414
type Config struct {
15-
Db DbConfig `mapstructure:"db-config"`
16-
Btc BtcConfig `mapstructure:"btc-config"`
17-
Signer RemoteSignerConfig `mapstructure:"remote-signer-config"`
15+
Db DbConfig `mapstructure:"db-config"`
16+
Btc BtcConfig `mapstructure:"btc-config"`
17+
Signer RemoteSignerConfig `mapstructure:"remote-signer-config"`
18+
Metrics MetricsConfig `mapstructure:"metrics-config"`
1819
}
1920

2021
func DefaultConfig() *Config {
2122
return &Config{
22-
Db: *DefaultDBConfig(),
23-
Btc: *DefaultBtcConfig(),
24-
Signer: *DefaultRemoteSignerConfig(),
23+
Db: *DefaultDBConfig(),
24+
Btc: *DefaultBtcConfig(),
25+
Signer: *DefaultRemoteSignerConfig(),
26+
Metrics: *DefaultMetricsConfig(),
2527
}
2628
}
2729

@@ -34,6 +36,10 @@ func (cfg *Config) Validate() error {
3436
return fmt.Errorf("invalid remote signer config: %w", err)
3537
}
3638

39+
if err := cfg.Metrics.Validate(); err != nil {
40+
return fmt.Errorf("invalid metrics config: %w", err)
41+
}
42+
3743
return nil
3844
}
3945

@@ -61,6 +67,15 @@ network = "{{ .Btc.Network }}"
6167
urls = [{{ range .Signer.Urls }}{{ printf "%q, " . }}{{end}}]
6268
# The timeout of each request to the remote signing server
6369
timeout_seconds = {{ .Signer.TimeoutSeconds }}
70+
71+
[metrics-config]
72+
# Enable reporting metrics
73+
enabled = {{ .Metrics.Enabled }}
74+
# IP of the Prometheus server
75+
host = "{{ .Metrics.Host }}"
76+
# Port of the Prometheus server
77+
port = {{ .Metrics.Port }}
78+
6479
`
6580

6681
var configTemplate *template.Template

internal/config/metrics.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const (
8+
defaultMetricsPort = 2112
9+
defaultMetricsHost = "127.0.0.1"
10+
)
11+
12+
// MetricsConfig defines the server's basic configuration
13+
type MetricsConfig struct {
14+
Enabled bool `long:"enabled" description:"Enable reporting metrics"`
15+
Host string `long:"host" description:"host of the Prometheus server"`
16+
Port int `long:"port" description:"Port of the Prometheus server"`
17+
}
18+
19+
func (cfg *MetricsConfig) Validate() error {
20+
if cfg.Port < 0 || cfg.Port > 65535 {
21+
return fmt.Errorf("invalid port: %d", cfg.Port)
22+
}
23+
24+
if cfg.Host == "" {
25+
return fmt.Errorf("host cannot be empty")
26+
}
27+
28+
return nil
29+
}
30+
31+
func (cfg *MetricsConfig) Address() (string, error) {
32+
if err := cfg.Validate(); err != nil {
33+
return "", err
34+
}
35+
return fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), nil
36+
}
37+
38+
func DefaultMetricsConfig() *MetricsConfig {
39+
return &MetricsConfig{
40+
Enabled: false,
41+
Port: defaultMetricsPort,
42+
Host: defaultMetricsHost,
43+
}
44+
}

internal/services/persistent_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ func (s *PersistentUnbondingStorage) AddTxWithSignature(
162162
sigBytes := sig.Serialize()
163163
sigHex := hex.EncodeToString(sigBytes)
164164

165-
stakerPkHex := pubKeyToString(info.StakerPk)
166-
fpPkHex := pubKeyToString(info.FinalityProviderPk)
165+
stakerPkHex := pubKeyToStringSchnorr(info.StakerPk)
166+
fpPkHex := pubKeyToStringSchnorr(info.FinalityProviderPk)
167167

168168
stakingTxHex, err := serializeBTCTxToHex(stakingtTxData.StakingTransaction)
169169
if err != nil {

internal/services/unbonding_pipeline.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"github.com/babylonchain/cli-tools/internal/btcclient"
1111
"github.com/babylonchain/cli-tools/internal/config"
1212
"github.com/babylonchain/cli-tools/internal/db"
13-
1413
"github.com/btcsuite/btcd/btcec/v2"
1514
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1615
"github.com/btcsuite/btcd/chaincfg"
1716
"github.com/btcsuite/btcd/wire"
17+
"github.com/prometheus/client_golang/prometheus/push"
1818
)
1919

2020
var (
@@ -29,10 +29,14 @@ func wrapCrititical(err error) error {
2929
return fmt.Errorf("%s:%w", err.Error(), ErrCriticalError)
3030
}
3131

32-
func pubKeyToString(pubKey *btcec.PublicKey) string {
32+
func pubKeyToStringSchnorr(pubKey *btcec.PublicKey) string {
3333
return hex.EncodeToString(schnorr.SerializePubKey(pubKey))
3434
}
3535

36+
func pubKeyToStringCompressed(pubKey *btcec.PublicKey) string {
37+
return hex.EncodeToString(pubKey.SerializeCompressed())
38+
}
39+
3640
type SystemParamsRetriever struct {
3741
CovenantPublicKeys []*btcec.PublicKey
3842
CovenantQuorum uint32
@@ -65,6 +69,7 @@ type UnbondingPipeline struct {
6569
signer CovenantSigner
6670
sender BtcSender
6771
retriever ParamsRetriever
72+
Metrics *PipelineMetrics
6873
btcParams *chaincfg.Params
6974
}
7075

@@ -101,12 +106,15 @@ func NewUnbondingPipelineFromConfig(
101106
return nil, err
102107
}
103108

109+
m := NewPipelineMetrics(&cfg.Metrics)
110+
104111
return NewUnbondingPipeline(
105112
logger,
106113
store,
107114
signer,
108115
bs,
109116
ret,
117+
m,
110118
cfg.Btc.MustGetBtcNetworkParams(),
111119
), nil
112120
}
@@ -117,6 +125,7 @@ func NewUnbondingPipeline(
117125
signer CovenantSigner,
118126
sender BtcSender,
119127
retriever ParamsRetriever,
128+
metrics *PipelineMetrics,
120129
btcParams *chaincfg.Params,
121130
) *UnbondingPipeline {
122131
return &UnbondingPipeline{
@@ -125,6 +134,7 @@ func NewUnbondingPipeline(
125134
signer: signer,
126135
sender: sender,
127136
retriever: retriever,
137+
Metrics: metrics,
128138
btcParams: btcParams,
129139
}
130140
}
@@ -174,21 +184,21 @@ func (up *UnbondingPipeline) signUnbondingTransaction(
174184
}
175185

176186
func (up *UnbondingPipeline) requestSigFromCovenant(req *SignRequest, resultChan chan *SignResult) {
177-
pkStr := pubKeyToString(req.SignerPubKey)
187+
pkStr := pubKeyToStringCompressed(req.SignerPubKey)
178188
up.logger.Debug("request signatures from covenant signer",
179189
"signer_pk", pkStr)
180190

181191
var res SignResult
182192
sigPair, err := up.signer.SignUnbondingTransaction(req)
183193
if err != nil {
184-
// TODO record metrics
194+
up.Metrics.RecordFailedSigningRequest(pkStr)
185195
up.logger.Error("failed to get signatures from covenant",
186196
"signer_pk", pkStr,
187197
"error", err)
188198

189199
res.Err = err
190200
} else {
191-
// TODO: record metrics
201+
up.Metrics.RecordSuccessSigningRequest(pkStr)
192202
up.logger.Debug("got signatures from covenant signer", "signer_pk", pkStr)
193203

194204
res.PubKeySig = sigPair
@@ -213,6 +223,22 @@ func outputsAreEqual(a, b *wire.TxOut) bool {
213223
return true
214224
}
215225

226+
func (up *UnbondingPipeline) pushMetrics() error {
227+
gatewayUrl, err := up.Metrics.Config.Address()
228+
if err != nil {
229+
return fmt.Errorf("failed to get gateway address: %w", err)
230+
}
231+
232+
up.logger.Info("Pushing metrics to gateway", "gateway", gatewayUrl)
233+
234+
return push.New(gatewayUrl, "unbonding-pipeline").
235+
Collector(up.Metrics.SuccessSigningReqs).
236+
Collector(up.Metrics.FailedSigningReqs).
237+
Collector(up.Metrics.SuccessfulSentTransactions).
238+
Collector(up.Metrics.FailureSentTransactions).
239+
Push()
240+
}
241+
216242
func (up *UnbondingPipeline) processUnbondingTransactions(
217243
ctx context.Context,
218244
transactions []*UnbondingTxData,
@@ -317,6 +343,7 @@ func (up *UnbondingPipeline) processUnbondingTransactions(
317343
if err := up.store.SetUnbondingTransactionProcessingFailed(ctx, utx); err != nil {
318344
return wrapCrititical(err)
319345
}
346+
up.Metrics.RecordFailedUnbodingTransaction()
320347
} else {
321348
up.logger.Info(
322349
"Successfully sent unbonding transaction",
@@ -325,8 +352,10 @@ func (up *UnbondingPipeline) processUnbondingTransactions(
325352
if err := up.store.SetUnbondingTransactionProcessed(ctx, utx); err != nil {
326353
return wrapCrititical(err)
327354
}
355+
up.Metrics.RecordSentUnbondingTransaction()
328356
}
329357
}
358+
330359
return nil
331360
}
332361

@@ -350,6 +379,14 @@ func (up *UnbondingPipeline) ProcessNewTransactions(ctx context.Context) error {
350379
return nil
351380
}
352381

382+
defer func() {
383+
if up.Metrics.Config.Enabled {
384+
if err := up.pushMetrics(); err != nil {
385+
up.logger.Error("Failed to push metrics", "error", err)
386+
}
387+
}
388+
}()
389+
353390
if err := up.processUnbondingTransactions(ctx, unbondingTransactions); err != nil {
354391
return err
355392
}
@@ -372,6 +409,14 @@ func (up *UnbondingPipeline) ProcessFailedTransactions(ctx context.Context) erro
372409
return nil
373410
}
374411

412+
defer func() {
413+
if up.Metrics.Config.Enabled {
414+
if err := up.pushMetrics(); err != nil {
415+
up.logger.Error("Failed to push metrics", "error", err)
416+
}
417+
}
418+
}()
419+
375420
if err := up.processUnbondingTransactions(ctx, unbondingTransactions); err != nil {
376421
return err
377422
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package services
2+
3+
import (
4+
"github.com/babylonchain/cli-tools/internal/config"
5+
"github.com/prometheus/client_golang/prometheus"
6+
)
7+
8+
type PipelineMetrics struct {
9+
SuccessSigningReqs *prometheus.CounterVec
10+
FailedSigningReqs *prometheus.CounterVec
11+
SuccessfulSentTransactions prometheus.Counter
12+
FailureSentTransactions prometheus.Counter
13+
Config *config.MetricsConfig
14+
}
15+
16+
func NewPipelineMetrics(cfg *config.MetricsConfig) *PipelineMetrics {
17+
return &PipelineMetrics{
18+
SuccessSigningReqs: prometheus.NewCounterVec(
19+
prometheus.CounterOpts{
20+
Name: "number_of_successful_signing_requests",
21+
Help: "How many signing requests to given covenant were successful",
22+
},
23+
[]string{"covenant_pk"},
24+
),
25+
FailedSigningReqs: prometheus.NewCounterVec(
26+
prometheus.CounterOpts{
27+
Name: "number_of_failed_signing_requests",
28+
Help: "How many signing requests to given covenant failed",
29+
},
30+
[]string{"covenant_pk"},
31+
),
32+
SuccessfulSentTransactions: prometheus.NewCounter(
33+
prometheus.CounterOpts{
34+
Name: "number_of_successful_unbonding_transactions",
35+
Help: "How many transactions were successfully sent to the network",
36+
},
37+
),
38+
FailureSentTransactions: prometheus.NewCounter(
39+
prometheus.CounterOpts{
40+
Name: "number_of_failed_unbonding_transactions",
41+
Help: "How many transactions failed to be sent to the network",
42+
},
43+
),
44+
Config: cfg,
45+
}
46+
}
47+
48+
func (pm *PipelineMetrics) RecordSuccessSigningRequest(covenantPk string) {
49+
pm.SuccessSigningReqs.WithLabelValues(covenantPk).Inc()
50+
}
51+
52+
func (pm *PipelineMetrics) RecordFailedSigningRequest(covenantPk string) {
53+
pm.FailedSigningReqs.WithLabelValues(covenantPk).Inc()
54+
}
55+
56+
func (pm *PipelineMetrics) RecordSentUnbondingTransaction() {
57+
pm.SuccessfulSentTransactions.Inc()
58+
}
59+
60+
func (pm *PipelineMetrics) RecordFailedUnbodingTransaction() {
61+
pm.FailureSentTransactions.Inc()
62+
}

internal/services/witness_gen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func createWitnessSignaturesForPubKeys(
3434
receivedSignatures := make(map[string]*schnorr.Signature)
3535

3636
for _, pair := range receivedSignaturePairs {
37-
receivedSignatures[pubKeyToString(pair.PubKey)] = pair.Signature
37+
receivedSignatures[pubKeyToStringSchnorr(pair.PubKey)] = pair.Signature
3838
}
3939

4040
sortedPubKeys := sortPubKeysForWitness(covenantPubKeys)
@@ -44,7 +44,7 @@ func createWitnessSignaturesForPubKeys(
4444

4545
for i, key := range sortedPubKeys {
4646
k := key
47-
if signature, found := receivedSignatures[pubKeyToString(k)]; found {
47+
if signature, found := receivedSignatures[pubKeyToStringSchnorr(k)]; found {
4848
signatures[i] = signature
4949
}
5050
}

0 commit comments

Comments
 (0)