Skip to content

Commit

Permalink
core/config: add EVM.OCR DeltaCOverride and DeltaCJitterOverride (#11250
Browse files Browse the repository at this point in the history
)

* core/config: add EVM.OCR OverrideDeltaC and OverrideDeltaCJitter

* update changelog
  • Loading branch information
jmank88 authored Nov 28, 2023
1 parent 41ab6be commit 9efb47d
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 34 deletions.
8 changes: 8 additions & 0 deletions core/chains/evm/config/chain_scoped_ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ func (o *ocrConfig) ObservationGracePeriod() time.Duration {
func (o *ocrConfig) DatabaseTimeout() time.Duration {
return o.c.DatabaseTimeout.Duration()
}

func (o *ocrConfig) DeltaCOverride() time.Duration {
return o.c.DeltaCOverride.Duration()
}

func (o *ocrConfig) DeltaCJitterOverride() time.Duration {
return o.c.DeltaCJitterOverride.Duration()
}
2 changes: 2 additions & 0 deletions core/chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type OCR interface {
ContractTransmitterTransmitTimeout() time.Duration
ObservationGracePeriod() time.Duration
DatabaseTimeout() time.Duration
DeltaCOverride() time.Duration
DeltaCJitterOverride() time.Duration
}

type OCR2 interface {
Expand Down
8 changes: 8 additions & 0 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,8 @@ type OCR struct {
ContractConfirmations *uint16
ContractTransmitterTransmitTimeout *models.Duration
DatabaseTimeout *models.Duration
DeltaCOverride *models.Duration
DeltaCJitterOverride *models.Duration
ObservationGracePeriod *models.Duration
}

Expand All @@ -728,6 +730,12 @@ func (o *OCR) setFrom(f *OCR) {
if v := f.DatabaseTimeout; v != nil {
o.DatabaseTimeout = v
}
if v := f.DeltaCOverride; v != nil {
o.DeltaCOverride = v
}
if v := f.DeltaCJitterOverride; v != nil {
o.DeltaCJitterOverride = v
}
if v := f.ObservationGracePeriod; v != nil {
o.ObservationGracePeriod = v
}
Expand Down
2 changes: 2 additions & 0 deletions core/chains/evm/config/toml/defaults/fallback.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ LeaseDuration = '0s'
ContractConfirmations = 4
ContractTransmitterTransmitTimeout = '10s'
DatabaseTimeout = '10s'
DeltaCOverride = '168h'
DeltaCJitterOverride = '1h'
ObservationGracePeriod = '1s'

[OCR2.Automation]
Expand Down
8 changes: 8 additions & 0 deletions core/config/docs/chains-evm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ ContractConfirmations = 4 # Default
ContractTransmitterTransmitTimeout = '10s' # Default
# DatabaseTimeout sets `OCR.DatabaseTimeout` for this EVM chain.
DatabaseTimeout = '10s' # Default
# **ADVANCED**
# DeltaCOverride (and `DeltaCJitterOverride`) determine the config override DeltaC.
# DeltaC is the maximum age of the latest report in the contract. If the maximum age is exceeded, a new report will be
# created by the report generation protocol.
DeltaCOverride = "168h" # Default
# **ADVANCED**
# DeltaCJitterOverride is the range for jitter to add to `DeltaCOverride`.
DeltaCJitterOverride = "1h" # Default
# ObservationGracePeriod sets `OCR.ObservationGracePeriod` for this EVM chain.
ObservationGracePeriod = '1s' # Default

Expand Down
4 changes: 4 additions & 0 deletions core/services/chainlink/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ func TestConfig_Marshal(t *testing.T) {
ContractConfirmations: ptr[uint16](11),
ContractTransmitterTransmitTimeout: &minute,
DatabaseTimeout: &second,
DeltaCOverride: models.MustNewDuration(time.Hour),
DeltaCJitterOverride: models.MustNewDuration(time.Second),
ObservationGracePeriod: &second,
},
OCR2: evmcfg.OCR2{
Expand Down Expand Up @@ -1019,6 +1021,8 @@ LeaseDuration = '0s'
ContractConfirmations = 11
ContractTransmitterTransmitTimeout = '1m0s'
DatabaseTimeout = '1s'
DeltaCOverride = '1h0m0s'
DeltaCJitterOverride = '1s'
ObservationGracePeriod = '1s'
[EVM.OCR2]
Expand Down
2 changes: 2 additions & 0 deletions core/services/chainlink/testdata/config-full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ LeaseDuration = '0s'
ContractConfirmations = 11
ContractTransmitterTransmitTimeout = '1m0s'
DatabaseTimeout = '1s'
DeltaCOverride = '1h0m0s'
DeltaCJitterOverride = '1s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ LeaseDuration = '0s'
ContractConfirmations = 4
ContractTransmitterTransmitTimeout = '10s'
DatabaseTimeout = '10s'
DeltaCOverride = '168h0m0s'
DeltaCJitterOverride = '1h0m0s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down Expand Up @@ -396,6 +398,8 @@ LeaseDuration = '0s'
ContractConfirmations = 4
ContractTransmitterTransmitTimeout = '10s'
DatabaseTimeout = '10s'
DeltaCOverride = '168h0m0s'
DeltaCJitterOverride = '1h0m0s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down Expand Up @@ -475,6 +479,8 @@ LeaseDuration = '0s'
ContractConfirmations = 4
ContractTransmitterTransmitTimeout = '10s'
DatabaseTimeout = '10s'
DeltaCOverride = '168h0m0s'
DeltaCJitterOverride = '1h0m0s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down
12 changes: 10 additions & 2 deletions core/services/ocr/config_overrider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting/types"

"github.com/smartcontractkit/chainlink-common/pkg/services"

"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/utils"
Expand Down Expand Up @@ -40,8 +41,14 @@ type ConfigOverriderImpl struct {
// InitialHibernationStatus - hibernation state set until the first successful update from the chain
const InitialHibernationStatus = false

type DeltaCConfig interface {
DeltaCOverride() time.Duration
DeltaCJitterOverride() time.Duration
}

func NewConfigOverriderImpl(
logger logger.Logger,
cfg DeltaCConfig,
contractAddress ethkey.EIP55Address,
flags *ContractFlags,
pollTicker utils.TickerBase,
Expand All @@ -53,8 +60,9 @@ func NewConfigOverriderImpl(
}

addressBig := contractAddress.Big()
addressSeconds := addressBig.Mod(addressBig, big.NewInt(3600)).Uint64()
deltaC := 23*time.Hour + time.Duration(addressSeconds)*time.Second
jitterSeconds := int64(cfg.DeltaCJitterOverride() / time.Second)
addressSeconds := addressBig.Mod(addressBig, big.NewInt(jitterSeconds)).Uint64()
deltaC := cfg.DeltaCOverride() + time.Duration(addressSeconds)*time.Second

ctx, cancel := context.WithCancel(context.Background())
co := ConfigOverriderImpl{
Expand Down
21 changes: 15 additions & 6 deletions core/services/ocr/config_overrider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

Expand All @@ -27,6 +28,12 @@ type configOverriderUni struct {
contractAddress ethkey.EIP55Address
}

type deltaCConfig struct{}

func (d deltaCConfig) DeltaCOverride() time.Duration { return time.Hour * 24 * 7 }

func (d deltaCConfig) DeltaCJitterOverride() time.Duration { return time.Hour }

func newConfigOverriderUni(t *testing.T, pollITicker utils.TickerBase, flagsContract *mocks.Flags) (uni configOverriderUni) {
var testLogger = logger.TestLogger(t)
contractAddress := cltest.NewEIP55Address()
Expand All @@ -35,6 +42,7 @@ func newConfigOverriderUni(t *testing.T, pollITicker utils.TickerBase, flagsCont
var err error
uni.overrider, err = ocr.NewConfigOverriderImpl(
testLogger,
deltaCConfig{},
contractAddress,
flags,
pollITicker,
Expand Down Expand Up @@ -141,6 +149,7 @@ func Test_OCRConfigOverrider(t *testing.T) {
flags := &ocr.ContractFlags{FlagsInterface: nil}
_, err := ocr.NewConfigOverriderImpl(
testLogger,
deltaCConfig{},
contractAddress,
flags,
nil,
Expand All @@ -160,18 +169,18 @@ func Test_OCRConfigOverrider(t *testing.T) {
address2, err := ethkey.NewEIP55Address(common.BigToAddress(big.NewInt(1234567890)).Hex())
require.NoError(t, err)

overrider1a, err := ocr.NewConfigOverriderImpl(testLogger, address1, flags, nil)
overrider1a, err := ocr.NewConfigOverriderImpl(testLogger, deltaCConfig{}, address1, flags, nil)
require.NoError(t, err)

overrider1b, err := ocr.NewConfigOverriderImpl(testLogger, address1, flags, nil)
overrider1b, err := ocr.NewConfigOverriderImpl(testLogger, deltaCConfig{}, address1, flags, nil)
require.NoError(t, err)

overrider2, err := ocr.NewConfigOverriderImpl(testLogger, address2, flags, nil)
overrider2, err := ocr.NewConfigOverriderImpl(testLogger, deltaCConfig{}, address2, flags, nil)
require.NoError(t, err)

require.Equal(t, overrider1a.DeltaCFromAddress, time.Duration(85600000000000))
require.Equal(t, overrider1b.DeltaCFromAddress, time.Duration(85600000000000))
require.Equal(t, overrider2.DeltaCFromAddress, time.Duration(84690000000000))
assert.Equal(t, cltest.MustParseDuration(t, "168h46m40s"), overrider1a.DeltaCFromAddress)
assert.Equal(t, cltest.MustParseDuration(t, "168h46m40s"), overrider1b.DeltaCFromAddress)
assert.Equal(t, cltest.MustParseDuration(t, "168h31m30s"), overrider2.DeltaCFromAddress)
})
}

Expand Down
2 changes: 1 addition & 1 deletion core/services/ocr/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (d *Delegate) maybeCreateConfigOverrider(logger logger.Logger, chain legacy
}

ticker := utils.NewPausableTicker(ConfigOverriderPollInterval)
return NewConfigOverriderImpl(logger, contractAddress, flags, &ticker)
return NewConfigOverriderImpl(logger, chain.Config().EVM().OCR(), contractAddress, flags, &ticker)
}
return nil, nil
}
2 changes: 2 additions & 0 deletions core/web/resolver/testdata/config-full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ LeaseDuration = '0s'
ContractConfirmations = 11
ContractTransmitterTransmitTimeout = '1m0s'
DatabaseTimeout = '1s'
DeltaCOverride = '1h0m0s'
DeltaCJitterOverride = '1s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down
6 changes: 6 additions & 0 deletions core/web/resolver/testdata/config-multi-chain-effective.toml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ LeaseDuration = '0s'
ContractConfirmations = 4
ContractTransmitterTransmitTimeout = '10s'
DatabaseTimeout = '10s'
DeltaCOverride = '168h0m0s'
DeltaCJitterOverride = '1h0m0s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down Expand Up @@ -396,6 +398,8 @@ LeaseDuration = '0s'
ContractConfirmations = 4
ContractTransmitterTransmitTimeout = '10s'
DatabaseTimeout = '10s'
DeltaCOverride = '168h0m0s'
DeltaCJitterOverride = '1h0m0s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down Expand Up @@ -475,6 +479,8 @@ LeaseDuration = '0s'
ContractConfirmations = 4
ContractTransmitterTransmitTimeout = '10s'
DatabaseTimeout = '10s'
DeltaCOverride = '168h0m0s'
DeltaCJitterOverride = '1h0m0s'
ObservationGracePeriod = '1s'

[EVM.OCR2]
Expand Down
50 changes: 25 additions & 25 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`mercury_transmit_queue_push_error_count`
Nops should consider alerting on these.
- Mercury now implements a local cache for fetching prices for fees, which ought to reduce latency and load on the mercury server, as well as increasing performance. It is enabled by default and can be configured with the following new config variables:
```
[Mercury]
# Mercury.Cache controls settings for the price retrieval cache querying a mercury server
[Mercury.Cache]
# LatestReportTTL controls how "stale" we will allow a price to be e.g. if
# set to 1s, a new price will always be fetched if the last result was
# from 1 second ago or older.
#
# Another way of looking at it is such: the cache will _never_ return a
# price that was queried from now-LatestReportTTL or before.
#
# Setting to zero disables caching entirely.
LatestReportTTL = "1s" # Default
# MaxStaleAge is that maximum amount of time that a value can be stale
# before it is deleted from the cache (a form of garbage collection).
#
# This should generally be set to something much larger than
# LatestReportTTL. Setting to zero disables garbage collection.
MaxStaleAge = "1h" # Default
# LatestReportDeadline controls how long to wait for a response from the
# mercury server before retrying. Setting this to zero will wait indefinitely.
LatestReportDeadline = "5s" # Default
```
```
[Mercury]
# Mercury.Cache controls settings for the price retrieval cache querying a mercury server
[Mercury.Cache]
# LatestReportTTL controls how "stale" we will allow a price to be e.g. if
# set to 1s, a new price will always be fetched if the last result was
# from 1 second ago or older.
#
# Another way of looking at it is such: the cache will _never_ return a
# price that was queried from now-LatestReportTTL or before.
#
# Setting to zero disables caching entirely.
LatestReportTTL = "1s" # Default
# MaxStaleAge is that maximum amount of time that a value can be stale
# before it is deleted from the cache (a form of garbage collection).
#
# This should generally be set to something much larger than
# LatestReportTTL. Setting to zero disables garbage collection.
MaxStaleAge = "1h" # Default
# LatestReportDeadline controls how long to wait for a response from the
# mercury server before retrying. Setting this to zero will wait indefinitely.
LatestReportDeadline = "5s" # Default
```
- New prom metrics for the mercury cache:
`mercury_cache_fetch_failure_count`
`mercury_cache_hit_count`
`mercury_cache_wait_count`
`mercury_cache_miss_count`

- Added new `EVM.OCR` TOML config fields `DeltaCOverride` and `DeltaCJitterOverride` for overriding the config DeltaC.
### Changed
Expand Down
Loading

0 comments on commit 9efb47d

Please sign in to comment.