Skip to content

Commit

Permalink
feat: support phase1 unbonding (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 authored Dec 13, 2024
1 parent 1f5423b commit 651007c
Show file tree
Hide file tree
Showing 40 changed files with 3,373 additions and 383 deletions.
20 changes: 11 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ on:

jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/reusable_go_lint_test.yml@v0.6.0
uses: babylonlabs-io/.github/.github/workflows/reusable_go_lint_test.yml@v0.7.0
with:
run-unit-tests: true
run-integration-tests: false
run-lint: true
run-build: true
run-gosec: true
gosec-args: "-no-fail ./..."

go-version: '1.23'
go-lint-version: 'v1.60.2'
run-unit-tests: true
run-integration-tests: false
run-lint: true
run-build: true
run-gosec: true
gosec-args: "-no-fail ./..."

docker_pipeline:
uses: babylonlabs-io/.github/.github/workflows/reusable_docker_pipeline.yml@v0.6.0
uses: babylonlabs-io/.github/.github/workflows/reusable_docker_pipeline.yml@v0.7.0
secrets: inherit
with:
publish: false
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/[email protected]
with:
go-version: '1.23'
go-lint-version: 'v1.60.2'
run-unit-tests: true
run-integration-tests: false
run-lint: true
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ stop-service:

run-local:
./bin/local-startup.sh;
go run cmd/staking-expiry-checker/main.go --config config/config-local.yml
go run cmd/staking-expiry-checker/main.go \
--config config/config-local.yml \
--params config/global-params.json

generate-mock-interface:
cd internal/db && mockery --name=DbInterface --output=../../tests/mocks --outpkg=mocks --filename=mock_db_client.go
Expand Down
15 changes: 12 additions & 3 deletions cmd/staking-expiry-checker/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
)

const (
defaultConfigFileName = "config.yml"
defaultConfigFileName = "config.yml"
defaultGlobalParamsFileName = "global_params.json"
)

