diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury.go index d24442b6ee9..45f1b54b2cd 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury.go @@ -26,7 +26,8 @@ const ( BlockNumber = "blockNumber" // valid for v0.2 Timestamp = "timestamp" // valid for v0.3 totalFastPluginRetries = 5 - totalMediumPluginRetries = 10 + totalMediumPluginRetries = totalFastPluginRetries + 1 + RetryIntervalTimeout = time.Duration(-1) ) var GenerateHMACFn = func(method string, path string, body []byte, clientId string, secret string, ts int64) string { @@ -45,8 +46,7 @@ var GenerateHMACFn = func(method string, path string, body []byte, clientId stri } // CalculateRetryConfig returns plugin retry interval based on how many times plugin has retried this work -var CalculateRetryConfigFn = func(prk string, mercuryConfig MercuryConfigProvider) time.Duration { - var retryInterval time.Duration +var CalculateRetryConfigFn = func(prk string, mercuryConfig MercuryConfigProvider) (retryInterval time.Duration) { var retries int totalAttempts, ok := mercuryConfig.GetPluginRetry(prk) if ok { @@ -55,9 +55,9 @@ var CalculateRetryConfigFn = func(prk string, mercuryConfig MercuryConfigProvide retryInterval = 1 * time.Second } else if retries < totalMediumPluginRetries { retryInterval = 5 * time.Second + } else { + retryInterval = RetryIntervalTimeout } - // if the core node has retried totalMediumPluginRetries times, do not set retry interval and plugin will use - // the default interval } else { retryInterval = 1 * time.Second } diff --git a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury_test.go b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury_test.go index ce82ec7ae8f..3854253d48a 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/mercury/mercury_test.go @@ -5,12 +5,15 @@ import ( "errors" "math/big" "testing" + "time" + "github.com/patrickmn/go-cache" "github.com/stretchr/testify/require" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/stretchr/testify/assert" + "github.com/smartcontractkit/chainlink-common/pkg/types" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/encoding" ) @@ -240,3 +243,77 @@ func TestPacker_UnpackCheckCallbackResult(t *testing.T) { }) } } + +func Test_CalculateRetryConfigFn(t *testing.T) { + tests := []struct { + name string + times int + expected time.Duration + }{ + { + name: "first retry", + times: 1, + expected: 1 * time.Second, + }, + { + name: "second retry", + times: 2, + expected: 1 * time.Second, + }, + { + name: "fifth retry", + times: 5, + expected: 1 * time.Second, + }, + { + name: "sixth retry", + times: 6, + expected: 5 * time.Second, + }, + { + name: "timeout", + times: totalMediumPluginRetries + 1, + expected: RetryIntervalTimeout, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := newMercuryConfigMock() + var result time.Duration + for i := 0; i < tc.times; i++ { + result = CalculateRetryConfigFn("prk", cfg) + } + assert.Equal(t, tc.expected, result) + }) + } +} + +type mercuryConfigMock struct { + pluginRetryCache *cache.Cache +} + +func newMercuryConfigMock() *mercuryConfigMock { + return &mercuryConfigMock{ + pluginRetryCache: cache.New(10*time.Second, time.Minute), + } +} + +func (c *mercuryConfigMock) Credentials() *types.MercuryCredentials { + return nil +} + +func (c *mercuryConfigMock) IsUpkeepAllowed(k string) (interface{}, bool) { + return nil, false +} + +func (c *mercuryConfigMock) SetUpkeepAllowed(k string, v interface{}, d time.Duration) { +} + +func (c *mercuryConfigMock) GetPluginRetry(k string) (interface{}, bool) { + return c.pluginRetryCache.Get(k) +} + +func (c *mercuryConfigMock) SetPluginRetry(k string, v interface{}, d time.Duration) { + c.pluginRetryCache.Set(k, v, d) +}