From 8f8d96043e9f676fd725a1b7a1e977daff5a1c3e Mon Sep 17 00:00:00 2001 From: amirylm Date: Tue, 2 Apr 2024 16:51:51 +0300 Subject: [PATCH] types alignment --- .../evmregistry/v21/logprovider/buffer_v1.go | 33 +++------- .../v21/logprovider/buffer_v1_test.go | 61 +++++++++++++++++++ .../v21/logprovider/{window.go => log.go} | 37 +++++++---- .../{window_test.go => log_test.go} | 61 ------------------- 4 files changed, 97 insertions(+), 95 deletions(-) rename core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/{window.go => log.go} (52%) rename core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/{window_test.go => log_test.go} (70%) diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1.go index 8116185729d..6f170b5c0ee 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1.go @@ -1,14 +1,12 @@ package logprovider import ( - "encoding/hex" "math" "math/big" "sort" "sync" "sync/atomic" - ocr2keepers "github.com/smartcontractkit/chainlink-common/pkg/types/automation" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" "github.com/smartcontractkit/chainlink/v2/core/logger" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/prommetrics" @@ -120,7 +118,7 @@ func (b *logBuffer) Dequeue(block int64, blockRate, upkeepLimit, maxResults int, b.lock.RLock() defer b.lock.RUnlock() - start, end := BlockWindow(block, blockRate) + start, end := getBlockWindow(block, blockRate) return b.dequeue(start, end, upkeepLimit, maxResults, upkeepSelector) } @@ -342,7 +340,7 @@ func (q *upkeepLogQueue) clean(blockThreshold int64) int { expired++ continue } - start, _ := BlockWindow(l.BlockNumber, blockRate) + start, _ := getBlockWindow(l.BlockNumber, blockRate) if start != currentWindowStart { // new window, reset capacity currentWindowStart = start @@ -381,24 +379,13 @@ func (q *upkeepLogQueue) cleanVisited(blockThreshold int64) { } } -// logID returns a unique identifier for a log, which is an hex string -// of ocr2keepers.LogTriggerExtension.LogIdentifier() -func logID(l logpoller.Log) string { - ext := ocr2keepers.LogTriggerExtension{ - Index: uint32(l.LogIndex), +// getBlockWindow returns the start and end block of the window for the given block. +func getBlockWindow(block int64, blockRate int) (start int64, end int64) { + windowSize := int64(blockRate) + if windowSize == 0 { + return block, block } - copy(ext.TxHash[:], l.TxHash[:]) - copy(ext.BlockHash[:], l.BlockHash[:]) - return hex.EncodeToString(ext.LogIdentifier()) -} - -// latestBlockNumber returns the latest block number from the given logs -func latestBlockNumber(logs ...logpoller.Log) int64 { - var latest int64 - for _, l := range logs { - if l.BlockNumber > latest { - latest = l.BlockNumber - } - } - return latest + start = block - (block % windowSize) + end = start + windowSize - 1 + return } diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1_test.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1_test.go index c728e37eb8c..48ad9ea4549 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/buffer_v1_test.go @@ -365,6 +365,67 @@ func TestLogEventBufferV1_UpkeepQueue_clean(t *testing.T) { }) } +func TestLogEventBufferV1_BlockWindow(t *testing.T) { + tests := []struct { + name string + block int64 + blockRate int + wantStart int64 + wantEnd int64 + }{ + { + name: "block 0, blockRate 1", + block: 0, + blockRate: 1, + wantStart: 0, + wantEnd: 0, + }, + { + name: "block 81, blockRate 1", + block: 81, + blockRate: 1, + wantStart: 81, + wantEnd: 81, + }, + { + name: "block 0, blockRate 4", + block: 0, + blockRate: 4, + wantStart: 0, + wantEnd: 3, + }, + { + name: "block 81, blockRate 4", + block: 81, + blockRate: 4, + wantStart: 80, + wantEnd: 83, + }, + { + name: "block 83, blockRate 4", + block: 83, + blockRate: 4, + wantStart: 80, + wantEnd: 83, + }, + { + name: "block 84, blockRate 4", + block: 84, + blockRate: 4, + wantStart: 84, + wantEnd: 87, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + start, end := getBlockWindow(tc.block, tc.blockRate) + require.Equal(t, tc.wantStart, start) + require.Equal(t, tc.wantEnd, end) + }) + } +} + type dequeueArgs struct { block int64 blockRate int diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/log.go similarity index 52% rename from core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window.go rename to core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/log.go index 8596afccef1..2b193b1b4bc 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/log.go @@ -1,20 +1,13 @@ package logprovider import ( + "encoding/hex" + + ocr2keepers "github.com/smartcontractkit/chainlink-common/pkg/types/automation" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" ) -// BlockWindow returns the start and end block for the given window. -func BlockWindow(block int64, blockRate int) (start int64, end int64) { - windowSize := int64(blockRate) - if windowSize == 0 { - return block, block - } - start = block - (block % windowSize) - end = start + windowSize - 1 - return -} - // LogSorter sorts the logs based on block number, tx hash and log index. // returns true if b should come before a. func LogSorter(a, b logpoller.Log) bool { @@ -39,3 +32,25 @@ func LogComparator(a, b logpoller.Log) int { } return int(logIndexDiff) } + +// logID returns a unique identifier for a log, which is an hex string +// of ocr2keepers.LogTriggerExtension.LogIdentifier() +func logID(l logpoller.Log) string { + ext := ocr2keepers.LogTriggerExtension{ + Index: uint32(l.LogIndex), + } + copy(ext.TxHash[:], l.TxHash[:]) + copy(ext.BlockHash[:], l.BlockHash[:]) + return hex.EncodeToString(ext.LogIdentifier()) +} + +// latestBlockNumber returns the latest block number from the given logs +func latestBlockNumber(logs ...logpoller.Log) int64 { + var latest int64 + for _, l := range logs { + if l.BlockNumber > latest { + latest = l.BlockNumber + } + } + return latest +} diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window_test.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/log_test.go similarity index 70% rename from core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window_test.go rename to core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/log_test.go index 3021ff3e6a1..9a2d2760a4f 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/log_test.go @@ -9,67 +9,6 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller" ) -func TestBlockWindow(t *testing.T) { - tests := []struct { - name string - block int64 - blockRate int - wantStart int64 - wantEnd int64 - }{ - { - name: "block 0, blockRate 1", - block: 0, - blockRate: 1, - wantStart: 0, - wantEnd: 0, - }, - { - name: "block 81, blockRate 1", - block: 81, - blockRate: 1, - wantStart: 81, - wantEnd: 81, - }, - { - name: "block 0, blockRate 4", - block: 0, - blockRate: 4, - wantStart: 0, - wantEnd: 3, - }, - { - name: "block 81, blockRate 4", - block: 81, - blockRate: 4, - wantStart: 80, - wantEnd: 83, - }, - { - name: "block 83, blockRate 4", - block: 83, - blockRate: 4, - wantStart: 80, - wantEnd: 83, - }, - { - name: "block 84, blockRate 4", - block: 84, - blockRate: 4, - wantStart: 84, - wantEnd: 87, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - start, end := BlockWindow(tc.block, tc.blockRate) - require.Equal(t, tc.wantStart, start) - require.Equal(t, tc.wantEnd, end) - }) - } -} - func TestLogComparatorSorter(t *testing.T) { tests := []struct { name string