Skip to content

Commit

Permalink
Fix a test, generate mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
connorwstein committed Oct 3, 2023
1 parent b877f2a commit 02b1958
Show file tree
Hide file tree
Showing 18 changed files with 963 additions and 246 deletions.
49 changes: 27 additions & 22 deletions core/services/ocr2/plugins/ccip/commit_inflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/commit_store"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipcalc"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

Expand All @@ -21,7 +21,7 @@ func TestCommitInflight(t *testing.T) {
c := newInflightCommitReportsContainer(time.Hour)

c.inFlightPriceUpdates = append(c.inFlightPriceUpdates, InflightPriceUpdate{
priceUpdates: commit_store.InternalPriceUpdates{DestChainSelector: 0}, // skipped when destChainSelector is 0
gasPrices: []ccipdata.GasPrice{{DestChainSelector: 0, Value: big.NewInt(0)}},
createdAt: time.Now(),
epochAndRound: ccipcalc.MergeEpochAndRound(2, 4),
})
Expand All @@ -36,7 +36,7 @@ func TestCommitInflight(t *testing.T) {

// Add a single report inflight
root1 := utils.Keccak256Fixed(hexutil.MustDecode("0xaa"))
require.NoError(t, c.add(lggr, commit_store.CommitStoreCommitReport{Interval: commit_store.CommitStoreInterval{Min: 1, Max: 2}, MerkleRoot: root1}, epochAndRound))
require.NoError(t, c.add(lggr, ccipdata.CommitStoreReport{Interval: ccipdata.CommitStoreInterval{Min: 1, Max: 2}, MerkleRoot: root1}, epochAndRound))
inflightUpdate, hasUpdate = c.getLatestInflightGasPriceUpdate()
assert.Equal(t, inflightUpdate, update{})
assert.False(t, hasUpdate)
Expand All @@ -45,18 +45,21 @@ func TestCommitInflight(t *testing.T) {

// Add another price report
root2 := utils.Keccak256Fixed(hexutil.MustDecode("0xab"))
require.NoError(t, c.add(lggr, commit_store.CommitStoreCommitReport{Interval: commit_store.CommitStoreInterval{Min: 3, Max: 4}, MerkleRoot: root2}, epochAndRound))
require.NoError(t, c.add(lggr, ccipdata.CommitStoreReport{Interval: ccipdata.CommitStoreInterval{Min: 3, Max: 4}, MerkleRoot: root2}, epochAndRound))
inflightUpdate, hasUpdate = c.getLatestInflightGasPriceUpdate()
assert.Equal(t, inflightUpdate, update{})
assert.False(t, hasUpdate)
assert.Equal(t, uint64(4), c.maxInflightSeqNr())
epochAndRound++

// Add gas price updates
require.NoError(t, c.add(lggr, commit_store.CommitStoreCommitReport{PriceUpdates: commit_store.InternalPriceUpdates{
DestChainSelector: uint64(1),
UsdPerUnitGas: big.NewInt(1),
}}, epochAndRound))
require.NoError(t, c.add(lggr, ccipdata.CommitStoreReport{
GasPrices: []ccipdata.GasPrice{
{
DestChainSelector: uint64(1),
Value: big.NewInt(1),
},
}}, epochAndRound))

inflightUpdate, hasUpdate = c.getLatestInflightGasPriceUpdate()
assert.Equal(t, big.NewInt(1), inflightUpdate.value)
Expand All @@ -66,27 +69,29 @@ func TestCommitInflight(t *testing.T) {

// Add a token price update
token := common.HexToAddress("0xa")
require.NoError(t, c.add(lggr, commit_store.CommitStoreCommitReport{PriceUpdates: commit_store.InternalPriceUpdates{
TokenPriceUpdates: []commit_store.InternalTokenPriceUpdate{
require.NoError(t, c.add(lggr, ccipdata.CommitStoreReport{
TokenPrices: []ccipdata.TokenPrice{
{
SourceToken: token,
UsdPerToken: big.NewInt(10),
Token: token,
Value: big.NewInt(10),
},
},
}}, epochAndRound))
}, epochAndRound))
// Apply cache price to existing
latestInflightTokenPriceUpdates := c.latestInflightTokenPriceUpdates()
require.Equal(t, len(latestInflightTokenPriceUpdates), 1)
assert.Equal(t, big.NewInt(10), latestInflightTokenPriceUpdates[token].value)

// larger epoch and round overrides existing price update
c.inFlightPriceUpdates = append(c.inFlightPriceUpdates, InflightPriceUpdate{
priceUpdates: commit_store.InternalPriceUpdates{
TokenPriceUpdates: []commit_store.InternalTokenPriceUpdate{
{SourceToken: token, UsdPerToken: big.NewInt(9999)},
tokenPrices: []ccipdata.TokenPrice{
{Token: token, Value: big.NewInt(9999)},
},
gasPrices: []ccipdata.GasPrice{
{
DestChainSelector: uint64(1),
Value: big.NewInt(999),
},
DestChainSelector: uint64(1),
UsdPerUnitGas: big.NewInt(999),
},
createdAt: time.Now(),
epochAndRound: ccipcalc.MergeEpochAndRound(999, 99),
Expand All @@ -101,22 +106,22 @@ func Test_inflightCommitReportsContainer_expire(t *testing.T) {
cacheExpiry: time.Minute,
inFlight: map[[32]byte]InflightCommitReport{
common.HexToHash("1"): {
report: commit_store.CommitStoreCommitReport{},
report: ccipdata.CommitStoreReport{},
createdAt: time.Now().Add(-5 * time.Minute),
},
common.HexToHash("2"): {
report: commit_store.CommitStoreCommitReport{},
report: ccipdata.CommitStoreReport{},
createdAt: time.Now().Add(-10 * time.Second),
},
},
inFlightPriceUpdates: []InflightPriceUpdate{
{
priceUpdates: commit_store.InternalPriceUpdates{DestChainSelector: 100},
gasPrices: []ccipdata.GasPrice{{DestChainSelector: 100, Value: big.NewInt(0)}},
createdAt: time.Now().Add(-PRICE_EXPIRY_MULTIPLIER * time.Minute),
epochAndRound: ccipcalc.MergeEpochAndRound(10, 5),
},
{
priceUpdates: commit_store.InternalPriceUpdates{DestChainSelector: 200},
gasPrices: []ccipdata.GasPrice{{DestChainSelector: 200, Value: big.NewInt(0)}},
createdAt: time.Now().Add(-PRICE_EXPIRY_MULTIPLIER * time.Second),
epochAndRound: ccipcalc.MergeEpochAndRound(20, 5),
},
Expand Down
71 changes: 0 additions & 71 deletions core/services/ocr2/plugins/ccip/commit_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@ import (
"context"
"fmt"
"strconv"
"sync"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
mocklp "github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller/mocks"
evmmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/mocks"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/commit_store"
mock_contracts "github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/mocks"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/testhelpers"
pipelinemocks "github.com/smartcontractkit/chainlink/v2/core/services/pipeline/mocks"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
Expand Down Expand Up @@ -88,66 +80,3 @@ func TestGetCommitPluginFilterNamesFromSpec(t *testing.T) {
}

}

func TestGetCommitPluginFilterNames(t *testing.T) {
onRampAddr := common.HexToAddress("0xdafea492d9c6733ae3d56b7ed1adb60692c98bc2")
priceRegAddr := common.HexToAddress("0xdafea492d9c6733ae3d56b7ed1adb60692c98bc3")
offRampAddr := common.HexToAddress("0xDAFeA492D9c6733Ae3D56b7eD1AdB60692C98BC4")

mockCommitStore, _ := testhelpers.NewFakeCommitStore(t, 1)
mockCommitStore.SetStaticConfig(commit_store.CommitStoreStaticConfig{OnRamp: onRampAddr})
mockCommitStore.SetDynamicConfig(commit_store.CommitStoreDynamicConfig{PriceRegistry: priceRegAddr})

srcLP := mocklp.NewLogPoller(t)
dstLP := mocklp.NewLogPoller(t)

dstLP.On("UnregisterFilter", "Commit price updates - 0xdafEa492d9C6733aE3D56b7eD1aDb60692c98bc3", mock.Anything).Return(nil)
dstLP.On("UnregisterFilter", "Fee token added - 0xdafEa492d9C6733aE3D56b7eD1aDb60692c98bc3", mock.Anything).Return(nil)
dstLP.On("UnregisterFilter", "Fee token removed - 0xdafEa492d9C6733aE3D56b7eD1aDb60692c98bc3", mock.Anything).Return(nil)
dstLP.On("UnregisterFilter", "Token pool added - 0xDAFeA492D9c6733Ae3D56b7eD1AdB60692C98BC4", mock.Anything).Return(nil)
dstLP.On("UnregisterFilter", "Token pool removed - 0xDAFeA492D9c6733Ae3D56b7eD1AdB60692C98BC4", mock.Anything).Return(nil)

err := unregisterCommitPluginFilters(context.Background(), dstLP, mockCommitStore, offRampAddr)
assert.NoError(t, err)

srcLP.AssertExpectations(t)
dstLP.AssertExpectations(t)
}

func Test_updateCommitPluginLogPollerFilters(t *testing.T) {
srcLP := &mocklp.LogPoller{}
dstLP := &mocklp.LogPoller{}

priceRegAddr := common.HexToAddress("0xdafea492d9c6733ae3d56b7ed1adb60692c98bc3")
offRampAddr := common.HexToAddress("0xDAFeA492D9c6733Ae3D56b7eD1AdB60692C98BC4")
offRamp := &mock_contracts.EVM2EVMOffRampInterface{}
offRamp.On("Address").Return(offRampAddr)

newDestFilters := getCommitPluginDestLpFilters(priceRegAddr, offRampAddr)

rf := &CommitReportingPluginFactory{
config: CommitPluginStaticConfig{
destLP: dstLP,
offRamp: offRamp,
},
destChainFilters: []logpoller.Filter{
{Name: "a"},
{Name: "b"},
},
filtersMu: &sync.Mutex{},
}

// make sure existing filters get unregistered
for _, f := range rf.destChainFilters {
dstLP.On("UnregisterFilter", f.Name, mock.Anything).Return(nil)
}
// make sure new filters are registered
for _, f := range newDestFilters {
dstLP.On("RegisterFilter", f).Return(nil)
}
err := rf.UpdateLogPollerFilters(priceRegAddr)
assert.NoError(t, err)

srcLP.AssertExpectations(t)
dstLP.AssertExpectations(t)
}
2 changes: 1 addition & 1 deletion core/services/ocr2/plugins/ccip/commit_reporting_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type CommitReportingPlugin struct {
// Dest
commitStoreReader ccipdata.CommitStoreReader
destPriceRegistryReader ccipdata.PriceRegistryReader
offchainConfig ccipdata.OffchainConfig
offchainConfig ccipdata.CommitOffchainConfig
tokenDecimalsCache cache.AutoSync[map[common.Address]uint8]
F int
// Offchain
Expand Down
Loading

0 comments on commit 02b1958

Please sign in to comment.