Skip to content

Commit

Permalink
Assert price and gas updates in integration tests (#14916)
Browse files Browse the repository at this point in the history
* WIP Add ConfirmTokenPriceUpdated

* WIP Add ConfirmGasPriceUpdatedForAll

* Assert token prices in initial_deploy_test.go

* TokenPrice assertion with filters

* GasPrice assertions

* linting
  • Loading branch information
asoliman92 authored Oct 24, 2024
1 parent 77176d0 commit 9bcacfc
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func TestInitialDeploy(t *testing.T) {

// Wait for all commit reports to land.
ccdeploy.ConfirmCommitForAllWithExpectedSeqNums(t, e, state, expectedSeqNum, startBlocks)
// TODO: use proper assertions to check gas and token prices using events

// Confirm token and gas prices are updated
ccdeploy.ConfirmTokenPriceUpdatedForAll(t, e, state, startBlocks)
ccdeploy.ConfirmGasPriceUpdatedForAll(t, e, state, startBlocks)

// Wait for all exec reports to land
ccdeploy.ConfirmExecWithSeqNrForAll(t, e, state, expectedSeqNum, startBlocks)
Expand Down
123 changes: 123 additions & 0 deletions integration-tests/deployment/ccip/test_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package ccipdeployment

import (
"context"

"fmt"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/stretchr/testify/require"
Expand All @@ -14,9 +18,128 @@ import (
"github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"

"github.com/smartcontractkit/chainlink/integration-tests/deployment"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/fee_quoter"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/offramp"
)

func ConfirmGasPriceUpdatedForAll(
t *testing.T,
e deployment.Environment,
state CCIPOnChainState,
startBlocks map[uint64]*uint64,
) {
var wg errgroup.Group
for src, srcChain := range e.Chains {
for dest, dstChain := range e.Chains {
if src == dest {
continue
}
srcChain := srcChain
dstChain := dstChain
wg.Go(func() error {
var startBlock *uint64
if startBlocks != nil {
startBlock = startBlocks[srcChain.Selector]
}
return ConfirmGasPriceUpdated(
t,
dstChain,
state.Chains[srcChain.Selector].FeeQuoter,
*startBlock,
)
})
}
}
require.NoError(t, wg.Wait())
}

func ConfirmGasPriceUpdated(
t *testing.T,
dest deployment.Chain,
srcFeeQuoter *fee_quoter.FeeQuoter,
startBlock uint64,
) error {
it, err := srcFeeQuoter.FilterUsdPerUnitGasUpdated(&bind.FilterOpts{
Context: context.Background(),
Start: startBlock,
}, []uint64{dest.Selector})

require.NoError(t, err)
require.True(t, it.Next())
require.NotEqual(t, InitialGasPrice, it.Event.Value)
return nil
}

func ConfirmTokenPriceUpdatedForAll(
t *testing.T,
e deployment.Environment,
state CCIPOnChainState,
startBlocks map[uint64]*uint64,
) {
var wg errgroup.Group
for _, chain := range e.Chains {
chain := chain
wg.Go(func() error {
var startBlock *uint64
if startBlocks != nil {
startBlock = startBlocks[chain.Selector]
}
linkAddress := state.Chains[chain.Selector].LinkToken.Address()
wethAddress := state.Chains[chain.Selector].Weth9.Address()
tokenToPrice := make(map[common.Address]*big.Int)
tokenToPrice[linkAddress] = InitialLinkPrice
tokenToPrice[wethAddress] = InitialWethPrice
return ConfirmTokenPriceUpdated(
t,
chain,
state.Chains[chain.Selector].FeeQuoter,
*startBlock,
tokenToPrice,
)
})
}
require.NoError(t, wg.Wait())
}

func ConfirmTokenPriceUpdated(
t *testing.T,
chain deployment.Chain,
feeQuoter *fee_quoter.FeeQuoter,
startBlock uint64,
tokenToInitialPrice map[common.Address]*big.Int,
) error {
tokens := make([]common.Address, 0, len(tokenToInitialPrice))
for token := range tokenToInitialPrice {
tokens = append(tokens, token)
}
it, err := feeQuoter.FilterUsdPerTokenUpdated(&bind.FilterOpts{
Context: context.Background(),
Start: startBlock,
}, tokens)
require.NoError(t, err)
for it.Next() {
token := it.Event.Token
initialValue, ok := tokenToInitialPrice[token]
if ok {
require.Contains(t, tokens, token)
// Initial Value should be changed
require.NotEqual(t, initialValue, it.Event.Value)
}

// Remove the token from the map until we assert all tokens are updated
delete(tokenToInitialPrice, token)
if len(tokenToInitialPrice) == 0 {
return nil
}
}

if len(tokenToInitialPrice) > 0 {
return fmt.Errorf("Not all tokens updated on chain %d", chain.Selector)
}

return nil
}

// ConfirmCommitForAllWithExpectedSeqNums waits for all chains in the environment to commit the given expectedSeqNums.
// expectedSeqNums is a map of destinationchain selector to expected sequence number
// startBlocks is a map of destination chain selector to start block number to start watching from.
Expand Down

0 comments on commit 9bcacfc

Please sign in to comment.