From b839bb3fbd5b7acf31979df63fa816cc1cf5ace0 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 21 Aug 2024 12:41:55 -0400 Subject: [PATCH] skip liquidation task loop if last committed block height is the same (backport #2124) (#2125) Co-authored-by: jayy04 <103467857+jayy04@users.noreply.github.com> --- .../liquidation/client/grpc_helper_test.go | 6 +++++- .../liquidation/client/sub_task_runner.go | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/protocol/daemons/liquidation/client/grpc_helper_test.go b/protocol/daemons/liquidation/client/grpc_helper_test.go index 73d1ad5a72..8454c3f54a 100644 --- a/protocol/daemons/liquidation/client/grpc_helper_test.go +++ b/protocol/daemons/liquidation/client/grpc_helper_test.go @@ -590,7 +590,11 @@ func TestSendLiquidatableSubaccountIds(t *testing.T) { tc.subaccountOpenPositionInfo, 1000, ) - require.Equal(t, tc.expectedError, err) + if tc.expectedError != nil { + require.ErrorContains(t, err, tc.expectedError.Error()) + } else { + require.NoError(t, err) + } }) } } diff --git a/protocol/daemons/liquidation/client/sub_task_runner.go b/protocol/daemons/liquidation/client/sub_task_runner.go index 4c3470d73d..594817034d 100644 --- a/protocol/daemons/liquidation/client/sub_task_runner.go +++ b/protocol/daemons/liquidation/client/sub_task_runner.go @@ -30,7 +30,9 @@ type SubTaskRunner interface { ) error } -type SubTaskRunnerImpl struct{} +type SubTaskRunnerImpl struct { + lastLoopBlockHeight uint32 +} // Ensure SubTaskRunnerImpl implements the SubTaskRunner interface. var _ SubTaskRunner = (*SubTaskRunnerImpl)(nil) @@ -54,6 +56,19 @@ func (s *SubTaskRunnerImpl) RunLiquidationDaemonTaskLoop( return err } + // Skip the loop if no new block has been committed. + // Note that lastLoopBlockHeight is initialized to 0, so the first loop will always run. + if lastCommittedBlockHeight == s.lastLoopBlockHeight { + daemonClient.logger.Info( + "Skipping liquidation daemon task loop as no new block has been committed", + "blockHeight", lastCommittedBlockHeight, + ) + return nil + } + + // Update the last loop block height. + s.lastLoopBlockHeight = lastCommittedBlockHeight + // 1. Fetch all information needed to calculate total net collateral and margin requirements. subaccounts, perpInfos,