Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync params #15

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ run-local:

generate-mock-interface:
cd internal/db && mockery --name=DbInterface --output=../../tests/mocks --outpkg=mocks --filename=mock_db_client.go
cd internal/btcclient && mockery --name=BtcInterface --output=../../tests/mocks --outpkg=mocks --filename=mock_btc_client.go
cd internal/clients/btcclient && mockery --name=BtcInterface --output=../../../tests/mocks --outpkg=mocks --filename=mock_btc_client.go
cd internal/clients/bbnclient && mockery --name=BbnInterface --output=../../../tests/mocks --outpkg=mocks --filename=mock_bbn_client.go

test:
./bin/local-startup.sh;
Expand Down
2 changes: 1 addition & 1 deletion cmd/babylon-staking-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
log.Fatal().Err(err).Msg("error while creating queue manager")
}

service := services.NewService(dbClient, btcClient, bbnClient, qm)
service := services.NewService(cfg, dbClient, btcClient, bbnClient, qm)
if err != nil {
log.Fatal().Err(err).Msg("error while creating delegation service")
}
Expand Down
5 changes: 2 additions & 3 deletions config/config-docker.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
poller:
interval: 5s
log-level: debug
db:
username: root
password: example
Expand All @@ -15,6 +12,8 @@ btc:
bbn:
rpc-addr: https://rpc.devnet.babylonchain.io:443
timeout: 30s
poller:
param-polling-interval: 60s
queue:
queue_user: user # can be replaced by values in .env file
queue_password: password
Expand Down
5 changes: 2 additions & 3 deletions config/config-local.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
poller:
interval: 5s
log-level: debug
db:
username: root
password: example
Expand All @@ -15,6 +12,8 @@ btc:
bbn:
rpc-addr: https://rpc.devnet.babylonchain.io:443
timeout: 30s
poller:
param-polling-interval: 10s
queue:
queue_user: user # can be replaced by values in .env file
queue_password: password
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/babylonlabs-io/babylon-staking-indexer
go 1.23.2

require (
github.com/babylonlabs-io/babylon v0.12.0
github.com/babylonlabs-io/babylon v0.12.1
github.com/babylonlabs-io/staking-queue-client v0.4.1
github.com/btcsuite/btcd v0.24.2
github.com/cometbft/cometbft v0.38.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX
github.com/aws/aws-sdk-go v1.44.312 h1:llrElfzeqG/YOLFFKjg1xNpZCFJ2xraIi3PqSuP+95k=
github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/babylonlabs-io/babylon v0.12.0 h1:s2OTcxpk0RzrkVBnVTfnPdJVYDSqnm/33YKPQqEzNCE=
github.com/babylonlabs-io/babylon v0.12.0/go.mod h1:ZOrTde9vs2xoqGTFw4xhupu2CMulnpywiuk0eh4kPOw=
github.com/babylonlabs-io/babylon v0.12.1 h1:Qfmrq3pdDEZGq6DtMXxwiQjx0HD+t+U0cXQzsJfX15U=
github.com/babylonlabs-io/babylon v0.12.1/go.mod h1:ZOrTde9vs2xoqGTFw4xhupu2CMulnpywiuk0eh4kPOw=
github.com/babylonlabs-io/staking-queue-client v0.4.1 h1:AW+jtrNxZYN/isRx+njqjHbUU9CzhF42Ke6roK+0N3I=
github.com/babylonlabs-io/staking-queue-client v0.4.1/go.mod h1:n3fr3c+9LNiJlyETmcrVk94Zn76rAADhGZKxX+rVf+Q=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
74 changes: 68 additions & 6 deletions internal/clients/bbnclient/bbnclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package bbnclient

import (
"context"
"fmt"
"net/http"

ctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/rs/zerolog/log"
"strings"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon/client/config"
"github.com/babylonlabs-io/babylon/client/query"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/rs/zerolog/log"
)

type BbnClient struct {
Expand All @@ -28,7 +30,9 @@ func (c *BbnClient) GetLatestBlockNumber(ctx context.Context) (int64, *types.Err
status, err := c.queryClient.RPCClient.Status(ctx)
if err != nil {
return 0, types.NewErrorWithMsg(
http.StatusInternalServerError, types.InternalServiceError, err.Error(),
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Sprintf("failed to get latest block number by fetching status: %s", err.Error()),
)
}
return status.SyncInfo.LatestBlockHeight, nil
Expand All @@ -38,13 +42,71 @@ func (c *BbnClient) GetBlockResults(ctx context.Context, blockHeight int64) (*ct
resp, err := c.queryClient.RPCClient.BlockResults(ctx, &blockHeight)
if err != nil {
return nil, types.NewErrorWithMsg(
http.StatusInternalServerError, types.InternalServiceError, err.Error(),
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Sprintf("failed to get block results for block %d: %s", blockHeight, err.Error()),
)
}

return resp, nil
}

func (c *BbnClient) GetCheckpointParams(ctx context.Context) (*CheckpointParams, *types.Error) {
params, err := c.queryClient.BTCCheckpointParams()
if err != nil {
return nil, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.ClientRequestError,
fmt.Sprintf("failed to get checkpoint params: %s", err.Error()),
)
}
if err := params.Params.Validate(); err != nil {
return nil, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.ValidationError,
fmt.Sprintf("failed to validate checkpoint params: %s", err.Error()),
)
}
return &params.Params, nil
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *BbnClient) GetAllStakingParams(ctx context.Context) (map[uint32]*StakingParams, *types.Error) {
allParams := make(map[uint32]*StakingParams) // Map to store versioned staking parameters
version := uint32(0)

for {
params, err := c.queryClient.BTCStakingParamsByVersion(version)
if err != nil {
if strings.Contains(err.Error(), bbntypes.ErrParamsNotFound.Error()) {
// Break the loop if an error occurs (indicating no more versions)
break
}
return nil, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.ClientRequestError,
fmt.Sprintf("failed to get staking params for version %d: %s", version, err.Error()),
)
}
if err := params.Params.Validate(); err != nil {
return nil, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.ValidationError,
fmt.Sprintf("failed to validate staking params for version %d: %s", version, err.Error()),
)
}
allParams[version] = FromBbnStakingParams(params.Params)
version++
}
if len(allParams) == 0 {
return nil, types.NewErrorWithMsg(
http.StatusNotFound,
types.NotFound,
"no staking params found",
)
}

return allParams, nil
}

func (c *BbnClient) getBlockResults(ctx context.Context, blockHeight *int64) (*ctypes.ResultBlockResults, *types.Error) {
resp, err := c.queryClient.RPCClient.BlockResults(ctx, blockHeight)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/clients/bbnclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

type BbnInterface interface {
GetCheckpointParams(ctx context.Context) (*CheckpointParams, *types.Error)
GetAllStakingParams(ctx context.Context) (map[uint32]*StakingParams, *types.Error)
GetLatestBlockNumber(ctx context.Context) (int64, *types.Error)
GetBlockResults(
ctx context.Context, blockHeight int64,
Expand Down
48 changes: 48 additions & 0 deletions internal/clients/bbnclient/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package bbnclient

import (
"encoding/hex"

checkpointtypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
stakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
)

// StakingParams represents the staking parameters of the BBN chain
// Reference: https://github.com/babylonlabs-io/babylon/blob/main/proto/babylon/btcstaking/v1/params.proto
type StakingParams struct {
CovenantPks []string
CovenantQuorum uint32
MinStakingValueSat int64
MaxStakingValueSat int64
MinStakingTimeBlocks uint32
MaxStakingTimeBlocks uint32
SlashingPkScript string
MinSlashingTxFeeSat int64
SlashingRate string
MinUnbondingTimeBlocks uint32
UnbondingFeeSat int64
MinCommissionRate string
MaxActiveFinalityProviders uint32
DelegationCreationBaseGasFee uint64
}

func FromBbnStakingParams(params stakingtypes.Params) *StakingParams {
return &StakingParams{
CovenantPks: params.CovenantPksHex(),
CovenantQuorum: params.CovenantQuorum,
MinStakingValueSat: params.MinStakingValueSat,
MaxStakingValueSat: params.MaxStakingValueSat,
MinStakingTimeBlocks: params.MinStakingTimeBlocks,
MaxStakingTimeBlocks: params.MaxStakingTimeBlocks,
SlashingPkScript: hex.EncodeToString(params.SlashingPkScript),
MinSlashingTxFeeSat: params.MinSlashingTxFeeSat,
SlashingRate: params.SlashingRate.String(),
MinUnbondingTimeBlocks: params.MinUnbondingTimeBlocks,
UnbondingFeeSat: params.UnbondingFeeSat,
MinCommissionRate: params.MinCommissionRate.String(),
MaxActiveFinalityProviders: params.MaxActiveFinalityProviders,
DelegationCreationBaseGasFee: params.DelegationCreationBaseGasFee,
}
}

type CheckpointParams = checkpointtypes.Params
52 changes: 0 additions & 52 deletions internal/clients/bbnclient/types/events.go

This file was deleted.

5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
Db DbConfig `mapstructure:"db"`
Btc BtcConfig `mapstructure:"btc"`
Bbn bbnconfig.BabylonQueryConfig `mapstructure:"bbn"`
Poller PollerConfig `mapstructure:"poller"`
Queue queue.QueueConfig `mapstructure:"queue"`
Metrics MetricsConfig `mapstructure:"metrics"`
}
Expand Down Expand Up @@ -42,6 +43,10 @@ func (cfg *Config) Validate() error {
return err
}

if err := cfg.Poller.Validate(); err != nil {
return err
}

return nil
}

Expand Down
28 changes: 3 additions & 25 deletions internal/config/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,17 @@ package config

import (
"errors"
"fmt"
"time"

"github.com/rs/zerolog"
)

type PollerConfig struct {
Interval time.Duration `mapstructure:"interval"`
LogLevel string `mapstructure:"log-level"`
ParamPollingInterval time.Duration `mapstructure:"param-polling-interval"`
}

func (cfg *PollerConfig) Validate() error {
if cfg.Interval < 0 {
return errors.New("poll interval cannot be negative")
}

if err := cfg.ValidateServiceLogLevel(); err != nil {
return err
if cfg.ParamPollingInterval < 0 {
return errors.New("param-polling-interval must be positive")
}

return nil
}

func (cfg *PollerConfig) ValidateServiceLogLevel() error {
// If log level is not set, we don't need to validate it, a default value will be used in service
if cfg.LogLevel == "" {
return nil
}

if parsedLevel, err := zerolog.ParseLevel(cfg.LogLevel); err != nil {
return fmt.Errorf("invalid log level: %w", err)
} else if parsedLevel < zerolog.DebugLevel || parsedLevel > zerolog.FatalLevel {
return fmt.Errorf("only log levels from debug to fatal are supported")
}
return nil
}
Loading
Loading