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

feat: add MarkDelegationAsTransitioned method #163

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions internal/v1/db/client/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ func (v1dbclient *V1Database) ScanDelegationsPaginated(
)
}

// MarkDelegationAsTransformed marks an existing delegation as transformed
func (v1dbclient *V1Database) MarkDelegationAsTransformed(ctx context.Context, stakingTxHashHex string) error {
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
client := v1dbclient.Client.Database(v1dbclient.DbName).Collection(dbmodel.V1DelegationCollection)
update := bson.M{"$set": bson.M{"transformed": 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
}

// TransitionState updates the state of a staking transaction to a new state
// It returns an NotFoundError if the staking transaction is not found or not in the eligible state to transition
func (v1dbclient *V1Database) transitionState(
Expand Down
1 change: 1 addition & 0 deletions internal/v1/db/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type V1DBClient interface {
ctx context.Context, stakingTxHashHex, unbondingTxHashHex, txHex, signatureHex string,
) error
FindDelegationByTxHashHex(ctx context.Context, txHashHex string) (*v1dbmodel.DelegationDocument, error)
MarkDelegationAsTransformed(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: 1 addition & 0 deletions internal/v1/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type DelegationDocument struct {
StakingTx *TimelockTransaction `bson:"staking_tx"` // Always exist
UnbondingTx *TimelockTransaction `bson:"unbonding_tx,omitempty"`
IsOverflow bool `bson:"is_overflow"`
Transformed bool `bson:"transformed"`
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

// Add unbonding transaction if it exists
Expand Down
5 changes: 5 additions & 0 deletions internal/v2/queue/handler/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func (h *V2QueueHandler) ActiveStakingHandler(ctx context.Context, messageBody s
return types.NewError(http.StatusBadRequest, types.BadRequest, err)
}

// Mark as v1 delegation as transformed if it exists
if err := h.Service.MarkV1DelegationAsTransformed(ctx, activeStakingEvent.StakingTxHashHex); err != nil {
log.Ctx(ctx).Error().Err(err).Msg("Failed to mark v1 delegation as transformed")
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
}

// Perform the address lookup conversion
addressLookupErr := h.performAddressLookupConversion(ctx, activeStakingEvent.StakerBtcPkHex, types.Active)
if addressLookupErr != nil {
Expand Down
18 changes: 18 additions & 0 deletions internal/v2/service/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,21 @@ func (s *V2Service) SaveUnprocessableMessages(ctx context.Context, messageBody,
}
return nil
}

func (s *V2Service) MarkV1DelegationAsTransformed(ctx context.Context, stakingTxHashHex string) *types.Error {
// check if v1 delegation exists
delegation, err := s.DbClients.V1DBClient.FindDelegationByTxHashHex(ctx, stakingTxHashHex)
if err != nil {
return types.NewInternalServiceError(err)
}
if delegation == nil {
return nil
}

// mark as transformed
err = s.DbClients.V1DBClient.MarkDelegationAsTransformed(ctx, stakingTxHashHex)
if err != nil {
return types.NewInternalServiceError(err)
}
return nil
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions internal/v2/service/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type V2ServiceProvider interface {
IsDelegationPresent(ctx context.Context, txHashHex string) (bool, *types.Error)
GetDelegation(ctx context.Context, stakingTxHashHex string) (*StakerDelegationPublic, *types.Error)
GetDelegations(ctx context.Context, stakerPKHex string, paginationKey string) ([]*StakerDelegationPublic, string, *types.Error)
MarkV1DelegationAsTransformed(ctx context.Context, stakingTxHashHex string) *types.Error
GetOverallStats(ctx context.Context) (*OverallStatsPublic, *types.Error)
GetStakerStats(ctx context.Context, stakerPKHex string) (*StakerStatsPublic, *types.Error)
ProcessAndSaveBtcAddresses(ctx context.Context, stakerPkHex string) *types.Error
Expand Down
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.

20 changes: 19 additions & 1 deletion tests/mocks/mock_v1_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_v2_db_client.go

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

Loading