Skip to content

Commit

Permalink
generate chain reader mock, fix tests (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
makramkd authored Aug 28, 2024
1 parent e7f42e1 commit 19e757f
Show file tree
Hide file tree
Showing 10 changed files with 631 additions and 180 deletions.
5 changes: 5 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ packages:
HomeChain:
CCIP:
TokenPrices:
github.com/smartcontractkit/chainlink-common/pkg/types:
interfaces:
ChainReader:
config:
dir: mocks/cl-common/chainreader
10 changes: 7 additions & 3 deletions execute/plugin_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/smartcontractkit/chainlink-ccip/internal/mocks"
"github.com/smartcontractkit/chainlink-ccip/internal/mocks/inmem"
"github.com/smartcontractkit/chainlink-ccip/internal/reader"
chainreadermocks "github.com/smartcontractkit/chainlink-ccip/mocks/cl-common/chainreader"
mock_types "github.com/smartcontractkit/chainlink-ccip/mocks/execute/exectypes"
"github.com/smartcontractkit/chainlink-ccip/pkg/consts"
"github.com/smartcontractkit/chainlink-ccip/pluginconfig"
Expand Down Expand Up @@ -90,8 +91,11 @@ type nodeSetup struct {
TokenDataReader *mock_types.MockTokenDataReader
}

func setupHomeChainPoller(lggr logger.Logger, chainConfigInfos []reader.ChainConfigInfo) reader.HomeChain {
homeChainReader := mocks.NewContractReaderMock()
func setupHomeChainPoller(
t *testing.T,
lggr logger.Logger,
chainConfigInfos []reader.ChainConfigInfo) reader.HomeChain {
homeChainReader := chainreadermocks.NewMockChainReader(t)
var firstCall = true
homeChainReader.On(
"GetLatestValue",
Expand Down Expand Up @@ -231,7 +235,7 @@ func setupSimpleTest(
},
}

homeChain := setupHomeChainPoller(lggr, chainConfigInfos)
homeChain := setupHomeChainPoller(t, lggr, chainConfigInfos)
err = homeChain.Start(ctx)
require.NoError(t, err, "failed to start home chain poller")

Expand Down
40 changes: 24 additions & 16 deletions execute/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/smartcontractkit/chainlink-ccip/execute/exectypes"
"github.com/smartcontractkit/chainlink-ccip/internal/libs/slicelib"
"github.com/smartcontractkit/chainlink-ccip/internal/plugincommon"
"github.com/smartcontractkit/chainlink-ccip/internal/reader"
codec_mocks "github.com/smartcontractkit/chainlink-ccip/mocks/execute/internal_/gen"
reader_mock "github.com/smartcontractkit/chainlink-ccip/mocks/internal_/reader"
"github.com/smartcontractkit/chainlink-ccip/pluginconfig"
Expand Down Expand Up @@ -209,16 +208,16 @@ func TestPlugin_ValidateObservation_SupportedChainsError(t *testing.T) {
func TestPlugin_ValidateObservation_IneligibleObserver(t *testing.T) {
lggr := logger.Test(t)

mockHomeChain := reader_mock.NewMockHomeChain(t)
mockHomeChain.EXPECT().GetSupportedChainsForPeer(mock.Anything).Return(mapset.NewSet[cciptypes.ChainSelector](), nil)
defer mockHomeChain.AssertExpectations(t)

p := &Plugin{
homeChain: setupHomeChainPoller(lggr, []reader.ChainConfigInfo{
{
ChainSelector: 0,
ChainConfig: reader.HomeChainConfigMapper{},
},
}),
homeChain: mockHomeChain,
oracleIDToP2pID: map[commontypes.OracleID]libocrtypes.PeerID{
0: {},
},
lggr: lggr,
}

observation := exectypes.NewObservation(nil, exectypes.MessageObservations{
Expand All @@ -242,13 +241,12 @@ func TestPlugin_ValidateObservation_IneligibleObserver(t *testing.T) {
func TestPlugin_ValidateObservation_ValidateObservedSeqNum_Error(t *testing.T) {
lggr := logger.Test(t)

mockHomeChain := reader_mock.NewMockHomeChain(t)
mockHomeChain.EXPECT().GetSupportedChainsForPeer(mock.Anything).Return(mapset.NewSet(cciptypes.ChainSelector(0)), nil)

p := &Plugin{
homeChain: setupHomeChainPoller(lggr, []reader.ChainConfigInfo{
{
ChainSelector: 1,
ChainConfig: reader.HomeChainConfigMapper{},
},
}),
lggr: lggr,
homeChain: mockHomeChain,
oracleIDToP2pID: map[commontypes.OracleID]libocrtypes.PeerID{
0: {},
},
Expand Down Expand Up @@ -283,8 +281,11 @@ func TestPlugin_Observation_BadPreviousOutcome(t *testing.T) {

func TestPlugin_Observation_EligibilityCheckFailure(t *testing.T) {
lggr := logger.Test(t)

mockHomeChain := reader_mock.NewMockHomeChain(t)

p := &Plugin{
homeChain: setupHomeChainPoller(lggr, []reader.ChainConfigInfo{}),
homeChain: mockHomeChain,
oracleIDToP2pID: map[commontypes.OracleID]libocrtypes.PeerID{},
lggr: lggr,
}
Expand Down Expand Up @@ -464,8 +465,10 @@ func TestPlugin_ShouldAcceptAttestedReport_ShouldAccept(t *testing.T) {

func TestPlugin_ShouldTransmitAcceptReport_ElegibilityCheckFailure(t *testing.T) {
lggr := logger.Test(t)

p := &Plugin{
homeChain: setupHomeChainPoller(lggr, []reader.ChainConfigInfo{}),
lggr: lggr,
homeChain: reader_mock.NewMockHomeChain(t),
oracleIDToP2pID: map[commontypes.OracleID]libocrtypes.PeerID{},
}

Expand All @@ -477,11 +480,16 @@ func TestPlugin_ShouldTransmitAcceptReport_ElegibilityCheckFailure(t *testing.T)

func TestPlugin_ShouldTransmitAcceptReport_Ineligible(t *testing.T) {
lggr, logs := logger.TestObserved(t, zapcore.DebugLevel)

mockHomeChain := reader_mock.NewMockHomeChain(t)
mockHomeChain.EXPECT().GetSupportedChainsForPeer(mock.Anything).Return(mapset.NewSet[cciptypes.ChainSelector](), nil)
defer mockHomeChain.AssertExpectations(t)

p := &Plugin{
lggr: lggr,
cfg: pluginconfig.ExecutePluginConfig{DestChain: 1},
reportingCfg: ocr3types.ReportingPluginConfig{OracleID: 2},
homeChain: setupHomeChainPoller(lggr, []reader.ChainConfigInfo{}),
homeChain: mockHomeChain,
oracleIDToP2pID: map[commontypes.OracleID]libocrtypes.PeerID{
2: {},
},
Expand Down
73 changes: 0 additions & 73 deletions internal/mocks/contract_reader.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/reader/ccip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/types"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"

"github.com/smartcontractkit/chainlink-ccip/internal/mocks"
chainreadermocks "github.com/smartcontractkit/chainlink-ccip/mocks/cl-common/chainreader"
"github.com/smartcontractkit/chainlink-ccip/pkg/consts"
)

func TestCCIPChainReader_getSourceChainsConfig(t *testing.T) {
sourceCRs := make(map[cciptypes.ChainSelector]*mocks.ContractReaderMock)
sourceCRs := make(map[cciptypes.ChainSelector]*chainreadermocks.MockChainReader)
for _, chain := range []cciptypes.ChainSelector{chainA, chainB} {
sourceCRs[chain] = mocks.NewContractReaderMock()
sourceCRs[chain] = chainreadermocks.NewMockChainReader(t)
}

destCR := mocks.NewContractReaderMock()
destCR := chainreadermocks.NewMockChainReader(t)

destCR.On(
"GetLatestValue",
Expand Down
9 changes: 5 additions & 4 deletions internal/reader/home_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
libocrtypes "github.com/smartcontractkit/libocr/ragep2p/types"

"github.com/smartcontractkit/chainlink-ccip/chainconfig"
"github.com/smartcontractkit/chainlink-ccip/internal/mocks"
"github.com/smartcontractkit/chainlink-ccip/pkg/consts"

"github.com/smartcontractkit/libocr/commontypes"
Expand All @@ -21,6 +20,8 @@ import (

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

chainreadermocks "github.com/smartcontractkit/chainlink-ccip/mocks/cl-common/chainreader"
)

var (
Expand All @@ -36,7 +37,7 @@ var (
)

func TestHomeChainConfigPoller_HealthReport(t *testing.T) {
homeChainReader := mocks.NewContractReaderMock()
homeChainReader := chainreadermocks.NewMockChainReader(t)
homeChainReader.On(
"GetLatestValue",
mock.Anything,
Expand Down Expand Up @@ -130,7 +131,7 @@ func Test_PollingWorking(t *testing.T) {
},
}

homeChainReader := mocks.NewContractReaderMock()
homeChainReader := chainreadermocks.NewMockChainReader(t)
homeChainReader.On(
"GetLatestValue",
mock.Anything,
Expand Down Expand Up @@ -185,7 +186,7 @@ func Test_PollingWorking(t *testing.T) {
func Test_HomeChainPoller_GetOCRConfig(t *testing.T) {
donID := uint32(1)
pluginType := uint8(1) // execution
homeChainReader := mocks.NewContractReaderMock()
homeChainReader := chainreadermocks.NewMockChainReader(t)
homeChainReader.On(
"GetLatestValue",
mock.Anything,
Expand Down
10 changes: 5 additions & 5 deletions internal/reader/onchain_prices_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

ocr2types "github.com/smartcontractkit/libocr/offchainreporting2plus/types"

commontyps "github.com/smartcontractkit/chainlink-common/pkg/types"
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"

"golang.org/x/sync/errgroup"
Expand All @@ -24,13 +24,13 @@ type TokenPrices interface {

type OnchainTokenPricesReader struct {
// Reader for the chain that will have the token prices on-chain
ContractReader commontyps.ContractReader
ContractReader commontypes.ContractReader
PriceSources map[ocr2types.Account]pluginconfig.ArbitrumPriceSource
TokenDecimals map[ocr2types.Account]uint8
}

func NewOnchainTokenPricesReader(
contractReader commontyps.ContractReader,
contractReader commontypes.ContractReader,
priceSources map[ocr2types.Account]pluginconfig.ArbitrumPriceSource,
tokenDecimals map[ocr2types.Account]uint8,
) *OnchainTokenPricesReader {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (pr *OnchainTokenPricesReader) getFeedDecimals(ctx context.Context, token o
consts.ContractNamePriceAggregator,
consts.MethodNameGetDecimals,
primitives.Unconfirmed,
nil,
map[string]any{},
&decimals,
//boundContract,
); err != nil {
Expand All @@ -124,7 +124,7 @@ func (pr *OnchainTokenPricesReader) getRawTokenPriceE18Normalized(
consts.ContractNamePriceAggregator,
consts.MethodNameGetLatestRoundData,
primitives.Unconfirmed,
nil,
map[string]any{},
&latestRoundData,
//boundContract,
); err != nil {
Expand Down
Loading

0 comments on commit 19e757f

Please sign in to comment.