Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rfq] fix duplicate cctp configs #2801

Open
trajan0x opened this issue Jun 27, 2024 · 1 comment
Open

[rfq] fix duplicate cctp configs #2801

trajan0x opened this issue Jun 27, 2024 · 1 comment

Comments

@trajan0x
Copy link
Contributor

The current CCTP config duplication w/ rfq is pretty unusable. Currently:

  • omnirpc_url
  • signer
  • etc

are duplicated

Copy link

greptile-apps bot commented Jun 27, 2024

To resolve the issue of duplicate CCTP configs in RFQ, follow these steps:

  1. Create a Shared Configuration File: Create a new file shared_config.yml to hold common configurations.
# shared_config.yml
omnirpc_url: 'https://rpc.interop-staging.com'
signer:
  type: File
  file: /tmp/test-signer
  1. Update RFQ Configuration: Modify services/rfq/config.yml to include the shared configuration.
rfq_url: 'http://localhost:8080'
!include shared_config.yml
tokens:
  421614:
    - '0x858355dAe73bc0FA4AB220e5296cfAa77e99900e'
  11155111:
    - '0x858355dAe73bc0FA4AB220e5296cfAa77e99900e'
bridges:
  421614:
    address: '0x58dBd847C0CBFc9a4B9942847124BFC3589c70aA'
  11155111:
    address: '0x6EBdE77d635496Dea99364E0DA18ad8A1AFA9F27'
database:
  type: 'sqlite'
  dsn: '/tmp/relayer'
quotable_tokens:
  421614-0x858355dAe73bc0FA4AB220e5296cfAa77e99900e:
    - 11155111-0x858355dAe73bc0FA4AB220e5296cfAa77e99900e
  11155111-0x858355dAe73bc0FA4AB220e5296cfAa77e99900e:
    - 421614-0x858355dAe73bc0FA4AB220e5296cfAa77e99900e
  1. Update CCTP Configuration: Modify services/cctp-relayer/config/config.go to include the shared configuration.
package config

import (
  "context"
  "fmt"
  "os"
  "path/filepath"
  "github.com/ImVexed/fasturl"
  submitterConfig "github.com/synapsecns/sanguine/ethergo/submitter/config"
  "github.com/davecgh/go-spew/spew"
  "github.com/jftuga/ellipsis"
  ethConfig "github.com/synapsecns/sanguine/ethergo/signer/config"
  "github.com/synapsecns/sanguine/services/cctp-relayer/types"
  "gopkg.in/yaml.v2"
)

// Config is used to configure an Executor agent.
type Config struct {
  Port uint16 `yaml:"port"`
  Host string `yaml:"host"`
  CircleAPIURl string `yaml:"circle_api_url"`
  CCTPType string `yaml:"cctp_type"`
  Chains ChainConfigs `yaml:"chains"`
  BaseOmnirpcURL string `yaml:"base_omnirpc_url"`
  Signer ethConfig.SignerConfig `yaml:"unbonded_signer"`
  RetryIntervalMS int `yaml:"retry_interval_ms"`
  HTTPBackoffMaxElapsedTimeMs int `yaml:"http_backoff_max_elapsed_time_ms"`
  SubmitterConfig submitterConfig.Config `yaml:"submitter_config"`
  ScreenerAPIUrl string `yaml:"screener_api_url"`
}

func (c *Config) IsValid(ctx context.Context) (ok bool, err error) {
  if ok, err = c.Chains.IsValid(ctx); !ok {
    return false, err
  }
  if c.BaseOmnirpcURL == "" {
    return false, fmt.Errorf("rpc url cannot be empty")
  }
  if _, err := fasturl.ParseURL(c.BaseOmnirpcURL); err != nil {
    return false, fmt.Errorf("rpc url is invalid: %w", err)
  }
  if _, err := c.Chains.IsValid(ctx); err != nil {
    return false, fmt.Errorf(fmt.Errorf("could not validate chains: %w", err).Error())
  }
  if ok, err = c.Signer.IsValid(ctx); !ok {
    return false, fmt.Errorf("unbonded signer is invalid: %w", err)
  }
  return true, nil
}