var (
cfgPath string
rootCmd = &cobra.Command{
cfgPath string
globalParamsPath string
rootCmd = &cobra.Command{
Use: "start-server",
}
)
Expand All @@ -26,8 +28,11 @@ func Setup() error {
}

defaultConfigPath := getDefaultConfigFile(homePath, defaultConfigFileName)
defaultGlobalParamsPath := getDefaultConfigFile(homePath, defaultGlobalParamsFileName)

rootCmd.PersistentFlags().StringVar(&cfgPath, "config", defaultConfigPath, fmt.Sprintf("config file (default %s)", defaultConfigPath))
rootCmd.PersistentFlags().StringVar(&globalParamsPath, "params", defaultGlobalParamsPath, fmt.Sprintf("global params file (default %s)", defaultGlobalParamsPath))

if err := rootCmd.Execute(); err != nil {
return err
}
Expand All @@ -42,3 +47,7 @@ func getDefaultConfigFile(homePath, filename string) string {
func GetConfigPath() string {
return cfgPath
}

func GetGlobalParamsPath() string {
return globalParamsPath
}
42 changes: 26 additions & 16 deletions cmd/staking-expiry-checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"fmt"
"os/signal"
"syscall"

"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
Expand All @@ -11,9 +13,8 @@ import (
"github.com/babylonlabs-io/staking-expiry-checker/internal/btcclient"
"github.com/babylonlabs-io/staking-expiry-checker/internal/config"
"github.com/babylonlabs-io/staking-expiry-checker/internal/db"
"github.com/babylonlabs-io/staking-expiry-checker/internal/observability/metrics"
"github.com/babylonlabs-io/staking-expiry-checker/internal/poller"
"github.com/babylonlabs-io/staking-expiry-checker/internal/services"
"github.com/babylonlabs-io/staking-expiry-checker/internal/types"
)

func init() {
Expand All @@ -23,43 +24,52 @@ func init() {
}

func main() {
ctx := context.Background()

// setup cli commands and flags
// Setup CLI commands and flags
if err := cli.Setup(); err != nil {
log.Fatal().Err(err).Msg("error while setting up cli")
}

// load config
// Load config
cfgPath := cli.GetConfigPath()
cfg, err := config.New(cfgPath)
if err != nil {
log.Fatal().Err(err).Msg(fmt.Sprintf("error while loading config file: %s", cfgPath))
}

// initialize metrics with the metrics port from config
metricsPort := cfg.Metrics.GetMetricsPort()
metrics.Init(metricsPort)
paramsPath := cli.GetGlobalParamsPath()
params, err := types.NewGlobalParams(paramsPath)
if err != nil {
log.Fatal().Err(err).Msg(fmt.Sprintf("error while loading global params file: %s", paramsPath))
}

// create new db client
// Create context with signal handling
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

// Create DB client
dbClient, err := db.New(ctx, cfg.Db)
if err != nil {
log.Fatal().Err(err).Msg("error while creating db client")
}

// Create BTC client
btcClient, err := btcclient.NewBtcClient(&cfg.Btc)
if err != nil {
log.Fatal().Err(err).Msg("error while creating btc client")
}

delegationService := services.NewService(dbClient, btcClient)
// Create BTC notifier
btcNotifier, err := btcclient.NewBTCNotifier(
&cfg.Btc,
&btcclient.EmptyHintCache{},
)
if err != nil {
log.Fatal().Err(err).Msg("error while creating delegation service")
log.Fatal().Err(err).Msg("error while creating btc notifier")
}

p, err := poller.NewPoller(cfg.Poller, delegationService)
if err != nil {
log.Fatal().Err(err).Msg("error while creating poller")
// Create service
service := services.NewService(cfg, params, dbClient, btcNotifier, btcClient)
if err := service.RunUntilShutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("failed to start service")
}
p.Start(ctx)
}
20 changes: 12 additions & 8 deletions config/config-docker.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
poller:
interval: 5s
pollers:
log-level: debug
timeout: 10s
expiry-checker:
interval: 5s
timeout: 10s
btc-subscriber:
interval: 5s
timeout: 10s
db:
username: root
password: example
address: "mongodb://localhost:27017"
db-name: staking-api-service
btc:
endpoint: localhost:18332
disable-tls: false
net-params: testnet
rpc-user: rpcuser
rpc-pass: rpcpass
endpoint: localhost:38332 # signet endpoint
disable-tls: true # should be disabled for local testing
net-params: signet # signet network params
rpc-user: rpcuser # rpc user
rpc-pass: rpcpass # rpc pass
metrics:
host: 0.0.0.0
port: 2112
20 changes: 12 additions & 8 deletions config/config-local.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
poller:
interval: 5s
pollers:
log-level: debug
timeout: 10s
expiry-checker:
interval: 5s
timeout: 1000s
btc-subscriber:
interval: 5s
timeout: 1000s
db:
username: root
password: example
address: "mongodb://localhost:27017"
db-name: staking-api-service
btc:
endpoint: localhost:18332
disable-tls: false
net-params: testnet
rpc-user: rpcuser
rpc-pass: rpcpass
endpoint: 127.0.0.1:38332 # signet endpoint
disable-tls: true # should be disabled for local testing
net-params: signet # signet network params
rpc-user: rpcuser # rpc user
rpc-pass: rpcpass # rpc pass
metrics:
host: 0.0.0.0
port: 2112
25 changes: 25 additions & 0 deletions config/global-params.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"versions": [
{
"version": 0,
"activation_height": 192840,
"staking_cap": 50000000000,
"tag": "01020304",
"covenant_pks": [
"0381b70c01535f5153a8039c21150c53f3e49a083555b57930103db8a7272ff336",
"02159f46467124f6bbba77060520571ddb07c7e95ff54d8b9958ec0b0d59d86c03",
"039705be04f3a3eb5c3d0dd61e648e06ea8170975744594fe702e8088bcceff375",
"02ce138027bfdfb4dd631e9cecf097082c8a505ab16de36f5e3eb816d105ba7575",
"03e15dba250612e79e22abf28a1828ba5e6bdfaaa6ed2d87462b046994c33fa46f"
],
"covenant_quorum": 3,
"unbonding_time": 1000,
"unbonding_fee": 20000,
"max_staking_amount": 1000000000,
"min_staking_amount": 1000000,
"max_staking_time": 65000,
"min_staking_time": 64000,
"confirmation_depth": 10
}
]
}
8 changes: 7 additions & 1 deletion contrib/images/staking-expiry-checker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -x

BINARY=${BINARY:-/bin/staking-expiry-checker}
CONFIG=${CONFIG:-/home/staking-expiry-checker/config.yml}
PARAMS=${PARAMS:-/home/staking-expiry-checker/global-params.json}

if ! [ -f "${BINARY}" ]; then
echo "The binary $(basename "${BINARY}") cannot be found."
Expand All @@ -15,4 +16,9 @@ if ! [ -f "${CONFIG}" ]; then
exit 1
fi

$BINARY --config "$CONFIG" 2>&1
if ! [ -f "${PARAMS}" ]; then
echo "The global parameters file $(basename "${PARAMS}") cannot be found. Please add the global parameters file to the shared folder. Use the PARAMS environment variable if the name of the global parameters file is not 'global-params.json'"
exit 1
fi

$BINARY --config "$CONFIG" --params "$PARAMS" 2>&1
Loading

0 comments on commit 651007c

Please sign in to comment.