Skip to content

Commit

Permalink
fix: return the transitioned state for phase-1 delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Dec 4, 2024
1 parent 918d24a commit f62a68e
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 126 deletions.
3 changes: 0 additions & 3 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,9 +960,6 @@ const docTemplate = `{
"is_slashed": {
"type": "boolean"
},
"is_transitioned": {
"type": "boolean"
},
"staker_pk_hex": {
"type": "string"
},
Expand Down
3 changes: 0 additions & 3 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,6 @@
"is_slashed": {
"type": "boolean"
},
"is_transitioned": {
"type": "boolean"
},
"staker_pk_hex": {
"type": "string"
},
Expand Down
2 changes: 0 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ definitions:
type: boolean
is_slashed:
type: boolean
is_transitioned:
type: boolean
staker_pk_hex:
type: string
staking_tx:
Expand Down
5 changes: 5 additions & 0 deletions internal/shared/utils/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ func QualifiedStatesToWithdraw() []types.DelegationState {
func OutdatedStatesForWithdraw() []types.DelegationState {
return []types.DelegationState{types.Withdrawn}
}

// QualifiedStatesToTransitioned returns the qualified exisitng states to transition to "transitioned"
func QualifiedStatesToTransitioned() []types.DelegationState {
return []types.DelegationState{types.Active, types.UnbondingRequested}
}
24 changes: 9 additions & 15 deletions internal/v1/db/client/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/shared/db"
dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model"
"github.com/babylonlabs-io/staking-api-service/internal/shared/types"
"github.com/babylonlabs-io/staking-api-service/internal/shared/utils"
v1dbmodel "github.com/babylonlabs-io/staking-api-service/internal/v1/db/model"
)

Expand Down Expand Up @@ -156,21 +157,14 @@ func (v1dbclient *V1Database) ScanDelegationsPaginated(
)
}

// MarkDelegationAsTransitioned marks an existing delegation as transitioned
func (v1dbclient *V1Database) MarkDelegationAsTransitioned(ctx context.Context, stakingTxHashHex string) error {
client := v1dbclient.Client.Database(v1dbclient.DbName).Collection(dbmodel.V1DelegationCollection)
update := bson.M{"$set": bson.M{"is_transitioned": true}}
result, err := client.UpdateOne(ctx, bson.M{"_id": stakingTxHashHex}, update)
if err != nil {
return err
}
if result.MatchedCount == 0 {
return &db.NotFoundError{
Key: stakingTxHashHex,
Message: "Delegation not found",
}
}
return nil
// TransitionToTransitionedState marks an existing delegation as transitioned
func (v1dbclient *V1Database) TransitionToTransitionedState(
ctx context.Context, stakingTxHashHex string,
) error {
return v1dbclient.transitionState(
ctx, stakingTxHashHex, types.Transitioned.ToString(),
utils.QualifiedStatesToTransitioned(), nil,
)
}

// TransitionState updates the state of a staking transaction to a new state
Expand Down
2 changes: 1 addition & 1 deletion internal/v1/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type V1DBClient interface {
ctx context.Context, stakingTxHashHex, unbondingTxHashHex, txHex, signatureHex string,
) error
FindDelegationByTxHashHex(ctx context.Context, txHashHex string) (*v1dbmodel.DelegationDocument, error)
MarkDelegationAsTransitioned(ctx context.Context, stakingTxHashHex string) error
TransitionToTransitionedState(ctx context.Context, stakingTxHashHex string) error
SaveTimeLockExpireCheck(ctx context.Context, stakingTxHashHex string, expireHeight uint64, txType string) error
TransitionToUnbondedState(
ctx context.Context, stakingTxHashHex string, eligiblePreviousState []types.DelegationState,
Expand Down
1 change: 0 additions & 1 deletion internal/v1/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type DelegationDocument struct {
StakingTx *TimelockTransaction `bson:"staking_tx"` // Always exist
UnbondingTx *TimelockTransaction `bson:"unbonding_tx,omitempty"`
IsOverflow bool `bson:"is_overflow"`
IsTransitioned bool `bson:"is_transitioned"`
}

type DelegationByStakerPagination struct {
Expand Down
2 changes: 0 additions & 2 deletions internal/v1/service/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type DelegationPublic struct {
IsOverflow bool `json:"is_overflow"`
IsEligibleForTransition bool `json:"is_eligible_for_transition"`
IsSlashed bool `json:"is_slashed"`
IsTransitioned bool `json:"is_transitioned"`
}

func (s *V1Service) DelegationsByStakerPk(
Expand Down Expand Up @@ -213,7 +212,6 @@ func (s *V1Service) FromDelegationDocument(
IsOverflow: d.IsOverflow,
IsEligibleForTransition: isFpTransitioned && !isSlashed && s.isEligibleForTransition(d, bbnHeight),
IsSlashed: isSlashed,
IsTransitioned: d.IsTransitioned,
}

// Add unbonding transaction if it exists
Expand Down
55 changes: 0 additions & 55 deletions internal/v2/db/client/deelgation.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/v2/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ type V2DBClient interface {
SubtractStakerStats(
ctx context.Context, stakingTxHashHex, stakerPkHex string, amount uint64,
) error
MarkV1DelegationAsTransitioned(ctx context.Context, stakingTxHashHex string) error
}
10 changes: 8 additions & 2 deletions internal/v2/service/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,18 @@ func (s *V2Service) SaveUnprocessableMessages(ctx context.Context, messageBody,
return nil
}

func (s *V2Service) MarkV1DelegationAsTransitioned(ctx context.Context, stakingTxHashHex string) *types.Error {
err := s.DbClients.V2DBClient.MarkV1DelegationAsTransitioned(ctx, stakingTxHashHex)
// MarkV1DelegationAsTransitioned marks a v1 delegation as transitioned
func (s *V2Service) MarkV1DelegationAsTransitioned(
ctx context.Context, stakingTxHashHex string,
) *types.Error {
err := s.DbClients.V1DBClient.TransitionToTransitionedState(ctx, stakingTxHashHex)
if err != nil {
if db.IsNotFoundError(err) {
// If the delegation is not found, it means it has already been transitioned
// or not relevant to phase-1 at all.
return nil
}
log.Ctx(ctx).Error().Err(err).Msg("Failed to transition v1 delegation to transitioned state")
return types.NewInternalServiceError(err)
}
return nil
Expand Down
1 change: 0 additions & 1 deletion internal/v2/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ type V2ServiceProvider interface {
finalityProviderBtcPksHex []string,
state types.DelegationState, amount uint64,
) *types.Error

SaveUnprocessableMessages(ctx context.Context, messageBody, receipt string) *types.Error
}
2 changes: 1 addition & 1 deletion tests/mocks/mock_db_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/mocks/mock_ordinal_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions tests/mocks/mock_v1_db_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 1 addition & 19 deletions tests/mocks/mock_v2_db_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f62a68e

Please sign in to comment.