Skip to content

Commit

Permalink
add params (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-babylonlabs authored Oct 30, 2024
1 parent 3d0a9d3 commit cf98662
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 102 deletions.
File renamed without changes.
14 changes: 14 additions & 0 deletions internal/indexer/db/client/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package indexerdbclient

import (
"context"

indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
)

type IndexerDBClient interface {
Ping(ctx context.Context) error
// Params
GetBbnStakingParams(ctx context.Context) ([]*indexertypes.BbnStakingParams, error)
GetBtcCheckpointParams(ctx context.Context) ([]*indexertypes.BtcCheckpointParams, error)
}
87 changes: 87 additions & 0 deletions internal/indexer/db/client/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package indexerdbclient

import (
"context"
"fmt"

indexerdbmodel "github.com/babylonlabs-io/staking-api-service/internal/indexer/db/model"
indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
"go.mongodb.org/mongo-driver/bson"
)

func (db *IndexerDatabase) GetBbnStakingParams(ctx context.Context) ([]*indexertypes.BbnStakingParams, error) {
cursor, err := db.Client.Database(db.DbName).Collection(indexerdbmodel.GlobalParamsCollection).Find(ctx, bson.M{
"type": indexertypes.STAKING_PARAMS_TYPE,
})
if err != nil {
return nil, fmt.Errorf("failed to query global params: %w", err)
}
defer cursor.Close(ctx)

var params []*indexertypes.BbnStakingParams

for cursor.Next(ctx) {
var model indexerdbmodel.IndexerGlobalParamsDocument
if err := cursor.Decode(&model); err != nil {
return nil, fmt.Errorf("failed to decode document: %w", err)
}

var stakingParams indexertypes.BbnStakingParams
bsonBytes, err := bson.Marshal(model.Params)
if err != nil {
return nil, fmt.Errorf("failed to marshal params: %w", err)
}
if err := bson.Unmarshal(bsonBytes, &stakingParams); err != nil {
return nil, fmt.Errorf("failed to unmarshal into staking params: %w", err)
}

stakingParams.Version = model.Version

params = append(params, &stakingParams)
}

if err := cursor.Err(); err != nil {
return nil, fmt.Errorf("cursor iteration error: %w", err)
}

return params, nil
}

func (db *IndexerDatabase) GetBtcCheckpointParams(ctx context.Context) ([]*indexertypes.BtcCheckpointParams, error) {
cursor, err := db.Client.Database(db.DbName).Collection(indexerdbmodel.GlobalParamsCollection).Find(ctx, bson.M{
"type": indexertypes.CHECKPOINT_PARAMS_TYPE,
})
if err != nil {
return nil, fmt.Errorf("failed to query global params: %w", err)
}
defer cursor.Close(ctx)

var params []*indexertypes.BtcCheckpointParams

for cursor.Next(ctx) {
var model indexerdbmodel.IndexerGlobalParamsDocument
if err := cursor.Decode(&model); err != nil {
return nil, fmt.Errorf("failed to decode document: %w", err)
}

var btcParams indexertypes.BtcCheckpointParams

bsonBytes, err := bson.Marshal(model.Params)
if err != nil {
return nil, fmt.Errorf("failed to marshal params: %w", err)
}
if err := bson.Unmarshal(bsonBytes, &btcParams); err != nil {
return nil, fmt.Errorf("failed to unmarshal into staking params: %w", err)
}

btcParams.Version = model.Version

params = append(params, &btcParams)
}

if err := cursor.Err(); err != nil {
return nil, fmt.Errorf("cursor iteration error: %w", err)
}

return params, nil
}
19 changes: 19 additions & 0 deletions internal/indexer/db/model/delegation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package indexerdbmodel

import (
indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
)

type BTCDelegationDetails struct {
StakingTxHashHex string `bson:"_id"` // Primary key
ParamsVersion string `bson:"params_version"`
FinalityProviderBtcPksHex []string `bson:"finality_provider_btc_pks_hex"`
StakerBtcPkHex string `bson:"staker_btc_pk_hex"`
StakingTime string `bson:"staking_time"`
StakingAmount string `bson:"staking_amount"`
UnbondingTime string `bson:"unbonding_time"`
UnbondingTx string `bson:"unbonding_tx"`
State indexertypes.DelegationState `bson:"state"`
StartHeight uint32 `bson:"start_height"`
EndHeight uint32 `bson:"end_height"`
}
18 changes: 18 additions & 0 deletions internal/indexer/db/model/finality_providers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package indexerdbmodel

type IndexerFinalityProviderDetails struct {
BtcPk string `bson:"_id"` // Primary key
BabylonAddress string `bson:"babylon_address"`
Commission string `bson:"commission"`
State string `bson:"state"`
Description Description `bson:"description"`
}

// Description represents the nested description field
type Description struct {
Moniker string `bson:"moniker"`
Identity string `bson:"identity"`
Website string `bson:"website"`
SecurityContact string `bson:"security_contact"`
Details string `bson:"details"`
}
11 changes: 11 additions & 0 deletions internal/indexer/db/model/global_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package indexerdbmodel

import (
indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
)

type IndexerGlobalParamsDocument struct {
Type indexertypes.GlobalParamsType `bson:"type"`
Version uint32 `bson:"version"`
Params interface{} `bson:"params"`
}
8 changes: 8 additions & 0 deletions internal/indexer/db/model/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package indexerdbmodel

const (
FinalityProviderDetailsCollection = "finality_provider_details"
BTCDelegationDetailsCollection = "btc_delegation_details"
TimeLockCollection = "timelock"
GlobalParamsCollection = "global_params"
)
7 changes: 7 additions & 0 deletions internal/indexer/db/model/timelock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package indexerdbmodel

type TimeLockDocument struct {
StakingTxHashHex string `bson:"_id"` // Primary key
ExpireHeight uint32 `bson:"expire_height"`
TxType string `bson:"tx_type"`
}
14 changes: 14 additions & 0 deletions internal/indexer/types/delegation_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package indexertypes

// Enum values for Delegation State
type DelegationState string

const (
StatePending DelegationState = "PENDING"
StateVerified DelegationState = "VERIFIED"
StateActive DelegationState = "ACTIVE"
StateUnbonding DelegationState = "UNBONDING"
StateWithdrawable DelegationState = "WITHDRAWABLE"
StateWithdrawn DelegationState = "WITHDRAWN"
StateSlashed DelegationState = "SLASHED"
)
32 changes: 32 additions & 0 deletions internal/indexer/types/global_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package indexertypes

type GlobalParamsType string

const (
CHECKPOINT_PARAMS_VERSION uint32 = 0
CHECKPOINT_PARAMS_TYPE GlobalParamsType = "CHECKPOINT"
STAKING_PARAMS_TYPE GlobalParamsType = "STAKING"
)

type BbnStakingParams struct {
Version uint32 `json:"version"`
CovenantPks []string `json:"covenant_pks"`
CovenantQuorum uint32 `json:"covenant_quorum"`
MinStakingValueSat int64 `json:"min_staking_value_sat"`
MaxStakingValueSat int64 `json:"max_staking_value_sat"`
MinStakingTimeBlocks uint32 `json:"min_staking_time_blocks"`
MaxStakingTimeBlocks uint32 `json:"max_staking_time_blocks"`
SlashingPkScript string `json:"slashing_pk_script"`
MinSlashingTxFeeSat int64 `json:"min_slashing_tx_fee_sat"`
SlashingRate string `json:"slashing_rate"`
MinUnbondingTimeBlocks uint32 `json:"min_unbonding_time_blocks"`
UnbondingFeeSat int64 `json:"unbonding_fee_sat"`
MinCommissionRate string `json:"min_commission_rate"`
MaxActiveFinalityProviders uint32 `json:"max_active_finality_providers"`
DelegationCreationBaseGasFee uint64 `json:"delegation_creation_base_gas_fee"`
}

type BtcCheckpointParams struct {
Version uint32 `json:"version"`
BtcConfirmationDepth uint64 `json:"btc_confirmation_depth"`
}
2 changes: 1 addition & 1 deletion internal/shared/db/clients/db_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package dbclients
import (
"context"

indexerdbclient "github.com/babylonlabs-io/staking-api-service/internal/indexer/db/client"
"github.com/babylonlabs-io/staking-api-service/internal/shared/config"
dbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/client"
indexerdbclient "github.com/babylonlabs-io/staking-api-service/internal/shared/db/indexer_db_client"
v1dbclient "github.com/babylonlabs-io/staking-api-service/internal/v1/db/client"
v2dbclient "github.com/babylonlabs-io/staking-api-service/internal/v2/db/client"
"github.com/rs/zerolog/log"
Expand Down
9 changes: 0 additions & 9 deletions internal/shared/db/indexer_db_client/interface.go

This file was deleted.

23 changes: 0 additions & 23 deletions internal/shared/types/global_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,6 @@ type VersionedGlobalParams = parser.VersionedGlobalParams

type GlobalParams = parser.GlobalParams

type BabylonParams struct {
Version int `json:"version"`
CovenantPKs []string `json:"covenant_pks"`
CovenantQuorum int `json:"covenant_quorum"`
MaxStakingAmount int64 `json:"max_staking_amount"`
MinStakingAmount int64 `json:"min_staking_amount"`
MaxStakingTime int64 `json:"max_staking_time"`
MinStakingTime int64 `json:"min_staking_time"`
SlashingPKScript string `json:"slashing_pk_script"`
MinSlashingTxFee int64 `json:"min_slashing_tx_fee"`
SlashingRate float64 `json:"slashing_rate"`
MinUnbondingTime int64 `json:"min_unbonding_time"`
UnbondingFee int64 `json:"unbonding_fee"`
MinCommissionRate float64 `json:"min_commission_rate"`
MaxActiveFinalityProviders int `json:"max_active_finality_providers"`
DelegationCreationBaseGasFee int64 `json:"delegation_creation_base_gas_fee"`
}

type BTCParams struct {
Version int `json:"version"`
BTCConfirmationDepth int `json:"btc_confirmation_depth"`
}

func NewGlobalParams(path string) (*GlobalParams, error) {
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
Expand Down
43 changes: 22 additions & 21 deletions internal/shared/utils/datagen/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/rand"
"time"

indexertypes "github.com/babylonlabs-io/staking-api-service/internal/indexer/types"
"github.com/babylonlabs-io/staking-api-service/internal/shared/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
Expand Down Expand Up @@ -174,30 +175,30 @@ func RandomFinalityProviderState(r *rand.Rand) types.FinalityProviderState {
return states[r.Intn(len(states))]
}

func GenerateRandomBabylonParams(r *rand.Rand) types.BabylonParams {
return types.BabylonParams{
Version: r.Intn(10),
CovenantPKs: GeneratePks(r.Intn(10)),
CovenantQuorum: r.Intn(10),
MaxStakingAmount: int64(r.Intn(1000000000000000000)),
MinStakingAmount: int64(r.Intn(1000000000000000000)),
MaxStakingTime: int64(r.Intn(1000000000000000000)),
MinStakingTime: int64(r.Intn(1000000000000000000)),
SlashingPKScript: RandomString(r, 10),
MinSlashingTxFee: int64(r.Intn(1000000000000000000)),
SlashingRate: RandomPostiveFloat64(r),
MinUnbondingTime: int64(r.Intn(1000000000000000000)),
UnbondingFee: int64(r.Intn(1000000000000000000)),
MinCommissionRate: RandomPostiveFloat64(r),
MaxActiveFinalityProviders: r.Intn(10),
DelegationCreationBaseGasFee: int64(r.Intn(1000000000000000000)),
func GenerateRandomBabylonParams(r *rand.Rand) indexertypes.BbnStakingParams {
return indexertypes.BbnStakingParams{
Version: uint32(r.Intn(10)),
CovenantPks: GeneratePks(r.Intn(10)),
CovenantQuorum: uint32(r.Intn(10)),
MinStakingValueSat: int64(r.Intn(10)),
MaxStakingValueSat: int64(r.Intn(10)),
MinStakingTimeBlocks: uint32(r.Intn(10)),
MaxStakingTimeBlocks: uint32(r.Intn(10)),
SlashingPkScript: RandomString(r, 10),
MinSlashingTxFeeSat: int64(r.Intn(10)),
SlashingRate: fmt.Sprintf("%f", RandomPostiveFloat64(r)),
MinUnbondingTimeBlocks: uint32(r.Intn(10)),
UnbondingFeeSat: int64(r.Intn(10)),
MinCommissionRate: fmt.Sprintf("%f", RandomPostiveFloat64(r)),
MaxActiveFinalityProviders: uint32(r.Intn(10)),
DelegationCreationBaseGasFee: uint64(r.Intn(10)),
}
}

func GenerateRandomBTCParams(r *rand.Rand) types.BTCParams {
return types.BTCParams{
Version: r.Intn(10),
BTCConfirmationDepth: r.Intn(10),
func GenerateRandomBTCParams(r *rand.Rand) indexertypes.BtcCheckpointParams {
return indexertypes.BtcCheckpointParams{
Version: uint32(r.Intn(10)),
BtcConfirmationDepth: uint64(r.Intn(10)),
}
}

Expand Down
25 changes: 0 additions & 25 deletions internal/v2/service/global_params.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/v2/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type V2ServiceProvider interface {
service.SharedServiceProvider
GetFinalityProviders(ctx context.Context, paginationKey string) ([]FinalityProviderPublic, string, *types.Error)
GetGlobalParams(ctx context.Context) (GlobalParamsPublic, *types.Error)
GetGlobalParams(ctx context.Context) (*GlobalParamsPublic, *types.Error)
GetOverallStats(ctx context.Context) (OverallStatsPublic, *types.Error)
GetStakerDelegations(ctx context.Context, paginationKey string) ([]StakerDelegationPublic, string, *types.Error)
GetStakerStats(ctx context.Context, stakerPKHex string) (StakerStatsPublic, *types.Error)
Expand Down
Loading

0 comments on commit cf98662

Please sign in to comment.