This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
CCIP-1704 Checking for curse on Source Chain #580
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
78c0351
Exposing IsSourceCursed method in OnRamp reader
mateusz-sekara 27e4a8d
Checking both source and destination curses before starting observation
mateusz-sekara ef56ac6
Linter and tests fixed
mateusz-sekara 377a143
Wrapping RPC errors with proper log message
mateusz-sekara 45ce63d
Merge remote-tracking branch 'origin/ccip-develop' into checking-sour…
mateusz-sekara 6b48b27
Update core/services/ocr2/plugins/ccip/internal/ccipcommon/shortcuts.go
mateusz-sekara 6bdc3ed
Minor fix
mateusz-sekara ebac474
Merge remote-tracking branch 'origin/ccip-develop' into checking-sour…
mateusz-sekara 7332aec
Post merge fix
mateusz-sekara 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,38 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| ) | ||
|
|
||
| type OnceCtxFunction[T any] func(ctx context.Context) (T, error) | ||
|
|
||
| // CallOnceOnNoError returns a new function that wraps the given function f with caching capabilities. | ||
| // If f returns an error, the result is not cached, allowing f to be retried on subsequent calls. | ||
| // Use case for that is to avoid caching an error forever in case of transient errors (e.g. flaky RPC) | ||
| func CallOnceOnNoError[T any](f OnceCtxFunction[T]) OnceCtxFunction[T] { | ||
| var ( | ||
| mu sync.Mutex | ||
| value T | ||
| err error | ||
| called bool | ||
| ) | ||
mateusz-sekara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return func(ctx context.Context) (T, error) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
|
|
||
| // If the function has been called successfully before, return the cached result. | ||
| if called && err == nil { | ||
| return value, nil | ||
| } | ||
|
|
||
| // Call the function and cache the result only if there is no error. | ||
| value, err = f(ctx) | ||
| if err == nil { | ||
| called = true | ||
| } | ||
|
|
||
| return value, err | ||
| } | ||
| } | ||
83 changes: 83 additions & 0 deletions
83
core/services/ocr2/plugins/ccip/internal/cache/once_test.go
This file contains hidden or 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,83 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/utils/tests" | ||
| ) | ||
|
|
||
| // TestCallOnceOnNoErrorCachingSuccess tests caching behavior when the function succeeds. | ||
| func TestCallOnceOnNoErrorCachingSuccess(t *testing.T) { | ||
| callCount := 0 | ||
| testFunc := func(ctx context.Context) (string, error) { | ||
| callCount++ | ||
| return "test result", nil | ||
| } | ||
|
|
||
| cachedFunc := CallOnceOnNoError(testFunc) | ||
|
|
||
| // Call the function twice. | ||
| _, err := cachedFunc(tests.Context(t)) | ||
| assert.NoError(t, err, "Expected no error on the first call") | ||
|
|
||
| _, err = cachedFunc(tests.Context(t)) | ||
| assert.NoError(t, err, "Expected no error on the second call") | ||
|
|
||
| assert.Equal(t, 1, callCount, "Function should be called exactly once") | ||
| } | ||
|
|
||
| // TestCallOnceOnNoErrorCachingError tests that the function is retried after an error. | ||
| func TestCallOnceOnNoErrorCachingError(t *testing.T) { | ||
| callCount := 0 | ||
| testFunc := func(ctx context.Context) (string, error) { | ||
| callCount++ | ||
| if callCount == 1 { | ||
| return "", errors.New("test error") | ||
| } | ||
| return "test result", nil | ||
| } | ||
|
|
||
| cachedFunc := CallOnceOnNoError(testFunc) | ||
|
|
||
| // First call should fail. | ||
| _, err := cachedFunc(tests.Context(t)) | ||
| require.Error(t, err, "Expected an error on the first call") | ||
|
|
||
| // Second call should succeed. | ||
| r, err := cachedFunc(tests.Context(t)) | ||
| assert.NoError(t, err, "Expected no error on the second call") | ||
| assert.Equal(t, "test result", r) | ||
| assert.Equal(t, 2, callCount, "Function should be called exactly twice") | ||
| } | ||
|
|
||
| // TestCallOnceOnNoErrorCachingConcurrency tests that the function works correctly under concurrent access. | ||
| func TestCallOnceOnNoErrorCachingConcurrency(t *testing.T) { | ||
| var wg sync.WaitGroup | ||
| callCount := 0 | ||
| testFunc := func(ctx context.Context) (string, error) { | ||
| callCount++ | ||
| return "test result", nil | ||
| } | ||
|
|
||
| cachedFunc := CallOnceOnNoError(testFunc) | ||
|
|
||
| // Simulate concurrent calls. | ||
| for i := 0; i < 10; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| _, err := cachedFunc(tests.Context(t)) | ||
| assert.NoError(t, err, "Expected no error in concurrent execution") | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| assert.Equal(t, 1, callCount, "Function should be called exactly once despite concurrent calls") | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,9 +5,12 @@ import ( | |
| "encoding/hex" | ||
| "fmt" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "golang.org/x/sync/errgroup" | ||
|
|
||
| "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/ccip" | ||
| "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/cciptypes" | ||
| "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata" | ||
| ) | ||
|
|
@@ -73,3 +76,41 @@ func FlattenUniqueSlice[T comparable](slices ...[]T) []T { | |
| } | ||
| return flattened | ||
| } | ||
|
|
||
| // VerifyNotDown returns error if the commitStore is down (paused or destination cursed) or if the source chain is cursed | ||
| // Both RPCs are called in parallel to save some time. These calls cannot be batched because they target different chains. | ||
| func VerifyNotDown(ctx context.Context, lggr logger.Logger, commitStore ccipdata.CommitStoreReader, onRamp ccipdata.OnRampReader) error { | ||
| var ( | ||
| eg = new(errgroup.Group) | ||
| isDown bool | ||
| isCursed bool | ||
| ) | ||
|
|
||
| eg.Go(func() error { | ||
| var err error | ||
| isDown, err = commitStore.IsDown(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(err, "commitStore isDown check errored") | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| eg.Go(func() error { | ||
| var err error | ||
| isCursed, err = onRamp.IsSourceCursed(ctx) | ||
| if err != nil { | ||
| return errors.Wrap(err, "onRamp isSourceCursed errored") | ||
| } | ||
| return nil | ||
| }) | ||
|
|
||
| if err := eg.Wait(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if isDown || isCursed { | ||
| lggr.Errorf("Source chain is cursed or CommitStore is down", "isDown", isDown, "isCursed", isCursed) | ||
| return ccip.ErrChainPausedOrCursed | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we care to be more specific here in this error or is the above log enough?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, good question, maybe having distinction between source and dest curse could be useful. Do you think that separate errors per source/dest would be enough?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the other hand, to be very clear it would require three different errors
|
||
| } | ||
| return nil | ||
| } | ||
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.
Simplified. One check, instead of two (bool + error)