func (c Config) Encode() ([]byte, error) {
  output, err := yaml.Marshal(&c)
  if err != nil {
    return nil, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(spew.Sdump(c), 20), err)
  }
  return output, nil
}

func DecodeConfig(filePath string) (cfg Config, err error) {
  input, err := os.ReadFile(filepath.Clean(filePath))
  if err != nil {
    return Config{}, fmt.Errorf("failed to read file: %w", err)
  }
  err = yaml.Unmarshal(input, &cfg)
  if err != nil {
    return Config{}, fmt.Errorf("could not unmarshall config %s: %w", ellipsis.Shorten(string(input), 30), err)
  }
  return cfg, nil
}

const defaultCCTPType = types.SynapseMessageType

func (c Config) GetCCTPType() (types.MessageType, error) {
  switch c.CCTPType {
  case "synapse":
    return types.SynapseMessageType, nil
  case "circle":
    return types.CircleMessageType, nil
  default:
    if len(c.CCTPType) == 0 {
      return defaultCCTPType, nil
    }
    return 0, fmt.Errorf("invalid cctp method: %s", c.CCTPType)
  }
}

func (c Config) GetChainConfig(chainID uint32) (cfg ChainConfig, err error) {
  for _, chainCfg := range c.Chains {
    if chainCfg.ChainID == chainID {
      return chainCfg, nil
    }
  }
  return cfg, fmt.Errorf("chain config not found for chain id: %d", chainID)
}
  1. Update Tests: Modify services/cctp-relayer/config/config_test.go to reflect the changes.
package config_test

import (
  "github.com/Flaque/filet"
  "github.com/brianvoe/gofakeit/v6"
  . "github.com/stretchr/testify/assert"
  "github.com/synapsecns/sanguine/ethergo/mocks"
  ethConfig "github.com/synapsecns/sanguine/ethergo/signer/config"
  "github.com/synapsecns/sanguine/ethergo/signer/wallet"
  "github.com/synapsecns/sanguine/services/cctp-relayer/config"
)

func configFixture(c ConfigSuite) config.Config {
  chainIDA := gofakeit.Uint32()
  chainIDB := chainIDA + 1
  testWallet, err := wallet.FromRandom()
  Nil(c.T(), err)
  return config.Config{
    Chains: config.ChainConfigs{
      config.ChainConfig{
        ChainID:     chainIDA,
        CCTPAddress: mocks.MockAddress().String(),
      },
      config.ChainConfig{
        ChainID:     chainIDB,
        CCTPAddress: mocks.MockAddress().String(),
      },
    },
    BaseOmnirpcURL: gofakeit.URL(),
    Signer: ethConfig.SignerConfig{
      Type: ethConfig.FileType.String(),
      File: filet.TmpFile(c.T(), "", testWallet.PrivateKeyHex()).Name(),
    },
    HTTPBackoffMaxElapsedTimeMs: int(gofakeit.Int64()),
  }
}

func (c ConfigSuite) TestConfigEncodeDecode() {
  testConfig := configFixture(c)
  encodedConfig, err := testConfig.Encode()
  Nil(c.T(), err)
  file := filet.TmpFile(c.T(), "", string(encodedConfig))
  decodedConfig, err := config.DecodeConfig(file.Name())
  Nil(c.T(), err)
  ok, err := decodedConfig.IsValid(c.GetTestContext())
  Nil(c.T(), err)
  True(c.T(), ok)
}

This will consolidate the duplicated configurations and ensure both RFQ and CCTP use the shared settings.

References

/services/rfq/config.yml
/services/cctp-relayer/config/config.go
/services/cctp-relayer/config/config_test.go

Ask Greptile · Edit Issue Bot Settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant