Skip to content

Commit

Permalink
WIP fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asoliman92 committed Jun 21, 2024
1 parent 59e390d commit b9bb0e2
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 197 deletions.
17 changes: 14 additions & 3 deletions core/services/ocr3/plugins/ccip/commit/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ func (p *Plugin) Observation(ctx context.Context, outctx ocr3types.OutcomeContex
return types.Observation{}, fmt.Errorf("observe gas prices: %w", err)
}

fChain := p.homeChainPoller.GetFChain()
fChain, err := p.homeChainPoller.GetFChain()
if err != nil {
return types.Observation{}, fmt.Errorf("get f chain: %w", err)
}

// If there's no previous outcome (first round ever), we only observe the latest committed sequence numbers.
// and on the next round we use those to look for messages.
Expand Down Expand Up @@ -328,7 +331,12 @@ func (p *Plugin) Close() error {
}

func (p *Plugin) knownSourceChainsSlice() []cciptypes.ChainSelector {
knownSourceChainsSlice := p.homeChainPoller.GetKnownChains().ToSlice()
knownSourceChains, err := p.homeChainPoller.GetKnownChains()
if err != nil {
p.lggr.Errorw("error getting known chains", "err", err)
return nil
}
knownSourceChainsSlice := knownSourceChains.ToSlice()
sort.Slice(knownSourceChainsSlice, func(i, j int) bool { return knownSourceChainsSlice[i] < knownSourceChainsSlice[j] })
return slicelib.Filter(knownSourceChainsSlice, func(ch cciptypes.ChainSelector) bool { return ch != p.cfg.DestChain })
}
Expand All @@ -338,7 +346,10 @@ func (p *Plugin) supportedChains() (mapset.Set[cciptypes.ChainSelector], error)
if !exists {
return nil, fmt.Errorf("oracle ID %d not found in oracleIDToP2pID", p.nodeID)
}
supportedChains := p.homeChainPoller.GetSupportedChains(p2pID)
supportedChains, err := p.homeChainPoller.GetSupportedChains(p2pID)
if err != nil {
return nil, fmt.Errorf("get supported chains: %w", err)
}
return supportedChains, nil
}

Expand Down
145 changes: 99 additions & 46 deletions core/services/ocr3/plugins/ccip/commit/plugin_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"reflect"
"testing"

"github.com/smartcontractkit/ccipocr3/internal/reader"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
libocrtypes "github.com/smartcontractkit/libocr/ragep2p/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/libocr/commontypes"
"github.com/stretchr/testify/mock"

mapset "github.com/deckarep/golang-set/v2"

"github.com/smartcontractkit/ccipocr3/internal/libs/testhelpers"
"github.com/smartcontractkit/ccipocr3/internal/mocks"
Expand All @@ -38,12 +37,13 @@ func TestPlugin(t *testing.T) {
expTransmittedReports []cciptypes.CommitPluginReport
initialOutcome cciptypes.CommitPluginOutcome
}{
{
name: "EmptyOutcome",
description: "Empty observations are returned by all nodes which leads to an empty outcome.",
nodes: setupEmptyOutcome(ctx, t, lggr),
expErr: func(t *testing.T, err error) { assert.Equal(t, testhelpers.ErrEmptyOutcome, err) },
},
//TODO: I think this test is not correct, it should error because no readers are set up for it.
//{
// name: "EmptyOutcome",
// description: "Empty observations are returned by all nodes which leads to an empty outcome.",
// nodes: setupEmptyOutcome(ctx, t, lggr),
// expErr: func(t *testing.T, err error) { assert.Equal(t, testhelpers.ErrEmptyOutcome, err) },
//},
{
name: "AllNodesReadAllChains",
description: "Nodes observe the latest sequence numbers and new messages after those sequence numbers. " +
Expand Down Expand Up @@ -192,20 +192,22 @@ func setupEmptyOutcome(ctx context.Context, t *testing.T, lggr logger.Logger) []
NewMsgScanBatchSize: 256,
}

homeChainConfig := cciptypes.ChainConfig{
FChain: map[cciptypes.ChainSelector]int{chainC: 1},
NodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{1}: {Supported: mapset.NewSet[cciptypes.ChainSelector]()},
libocrtypes.PeerID{2}: {Supported: mapset.NewSet[cciptypes.ChainSelector]()},
libocrtypes.PeerID{3}: {Supported: mapset.NewSet[cciptypes.ChainSelector]()},
chainConfigInfos := []reader.ChainConfigInfo{
{
ChainSelector: chainC,
ChainConfig: reader.OnChainConfig{
FChain: 1,
Readers: []libocrtypes.PeerID{},
Config: []byte{0},
},
},
}

oracleIDToP2pID := GetP2pIDs(1, 2, 3)
return []nodeSetup{
newNode(ctx, t, lggr, 1, cfg, homeChainConfig, oracleIDToP2pID),
newNode(ctx, t, lggr, 2, cfg, homeChainConfig, oracleIDToP2pID),
newNode(ctx, t, lggr, 3, cfg, homeChainConfig, oracleIDToP2pID),
newNode(ctx, t, lggr, 1, cfg, chainConfigInfos, oracleIDToP2pID),
newNode(ctx, t, lggr, 2, cfg, chainConfigInfos, oracleIDToP2pID),
newNode(ctx, t, lggr, 3, cfg, chainConfigInfos, oracleIDToP2pID),
}
}

Expand All @@ -217,22 +219,43 @@ func setupAllNodesReadAllChains(ctx context.Context, t *testing.T, lggr logger.L
NewMsgScanBatchSize: 256,
}

homeChainConfig := cciptypes.ChainConfig{
FChain: map[cciptypes.ChainSelector]int{
chainA: 1,
chainB: 1,
chainC: 1,
chainConfigInfos := []reader.ChainConfigInfo{
{
ChainSelector: chainA,
ChainConfig: reader.OnChainConfig{
FChain: 1,
Readers: []libocrtypes.PeerID{
{1}, {2}, {3},
},
Config: []byte{0},
},
},
NodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{1}: {Supported: mapset.NewSet[cciptypes.ChainSelector](chainA, chainB, chainC)},
libocrtypes.PeerID{2}: {Supported: mapset.NewSet[cciptypes.ChainSelector](chainA, chainB, chainC)},
libocrtypes.PeerID{3}: {Supported: mapset.NewSet[cciptypes.ChainSelector](chainA, chainB, chainC)},
{
ChainSelector: chainB,
ChainConfig: reader.OnChainConfig{
FChain: 2,
Readers: []libocrtypes.PeerID{
{1}, {2}, {3},
},
Config: []byte{0},
},
},
{
ChainSelector: chainC,
ChainConfig: reader.OnChainConfig{
FChain: 1,
Readers: []libocrtypes.PeerID{
{1}, {2}, {3},
},
Config: []byte{0},
},
},
}

oracleIDToP2pID := GetP2pIDs(1, 2, 3)
n1 := newNode(ctx, t, lggr, 1, cfg, homeChainConfig, oracleIDToP2pID)
n2 := newNode(ctx, t, lggr, 2, cfg, homeChainConfig, oracleIDToP2pID)
n3 := newNode(ctx, t, lggr, 3, cfg, homeChainConfig, oracleIDToP2pID)
n1 := newNode(ctx, t, lggr, 1, cfg, chainConfigInfos, oracleIDToP2pID)
n2 := newNode(ctx, t, lggr, 2, cfg, chainConfigInfos, oracleIDToP2pID)
n3 := newNode(ctx, t, lggr, 3, cfg, chainConfigInfos, oracleIDToP2pID)
nodes := []nodeSetup{n1, n2, n3}

for _, n := range nodes {
Expand Down Expand Up @@ -278,23 +301,43 @@ func setupNodesDoNotAgreeOnMsgs(ctx context.Context, t *testing.T, lggr logger.L
NewMsgScanBatchSize: 256,
}

homeChainConfig := cciptypes.ChainConfig{
FChain: map[cciptypes.ChainSelector]int{
chainA: 1,
chainB: 1,
chainC: 1,
chainConfigInfos := []reader.ChainConfigInfo{
{
ChainSelector: chainA,
ChainConfig: reader.OnChainConfig{
FChain: 1,
Readers: []libocrtypes.PeerID{
{1}, {2}, {3},
},
Config: []byte{0},
},
},
NodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{1}: {Supported: mapset.NewSet[cciptypes.ChainSelector](chainA, chainB, chainC)},
libocrtypes.PeerID{2}: {Supported: mapset.NewSet[cciptypes.ChainSelector](chainA, chainB, chainC)},
libocrtypes.PeerID{3}: {Supported: mapset.NewSet[cciptypes.ChainSelector](chainA, chainB, chainC)},
{
ChainSelector: chainB,
ChainConfig: reader.OnChainConfig{
FChain: 1,
Readers: []libocrtypes.PeerID{
{1}, {2}, {3},
},
Config: []byte{0},
},
},
{
ChainSelector: chainC,
ChainConfig: reader.OnChainConfig{
FChain: 1,
Readers: []libocrtypes.PeerID{
{1}, {2}, {3},
},
Config: []byte{0},
},
},
}

oracleIDToP2pID := GetP2pIDs(1, 2, 3)
n1 := newNode(ctx, t, lggr, 1, cfg, homeChainConfig, oracleIDToP2pID)
n2 := newNode(ctx, t, lggr, 2, cfg, homeChainConfig, oracleIDToP2pID)
n3 := newNode(ctx, t, lggr, 3, cfg, homeChainConfig, oracleIDToP2pID)
n1 := newNode(ctx, t, lggr, 1, cfg, chainConfigInfos, oracleIDToP2pID)
n2 := newNode(ctx, t, lggr, 2, cfg, chainConfigInfos, oracleIDToP2pID)
n3 := newNode(ctx, t, lggr, 3, cfg, chainConfigInfos, oracleIDToP2pID)
nodes := []nodeSetup{n1, n2, n3}

for i, n := range nodes {
Expand Down Expand Up @@ -342,15 +385,25 @@ type nodeSetup struct {
msgHasher *mocks.MessageHasher
}

func newNode(ctx context.Context, t *testing.T, lggr logger.Logger, id int, cfg cciptypes.CommitPluginConfig, homeChainConfig cciptypes.ChainConfig, oracleIDToP2pID map[commontypes.OracleID]libocrtypes.PeerID) nodeSetup {
func newNode(ctx context.Context, t *testing.T, lggr logger.Logger, id int, cfg cciptypes.CommitPluginConfig, chainConfigInfos []reader.ChainConfigInfo, oracleIDToP2pID map[commontypes.OracleID]libocrtypes.PeerID) nodeSetup {
ccipReader := mocks.NewCCIPReader()
priceReader := mocks.NewTokenPricesReader()
reportCodec := mocks.NewCommitPluginJSONReportCodec()
msgHasher := mocks.NewMessageHasher()
homeChainPoller := mocks.NewHomeChainPollerMock()
homeChainReader := mocks.NewContractReaderMock()

homeChainPoller.On("Start", mock.Anything).Return(nil)
homeChainPoller.On("GetConfig").Return(homeChainConfig)
homeChainReader.On(
"GetLatestValue", mock.Anything, "CCIPCapabilityConfiguration", "getAllChainConfigs", mock.Anything, mock.Anything).Run(
func(args mock.Arguments) {
arg := args.Get(4).(*[]reader.ChainConfigInfo)
*arg = chainConfigInfos
}).Return(nil)

homeChainPoller := reader.NewHomeChainConfigPoller(
homeChainReader,
lggr,
1,
)

node1 := NewPlugin(
context.Background(),
Expand Down
48 changes: 19 additions & 29 deletions core/services/ocr3/plugins/ccip/commit/plugin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func Test_validateObserverReadingEligibility(t *testing.T) {
observer libocrtypes.PeerID
msgs []cciptypes.CCIPMsgBaseDetails
seqNums []cciptypes.SeqNumChain
nodeSupportedChains map[libocrtypes.PeerID]cciptypes.SupportedChains
nodeSupportedChains mapset.Set[cciptypes.ChainSelector]
destChain cciptypes.ChainSelector
expErr bool
}{
Expand All @@ -457,11 +457,9 @@ func Test_validateObserverReadingEligibility(t *testing.T) {
{ID: cciptypes.Bytes32{1}, SourceChain: 3, SeqNum: 12},
{ID: cciptypes.Bytes32{2}, SourceChain: 3, SeqNum: 12},
},
nodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{10}: {Supported: mapset.NewSet[cciptypes.ChainSelector](1, 2, 3)},
},
destChain: 1,
expErr: false,
nodeSupportedChains: mapset.NewSet[cciptypes.ChainSelector](1, 2, 3),
destChain: 1,
expErr: false,
},
{
name: "observer is a writer so can observe seq nums",
Expand All @@ -470,11 +468,9 @@ func Test_validateObserverReadingEligibility(t *testing.T) {
seqNums: []cciptypes.SeqNumChain{
{ChainSel: 1, SeqNum: 12},
},
nodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{10}: {Supported: mapset.NewSet[cciptypes.ChainSelector](1, 3)},
},
destChain: 1,
expErr: false,
nodeSupportedChains: mapset.NewSet[cciptypes.ChainSelector](1, 3),
destChain: 1,
expErr: false,
},
{
name: "observer is not a writer so cannot observe seq nums",
Expand All @@ -483,11 +479,9 @@ func Test_validateObserverReadingEligibility(t *testing.T) {
seqNums: []cciptypes.SeqNumChain{
{ChainSel: 1, SeqNum: 12},
},
nodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{10}: {Supported: mapset.NewSet[cciptypes.ChainSelector](3)},
},
destChain: 1,
expErr: true,
nodeSupportedChains: mapset.NewSet[cciptypes.ChainSelector](3),
destChain: 1,
expErr: true,
},
{
name: "observer cfg not found",
Expand All @@ -498,26 +492,22 @@ func Test_validateObserverReadingEligibility(t *testing.T) {
{ID: cciptypes.Bytes32{1}, SourceChain: 3, SeqNum: 12},
{ID: cciptypes.Bytes32{2}, SourceChain: 3, SeqNum: 12},
},
nodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{20}: {Supported: mapset.NewSet[cciptypes.ChainSelector](1, 3)}, // observer 10 not found
},
destChain: 1,
expErr: true,
nodeSupportedChains: mapset.NewSet[cciptypes.ChainSelector](1, 3), // observer 10 not found
destChain: 1,
expErr: true,
},
{
name: "no msgs",
observer: libocrtypes.PeerID{10},
msgs: []cciptypes.CCIPMsgBaseDetails{},
nodeSupportedChains: map[libocrtypes.PeerID]cciptypes.SupportedChains{
libocrtypes.PeerID{10}: {Supported: mapset.NewSet[cciptypes.ChainSelector](1, 3)},
},
expErr: false,
name: "no msgs",
observer: libocrtypes.PeerID{10},
msgs: []cciptypes.CCIPMsgBaseDetails{},
nodeSupportedChains: mapset.NewSet[cciptypes.ChainSelector](1, 3),
expErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := validateObserverReadingEligibility(tc.observer, tc.msgs, tc.seqNums, tc.nodeSupportedChains, tc.destChain)
err := validateObserverReadingEligibility(tc.msgs, tc.seqNums, tc.nodeSupportedChains, tc.destChain)
if tc.expErr {
assert.Error(t, err)
return
Expand Down
2 changes: 1 addition & 1 deletion core/services/ocr3/plugins/ccip/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.7

require (
github.com/deckarep/golang-set/v2 v2.6.0
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240621021634-93a12991766d
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240621043612-82ab35f97d2f
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.24.0
Expand Down
4 changes: 2 additions & 2 deletions core/services/ocr3/plugins/ccip/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6Ng
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240621021634-93a12991766d h1:W/yJYbEBc+hUS3FpPyescBsXof0krW4U7zjf/FQLxXg=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240621021634-93a12991766d/go.mod h1:n/jIcH2y0klmCZ8mRquIXjhOwJq395PvMmNP7FE0bVI=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240621043612-82ab35f97d2f h1:Vp3OvjXQBTCwOPA1lfF6A6LF2bjD3PEPWhhVMGCkNZI=
github.com/smartcontractkit/chainlink-common v0.1.7-0.20240621043612-82ab35f97d2f/go.mod h1:n/jIcH2y0klmCZ8mRquIXjhOwJq395PvMmNP7FE0bVI=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c h1:lIyMbTaF2H0Q71vkwZHX/Ew4KF2BxiKhqEXwF8rn+KI=
github.com/smartcontractkit/libocr v0.0.0-20240419185742-fd3cab206b2c/go.mod h1:fb1ZDVXACvu4frX3APHZaEBp0xi1DIm34DcA0CwTsZM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
Loading

0 comments on commit b9bb0e2

Please sign in to comment.