-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log dropping and block windows #12342
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package logprovider | ||
|
||
import ( | ||
"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 = block + (windowSize - (block % 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 { | ||
return LogComparator(a, b) > 0 | ||
} | ||
|
||
// LogComparator compares the logs based on block number, tx hash and log index. | ||
// | ||
// Returns: | ||
// | ||
// -1 if a < b | ||
// 0 if a == b | ||
// +1 if a > b | ||
func LogComparator(a, b logpoller.Log) int { | ||
if b.BlockNumber != a.BlockNumber { | ||
return int(a.BlockNumber - b.BlockNumber) | ||
} | ||
if txDiff := a.TxHash.Big().Cmp(b.TxHash.Big()); txDiff != 0 { | ||
return txDiff | ||
} | ||
return int(a.LogIndex - b.LogIndex) | ||
} |
157 changes: 157 additions & 0 deletions
157
core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/logprovider/window_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package logprovider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
|
||
"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 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 | ||
a logpoller.Log | ||
b logpoller.Log | ||
wantCmp int | ||
wantSort bool | ||
}{ | ||
{ | ||
name: "a == b", | ||
a: logpoller.Log{ | ||
BlockNumber: 1, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 1, | ||
}, | ||
b: logpoller.Log{ | ||
BlockNumber: 1, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 1, | ||
}, | ||
wantCmp: 0, | ||
wantSort: false, | ||
}, | ||
{ | ||
name: "a < b: block number", | ||
a: logpoller.Log{ | ||
BlockNumber: 1, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 1, | ||
}, | ||
b: logpoller.Log{ | ||
BlockNumber: 2, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 1, | ||
}, | ||
wantCmp: -1, | ||
wantSort: false, | ||
}, | ||
{ | ||
name: "a < b: log index", | ||
a: logpoller.Log{ | ||
BlockNumber: 1, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 1, | ||
}, | ||
b: logpoller.Log{ | ||
BlockNumber: 1, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 2, | ||
}, | ||
wantCmp: -1, | ||
wantSort: false, | ||
}, | ||
{ | ||
name: "a > b: block number", | ||
a: logpoller.Log{ | ||
BlockNumber: 3, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 1, | ||
}, | ||
b: logpoller.Log{ | ||
BlockNumber: 2, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 1, | ||
}, | ||
wantCmp: 1, | ||
wantSort: true, | ||
}, | ||
{ | ||
name: "a > b: log index", | ||
a: logpoller.Log{ | ||
BlockNumber: 1, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 3, | ||
}, | ||
b: logpoller.Log{ | ||
BlockNumber: 1, | ||
TxHash: common.HexToHash("0x1"), | ||
LogIndex: 2, | ||
}, | ||
wantCmp: 1, | ||
wantSort: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
require.Equal(t, tc.wantCmp, LogComparator(tc.a, tc.b)) | ||
require.Equal(t, tc.wantSort, LogSorter(tc.a, tc.b)) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we return an error here/enforce non-zero windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather validate it somewhere else but still prefer to check for zero here to avoid doing mod with 0