Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
AnieeG committed Apr 2, 2024
1 parent 44456c5 commit c8872f6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 85 deletions.
27 changes: 9 additions & 18 deletions integration-tests/ccip-tests/actions/ccip_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ type CCIPCommon struct {
FeeToken *contracts.LinkToken
BridgeTokens []*contracts.ERC20Token
PriceAggregators map[common.Address]*contracts.MockAggregator
RemoteChains []uint64
BridgeTokenPools []*contracts.TokenPool
RemoteChains *sync.Map
RateLimiterConfig contracts.RateLimiterConfig
ARMContract *common.Address
ARM *contracts.ARM // populate only if the ARM contracts is not a mock and can be used to verify various ARM events; keep this nil for mock ARM
Expand Down Expand Up @@ -212,15 +212,8 @@ func (ccipModule *CCIPCommon) SetRemoteChainsOnPools() error {
if ccipModule.ExistingDeployment {
return nil
}
var selectors []uint64
ccipModule.RemoteChains.Range(func(key, value any) bool {
if value != nil {
selectors = append(selectors, key.(uint64))
}
return true
})
for _, pool := range ccipModule.BridgeTokenPools {
err := pool.SetRemoteChainOnPool(selectors)
err := pool.SetRemoteChainOnPool(ccipModule.RemoteChains)
if err != nil {
return fmt.Errorf("error updating remote chain selectors %w", err)
}
Expand Down Expand Up @@ -509,6 +502,9 @@ func (ccipModule *CCIPCommon) WatchForPriceUpdates(ctx context.Context) error {
// UpdateTokenPricesAtRegularInterval updates aggregator contract with updated answer at regular interval.
// At each iteration of ticker it chooses one of the aggregator contracts and updates its round answer.
func (ccipModule *CCIPCommon) UpdateTokenPricesAtRegularInterval(ctx context.Context, interval time.Duration) {
if ccipModule.ExistingDeployment {
return
}
var aggregators []contracts.MockAggregator
for _, aggregatorContract := range ccipModule.PriceAggregators {
contract := *aggregatorContract
Expand Down Expand Up @@ -912,6 +908,10 @@ func (ccipModule *CCIPCommon) DeployContracts(noOfTokens int,
}

log.Info().Msg("finished deploying common contracts")
err = ccipModule.SetRemoteChainsOnPools()
if err != nil {
return fmt.Errorf("error setting remote chains %w", err)
}
// approve router to spend fee token
return ccipModule.ApproveTokens()
}
Expand Down Expand Up @@ -1069,7 +1069,6 @@ func DefaultCCIPModule(logger zerolog.Logger, chainClient blockchain.EVMClient,
Capacity: contracts.HundredCoins,
},
ExistingDeployment: existingDeployment,
RemoteChains: &sync.Map{},
MulticallEnabled: multiCall,
NoOfUSDCTokens: NoOfUSDCToken,
poolFunds: testhelpers.Link(5),
Expand Down Expand Up @@ -1240,10 +1239,6 @@ func (sourceCCIP *SourceCCIPModule) DeployContracts(lane *laneconfig.LaneConfig)
return fmt.Errorf("getting new onramp contractshouldn't fail %w", err)
}
}
if !sourceCCIP.Common.ExistingDeployment {
// update list of remote chains to add on the pools later
sourceCCIP.Common.RemoteChains.Store(sourceCCIP.DestChainSelector, true)
}
return nil
}

Expand Down Expand Up @@ -1707,10 +1702,6 @@ func (destCCIP *DestCCIPModule) DeployContracts(
return fmt.Errorf("getting new offramp shouldn't fail %w", err)
}
}
if !destCCIP.Common.ExistingDeployment {
// update list of remote chains to add on the pools later
destCCIP.Common.RemoteChains.Store(destCCIP.SourceChainSelector, true)
}
if destCCIP.ReceiverDapp == nil {
// ReceiverDapp
destCCIP.ReceiverDapp, err = contractDeployer.DeployReceiverDapp(false)
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/ccip-tests/load/ccip_loadgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ func NewCCIPLoad(t *testing.T, lane *actions.CCIPLane, timeout time.Duration, no
Dest: lane.Dest,
Reports: lane.Reports,
}
// This is to optimize memory space for load tests with high number of networks, lanes, tokens
lane.OptimizeStorage()

return &CCIPE2ELoad{
t: t,
Lane: loadLane,
Expand Down
1 change: 1 addition & 0 deletions integration-tests/ccip-tests/testconfig/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type CCIPTestConfig struct {
ExecOCRParams *contracts.OffChainAggregatorV2Config `toml:",omitempty"`
CommitInflightExpiry *config.Duration `toml:",omitempty"`
ExecInflightExpiry *config.Duration `toml:",omitempty"`
OptimizeSpace *bool `toml:",omitempty"`
}

func (c *CCIPTestConfig) SetTestRunName(name string) {
Expand Down
110 changes: 45 additions & 65 deletions integration-tests/ccip-tests/testsetups/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ type BiDirectionalLaneConfig struct {
type CCIPTestSetUpOutputs struct {
SetUpContext context.Context
Cfg *CCIPTestConfig
LaneContractsByNetwork sync.Map
LaneContractsByNetwork *sync.Map
laneMutex *sync.Mutex
Lanes []*BiDirectionalLaneConfig
Reporter *testreporters.CCIPTestReporter
Expand All @@ -360,11 +360,12 @@ func (o *CCIPTestSetUpOutputs) ReadLanes() []*BiDirectionalLaneConfig {
}

func (o *CCIPTestSetUpOutputs) DeployChainContracts(
lggr zerolog.Logger,
chainClient blockchain.EVMClient,
networkCfg blockchain.EVMNetwork,
noOfTokens int,
tokenDeployerFns []blockchain.ContractDeployer,
lggr zerolog.Logger,
selectors []uint64,
) error {
var k8Env *environment.Environment
ccipEnv := o.Env
Expand All @@ -390,7 +391,7 @@ func (o *CCIPTestSetUpOutputs) DeployChainContracts(
if err != nil {
return errors.WithStack(fmt.Errorf("failed to create ccip common module for %s: %w", networkCfg.Name, err))
}

ccipCommon.RemoteChains = selectors
cfg := o.LaneConfig.ReadLaneConfig(networkCfg.Name)

err = ccipCommon.DeployContracts(noOfTokens, tokenDeployerFns, cfg)
Expand All @@ -399,6 +400,14 @@ func (o *CCIPTestSetUpOutputs) DeployChainContracts(
}
ccipCommon.WriteLaneConfig(cfg)
o.LaneContractsByNetwork.Store(networkCfg.Name, cfg)
// if dynamic price update is required
if o.Cfg.TestGroupInput.DynamicPriceUpdateInterval != nil {
ccipCommon.UpdateTokenPricesAtRegularInterval(
testcontext.Get(o.Cfg.Test),
o.Cfg.TestGroupInput.DynamicPriceUpdateInterval.Duration(),
)
}

return nil
}

Expand Down Expand Up @@ -539,10 +548,11 @@ func (o *CCIPTestSetUpOutputs) AddLanesForNetworkPair(
allErrors.Store(multierr.Append(allErrors.Load(), fmt.Errorf("writing lane config for %s; err - %w", networkB.Name, errors.WithStack(err))))
return err
}
// no need for this anymore we copied it to CCIPTestSetUpOutputs' laneconfig
ccipLaneA2B.DstNetworkLaneCfg = nil
ccipLaneA2B.SrcNetworkLaneCfg = nil
lggr.Info().Msgf("done setting up lane %s to %s", networkA.Name, networkB.Name)
if pointer.GetBool(o.Cfg.TestGroupInput.OptimizeSpace) {
// This is to optimize memory space for load tests with high number of networks, lanes, tokens
ccipLaneA2B.OptimizeStorage()
}
return nil
})

Expand Down Expand Up @@ -572,10 +582,11 @@ func (o *CCIPTestSetUpOutputs) AddLanesForNetworkPair(
allErrors.Store(multierr.Append(allErrors.Load(), fmt.Errorf("writing lane config for %s; err - %w", networkB.Name, errors.WithStack(err))))
return err
}
// no need for this anymore we copied it to CCIPTestSetUpOutputs' laneconfig
ccipLaneB2A.DstNetworkLaneCfg = nil
ccipLaneB2A.SrcNetworkLaneCfg = nil
lggr.Info().Msgf("done setting up lane %s to %s", networkB.Name, networkA.Name)
if pointer.GetBool(o.Cfg.TestGroupInput.OptimizeSpace) {
// This is to optimize memory space for load tests with high number of networks, lanes, tokens
ccipLaneB2A.OptimizeStorage()
}
return nil
}
return nil
Expand Down Expand Up @@ -611,47 +622,6 @@ func (o *CCIPTestSetUpOutputs) StartEventWatchers() {
}
}

func (o *CCIPTestSetUpOutputs) SetupDynamicTokenPriceUpdates() {
interval := o.Cfg.TestGroupInput.DynamicPriceUpdateInterval.Duration()
covered := make(map[string]struct{})
for _, lanes := range o.ReadLanes() {
lane := lanes.ForwardLane
if _, exists := covered[lane.SourceNetworkName]; !exists {
covered[lane.SourceNetworkName] = struct{}{}
lane.Source.Common.UpdateTokenPricesAtRegularInterval(lane.Context, interval)
}
if _, exists := covered[lane.DestNetworkName]; !exists {
covered[lane.DestNetworkName] = struct{}{}
lane.Dest.Common.UpdateTokenPricesAtRegularInterval(lane.Context, interval)
}
}
}

func (o *CCIPTestSetUpOutputs) AddRemoteChainsToPools() {
ccipCommonByNetwork := make(map[string]*actions.CCIPCommon)
var allLanes []*actions.CCIPLane
for _, lanes := range o.ReadLanes() {
allLanes = append(allLanes, lanes.ForwardLane)
allLanes = append(allLanes, lanes.ReverseLane)
}
for _, lane := range allLanes {
if _, exists := ccipCommonByNetwork[lane.SourceNetworkName]; !exists {
ccipCommonByNetwork[lane.SourceNetworkName] = lane.Source.Common
}
if _, exists := ccipCommonByNetwork[lane.DestNetworkName]; !exists {
ccipCommonByNetwork[lane.DestNetworkName] = lane.Dest.Common
}
}
grp, _ := errgroup.WithContext(o.SetUpContext)
for _, cmn := range ccipCommonByNetwork {
cmn := cmn
grp.Go(func() error {
return cmn.SetRemoteChainsOnPools()
})
}
require.NoError(o.Cfg.Test, grp.Wait(), "error waiting for setting remote chains on pools")
}

func (o *CCIPTestSetUpOutputs) WaitForPriceUpdates() {
t := o.Cfg.Test
priceUpdateGrp, _ := errgroup.WithContext(o.SetUpContext)
Expand Down Expand Up @@ -731,14 +701,15 @@ func CCIPDefaultTestSetUp(
parent, cancel := context.WithCancel(context.Background())
defer cancel()
setUpArgs := &CCIPTestSetUpOutputs{
SetUpContext: parent,
Cfg: testConfig,
Reporter: testreporters.NewCCIPTestReporter(t, lggr),
LaneConfigFile: filename,
Balance: actions.NewBalanceSheet(),
BootstrapAdded: atomic.NewBool(false),
JobAddGrp: &errgroup.Group{},
laneMutex: &sync.Mutex{},
SetUpContext: parent,
Cfg: testConfig,
Reporter: testreporters.NewCCIPTestReporter(t, lggr),
LaneConfigFile: filename,
LaneContractsByNetwork: &sync.Map{},
Balance: actions.NewBalanceSheet(),
BootstrapAdded: atomic.NewBool(false),
JobAddGrp: &errgroup.Group{},
laneMutex: &sync.Mutex{},
}

chainByChainID := setUpArgs.CreateEnvironment(lggr, envName)
Expand Down Expand Up @@ -785,13 +756,26 @@ func CCIPDefaultTestSetUp(
// deploy all chain specific common contracts
chainAddGrp, _ := errgroup.WithContext(setUpArgs.SetUpContext)
lggr.Info().Msg("Deploying common contracts")
chainSelectors := make(map[int64]uint64)
for _, net := range testConfig.AllNetworks {
if _, exists := chainSelectors[net.ChainID]; !exists {
chainSelectors[net.ChainID], err = chainselectors.SelectorFromChainId(uint64(net.ChainID))
require.NoError(t, err)
}
}
for _, net := range testConfig.AllNetworks {
chain := chainByChainID[net.ChainID]
net := net
net.HTTPURLs = chain.GetNetworkConfig().HTTPURLs
net.URLs = chain.GetNetworkConfig().URLs
var selectors []uint64
for chainId, selector := range chainSelectors {
if chainId != net.ChainID {
selectors = append(selectors, selector)
}
}
chainAddGrp.Go(func() error {
return setUpArgs.DeployChainContracts(chain, net, testConfig.TestGroupInput.NoOfTokensPerChain, tokenDeployerFns, lggr)
return setUpArgs.DeployChainContracts(lggr, chain, net, testConfig.TestGroupInput.NoOfTokensPerChain, tokenDeployerFns, selectors)
})
}
require.NoError(t, chainAddGrp.Wait(), "Deploying common contracts shouldn't fail")
Expand Down Expand Up @@ -848,19 +832,15 @@ func CCIPDefaultTestSetUp(
require.NoError(t, err)
require.Equal(t, len(setUpArgs.Lanes), len(testConfig.NetworkPairs),
"Number of bi-directional lanes should be equal to number of network pairs")
// only required for env set up
setUpArgs.LaneContractsByNetwork = nil

if configureCLNode {
// add all remote chains to pools
setUpArgs.AddRemoteChainsToPools()
// wait for all jobs to get created
lggr.Info().Msg("Waiting for jobs to be created")
require.NoError(t, setUpArgs.JobAddGrp.Wait(), "Creating jobs shouldn't fail")
// wait for price updates to be available
setUpArgs.WaitForPriceUpdates()
// if dynamic price update is required
if setUpArgs.Cfg.TestGroupInput.DynamicPriceUpdateInterval != nil {
setUpArgs.SetupDynamicTokenPriceUpdates()
}
}

// start event watchers for all lanes
Expand Down

0 comments on commit c8872f6

Please sign in to comment.