Skip to content

Commit

Permalink
Clean up after test to recover CCIP fees paid (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnieeG authored Sep 27, 2023
1 parent 1f14684 commit 36d0e52
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 18 deletions.
37 changes: 34 additions & 3 deletions integration-tests/ccip-tests/actions/ccip_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,26 @@ type SourceCCIPModule struct {
NewFinalizedBlockTimestamp atomic.Time
}

func (sourceCCIP *SourceCCIPModule) PayCCIPFeeToOwnerAddress() error {
isNativeFee := sourceCCIP.Common.FeeToken.EthAddress == common.HexToAddress("0x0")
if isNativeFee {
err := sourceCCIP.OnRamp.WithdrawNonLinkFees(sourceCCIP.Common.WrappedNative)
if err != nil {
return err
}
} else {
err := sourceCCIP.OnRamp.SetNops()
if err != nil {
return err
}
err = sourceCCIP.OnRamp.PayNops()
if err != nil {
return err
}
}
return nil
}

func (sourceCCIP *SourceCCIPModule) LoadContracts(conf *laneconfig.LaneConfig) {
if conf != nil {
cfg, ok := conf.SrcContracts[sourceCCIP.DestNetworkName]
Expand All @@ -609,6 +629,7 @@ func (sourceCCIP *SourceCCIPModule) DeployContracts(lane *laneconfig.LaneConfig)
if err != nil {
return err
}

sourceCCIP.LoadContracts(lane)
// update transfer amount array length to be equal to the number of tokens
// each index in TransferAmount array corresponds to the amount to be transferred for the token at the same index in BridgeTokens array
Expand Down Expand Up @@ -1711,7 +1732,7 @@ func (lane *CCIPLane) StartEventWatchers() error {
return nil
}

func (lane *CCIPLane) CleanUp() {
func (lane *CCIPLane) CleanUp(clearFees bool) error {
lane.Logger.Info().Msg("Cleaning up lane")
if !lane.Source.Common.ChainClient.NetworkSimulated() &&
lane.Source.Common.ChainClient.GetNetworkConfig().FinalityDepth == 0 {
Expand All @@ -1720,8 +1741,18 @@ func (lane *CCIPLane) CleanUp() {
for _, sub := range lane.Subscriptions {
sub.Unsubscribe()
}
require.NoError(lane.Test, lane.DestChain.Close())
require.NoError(lane.Test, lane.SourceChain.Close())
// recover fees from onRamp contract
if clearFees && !lane.Source.Common.ChainClient.NetworkSimulated() {
err := lane.Source.PayCCIPFeeToOwnerAddress()
if err != nil {
return err
}
}
err := lane.Dest.Common.ChainClient.Close()
if err != nil {
return err
}
return lane.Source.Common.ChainClient.Close()
}

// DeployNewCCIPLane sets up a lane and initiates lane.Source and lane.Destination
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/ccip-tests/chaos/ccip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@ func TestChaosCCIP(t *testing.T) {
require.NoError(t, err)
t.Cleanup(func() {
if chaosId != "" {
testEnvironment.Chaos.Stop(chaosId)
require.NoError(t, testEnvironment.Chaos.Stop(chaosId))
}
tearDown()
require.NoError(t, tearDown())
})
lane.RecordStateBeforeTransfer()
// Now send the ccip-request while the chaos is at play
err = lane.SendRequests(numOfRequests, testCfg.MsgType)
require.NoError(t, err)
if in.waitForChaosRecovery {
// wait for chaos to be recovered before further validation
testEnvironment.Chaos.WaitForAllRecovered(chaosId, 1*time.Minute)
require.NoError(t, testEnvironment.Chaos.WaitForAllRecovered(chaosId, 1*time.Minute))
} else {
l.Info().Msg("proceeding without waiting for chaos recovery")
}
Expand Down
42 changes: 42 additions & 0 deletions integration-tests/ccip-tests/contracts/contract_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,48 @@ func (onRamp *OnRamp) Address() string {
return onRamp.EthAddress.Hex()
}

func (onRamp *OnRamp) SetNops() error {
opts, err := onRamp.client.TransactionOpts(onRamp.client.GetDefaultWallet())
if err != nil {
return err
}
owner := common.HexToAddress(onRamp.client.GetDefaultWallet().Address())
// set the payee to the default wallet
tx, err := onRamp.Instance.SetNops(opts, []evm_2_evm_onramp.EVM2EVMOnRampNopAndWeight{{
Nop: owner,
Weight: 1,
}})
if err != nil {
return err
}
return onRamp.client.ProcessTransaction(tx)
}

func (onRamp *OnRamp) PayNops() error {
opts, err := onRamp.client.TransactionOpts(onRamp.client.GetDefaultWallet())
if err != nil {
return err
}
tx, err := onRamp.Instance.PayNops(opts)
if err != nil {
return err
}
return onRamp.client.ProcessTransaction(tx)
}

func (onRamp *OnRamp) WithdrawNonLinkFees(wrappedNative common.Address) error {
opts, err := onRamp.client.TransactionOpts(onRamp.client.GetDefaultWallet())
if err != nil {
return err
}
owner := common.HexToAddress(onRamp.client.GetDefaultWallet().Address())
tx, err := onRamp.Instance.WithdrawNonLinkFees(opts, wrappedNative, owner)
if err != nil {
return err
}
return onRamp.client.ProcessTransaction(tx)
}

func (onRamp *OnRamp) SetRateLimit(rlConfig evm_2_evm_onramp.RateLimiterConfig) error {
opts, err := onRamp.client.TransactionOpts(onRamp.client.GetDefaultWallet())
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@
"Base Goerli-1": {
"is_mock_arm": true,
"fee_token": "0xD886E2286Fd1073df82462ea1822119600Af80b6",
"is_native_fee_token" : true,
"bridge_tokens": null,
"bridge_tokens_pools": null,
"arm": "0x1E5b6ce8EB1071B2B1686ddd241fE6a3c07560DE",
Expand Down Expand Up @@ -1123,6 +1124,7 @@
"Base Goerli-2": {
"is_mock_arm": true,
"fee_token": "0xD886E2286Fd1073df82462ea1822119600Af80b6",
"is_native_fee_token" : true,
"bridge_tokens": null,
"bridge_tokens_pools": null,
"arm": "0xE42597d226Eab595893f1ad1B0d7F58d7F1Cc375",
Expand Down Expand Up @@ -1182,6 +1184,7 @@
"Base Goerli-3": {
"is_mock_arm": true,
"fee_token": "0xD886E2286Fd1073df82462ea1822119600Af80b6",
"is_native_fee_token" : true,
"bridge_tokens": null,
"bridge_tokens_pools": null,
"arm": "0xE42597d226Eab595893f1ad1B0d7F58d7F1Cc375",
Expand Down Expand Up @@ -1241,6 +1244,7 @@
"Base Goerli-4": {
"is_mock_arm": true,
"fee_token": "0xD886E2286Fd1073df82462ea1822119600Af80b6",
"is_native_fee_token" : true,
"bridge_tokens": null,
"bridge_tokens_pools": null,
"arm": "0xE42597d226Eab595893f1ad1B0d7F58d7F1Cc375",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ func (l *Lanes) CopyCommonContracts(fromNetwork, toNetwork string, reuse, isToke
DestContractsMu: &sync.Mutex{},
DestContracts: make(map[string]DestContracts),
CommonContracts: CommonContracts{
FeeToken: existing.FeeToken,
WrappedNative: existing.WrappedNative,
FeeToken: existing.FeeToken,
WrappedNative: existing.WrappedNative,
IsNativeFeeToken: existing.IsNativeFeeToken,
},
}
// if reuse is set to true, it copies all the common contracts except the router
Expand Down
7 changes: 4 additions & 3 deletions integration-tests/ccip-tests/load/ccip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/smartcontractkit/chainlink-env/chaos"
a "github.com/smartcontractkit/chainlink-env/pkg/alias"
"github.com/smartcontractkit/chainlink-testing-framework/logging"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/integration-tests/ccip-tests/actions"
)
Expand All @@ -24,7 +25,7 @@ func TestLoadCCIPStableRPS(t *testing.T) {
}
t.Cleanup(func() {
log.Info().Msg("Tearing down the environment")
testArgs.TestSetupArgs.TearDown()
require.NoError(t, testArgs.TestSetupArgs.TearDown())
})
testArgs.TriggerLoad()
testArgs.Wait()
Expand All @@ -46,7 +47,7 @@ func TestLoadCCIPSequentialLaneAdd(t *testing.T) {
}
t.Cleanup(func() {
log.Info().Msg("Tearing down the environment")
testArgs.TestSetupArgs.TearDown()
require.NoError(t, testArgs.TestSetupArgs.TearDown())
})
testArgs.TriggerLoad()
testArgs.AddMoreLanesToRun()
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestLoadCCIPStableRequestTriggeringWithPodChaos(t *testing.T) {
}
t.Cleanup(func() {
log.Info().Msg("Tearing down the environment")
testArgs.TestSetupArgs.TearDown()
require.NoError(t, testArgs.TestSetupArgs.TearDown())
})
testArgs.SanityCheck()
testArgs.TriggerLoad()
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/ccip-tests/load/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (l *loadArgs) TearDown() {
for i := range l.ccipLoad {
l.ccipLoad[i].ReportAcceptedLog()
}
l.TestSetupArgs.TearDown()
require.NoError(l.t, l.TestSetupArgs.TearDown())
}
}

Expand Down
5 changes: 3 additions & 2 deletions integration-tests/ccip-tests/smoke/ccip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestSmokeCCIPForBidirectionalLane(t *testing.T) {
if TestCfg.MsgType == actions.TokenTransfer {
setUpOutput.Balance.Verify(t)
}
require.NoError(t, setUpOutput.TearDown())
})
for i := range setUpOutput.Lanes {
tcs = append(tcs, subtestInput{
Expand All @@ -50,7 +51,7 @@ func TestSmokeCCIPForBidirectionalLane(t *testing.T) {
})
}
}

l.Info().Int("Total Lanes", len(tcs)).Msg("Starting CCIP test")
for _, testcase := range tcs {
tc := testcase
t.Run(tc.testName, func(t *testing.T) {
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestSmokeCCIPRateLimit(t *testing.T) {
}

t.Cleanup(func() {
setUpOutput.TearDown()
require.NoError(t, setUpOutput.TearDown())
})

for i := range setUpOutput.Lanes {
Expand Down
18 changes: 14 additions & 4 deletions integration-tests/ccip-tests/testsetups/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ type CCIPTestSetUpOutputs struct {
Reporter *testreporters.CCIPTestReporter
LaneConfigFile string
LaneConfig *laneconfig.Lanes
TearDown func()
TearDown func() error
Env *actions.CCIPTestEnv
Balance *actions.BalanceSheet
BootstrapAdded *atomic.Bool
Expand Down Expand Up @@ -1081,13 +1081,23 @@ func CCIPDefaultTestSetUp(
// start event watchers for all lanes
setUpArgs.StartEventWatchers()

setUpArgs.TearDown = func() {
setUpArgs.TearDown = func() error {
var errs error
for _, lanes := range setUpArgs.Lanes {
lanes.ForwardLane.CleanUp()
// if existing deployment is true, don't attempt to pay ccip fees
err := lanes.ForwardLane.CleanUp(!setUpArgs.Cfg.ExistingDeployment)
if err != nil {
errs = multierr.Append(errs, err)
}
if lanes.ReverseLane != nil {
lanes.ReverseLane.CleanUp()
// if existing deployment is true, don't attempt to pay ccip fees
err := lanes.ReverseLane.CleanUp(!setUpArgs.Cfg.ExistingDeployment)
if err != nil {
errs = multierr.Append(errs, err)
}
}
}
return errs
}
lggr.Info().Msg("Test setup completed")
return setUpArgs
Expand Down

0 comments on commit 36d0e52

Please sign in to comment.