Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bukata-sa committed Feb 24, 2025
1 parent 50172a9 commit d295948
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 42 deletions.
4 changes: 2 additions & 2 deletions execute/plugin_tokendata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/smartcontractkit/chainlink-ccip/execute/exectypes"
"github.com/smartcontractkit/chainlink-ccip/internal/mocks/inmem"
"github.com/smartcontractkit/chainlink-ccip/pkg/ocrtypecodec"
ocrtypecodec "github.com/smartcontractkit/chainlink-ccip/pkg/ocrtypecodec/v1"
readerpkg "github.com/smartcontractkit/chainlink-ccip/pkg/reader"
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
)
Expand All @@ -33,7 +33,7 @@ func runRoundAndGetOutcome(ctx context.Context, ocrTypeCodec ocrtypecodec.ExecCo
}

func Test_LBTC_USDC_Transfer(t *testing.T) {
ocrTypeCodec := ocrtypecodec.NewExecCodecJSON()
ocrTypeCodec := ocrtypecodec.NewExecCodecProto()
ctx := tests.Context(t)

sourceChain := cciptypes.ChainSelector(sel.ETHEREUM_TESTNET_SEPOLIA.Selector)
Expand Down
2 changes: 2 additions & 0 deletions execute/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (it *IntTest) WithUSDC(
AttestationAPI: it.usdcServer.server.URL,
AttestationAPIInterval: commonconfig.MustNewDuration(1 * time.Millisecond),
AttestationAPITimeout: commonconfig.MustNewDuration(1 * time.Second),
AttestationAPICooldown: commonconfig.MustNewDuration(5 * time.Minute),
},
Tokens: map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig{
it.srcSelector: {
Expand Down Expand Up @@ -207,6 +208,7 @@ func (it *IntTest) WithLBTC(
AttestationAPI: it.lbtcServer.server.URL,
AttestationAPIInterval: commonconfig.MustNewDuration(1 * time.Millisecond),
AttestationAPITimeout: commonconfig.MustNewDuration(1 * time.Second),
AttestationAPICooldown: commonconfig.MustNewDuration(1 * time.Second),
},
SourcePoolAddressByChain: map[cciptypes.ChainSelector]string{
it.srcSelector: sourcePoolAddress,
Expand Down
8 changes: 3 additions & 5 deletions execute/tokendata/lbtc/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"maps"
"time"

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

Expand All @@ -19,9 +18,8 @@ import (
type AttestationStatus string

const (
apiVersion = "v1"
attestationPath = "deposits/getByHash"
defaultCoolDownDuration = 30 * time.Second
apiVersion = "v1"
attestationPath = "deposits/getByHash"

attestationStatusUnspecified AttestationStatus = "NOTARIZATION_STATUS_UNSPECIFIED"
attestationStatusFailed AttestationStatus = "NOTARIZATION_STATUS_FAILED"
Expand Down Expand Up @@ -62,7 +60,7 @@ func NewLBTCAttestationClient(
config.AttestationAPI,
config.AttestationAPIInterval.Duration(),
config.AttestationAPITimeout.Duration(),
defaultCoolDownDuration,
config.AttestationAPICooldown.Duration(),
)
if err != nil {
return nil, fmt.Errorf("get http client: %w", err)
Expand Down
1 change: 1 addition & 0 deletions execute/tokendata/lbtc/lbtc_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func Test_LBTC_Flow(t *testing.T) {
AttestationAPI: server.URL,
AttestationAPIInterval: commonconfig.MustNewDuration(1 * time.Microsecond),
AttestationAPITimeout: commonconfig.MustNewDuration(1 * time.Second),
AttestationAPICooldown: commonconfig.MustNewDuration(5 * time.Minute),
},
SourcePoolAddressByChain: map[cciptypes.ChainSelector]string{
bscChain: bscPool,
Expand Down
7 changes: 1 addition & 6 deletions execute/tokendata/usdc/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
"github.com/smartcontractkit/chainlink-common/pkg/logger"
Expand All @@ -24,10 +23,6 @@ const (
apiVersion = "v1"
attestationPath = "attestations"

// defaultCoolDownDurationSec defines the default time to wait after getting rate limited.
// this value is only used if the 429 response does not contain the Retry-After header
defaultCoolDownDuration = 5 * time.Minute

attestationStatusSuccess attestationStatus = "complete"
attestationStatusPending attestationStatus = "pending_confirmations"
)
Expand Down Expand Up @@ -93,7 +88,7 @@ func NewSequentialAttestationClient(
config.AttestationAPI,
config.AttestationAPIInterval.Duration(),
config.AttestationAPITimeout.Duration(),
defaultCoolDownDuration,
config.AttestationAPICooldown.Duration(),
)
if err != nil {
return nil, fmt.Errorf("create HTTP client: %w", err)
Expand Down
1 change: 1 addition & 0 deletions execute/tokendata/usdc/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func Test_AttestationClient(t *testing.T) {
AttestationAPI: server.URL,
AttestationAPIInterval: commonconfig.MustNewDuration(1 * time.Millisecond),
AttestationAPITimeout: commonconfig.MustNewDuration(5 * time.Second),
AttestationAPICooldown: commonconfig.MustNewDuration(5 * time.Minute),
},
},
)
Expand Down
1 change: 1 addition & 0 deletions execute/tokendata/usdc/usdc_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func Test_USDC_CCTP_Flow(t *testing.T) {
AttestationAPI: server.URL,
AttestationAPIInterval: commonconfig.MustNewDuration(1 * time.Microsecond),
AttestationAPITimeout: commonconfig.MustNewDuration(1 * time.Second),
AttestationAPICooldown: commonconfig.MustNewDuration(5 * time.Minute),
},
Tokens: map[cciptypes.ChainSelector]pluginconfig.USDCCCTPTokenConfig{
fujiChain: {
Expand Down
76 changes: 48 additions & 28 deletions pluginconfig/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ func (t *TokenDataObserverConfig) WellFormed() error {
// Validate checks that the observer's config is semantically correct - fields are set correctly
// depending on the config's type
func (t *TokenDataObserverConfig) Validate() error {
if err := t.WellFormed(); err != nil {
return err
}
if t.IsUSDC() {
if t.LBTCObserverConfig != nil {
return errors.New("LBTCObserverConfig must be null with USDC plugin type")
}
return t.USDCCCTPObserverConfig.Validate()
}
if t.IsLBTC() {
if t.USDCCCTPObserverConfig != nil {
return errors.New("USDCCCTPObserverConfig must be null with LBTC plugin type")
}
return t.LBTCObserverConfig.Validate()
}
return errors.New("unknown token data observer type " + t.Type)
Expand All @@ -98,31 +107,30 @@ func (t *TokenDataObserverConfig) IsLBTC() bool {
// It constructs raw map based on provided type. Custom marshaller is needed because default golang marshaller
// doesn't marshal clashing fields of pointer embeddings even if only one pointer is present and rest are set to nil
func (t *TokenDataObserverConfig) MarshalJSON() ([]byte, error) {
raw := map[string]interface{}{
"type": t.Type,
"version": t.Version,
}
var err error
var configJSONRaw []byte
switch t.Type {
case USDCCCTPHandlerType:
configJSONRaw, err = json.Marshal(t.USDCCCTPObserverConfig)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
Type string `json:"type"`
Version string `json:"version"`
*USDCCCTPObserverConfig
}{
Type: t.Type,
Version: t.Version,
USDCCCTPObserverConfig: t.USDCCCTPObserverConfig,
})
case LBTCHandlerType:
configJSONRaw, err = json.Marshal(t.LBTCObserverConfig)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
Type string `json:"type"`
Version string `json:"version"`
*LBTCObserverConfig
}{
Type: t.Type,
Version: t.Version,
LBTCObserverConfig: t.LBTCObserverConfig,
})
default:
return nil, fmt.Errorf("unknown token data observer type: %q", t.Type)
}
err = json.Unmarshal(configJSONRaw, &raw)
if err != nil {
return nil, err
}
return json.Marshal(raw)
}

// UnmarshalJSON is a custom JSON unmarshaller for TokenDataObserverConfig.
Expand Down Expand Up @@ -167,6 +175,9 @@ type AttestationConfig struct {
// AttestationAPIInterval defines the rate in requests per second that the attestation API can be called.
// Default set according to the APIs documentated 10 requests per second rate limit.
AttestationAPIInterval *commonconfig.Duration `json:"attestationAPIInterval"`
// AttestationAPICooldown defines in what time it is allowed to make next call to API.
// Activates when plugin hits API's rate limits
AttestationAPICooldown *commonconfig.Duration `json:"attestationAPICooldown"`
}

func (p *AttestationConfig) setDefaults() {
Expand Down Expand Up @@ -251,7 +262,14 @@ type USDCCCTPObserverConfig struct {
Tokens map[cciptypes.ChainSelector]USDCCCTPTokenConfig `json:"tokens"`
}

func (p *USDCCCTPObserverConfig) setDefaults() {
if p.AttestationAPICooldown == nil || p.AttestationAPICooldown.Duration() == 0 {
p.AttestationAPICooldown = commonconfig.MustNewDuration(5 * time.Minute)
}
}

func (p *USDCCCTPObserverConfig) Validate() error {
p.setDefaults()
err := p.AttestationConfig.Validate()
if err != nil {
return err
Expand All @@ -260,7 +278,6 @@ func (p *USDCCCTPObserverConfig) Validate() error {
if err != nil {
return err
}

if len(p.Tokens) == 0 {
return errors.New("Tokens not set")
}
Expand Down Expand Up @@ -299,20 +316,15 @@ type LBTCObserverConfig struct {
}

func (c *LBTCObserverConfig) setDefaults() {
if c.AttestationAPICooldown == nil || c.AttestationAPICooldown.Duration() == 0 {
c.AttestationAPICooldown = commonconfig.MustNewDuration(1 * time.Second)
}
if c.AttestationAPIBatchSize == 0 {
c.AttestationAPIBatchSize = 50
}
}

func (c *LBTCObserverConfig) Validate() error {
err := c.AttestationConfig.Validate()
if err != nil {
return err
}
err = c.WorkerConfig.Validate()
if err != nil {
return err
}
c.setDefaults()
if c.AttestationAPIBatchSize == 0 {
return errors.New("AttestationAPIBatchSize is not set")
Expand All @@ -325,5 +337,13 @@ func (c *LBTCObserverConfig) Validate() error {
return errors.New("SourcePoolAddressByChain is empty")
}
}
err := c.AttestationConfig.Validate()
if err != nil {
return err
}
err = c.WorkerConfig.Validate()
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit d295948

Please sign in to comment.