Skip to content

Commit dd6a0c8

Browse files
committed
optimize error handling for end blocker
1 parent 713693d commit dd6a0c8

10 files changed

Lines changed: 64 additions & 51 deletions

File tree

x/dlc/module/abci.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
)
99

1010
// EndBlocker called at the end of every block
11-
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
11+
func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
1212
generateLendingEventNonces(ctx, k)
13+
14+
return nil
1315
}
1416

1517
// generateLendingEventNonces generates nonces events for dlc lending events
@@ -26,7 +28,9 @@ func generateLendingEventNonces(ctx sdk.Context, k keeper.Keeper) {
2628
}
2729

2830
// check if there are sufficient oracle participants
29-
if len(k.GetOracleParticipantBaseSet(ctx)) < int(k.OracleParticipantNum(ctx)) {
31+
oracleParticipantBaseSet := k.GetOracleParticipantBaseSet(ctx)
32+
if len(oracleParticipantBaseSet) < int(k.OracleParticipantNum(ctx)) {
33+
k.Logger(ctx).Warn("insufficient oracle participants", "num", len(oracleParticipantBaseSet), "required num", k.OracleParticipantNum(ctx))
3034
return
3135
}
3236

x/dlc/module/module.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,5 @@ func (am AppModule) BeginBlock(_ context.Context) error {
151151
// EndBlock contains the logic that is automatically triggered at the end of each block
152152
func (am AppModule) EndBlock(ctx context.Context) error {
153153
c := sdk.UnwrapSDKContext(ctx)
154-
EndBlocker(c, am.keeper)
155-
156-
return nil
154+
return EndBlocker(c, am.keeper)
157155
}

x/farming/module/abci.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import (
88
)
99

1010
// EndBlocker called at the end of every block
11-
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
11+
func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
12+
// handle mature stakings
1213
handleMatureStakings(ctx, k)
1314

15+
// handle epoch
1416
handleEpoch(ctx, k)
17+
18+
return nil
1519
}
1620

1721
// handleEpoch handles the epoch

x/farming/module/module.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,5 @@ func (am AppModule) BeginBlock(_ context.Context) error {
151151
// EndBlock contains the logic that is automatically triggered at the end of each block
152152
func (am AppModule) EndBlock(ctx context.Context) error {
153153
c := sdk.UnwrapSDKContext(ctx)
154-
EndBlocker(c, am.keeper)
155-
156-
return nil
154+
return EndBlocker(c, am.keeper)
157155
}

x/lending/module/abci.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,25 @@ import (
1515
)
1616

1717
// BeginBlocker called at the beginning of each block
18-
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
18+
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) error {
1919
updatePools(ctx, k)
20+
21+
return nil
2022
}
2123

2224
// EndBlocker called at the end of each block
23-
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
25+
func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
26+
// handle pending loans
2427
handlePendingLoans(ctx, k)
28+
29+
// handle active loans
2530
handleActiveLoans(ctx, k)
2631

32+
// handle liquidated loans
2733
handleLiquidatedLoans(ctx, k)
28-
handleRepayments(ctx, k)
34+
35+
// handle repayments
36+
return handleRepayments(ctx, k)
2937
}
3038

