Skip to content

Commit

Permalink
chore(ccipdata): don't use generated types in OffRampReader (#416)
Browse files Browse the repository at this point in the history
## Motivation

Currently, the `OffRampReader` interface uses the `evm_2_evm_offramp.RateLimiterTokenBucket` type, a generated version-dependent type. The interface abstracts the rest of the system from the version-specific components, so using generated types in its methods is inappropriate.

## Solution

This change updates the interface to use the `ccipdata.TokenBucketRateLimit` type (equivalent to `evm_2_evm_offramp.RateLimiterTokenBucket`) instead. Another interface method, `GetTokenPoolsRateLimits`, already used the version-independent type; this change makes the interface more consistent.
  • Loading branch information
roman-kashitsyn authored Jan 10, 2024
1 parent b96150b commit 1a4020f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 29 deletions.
3 changes: 1 addition & 2 deletions core/services/ocr2/plugins/ccip/ccipexec/ocr2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp"
"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/internal"
Expand Down Expand Up @@ -1018,7 +1017,7 @@ func (r *ExecutionReportingPlugin) getUnexpiredCommitReports(
}

type execTokenData struct {
rateLimiterTokenBucket evm_2_evm_offramp.RateLimiterTokenBucket
rateLimiterTokenBucket ccipdata.TokenBucketRateLimit
sourceTokenPrices map[common.Address]*big.Int
destTokenPrices map[common.Address]*big.Int
sourceToDestTokens map[common.Address]common.Address
Expand Down
9 changes: 4 additions & 5 deletions core/services/ocr2/plugins/ccip/ccipexec/ocr2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/testhelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/tokendata"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
)
Expand All @@ -52,7 +51,7 @@ func TestExecutionReportingPlugin_Observation(t *testing.T) {
tokenPoolsMapping map[common.Address]common.Address
blessedRoots map[[32]byte]bool
senderNonce uint64
rateLimiterState evm_2_evm_offramp.RateLimiterTokenBucket
rateLimiterState ccipdata.TokenBucketRateLimit
expErr bool
}{
{
Expand All @@ -73,9 +72,9 @@ func TestExecutionReportingPlugin_Observation(t *testing.T) {
},
},
blessedRoots: map[[32]byte]bool{
[32]byte{123}: true,
{123}: true,
},
rateLimiterState: evm_2_evm_offramp.RateLimiterTokenBucket{
rateLimiterState: ccipdata.TokenBucketRateLimit{
IsEnabled: false,
},
tokenPoolsMapping: map[common.Address]common.Address{},
Expand Down Expand Up @@ -1802,7 +1801,7 @@ func Test_prepareTokenExecData(t *testing.T) {
destPriceRegistry := ccipdatamocks.NewPriceRegistryReader(t)
gasPriceEstimator := prices.NewMockGasPriceEstimatorExec(t)

offrampReader.On("CurrentRateLimiterState", ctx).Return(evm_2_evm_offramp.RateLimiterTokenBucket{}, nil).Maybe()
offrampReader.On("CurrentRateLimiterState", ctx).Return(ccipdata.TokenBucketRateLimit{}, nil).Maybe()
offrampReader.On("GetSourceToDestTokensMapping", ctx).Return(map[common.Address]common.Address{}, nil).Maybe()
gasPriceEstimator.On("GetGasPrice", ctx).Return(prices.GasPrice(big.NewInt(1e9)), nil).Maybe()

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/prices"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
Expand Down Expand Up @@ -100,7 +99,7 @@ type OffRampReader interface {
OnchainConfig() ExecOnchainConfig
GasPriceEstimator() prices.GasPriceEstimatorExec
GetSenderNonce(ctx context.Context, sender common.Address) (uint64, error)
CurrentRateLimiterState(ctx context.Context) (evm_2_evm_offramp.RateLimiterTokenBucket, error)
CurrentRateLimiterState(ctx context.Context) (TokenBucketRateLimit, error)
GetExecutionState(ctx context.Context, sequenceNumber uint64) (uint8, error)
GetStaticConfig(ctx context.Context) (OffRampStaticConfig, error)
GetSourceToDestTokensMapping(ctx context.Context) (map[common.Address]common.Address, error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/custom_token_pool"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp_1_0_0"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/router"
"github.com/smartcontractkit/chainlink/v2/core/logger"
Expand Down Expand Up @@ -193,12 +192,12 @@ func (o *OffRamp) GetSenderNonce(ctx context.Context, sender common.Address) (ui
return o.offRampV100.GetSenderNonce(&bind.CallOpts{Context: ctx}, sender)
}

func (o *OffRamp) CurrentRateLimiterState(ctx context.Context) (evm_2_evm_offramp.RateLimiterTokenBucket, error) {
func (o *OffRamp) CurrentRateLimiterState(ctx context.Context) (ccipdata.TokenBucketRateLimit, error) {
state, err := o.offRampV100.CurrentRateLimiterState(&bind.CallOpts{Context: ctx})
if err != nil {
return *new(evm_2_evm_offramp.RateLimiterTokenBucket), err
return ccipdata.TokenBucketRateLimit{}, err
}
return evm_2_evm_offramp.RateLimiterTokenBucket{
return ccipdata.TokenBucketRateLimit{
Tokens: state.Tokens,
LastUpdated: state.LastUpdated,
IsEnabled: state.IsEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,18 @@ type OffRamp struct {
offRampV120 *evm_2_evm_offramp.EVM2EVMOffRamp
}

func (o *OffRamp) CurrentRateLimiterState(ctx context.Context) (evm_2_evm_offramp.RateLimiterTokenBucket, error) {
return o.offRampV120.CurrentRateLimiterState(&bind.CallOpts{Context: ctx})
func (o *OffRamp) CurrentRateLimiterState(ctx context.Context) (ccipdata.TokenBucketRateLimit, error) {
bucket, err := o.offRampV120.CurrentRateLimiterState(&bind.CallOpts{Context: ctx})
if err != nil {
return ccipdata.TokenBucketRateLimit{}, err
}
return ccipdata.TokenBucketRateLimit{
Tokens: bucket.Tokens,
LastUpdated: bucket.LastUpdated,
IsEnabled: bucket.IsEnabled,
Capacity: bucket.Capacity,
Rate: bucket.Rate,
}, nil
}

func (o *OffRamp) ChangeConfig(onchainConfigBytes []byte, offchainConfigBytes []byte) (common.Address, common.Address, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/ethereum/go-ethereum/common"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
)

Expand Down Expand Up @@ -51,8 +50,8 @@ func (o *ObservedOffRampReader) GetSenderNonce(ctx context.Context, sender commo
})
}

func (o *ObservedOffRampReader) CurrentRateLimiterState(ctx context.Context) (evm_2_evm_offramp.RateLimiterTokenBucket, error) {
return withObservedInteraction(o.metric, "CurrentRateLimiterState", func() (evm_2_evm_offramp.RateLimiterTokenBucket, error) {
func (o *ObservedOffRampReader) CurrentRateLimiterState(ctx context.Context) (ccipdata.TokenBucketRateLimit, error) {
return withObservedInteraction(o.metric, "CurrentRateLimiterState", func() (ccipdata.TokenBucketRateLimit, error) {
return o.OffRampReader.CurrentRateLimiterState(ctx)
})
}
Expand Down
9 changes: 5 additions & 4 deletions core/services/ocr2/plugins/ccip/testhelpers/offramp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp"
mock_contracts "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/mocks"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
)

type FakeOffRamp struct {
*mock_contracts.EVM2EVMOffRampInterface

rateLimiterState evm_2_evm_offramp.RateLimiterTokenBucket
rateLimiterState ccipdata.TokenBucketRateLimit
senderNonces map[common.Address]uint64
tokenToPool map[common.Address]common.Address
dynamicConfig evm_2_evm_offramp.EVM2EVMOffRampDynamicConfig
Expand All @@ -34,11 +35,11 @@ func NewFakeOffRamp(t *testing.T) (*FakeOffRamp, common.Address) {
return offRamp, addr
}

func (o *FakeOffRamp) CurrentRateLimiterState(opts *bind.CallOpts) (evm_2_evm_offramp.RateLimiterTokenBucket, error) {
return getOffRampVal(o, func(o *FakeOffRamp) (evm_2_evm_offramp.RateLimiterTokenBucket, error) { return o.rateLimiterState, nil })
func (o *FakeOffRamp) CurrentRateLimiterState(opts *bind.CallOpts) (ccipdata.TokenBucketRateLimit, error) {
return getOffRampVal(o, func(o *FakeOffRamp) (ccipdata.TokenBucketRateLimit, error) { return o.rateLimiterState, nil })
}

func (o *FakeOffRamp) SetRateLimiterState(state evm_2_evm_offramp.RateLimiterTokenBucket) {
func (o *FakeOffRamp) SetRateLimiterState(state ccipdata.TokenBucketRateLimit) {
setOffRampVal(o, func(o *FakeOffRamp) { o.rateLimiterState = state })
}

Expand Down

0 comments on commit 1a4020f

Please sign in to comment.