Skip to content

Commit

Permalink
types alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
amirylm committed Apr 2, 2024
1 parent 09e530d commit 8f8d960
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f8d960

Please sign in to comment.