3139
// handleActiveLoans handles pending loans
@@ -123,7 +131,7 @@ func handleActiveLoans(ctx sdk.Context, k keeper.Keeper) {
123131

124132
currentPrice, err := k.GetPrice(ctx, pricePair)
125133
if err != nil {
126-
k.Logger(ctx).Info("failed to get price", "pair", pricePair, "err", err)
134+
k.Logger(ctx).Warn("failed to get price", "pair", pricePair, "err", err)
127135
}
128136

129137
dlcMeta := k.GetDLCMeta(ctx, loan.VaultAddress)
@@ -259,7 +267,7 @@ func handleLiquidatedLoans(ctx sdk.Context, k keeper.Keeper) {
259267
if len(cet.DCMSignatures) != 0 {
260268
signedTx, txHash, err := types.BuildSignedCet(cet.Tx, loan.BorrowerAuthPubKey, cet.BorrowerAdaptedSignatures, loan.DCM, cet.DCMSignatures, cetType)
261269
if err != nil {
262-
k.Logger(ctx).Info("failed to build signed liquidation cet", "loan id", loan.VaultAddress, "err", err)
270+
k.Logger(ctx).Error("failed to build signed liquidation cet", "loan id", loan.VaultAddress, "err", err)
263271
} else {
264272
cet.SignedTxHex = hex.EncodeToString(signedTx)
265273

@@ -286,7 +294,7 @@ func handleLiquidatedLoans(ctx sdk.Context, k keeper.Keeper) {
286294
}
287295

288296
// handleRepayments handles repayments
289-
func handleRepayments(ctx sdk.Context, k keeper.Keeper) {
297+
func handleRepayments(ctx sdk.Context, k keeper.Keeper) error {
290298
// get all repaid loans
291299
loans := k.GetLoans(ctx, types.LoanStatus_Repaid)
292300

@@ -297,11 +305,7 @@ func handleRepayments(ctx sdk.Context, k keeper.Keeper) {
297305
continue
298306
}
299307

300-
// check if the repayment cet has been signed
301308
dlcMeta := k.GetDLCMeta(ctx, loan.VaultAddress)
302-
if len(dlcMeta.RepaymentCet.SignedTxHex) != 0 {
303-
continue
304-
}
305309

306310
// check if the DCM adaptor signatures have been submitted
307311
if len(dlcMeta.RepaymentCet.DCMAdaptorSignatures) == 0 {
@@ -333,12 +337,15 @@ func handleRepayments(ctx sdk.Context, k keeper.Keeper) {
333337
// build signed repayment cet
334338
signedTx, txHash, err := types.BuildSignedCet(dlcMeta.RepaymentCet.Tx, loan.BorrowerPubKey, dlcMeta.RepaymentCet.BorrowerSignatures, loan.DCM, dlcMeta.RepaymentCet.DCMAdaptedSignatures, types.CetType_REPAYMENT)
335339
if err != nil {
336-
k.Logger(ctx).Info("failed to build signed repayment cet", "loan id", loan.VaultAddress, "err", err)
340+
k.Logger(ctx).Error("failed to build signed repayment cet", "loan id", loan.VaultAddress, "err", err)
337341
} else {
338342
dlcMeta.RepaymentCet.SignedTxHex = hex.EncodeToString(signedTx)
339343

344+
// complete repayment
340345
if err := k.CompleteRepayment(ctx, loan); err != nil {
341-
k.Logger(ctx).Info("failed to complete repayment", "loan id", loan.VaultAddress, "err", err)
346+
// unexpected error
347+
k.Logger(ctx).Error("failed to complete repayment", "loan id", loan.VaultAddress, "err", err)
348+
return err
342349
}
343350

344351
// emit event
@@ -353,6 +360,8 @@ func handleRepayments(ctx sdk.Context, k keeper.Keeper) {
353360

354361
k.SetDLCMeta(ctx, loan.VaultAddress, dlcMeta)
355362
}
363+
364+
return nil
356365
}
357366

358367
// updatePools updates all active pools at the beginning of each block

x/lending/module/module.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,11 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }
146146
// BeginBlock contains the logic that is automatically triggered at the beginning of each block
147147
func (am AppModule) BeginBlock(ctx context.Context) error {
148148
c := sdk.UnwrapSDKContext(ctx)
149-
BeginBlocker(c, am.keeper)
150-
151-
return nil
149+
return BeginBlocker(c, am.keeper)
152150
}
153151

154152
// EndBlock contains the logic that is automatically triggered at the end of each block
155153
func (am AppModule) EndBlock(ctx context.Context) error {
156154
c := sdk.UnwrapSDKContext(ctx)
157-
EndBlocker(c, am.keeper)
158-
159-
return nil
155+
return EndBlocker(c, am.keeper)
160156
}

x/liquidation/module/abci.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import (
99
)
1010

1111
// EndBlocker called at the end of every block
12-
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
12+
func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
13+
// handle pending liquidations
1314
handlePendingLiquidations(ctx, k)
14-
handleCompletedLiquidations(ctx, k)
15+
16+
// handle completed liquidations
17+
return handleCompletedLiquidations(ctx, k)
1518
}
1619

1720
// handlePendingLiquidations handles the pending liquidations
@@ -72,37 +75,35 @@ func handlePendingLiquidations(ctx sdk.Context, k keeper.Keeper) {
7275
}
7376

7477
// handleCompletedLiquidations handles the completed liquidations
75-
func handleCompletedLiquidations(ctx sdk.Context, k keeper.Keeper) {
78+
func handleCompletedLiquidations(ctx sdk.Context, k keeper.Keeper) error {
7679
// get completed liquidations
7780
liquidations := k.GetLiquidationsByStatus(ctx, types.LiquidationStatus_LIQUIDATION_STATUS_LIQUIDATED)
7881
if len(liquidations) == 0 {
79-
return
82+
return nil
8083
}
8184

8285
// get fee rate
8386
feeRate := k.BtcBridgeKeeper().GetFeeRate(ctx)
8487
if err := k.BtcBridgeKeeper().CheckFeeRate(ctx, feeRate); err != nil {
85-
k.Logger(ctx).Info("Failed to get fee rate to handle liquidation", "err", err)
86-
87-
return
88+
k.Logger(ctx).Warn("Failed to get fee rate to handle liquidation", "err", err)
89+
return nil
8890
}
8991

9092
for _, liquidation := range liquidations {
91-
// handle liquidated debt(repay the lending pool)
92-
if err := k.LiquidatedDebtHandler()(ctx, liquidation.Id, liquidation.LoanId, types.ModuleName, liquidation.LiquidatedDebtAmount); err != nil {
93-
k.Logger(ctx).Info("Failed to call LiquidatedDebtHandler", "liquidation id", liquidation.Id, "debt amount", liquidation.LiquidatedDebtAmount, "err", err)
94-
95-
continue
96-
}
97-
9893
// build settlement tx
9994
settlementTx, txHash, sigHashes, changeAmount, err := types.BuildSettlementTransaction(liquidation, k.GetLiquidationRecords(ctx, liquidation.Id), k.ProtocolLiquidationFeeCollector(ctx), feeRate.Value)
10095
if err != nil {
101-
k.Logger(ctx).Info("Failed to build settlement transaction", "liquidation id", liquidation.Id, "fee rate", feeRate.Value, "err", err)
102-
96+
k.Logger(ctx).Error("Failed to build settlement transaction", "liquidation id", liquidation.Id, "fee rate", feeRate.Value, "err", err)
10397
continue
10498
}
10599

100+
// handle liquidated debt (repay the lending pool)
101+
if err := k.LiquidatedDebtHandler()(ctx, liquidation.Id, liquidation.LoanId, types.ModuleName, liquidation.LiquidatedDebtAmount); err != nil {
102+
// unexpected error
103+
k.Logger(ctx).Error("Failed to call LiquidatedDebtHandler", "liquidation id", liquidation.Id, "debt amount", liquidation.LiquidatedDebtAmount, "err", err)
104+
return err
105+
}
106+
106107
liquidation.UnliquidatedCollateralAmount = sdk.NewInt64Coin(liquidation.CollateralAsset.Denom, changeAmount)
107108
liquidation.SettlementTx = settlementTx
108109
liquidation.SettlementTxId = txHash.String()
@@ -114,4 +115,6 @@ func handleCompletedLiquidations(ctx sdk.Context, k keeper.Keeper) {
114115
// initiate signing request via TSS
115116
k.TSSKeeper().InitiateSigningRequest(ctx, types.ModuleName, types.ToScopedId(liquidation.Id), tsstypes.SigningType_SIGNING_TYPE_SCHNORR_WITH_TWEAK, int32(types.SigningIntent_SIGNING_INTENT_DEFAULT), liquidation.DCM, sigHashes, &tsstypes.SigningOptions{Tweak: ""})
116117
}
118+
119+
return nil
117120
}

x/liquidation/module/module.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,5 @@ func (am AppModule) BeginBlock(_ context.Context) error {
151151
// EndBlock contains the logic that is automatically triggered at the end of each block
152152
func (am AppModule) EndBlock(ctx context.Context) error {
153153
c := sdk.UnwrapSDKContext(ctx)
154-
EndBlocker(c, am.keeper)
155-
156-
return nil
154+
return EndBlocker(c, am.keeper)
157155
}

x/tss/module/abci.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import (
1010
)
1111

1212
// EndBlocker called at the end of every block
13-
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
13+
func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
14+
// handle dkg
1415
handleDKGRequests(ctx, k)
16+
17+
// handle refreshing
1518
handleRefreshingRequests(ctx, k)
19+
20+
return nil
1621
}
1722

1823
// handleDKGRequests performs the DKG request handling
@@ -28,7 +33,7 @@ func handleDKGRequests(ctx sdk.Context, k keeper.Keeper) {
2833

2934
// callback the corresponding module handler
3035
if err := k.GetDKGRequestTimeoutHandler(req.Module)(ctx, req.Id, req.Type, req.Intent, k.GetAbsentDKGParticipants(ctx, req)); err != nil {
31-
k.Logger(ctx).Info("Failed to call DGKRequestTimeoutHandler", "module", req.Module, "type", req.Type, "intent", req.Intent)
36+
k.Logger(ctx).Warn("Failed to call DGKRequestTimeoutHandler", "module", req.Module, "type", req.Type, "intent", req.Intent)
3237
}
3338

3439
continue

x/tss/module/module.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,5 @@ func (am AppModule) BeginBlock(_ context.Context) error {
151151
// EndBlock contains the logic that is automatically triggered at the end of each block
152152
func (am AppModule) EndBlock(ctx context.Context) error {
153153
c := sdk.UnwrapSDKContext(ctx)
154-
EndBlocker(c, am.keeper)
155-
156-
return nil
154+
return EndBlocker(c, am.keeper)
157155
}

0 commit comments

Comments
 (0)