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

[CCIP-5108] integration-tests/smoke/ccip: extract msging test case into pkg #16168

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions deployment/ccip/changeset/testhelpers/messagingtest/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package messagingtest

import (
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
chain_selectors "github.com/smartcontractkit/chain-selectors"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
"github.com/smartcontractkit/chainlink/deployment"
"github.com/smartcontractkit/chainlink/deployment/ccip/changeset"
"github.com/smartcontractkit/chainlink/deployment/ccip/changeset/testhelpers"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/onramp"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/router"
)

// Use this when testhelpers.DeployedEnv is available (usually in ephemeral test environments).
func NewTestSetupWithDeployedEnv(
t *testing.T,
depEnv testhelpers.DeployedEnv,
onchainState changeset.CCIPOnChainState,
sourceChain,
destChain uint64,
sender []byte,
testRouter,
validateResp bool,
) TestSetup {
return TestSetup{
T: t,
Sender: sender,
Env: depEnv.Env,
DeployedEnv: depEnv,
OnchainState: onchainState,
SourceChain: sourceChain,
DestChain: destChain,
TestRouter: testRouter,
ValidateResp: validateResp,
}
}

// Use this when testhelpers.DeployedEnv is not available (usually in long-running test environments like staging).
func NewTestSetup(
t *testing.T,
env deployment.Environment,
onchainState changeset.CCIPOnChainState,
sourceChain,
destChain uint64,
sender []byte,
testRouter,
validateResp bool,
) TestSetup {
return TestSetup{
T: t,
Sender: sender,
Env: env,
// no DeployedEnv
OnchainState: onchainState,
SourceChain: sourceChain,
DestChain: destChain,
TestRouter: testRouter,
ValidateResp: validateResp,
}
}

type TestSetup struct {
T *testing.T
Sender []byte
Env deployment.Environment
DeployedEnv testhelpers.DeployedEnv
OnchainState changeset.CCIPOnChainState
SourceChain uint64
DestChain uint64
TestRouter bool
ValidateResp bool
}

type TestCase struct {
TestSetup
Replayed bool
Nonce uint64
Receiver common.Address
MsgData []byte
ExtraArgs []byte
ExpectedExecutionState int
ExtraAssertions []func(t *testing.T)
}

type TestCaseOutput struct {
Replayed bool
Nonce uint64
MsgSentEvent *onramp.OnRampCCIPMessageSent
}

func sleepAndReplay(t *testing.T, e testhelpers.DeployedEnv, sourceChain, destChain uint64) {
time.Sleep(30 * time.Second)
replayBlocks := make(map[uint64]uint64)
replayBlocks[sourceChain] = 1
replayBlocks[destChain] = 1

testhelpers.ReplayLogs(t, e.Env.Offchain, replayBlocks)
}

func getLatestNonce(tc TestCase) uint64 {
family, err := chain_selectors.GetSelectorFamily(tc.DestChain)
require.NoError(tc.T, err)

var latestNonce uint64
switch family {
case chain_selectors.FamilyEVM:
latestNonce, err = tc.OnchainState.Chains[tc.DestChain].NonceManager.GetInboundNonce(&bind.CallOpts{
Context: tests.Context(tc.T),
}, tc.SourceChain, tc.Sender)
require.NoError(tc.T, err)
case chain_selectors.FamilySolana:
// var nonceCounterAccount ccip_router.Nonce
// err = common.GetAccountDataBorshInto(ctx, solanaGoClient, nonceEvmPDA, config.DefaultCommitment, &nonceCounterAccount)
// require.NoError(t, err, "failed to get account info")
// require.Equal(t, uint64(1), nonceCounterAccount.Counter)
}
return latestNonce
}

// Run runs a messaging test case.
func Run(tc TestCase) (out TestCaseOutput) {
// check latest nonce
latestNonce := getLatestNonce(tc)
require.Equal(tc.T, tc.Nonce, latestNonce)

startBlocks := make(map[uint64]*uint64)
msgSentEvent := testhelpers.TestSendRequest(
tc.T,
tc.Env,
tc.OnchainState,
tc.SourceChain,
tc.DestChain,
tc.TestRouter,
router.ClientEVM2AnyMessage{
Receiver: common.LeftPadBytes(tc.Receiver.Bytes(), 32),
Data: tc.MsgData,
TokenAmounts: nil,
FeeToken: common.HexToAddress("0x0"),
ExtraArgs: tc.ExtraArgs,
})
expectedSeqNum := map[testhelpers.SourceDestPair]uint64{
{
SourceChainSelector: tc.SourceChain,
DestChainSelector: tc.DestChain,
}: msgSentEvent.SequenceNumber,
}
expectedSeqNumExec := map[testhelpers.SourceDestPair][]uint64{
{
SourceChainSelector: tc.SourceChain,
DestChainSelector: tc.DestChain,
}: {msgSentEvent.SequenceNumber},
}
out.MsgSentEvent = msgSentEvent

// hack
if !tc.Replayed {
require.NotNil(tc.T, tc.DeployedEnv)
sleepAndReplay(tc.T, tc.DeployedEnv, tc.SourceChain, tc.DestChain)
out.Replayed = true
}

if tc.ValidateResp {
testhelpers.ConfirmCommitForAllWithExpectedSeqNums(tc.T, tc.Env, tc.OnchainState, expectedSeqNum, startBlocks)
execStates := testhelpers.ConfirmExecWithSeqNrsForAll(tc.T, tc.Env, tc.OnchainState, expectedSeqNumExec, startBlocks)

require.Equalf(
tc.T,
tc.ExpectedExecutionState,
execStates[testhelpers.SourceDestPair{
SourceChainSelector: tc.SourceChain,
DestChainSelector: tc.DestChain,
}][msgSentEvent.SequenceNumber],
"wrong execution state for seq nr %d, expected %d, got %d",
msgSentEvent.SequenceNumber,
tc.ExpectedExecutionState,
execStates[testhelpers.SourceDestPair{
SourceChainSelector: tc.SourceChain,
DestChainSelector: tc.DestChain,
}][msgSentEvent.SequenceNumber],
)

// check the sender latestNonce on the dest, should be incremented
latestNonce = getLatestNonce(tc)
require.Equal(tc.T, tc.Nonce+1, latestNonce)
out.Nonce = latestNonce
tc.T.Logf("confirmed nonce bump for sender %x, latestNonce %d", tc.Sender, latestNonce)

for _, assertion := range tc.ExtraAssertions {
assertion(tc.T)
}
} else {
tc.T.Logf("skipping validation of sent message")
}

return
}
Loading
Loading