Skip to content

Commit

Permalink
fix: order of registering spend ntfn (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 authored Dec 3, 2024
1 parent e0f8b2a commit f86c68d
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ func (s *Service) processCovenantQuorumReachedEvent(
return nil
}

// Update delegation state
newState := types.DelegationState(covenantQuorumReachedEvent.NewState)
if dbErr := s.db.UpdateBTCDelegationState(
ctx,
covenantQuorumReachedEvent.StakingTxHash,
types.QualifiedStatesForCovenantQuorumReached(covenantQuorumReachedEvent.NewState),
newState,
nil,
); dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation state: %w", dbErr),
)
}

// Emit event and register spend notification
delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, covenantQuorumReachedEvent.StakingTxHash)
if dbErr != nil {
return types.NewError(
Expand All @@ -148,7 +165,6 @@ func (s *Service) processCovenantQuorumReachedEvent(
fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr),
)
}
newState := types.DelegationState(covenantQuorumReachedEvent.NewState)
if newState == types.StateActive {
err = s.emitActiveDelegationEvent(ctx, delegation)
if err != nil {
Expand All @@ -161,20 +177,6 @@ func (s *Service) processCovenantQuorumReachedEvent(
}
}

if dbErr := s.db.UpdateBTCDelegationState(
ctx,
covenantQuorumReachedEvent.StakingTxHash,
types.QualifiedStatesForCovenantQuorumReached(covenantQuorumReachedEvent.NewState),
newState,
nil,
); dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation state: %w", dbErr),
)
}

return nil
}

Expand All @@ -197,6 +199,20 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
return nil
}

// Update delegation details
if dbErr := s.db.UpdateBTCDelegationDetails(
ctx,
inclusionProofEvent.StakingTxHash,
model.FromEventBTCDelegationInclusionProofReceived(inclusionProofEvent),
); dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation details: %w", dbErr),
)
}

// Emit event and register spend notification
delegation, dbErr := s.db.GetBTCDelegationByStakingTxHash(ctx, inclusionProofEvent.StakingTxHash)
if dbErr != nil {
return types.NewError(
Expand All @@ -205,7 +221,6 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
fmt.Errorf("failed to get BTC delegation by staking tx hash: %w", dbErr),
)
}

newState := types.DelegationState(inclusionProofEvent.NewState)
if newState == types.StateActive {
err = s.emitActiveDelegationEvent(ctx, delegation)
Expand All @@ -219,18 +234,6 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
}
}

if dbErr := s.db.UpdateBTCDelegationDetails(
ctx,
inclusionProofEvent.StakingTxHash,
model.FromEventBTCDelegationInclusionProofReceived(inclusionProofEvent),
); dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation details: %w", dbErr),
)
}

return nil
}

Expand Down

0 comments on commit f86c68d

Please sign in to comment.