From 231436fa3723a1765bac28cf155f31ee39a135fe Mon Sep 17 00:00:00 2001 From: Jay Yu <103467857+jayy04@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:13:04 -0400 Subject: [PATCH 1/3] skip liquidation task loop if last committed block height is the same --- .../daemons/liquidation/client/grpc_helper_test.go | 6 +++++- .../daemons/liquidation/client/sub_task_runner.go | 12 +++++++++++- 2 files changed, 16 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 f9600f97e1..d84bfcd05c 100644 --- a/protocol/daemons/liquidation/client/sub_task_runner.go +++ b/protocol/daemons/liquidation/client/sub_task_runner.go @@ -26,7 +26,9 @@ type SubTaskRunner interface { ) error } -type SubTaskRunnerImpl struct{} +type SubTaskRunnerImpl struct { + lastLoopBlockHeight uint32 +} // Ensure SubTaskRunnerImpl implements the SubTaskRunner interface. var _ SubTaskRunner = (*SubTaskRunnerImpl)(nil) @@ -50,6 +52,14 @@ func (s *SubTaskRunnerImpl) RunLiquidationDaemonTaskLoop( return err } + if lastCommittedBlockHeight == s.lastLoopBlockHeight { + daemonClient.logger.Info( + "Skipping liquidation daemon task loop as no new block has been committed", + "blockHeight", lastCommittedBlockHeight, + ) + return nil + } + // 1. Fetch all information needed to calculate total net collateral and margin requirements. subaccounts, perpInfos, From 78cb3fe5b807e0a10e53ce1021cbe41319f7c875 Mon Sep 17 00:00:00 2001 From: Jay Yu <103467857+jayy04@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:15:09 -0400 Subject: [PATCH 2/3] update block height --- protocol/daemons/liquidation/client/sub_task_runner.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protocol/daemons/liquidation/client/sub_task_runner.go b/protocol/daemons/liquidation/client/sub_task_runner.go index d84bfcd05c..d3a32925c7 100644 --- a/protocol/daemons/liquidation/client/sub_task_runner.go +++ b/protocol/daemons/liquidation/client/sub_task_runner.go @@ -60,6 +60,8 @@ func (s *SubTaskRunnerImpl) RunLiquidationDaemonTaskLoop( return nil } + s.lastLoopBlockHeight = lastCommittedBlockHeight + // 1. Fetch all information needed to calculate total net collateral and margin requirements. subaccounts, perpInfos, From 1eeb66d91a9cac318793b2e78ac7f15daab10171 Mon Sep 17 00:00:00 2001 From: Jay Yu <103467857+jayy04@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:50:40 -0400 Subject: [PATCH 3/3] comments --- protocol/daemons/liquidation/client/sub_task_runner.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/protocol/daemons/liquidation/client/sub_task_runner.go b/protocol/daemons/liquidation/client/sub_task_runner.go index d3a32925c7..f9f2e77675 100644 --- a/protocol/daemons/liquidation/client/sub_task_runner.go +++ b/protocol/daemons/liquidation/client/sub_task_runner.go @@ -52,6 +52,8 @@ 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", @@ -60,6 +62,7 @@ func (s *SubTaskRunnerImpl) RunLiquidationDaemonTaskLoop( return nil } + // Update the last loop block height. s.lastLoopBlockHeight = lastCommittedBlockHeight // 1. Fetch all information needed to calculate total net collateral and margin requirements.