Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utils.ParseUint32(...) function. Replace usage of strconv.PaseUin…
Browse files Browse the repository at this point in the history
…t(s, 10, 32)
kirugan committed Jan 17, 2025
1 parent fab471c commit 9591ed2
Showing 3 changed files with 27 additions and 22 deletions.
30 changes: 14 additions & 16 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@ package model

import (
"fmt"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
@@ -59,22 +57,22 @@ func FromEventBTCDelegationCreated(
bbnBlockHeight,
bbnBlockTime int64,
) (*BTCDelegationDetails, error) {
stakingOutputIdx, err := strconv.ParseUint(event.StakingOutputIndex, 10, 32)
stakingOutputIdx, err := utils.ParseUint32(event.StakingOutputIndex)
if err != nil {
return nil, fmt.Errorf("failed to parse staking output index: %w", err)
}

paramsVersion, err := strconv.ParseUint(event.ParamsVersion, 10, 32)
paramsVersion, err := utils.ParseUint32(event.ParamsVersion)
if err != nil {
return nil, fmt.Errorf("failed to parse params version: %w", err)
}

stakingTime, err := strconv.ParseUint(event.StakingTime, 10, 32)
stakingTime, err := utils.ParseUint32(event.StakingTime)
if err != nil {
return nil, fmt.Errorf("failed to parse staking time: %w", err)
}

unbondingTime, err := strconv.ParseUint(event.UnbondingTime, 10, 32)
unbondingTime, err := utils.ParseUint32(event.UnbondingTime)
if err != nil {
return nil, fmt.Errorf("failed to parse unbonding time: %w", err)
}
@@ -89,17 +87,17 @@ func FromEventBTCDelegationCreated(
return &BTCDelegationDetails{
StakingTxHashHex: stakingTx.TxHash().String(),
StakingTxHex: event.StakingTxHex,
StakingTime: uint32(stakingTime),
StakingTime: stakingTime,
StakingAmount: uint64(stakingValue),
StakingOutputIdx: uint32(stakingOutputIdx),
StakingOutputIdx: stakingOutputIdx,
StakerBtcPkHex: event.StakerBtcPkHex,
FinalityProviderBtcPksHex: event.FinalityProviderBtcPksHex,
ParamsVersion: uint32(paramsVersion),
UnbondingTime: uint32(unbondingTime),
ParamsVersion: paramsVersion,
UnbondingTime: unbondingTime,
UnbondingTx: event.UnbondingTx,
State: types.StatePending, // initial state will always be PENDING
StartHeight: uint32(0), // it should be set when the inclusion proof is received
EndHeight: uint32(0), // it should be set when the inclusion proof is received
StartHeight: 0, // it should be set when the inclusion proof is received
EndHeight: 0, // it should be set when the inclusion proof is received
CovenantUnbondingSignatures: []CovenantSignature{},
BTCDelegationCreatedBlock: BTCDelegationCreatedBbnBlock{
Height: bbnBlockHeight,
@@ -117,11 +115,11 @@ func FromEventBTCDelegationCreated(
func FromEventBTCDelegationInclusionProofReceived(
event *bbntypes.EventBTCDelegationInclusionProofReceived,
) *BTCDelegationDetails {
startHeight, _ := strconv.ParseUint(event.StartHeight, 10, 32)
endHeight, _ := strconv.ParseUint(event.EndHeight, 10, 32)
startHeight, _ := utils.ParseUint32(event.StartHeight)
endHeight, _ := utils.ParseUint32(event.EndHeight)
return &BTCDelegationDetails{
StartHeight: uint32(startHeight),
EndHeight: uint32(endHeight),
StartHeight: startHeight,
EndHeight: endHeight,
State: types.DelegationState(event.NewState),
}
}
11 changes: 5 additions & 6 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
@@ -3,15 +3,14 @@ package services
import (
"context"
"fmt"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
abcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/rs/zerolog/log"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
)

const (
@@ -196,7 +195,7 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
}
newState := types.DelegationState(inclusionProofEvent.NewState)
if newState == types.StateActive {
stakingStartHeight, _ := strconv.ParseUint(inclusionProofEvent.StartHeight, 10, 32)
stakingStartHeight, _ := utils.ParseUint32(inclusionProofEvent.StartHeight)

log.Debug().
Str("staking_tx", inclusionProofEvent.StakingTxHash).
@@ -216,7 +215,7 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
delegation.StakingTxHashHex,
delegation.StakingTxHex,
delegation.StakingOutputIdx,
uint32(stakingStartHeight),
stakingStartHeight,
); err != nil {
return err
}
@@ -269,15 +268,15 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
return err
}

unbondingStartHeight, parseErr := strconv.ParseUint(unbondedEarlyEvent.StartHeight, 10, 32)
unbondingStartHeight, parseErr := utils.ParseUint32(unbondedEarlyEvent.StartHeight)
if parseErr != nil {
return fmt.Errorf("failed to parse start height: %w", parseErr)
}

subState := types.SubStateEarlyUnbonding

// Save timelock expire
unbondingExpireHeight := uint32(unbondingStartHeight) + delegation.UnbondingTime
unbondingExpireHeight := unbondingStartHeight + delegation.UnbondingTime
if err := s.db.SaveNewTimeLockExpire(
ctx,
delegation.StakingTxHashHex,
8 changes: 8 additions & 0 deletions internal/utils/strconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package utils

import "strconv"

func ParseUint32(s string) (uint32, error) {
v, err := strconv.ParseUint(s, 10, 32)
return uint32(v), err
}

0 comments on commit 9591ed2

Please sign in to comment.