From 70b500b7b5f8725961cd1136be540ab6b84c76a8 Mon Sep 17 00:00:00 2001 From: pavel-raykov <165708424+pavel-raykov@users.noreply.github.com> Date: Mon, 21 Sep 2026 10:29:39 +0000 Subject: [PATCH 1/2] [CRE-4972] Test that capabilities and workflows DONs are only attached to each other if share the same family (#23782) * Minor. * Minor. --- core/capabilities/launcher_test.go | 223 +++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) diff --git a/core/capabilities/launcher_test.go b/core/capabilities/launcher_test.go index edc13b9e6fb..426c2397b05 100644 --- a/core/capabilities/launcher_test.go +++ b/core/capabilities/launcher_test.go @@ -10,6 +10,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/durationpb" @@ -23,6 +25,7 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/services/servicetest" "github.com/smartcontractkit/chainlink-common/pkg/settings/limits" "github.com/smartcontractkit/chainlink/v2/core/capabilities/remote" + remotetypes "github.com/smartcontractkit/chainlink/v2/core/capabilities/remote/types" remoteMocks "github.com/smartcontractkit/chainlink/v2/core/capabilities/remote/types/mocks" p2ptypes "github.com/smartcontractkit/chainlink/v2/core/services/p2p/types" "github.com/smartcontractkit/chainlink/v2/core/services/p2p/types/mocks" @@ -917,6 +920,226 @@ func TestLauncher_OnNewRegistry_NilLocalCapabilityManager(t *testing.T) { require.NoError(t, err) } +// Unlike TestLauncher_ShardedCapabilityRoutingByFamily, this test has two capability DONs hosting +// *different* capability IDs. That makes the negative case directly observable in the registry +// rather than relying on the dispatcher mock rejecting an unexpected call. +func TestLauncher_OnNewRegistry_FiltersRemoteCapabilityDONsByFamily(t *testing.T) { + t.Parallel() + lggr := logger.Test(t) + registry := regpkg.NewRegistry(lggr) + dispatcher := remoteMocks.NewDispatcher(t) + + workflowDonNodes, capZoneANodes, capZoneBNodes := newNodes(4), newNodes(4), newNodes(4) + + inFamilyCapID := "write-chain_evm_1@1.0.0" + outOfFamilyCapID := "write-chain_evm_2@1.0.0" + + wfDONID := uint32(1) + capZoneAID := uint32(2) + capZoneBID := uint32(3) + + cfg, err := proto.Marshal(&capabilitiespb.CapabilityConfig{ + MethodConfigs: map[string]*capabilitiespb.CapabilityMethodConfig{ + "Write": { + RemoteConfig: &capabilitiespb.CapabilityMethodConfig_RemoteExecutableConfig{ + RemoteExecutableConfig: &capabilitiespb.RemoteExecutableConfig{ + RequestTimeout: durationpb.New(30 * time.Second), + }, + }, + }, + }, + }) + require.NoError(t, err) + + localRegistry := buildLocalRegistry() + // The node under test belongs to the zone-a workflow DON. + addDON(localRegistry, wfDONID, uint32(0), uint8(1), true, true, workflowDonNodes, []string{"zone-a"}, 1, nil) + addDON(localRegistry, capZoneAID, uint32(0), uint8(1), true, false, capZoneANodes, []string{"zone-a"}, 1, [][32]byte{RandomUTF8BytesWord()}) + addCapabilityToDON(localRegistry, capZoneAID, inFamilyCapID, capabilities.CapabilityTypeTarget, cfg) + addDON(localRegistry, capZoneBID, uint32(0), uint8(1), true, false, capZoneBNodes, []string{"zone-b"}, 1, [][32]byte{RandomUTF8BytesWord()}) + addCapabilityToDON(localRegistry, capZoneBID, outOfFamilyCapID, capabilities.CapabilityTypeTarget, cfg) + + var capturedPairs []p2ptypes.DonPair + sharedPeer := mocks.NewSharedPeer(t) + sharedPeer.On("ID").Return(workflowDonNodes[0]) + sharedPeer.On("IsBootstrap").Return(false) + sharedPeer.On("UpdateConnectionsByDONs", mock.Anything, mock.Anything, mock.Anything). + Run(func(args mock.Arguments) { + pairs, ok := args.Get(1).([]p2ptypes.DonPair) + require.True(t, ok, "expected []p2ptypes.DonPair as second argument") + capturedPairs = pairs + }).Return(nil) + + launcher, err := NewLauncher( + lggr, + sharedPeer, + nil, + dispatcher, + registry, + &mockDonNotifier{}, limits.Factory{}, + false, 0, + ) + require.NoError(t, err) + require.NoError(t, launcher.Start(t.Context())) + defer launcher.Close() + + dispatcher.On("SetReceiverForMethod", inFamilyCapID, capZoneAID, "Write", mock.AnythingOfType("*executable.client")).Return(nil) + // Catch-all so that a regression surfaces through the explicit assertions below rather than + // as an "unexpected method call" panic that aborts the test before they run. + dispatcher.On("SetReceiverForMethod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Maybe() + + require.NoError(t, launcher.OnNewRegistry(t.Context(), localRegistry)) + + // addRemoteCapabilities ran for the zone-a DON: the capability is in the local registry and + // points at that DON. + inFamilyCap, err := registry.Get(t.Context(), inFamilyCapID) + require.NoError(t, err, "expected the in-family capability to be added") + inFamilyInfo, err := inFamilyCap.Info(t.Context()) + require.NoError(t, err) + require.NotNil(t, inFamilyInfo.DON) + assert.Equal(t, capZoneAID, inFamilyInfo.DON.ID) + + // It did not run for the zone-b DON: filterDONsByFamilies dropped it before the loop. + _, err = registry.Get(t.Context(), outOfFamilyCapID) + require.Error(t, err, "expected the out-of-family capability to never be added to the registry") + dispatcher.AssertNotCalled(t, "SetReceiverForMethod", outOfFamilyCapID, mock.Anything, mock.Anything, mock.Anything) + dispatcher.AssertNotCalled(t, "SetReceiverForMethod", mock.Anything, capZoneBID, mock.Anything, mock.Anything) + + // The workflow node is only peered with the capability DON in its own family. + wfDON := localRegistry.IDsToDONs[regpkg.DonID(wfDONID)].DON + capZoneADON := localRegistry.IDsToDONs[regpkg.DonID(capZoneAID)].DON + require.Equal(t, []p2ptypes.DonPair{{wfDON, capZoneADON}}, capturedPairs, + "workflow node should only be connected to the zone-a capability DON") +} + +// executeMsgFrom builds the smallest Execute message that reaches the executable server's +// caller-DON check in core/capabilities/remote/executable/server.go. +func executeMsgFrom(t *testing.T, capID string, capDonID, callerDonID uint32, execID string) *remotetypes.MessageBody { + t.Helper() + rawRequest, err := capabilitiespb.MarshalCapabilityRequest(capabilities.CapabilityRequest{ + Metadata: capabilities.RequestMetadata{WorkflowExecutionID: execID}, + }) + require.NoError(t, err) + return &remotetypes.MessageBody{ + CapabilityId: capID, + CapabilityDonId: capDonID, + CallerDonId: callerDonID, + Method: remotetypes.MethodExecute, + Payload: rawRequest, + MessageId: []byte(remotetypes.MethodExecute + ":" + execID), + } +} + +// rejectedDonIDs returns the DON IDs the served capability turned away as unregistered callers. +func rejectedDonIDs(observed *observer.ObservedLogs) []uint32 { + var out []uint32 + for _, entry := range observed.FilterMessage("received request from unregistered don").All() { + if donID, ok := entry.ContextMap()["donId"].(uint32); ok { + out = append(out, donID) + } + } + return out +} + +// TestLauncher_OnNewRegistry_FiltersRemoteWorkflowDONsByFamily is the serve-side mirror of +// TestLauncher_OnNewRegistry_FiltersRemoteCapabilityDONsByFamily. onNewRegistry runs +// remoteWorkflowDONs through filterDONsByFamilies before handing them to serveCapabilities, so a +// capability DON must only expose itself to workflow DONs in its own family, and must only be +// connected to those workflow DONs' peers. +func TestLauncher_OnNewRegistry_FiltersRemoteWorkflowDONsByFamily(t *testing.T) { + t.Parallel() + lggr, observedLogs := logger.TestObserved(t, zapcore.DebugLevel) + registry := regpkg.NewRegistry(lggr) + dispatcher := remoteMocks.NewDispatcher(t) + + capDonNodes, wfZoneANodes, wfZoneBNodes := newNodes(4), newNodes(4), newNodes(4) + + servedCapID := "evm@1.0.0" + require.NoError(t, registry.Add(t.Context(), &mockCapability{ + CapabilityInfo: capabilities.MustNewCapabilityInfo(servedCapID, capabilities.CapabilityTypeTarget, "evm"), + })) + + capDonID := uint32(1) + wfZoneAID := uint32(2) + wfZoneBID := uint32(3) + + execCfg, err := proto.Marshal(&capabilitiespb.CapabilityConfig{ + MethodConfigs: map[string]*capabilitiespb.CapabilityMethodConfig{ + "Write": { + RemoteConfig: &capabilitiespb.CapabilityMethodConfig_RemoteExecutableConfig{ + RemoteExecutableConfig: &capabilitiespb.RemoteExecutableConfig{ + RequestTimeout: durationpb.New(30 * time.Second), + // Required by the real executable server's SetConfig validation. + ServerMaxParallelRequests: 10, + }, + }, + }, + }, + }) + require.NoError(t, err) + + localRegistry := buildLocalRegistry() + // The node under test belongs to the zone-a capability DON, and to no workflow DON. + addDON(localRegistry, capDonID, uint32(0), uint8(1), true, false, capDonNodes, []string{"zone-a"}, 1, [][32]byte{RandomUTF8BytesWord()}) + addCapabilityToDON(localRegistry, capDonID, servedCapID, capabilities.CapabilityTypeTarget, execCfg) + addDON(localRegistry, wfZoneAID, uint32(0), uint8(1), true, true, wfZoneANodes, []string{"zone-a"}, 1, nil) + addDON(localRegistry, wfZoneBID, uint32(0), uint8(1), true, true, wfZoneBNodes, []string{"zone-b"}, 1, nil) + + var capturedPairs []p2ptypes.DonPair + sharedPeer := mocks.NewSharedPeer(t) + sharedPeer.On("ID").Return(capDonNodes[0]) + sharedPeer.On("IsBootstrap").Return(false) + sharedPeer.On("UpdateConnectionsByDONs", mock.Anything, mock.Anything, mock.Anything). + Run(func(args mock.Arguments) { + pairs, ok := args.Get(1).([]p2ptypes.DonPair) + require.True(t, ok, "expected []p2ptypes.DonPair as second argument") + capturedPairs = pairs + }).Return(nil) + + // Grab the real executable server the launcher serves the capability through. + var served remotetypes.Receiver + dispatcher.On("SetReceiverForMethod", servedCapID, capDonID, "Write", mock.Anything). + Run(func(args mock.Arguments) { + receiver, ok := args.Get(3).(remotetypes.Receiver) + require.True(t, ok, "expected a remotetypes.Receiver as fourth argument") + served = receiver + }).Return(nil) + + launcher, err := NewLauncher( + lggr, + sharedPeer, + nil, + dispatcher, + registry, + &mockDonNotifier{}, limits.Factory{}, + false, 0, + ) + require.NoError(t, err) + require.NoError(t, launcher.Start(t.Context())) + defer launcher.Close() + + require.NoError(t, launcher.OnNewRegistry(t.Context(), localRegistry)) + require.NotNil(t, served, "capability was never served: no receiver was registered") + + served.Receive(t.Context(), executeMsgFrom(t, servedCapID, capDonID, wfZoneBID, "exec-zone-b")) + served.Receive(t.Context(), executeMsgFrom(t, servedCapID, capDonID, wfZoneAID, "exec-zone-a")) + + // Both messages were well formed enough to reach the caller-DON check, so the assertions below + // cannot pass just because a request was dropped earlier for being malformed. + require.Equal(t, 2, observedLogs.FilterMessage("received request").Len(), + "expected both Execute requests to reach the caller-DON check") + + // Only the out-of-family caller was turned away. + assert.Equal(t, []uint32{wfZoneBID}, rejectedDonIDs(observedLogs), + "expected only the zone-b workflow DON to be rejected as an unregistered caller") + + // The capability node is only peered with the workflow DON in its own family. + capDON := localRegistry.IDsToDONs[regpkg.DonID(capDonID)].DON + wfZoneADON := localRegistry.IDsToDONs[regpkg.DonID(wfZoneAID)].DON + require.Equal(t, []p2ptypes.DonPair{{capDON, wfZoneADON}}, capturedPairs, + "capability node should only be connected to the zone-a workflow DON") +} + // mockLocalCapabilityManager is a test mock that records calls to Reconcile. type mockLocalCapabilityManager struct { reconcileFn func(ctx context.Context, dons []regpkg.DON) error From 14bc7201e28c98fca1496f230865e3d8e61cea38 Mon Sep 17 00:00:00 2001 From: Cedric Date: Mon, 21 Sep 2026 12:09:39 +0000 Subject: [PATCH 2/2] Migrate test framework to V2 contracts (#23778) * Restore framework library * Framework: Uses v2 capabilities registry * Framework: update v2 workflow registry * Add method support to receiver --- .../framework/capabilities_registry.go | 57 ++-- .../integration_tests/framework/don.go | 24 +- .../integration_tests/framework/ethereum.go | 21 +- .../framework/fake_dispatcher.go | 25 +- .../integration_tests/framework/peer.go | 3 +- .../framework/workflow_registry.go | 245 +++++++++++------- 6 files changed, 240 insertions(+), 135 deletions(-) diff --git a/core/capabilities/integration_tests/framework/capabilities_registry.go b/core/capabilities/integration_tests/framework/capabilities_registry.go index c2200b1d985..5da0717140d 100644 --- a/core/capabilities/integration_tests/framework/capabilities_registry.go +++ b/core/capabilities/integration_tests/framework/capabilities_registry.go @@ -2,17 +2,18 @@ package framework import ( "context" + "encoding/json" "testing" "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" "google.golang.org/protobuf/proto" "github.com/smartcontractkit/chainlink-common/pkg/capabilities/pb" - kcr "github.com/smartcontractkit/chainlink-evm/gethwrappers/keystone/generated/capabilities_registry_1_1_0" + kcr "github.com/smartcontractkit/chainlink-evm/gethwrappers/workflow/generated/capabilities_registry_wrapper_v2" p2ptypes "github.com/smartcontractkit/chainlink/v2/core/services/p2p/types" + registrysyncerv2 "github.com/smartcontractkit/chainlink/v2/core/services/registrysyncer/v2" ) type CapabilitiesRegistry struct { @@ -25,11 +26,13 @@ type CapabilitiesRegistry struct { } func NewCapabilitiesRegistry(ctx context.Context, t *testing.T, backend *EthBlockchain) *CapabilitiesRegistry { - addr, _, contract, err := kcr.DeployCapabilitiesRegistry(backend.transactionOpts, backend.Client()) + // Tests routinely run DONs with a single node and F=0, which the registry rejects unless explicitly allowed. + addr, _, contract, err := kcr.DeployCapabilitiesRegistry(backend.transactionOpts, backend.Client(), + kcr.CapabilitiesRegistryConstructorParams{CanAddOneNodeDONs: true}) require.NoError(t, err) backend.Commit() - _, err = contract.AddNodeOperators(backend.transactionOpts, []kcr.CapabilitiesRegistryNodeOperator{ + _, err = contract.AddNodeOperators(backend.transactionOpts, []kcr.CapabilitiesRegistryNodeOperatorParams{ { Admin: backend.transactionOpts.From, Name: "TEST_NODE_OPERATOR", @@ -60,6 +63,24 @@ func (r *CapabilitiesRegistry) getAddress() common.Address { return r.addr } +// NewRegistryCapability builds the registry entry for a capability. The v2 registry identifies +// capabilities by their "@" ID and carries the capability and response types as JSON +// metadata rather than as dedicated fields, so it is encoded here using the struct the registry +// syncer decodes it with. +func NewRegistryCapability(t *testing.T, capabilityID string, capabilityType registrysyncerv2.ContractCapabilityType, + responseType uint8) kcr.CapabilitiesRegistryCapability { + metadata, err := json.Marshal(registrysyncerv2.CapabilityMetadata{ + CapabilityType: uint8(capabilityType), + ResponseType: responseType, + }) + require.NoError(t, err) + + return kcr.CapabilitiesRegistryCapability{ + CapabilityId: capabilityID, + Metadata: metadata, + } +} + type capability struct { donCapabilityConfig *pb.CapabilityConfig registryConfig kcr.CapabilitiesRegistryCapability @@ -69,17 +90,11 @@ type capability struct { // SetupDON sets up a new DON with the given capabilities and returns the DON ID func (r *CapabilitiesRegistry) setupDON(donInfo DonConfiguration, capabilities []capability) int { - hashedCapabilityIDs := make([][32]byte, 0, len(capabilities)) - - for _, c := range capabilities { - id, err := r.contract.GetHashedCapabilityId(&bind.CallOpts{}, c.registryConfig.LabelledName, c.registryConfig.Version) - require.NoError(r.t, err) - hashedCapabilityIDs = append(hashedCapabilityIDs, id) - } - registryCapabilities := make([]kcr.CapabilitiesRegistryCapability, 0, len(capabilities)) + capabilityIDs := make([]string, 0, len(capabilities)) for _, c := range capabilities { registryCapabilities = append(registryCapabilities, c.registryConfig) + capabilityIDs = append(capabilityIDs, c.registryConfig.CapabilityId) } _, err := r.contract.AddCapabilities(r.backend.transactionOpts, registryCapabilities) @@ -97,7 +112,7 @@ func (r *CapabilitiesRegistry) setupDON(donInfo DonConfiguration, capabilities [ n, innerErr := peerToNode(r.nodeOperatorID, peer) require.NoError(r.t, innerErr) - n.HashedCapabilityIds = hashedCapabilityIDs + n.CapabilityIds = capabilityIDs nodes = append(nodes, n) } @@ -106,17 +121,27 @@ func (r *CapabilitiesRegistry) setupDON(donInfo DonConfiguration, capabilities [ r.backend.Commit() capabilityConfigurations := make([]kcr.CapabilitiesRegistryCapabilityConfiguration, 0, len(capabilities)) - for i, c := range capabilities { + for _, c := range capabilities { configBinary, err2 := proto.Marshal(c.donCapabilityConfig) require.NoError(r.t, err2) capabilityConfigurations = append(capabilityConfigurations, kcr.CapabilitiesRegistryCapabilityConfiguration{ - CapabilityId: hashedCapabilityIDs[i], + CapabilityId: c.registryConfig.CapabilityId, Config: configBinary, }) } - _, err = r.contract.AddDON(r.backend.transactionOpts, peerIDs, capabilityConfigurations, true, donInfo.AcceptsWorkflows, donInfo.F) + _, err = r.contract.AddDONs(r.backend.transactionOpts, []kcr.CapabilitiesRegistryNewDONParams{ + { + Name: donInfo.name, + DonFamilies: []string{donInfo.name}, + CapabilityConfigurations: capabilityConfigurations, + Nodes: peerIDs, + F: donInfo.F, + IsPublic: true, + AcceptsWorkflows: donInfo.AcceptsWorkflows, + }, + }) require.NoError(r.t, err) r.backend.Commit() diff --git a/core/capabilities/integration_tests/framework/don.go b/core/capabilities/integration_tests/framework/don.go index 45a949a7c2c..54e40a52ba3 100644 --- a/core/capabilities/integration_tests/framework/don.go +++ b/core/capabilities/integration_tests/framework/don.go @@ -24,7 +24,7 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/logger" "github.com/smartcontractkit/chainlink-common/pkg/services" "github.com/smartcontractkit/chainlink-common/pkg/services/servicetest" - kcr "github.com/smartcontractkit/chainlink-evm/gethwrappers/keystone/generated/capabilities_registry_1_1_0" + kcr "github.com/smartcontractkit/chainlink-evm/gethwrappers/workflow/generated/capabilities_registry_wrapper_v2" "github.com/smartcontractkit/chainlink-evm/pkg/assets" evmtestutils "github.com/smartcontractkit/chainlink-evm/pkg/testutils" remotetypes "github.com/smartcontractkit/chainlink/v2/core/capabilities/remote/types" @@ -86,13 +86,13 @@ func (c DonContext) WaitForCapabilitiesToBeExposed(t *testing.T, dons ...*DON) { }, 1*time.Minute, 1*time.Second, "timeout waiting for capabilities to be exposed") } -func (c DonContext) WaitForWorkflowRegistryMetadata(t *testing.T, workflowName string, owner string, workflowID [32]byte) { +func (c DonContext) WaitForWorkflowRegistryMetadata(t *testing.T, workflowName string, tag string, owner string, workflowID [32]byte) { require.Eventually(t, func() bool { - wf, err := c.workflowRegistry.contract.GetWorkflowMetadata(&bind.CallOpts{}, common.HexToAddress(owner), workflowName) + wf, err := c.workflowRegistry.contract.GetWorkflow(&bind.CallOpts{}, common.HexToAddress(owner), workflowName, tag) if err != nil { return false } - return wf.WorkflowID == workflowID + return wf.WorkflowId == workflowID }, 1*time.Minute, 5*time.Second, "timeout waiting for workflow") } @@ -202,11 +202,12 @@ func (d *DON) Initialise() { d.id = &d.config.ID if d.config.AcceptsWorkflows && d.workflowRegistry != nil { - d.workflowRegistry.UpdateAllowedDons([]uint32{d.config.ID}) + d.workflowRegistry.UpdateAllowedDons([]string{d.config.name}) d.nodeConfigModifiers = append(d.nodeConfigModifiers, func(c *chainlink.Config, node *capabilityNode) { workflowRegistryAddressStr := d.workflowRegistry.addr.String() c.Capabilities.WorkflowRegistry.Address = &workflowRegistryAddressStr c.Capabilities.WorkflowRegistry.ChainID = new(fmt.Sprintf("%d", testutils.SimulatedChainID)) + c.Capabilities.WorkflowRegistry.ContractVersion = new("2.0.0") }) } d.initialised = true @@ -230,7 +231,7 @@ func (d *DON) GetExternalCapabilities() (map[CapabilityRegistration]bool, error) for _, node := range d.nodes { result[CapabilityRegistration{ nodePeerID: hex.EncodeToString(node.peer.PeerID[:]), - capabilityID: publishedCapability.registryConfig.LabelledName + "@" + publishedCapability.registryConfig.Version, + capabilityID: publishedCapability.registryConfig.CapabilityId, capabilityDonID: d.GetID(), }] = true } @@ -368,12 +369,12 @@ func (d *DON) AddWorkflow(workflow Workflow) error { return errors.New("cannot add workflow to non-initialised DON") } - d.workflowRegistry.RegisterWorkflow(workflow, *d.id) + d.workflowRegistry.RegisterWorkflow(workflow, d.config.name) return nil } -func (d *DON) UpdateWorkflow(workflow UpdatedWorkflow) error { +func (d *DON) UpdateWorkflow(workflow Workflow) error { if !d.config.AcceptsWorkflows { return errors.New("cannot add workflow to non-workflow DON") } @@ -382,15 +383,11 @@ func (d *DON) UpdateWorkflow(workflow UpdatedWorkflow) error { return errors.New("cannot add workflow to non-initialised DON") } - d.workflowRegistry.UpdateWorkflow(workflow, *d.id) + d.workflowRegistry.UpdateWorkflow(workflow, d.config.name) return nil } -func (d *DON) ComputeHashKey(owner string, field string) [32]byte { - return d.workflowRegistry.ComputeHashKey(owner, field) -} - type TriggerFactory interface { CreateNewTrigger(t *testing.T) commoncap.TriggerCapability GetTriggerID() string @@ -419,6 +416,7 @@ func startNewNode(ctx context.Context, config, _ := heavyweight.FullTestDBV2(t, func(c *chainlink.Config, s *chainlink.Secrets) { c.Capabilities.ExternalRegistry.ChainID = new(fmt.Sprintf("%d", testutils.SimulatedChainID)) c.Capabilities.ExternalRegistry.Address = new(capRegistryAddr.String()) + c.Capabilities.ExternalRegistry.ContractVersion = new("2.0.0") c.Capabilities.Peering.V2.Enabled = new(true) c.Capabilities.WorkflowRegistry.SyncStrategy = new(syncer.SyncStrategyReconciliation) c.Feature.FeedsManager = new(false) diff --git a/core/capabilities/integration_tests/framework/ethereum.go b/core/capabilities/integration_tests/framework/ethereum.go index 9bfde22b5a3..f25365ff419 100644 --- a/core/capabilities/integration_tests/framework/ethereum.go +++ b/core/capabilities/integration_tests/framework/ethereum.go @@ -2,6 +2,7 @@ package framework import ( "context" + "crypto/ecdsa" "os" "sync" "testing" @@ -9,8 +10,10 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth/ethconfig" gethlog "github.com/ethereum/go-ethereum/log" + "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink-common/pkg/services" "github.com/smartcontractkit/chainlink-evm/pkg/assets" @@ -23,6 +26,7 @@ type EthBlockchain struct { services.StateMachine evmtypes.Backend transactionOpts *bind.TransactOpts + ownerKey *ecdsa.PrivateKey blockTimeProcessingTime time.Duration @@ -31,14 +35,20 @@ type EthBlockchain struct { } func NewEthBlockchain(t *testing.T, initialEth int, blockTimeProcessingTime time.Duration) *EthBlockchain { - transactOpts := testutils.MustNewSimTransactor(t) // config contract deployer and owner + // The owner key is retained so that contracts requiring off-chain signatures from the deployer, + // such as the workflow registry's owner link, can be driven from tests. + ownerKey, err := crypto.GenerateKey() + require.NoError(t, err) + transactOpts, err := bind.NewKeyedTransactorWithChainID(ownerKey, testutils.SimulatedChainID) // contract deployer and owner + require.NoError(t, err) + genesisData := types.GenesisAlloc{transactOpts.From: {Balance: assets.Ether(initialEth).ToInt()}} backend := cltest.NewSimulatedBackend(t, genesisData, ethconfig.Defaults.Miner.GasCeil) gethlog.SetDefault(gethlog.NewLogger(gethlog.NewTerminalHandlerWithLevel(os.Stderr, gethlog.LevelWarn, true))) backend.Commit() return &EthBlockchain{Backend: backend, stopCh: make(services.StopChan), - blockTimeProcessingTime: blockTimeProcessingTime, transactionOpts: transactOpts} + blockTimeProcessingTime: blockTimeProcessingTime, transactionOpts: transactOpts, ownerKey: ownerKey} } func (b *EthBlockchain) Start(ctx context.Context) error { @@ -74,3 +84,10 @@ func (b *EthBlockchain) Close() error { func (b *EthBlockchain) TransactionOpts() *bind.TransactOpts { return b.transactionOpts } + +// SignHash signs a 32 byte hash with the key owning the contracts deployed against this backend. +// The returned signature has a recovery ID of 0 or 1, callers needing an EIP-191 signature must +// add 27 to it. +func (b *EthBlockchain) SignHash(hash []byte) ([]byte, error) { + return crypto.Sign(hash, b.ownerKey) +} diff --git a/core/capabilities/integration_tests/framework/fake_dispatcher.go b/core/capabilities/integration_tests/framework/fake_dispatcher.go index 5cb5779af7c..b65b2b3e039 100644 --- a/core/capabilities/integration_tests/framework/fake_dispatcher.go +++ b/core/capabilities/integration_tests/framework/fake_dispatcher.go @@ -113,7 +113,7 @@ func (a *FakeRageP2PNetwork) GetCapabilityRegistrations() map[CapabilityRegistra return copiedRegistrations } -func (a *FakeRageP2PNetwork) registerReceiverNode(nodePeerID p2ptypes.PeerID, capabilityID string, capabilityDonID uint32, receiver remotetypes.Receiver) { +func (a *FakeRageP2PNetwork) registerReceiverNode(nodePeerID p2ptypes.PeerID, capabilityID string, capabilityDonID uint32, method string, receiver remotetypes.Receiver) { a.mux.Lock() defer a.mux.Unlock() @@ -133,6 +133,7 @@ func (a *FakeRageP2PNetwork) registerReceiverNode(nodePeerID p2ptypes.PeerID, ca receiverKey: receiverKey{ capabilityID: capabilityID, donID: capabilityDonID, + method: method, }, receiver: receiver, } @@ -164,6 +165,7 @@ type brokerNode struct { type receiverKey struct { capabilityID string donID uint32 + method string } type registerReceiverRequest struct { @@ -187,6 +189,7 @@ func (a *FakeRageP2PNetwork) newNode() *brokerNode { k := receiverKey{ capabilityID: msg.CapabilityId, donID: msg.CapabilityDonId, + method: msg.CapabilityMethod, } r, ok := receivers[k] @@ -221,8 +224,9 @@ type brokerDispatcher struct { } type key struct { - capID string - donID uint32 + capID string + donID uint32 + method string } func (t *brokerDispatcher) Send(peerID p2ptypes.PeerID, msgBody *remotetypes.MessageBody) error { @@ -236,23 +240,30 @@ func (t *brokerDispatcher) Send(peerID p2ptypes.PeerID, msgBody *remotetypes.Mes } func (t *brokerDispatcher) SetReceiver(capabilityID string, donID uint32, receiver remotetypes.Receiver) error { + k := key{capID: capabilityID, donID: donID} + return t.setReceiver(k, receiver) +} + +func (t *brokerDispatcher) setReceiver(k key, receiver remotetypes.Receiver) error { t.mu.Lock() defer t.mu.Unlock() - k := key{capID: capabilityID, donID: donID} _, ok := t.receivers[k] if ok { - return fmt.Errorf("%w: receiver already exists for capability %s and don %d", remote.ErrReceiverExists, capabilityID, donID) + return fmt.Errorf("%w: receiver already exists for capability %s and don %d", remote.ErrReceiverExists, k.capID, k.donID) } t.receivers[k] = receiver - t.broker.(*FakeRageP2PNetwork).registerReceiverNode(t.callerPeerID, capabilityID, donID, receiver) + t.broker.(*FakeRageP2PNetwork).registerReceiverNode(t.callerPeerID, k.capID, k.donID, k.method, receiver) return nil } + func (t *brokerDispatcher) RemoveReceiver(capabilityID string, donID uint32) {} func (t *brokerDispatcher) SetReceiverForMethod(capabilityID string, donID uint32, method string, receiver remotetypes.Receiver) error { - return errors.New("not implemented") + k := key{capID: capabilityID, donID: donID, method: method} + return t.setReceiver(k, receiver) } + func (t *brokerDispatcher) RemoveReceiverForMethod(capabilityID string, donID uint32, method string) { } diff --git a/core/capabilities/integration_tests/framework/peer.go b/core/capabilities/integration_tests/framework/peer.go index 84747e92889..059260626fd 100644 --- a/core/capabilities/integration_tests/framework/peer.go +++ b/core/capabilities/integration_tests/framework/peer.go @@ -10,7 +10,7 @@ import ( "github.com/smartcontractkit/chainlink-common/keystore/corekeys" "github.com/smartcontractkit/chainlink-common/keystore/corekeys/ocr2key" "github.com/smartcontractkit/chainlink-common/keystore/corekeys/p2pkey" - kcr "github.com/smartcontractkit/chainlink-evm/gethwrappers/keystone/generated/capabilities_registry_1_1_0" + kcr "github.com/smartcontractkit/chainlink-evm/gethwrappers/workflow/generated/capabilities_registry_wrapper_v2" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" p2ptypes "github.com/smartcontractkit/chainlink/v2/core/services/p2p/types" ) @@ -35,6 +35,7 @@ func peerToNode(nopID uint32, p peerIDAndOCRSigner) (kcr.CapabilitiesRegistryNod P2pId: p.PeerID, Signer: sigb, EncryptionPublicKey: testutils.Random32Byte(), + CsaKey: testutils.Random32Byte(), }, nil } diff --git a/core/capabilities/integration_tests/framework/workflow_registry.go b/core/capabilities/integration_tests/framework/workflow_registry.go index abe7d44c821..91a2ea3790d 100644 --- a/core/capabilities/integration_tests/framework/workflow_registry.go +++ b/core/capabilities/integration_tests/framework/workflow_registry.go @@ -2,13 +2,31 @@ package framework import ( "context" + "crypto/sha256" + "fmt" + "math/big" "testing" + "time" + "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/require" - workflow_registry_wrapper "github.com/smartcontractkit/chainlink-evm/gethwrappers/workflow/generated/workflow_registry_wrapper_v1" + workflow_registry_wrapper "github.com/smartcontractkit/chainlink-evm/gethwrappers/workflow/generated/workflow_registry_wrapper_v2" +) + +const ( + // The v2 registry gates workflows on a per DON family limit rather than on the v1 allowlist of + // DON IDs, so a family is only usable once these limits are set. + workflowRegistryDONLimit = uint32(10000) + workflowRegistryUserDefaultLimit = uint32(1000) + + // Request type discriminator the registry expects in the payload signed for LinkOwner. + workflowRegistryLinkRequestType = uint8(0) + + workflowRegistryOwnerLinkValidity = time.Hour ) type WorkflowRegistry struct { @@ -24,123 +42,158 @@ func NewWorkflowRegistry(ctx context.Context, t *testing.T, backend *EthBlockcha backend.Commit() require.NoError(t, err) - // setup contract state to allow the secrets to be updated - updateAuthorizedAddress(t, backend, wfRegistryC, []common.Address{backend.transactionOpts.From}, true) - - return &WorkflowRegistry{t: t, addr: wfRegistryAddr, contract: wfRegistryC, backend: backend} -} - -func (r *WorkflowRegistry) UpdateAllowedDons(donIDs []uint32) { - updateAllowedDONs(r.t, r.backend, r.contract, donIDs, true) -} + r := &WorkflowRegistry{t: t, addr: wfRegistryAddr, contract: wfRegistryC, backend: backend} -func (r *WorkflowRegistry) RegisterWorkflow(input Workflow, donID uint32) { - registerWorkflow(r.t, r.backend, r.contract, input, donID) -} + // The v2 registry only accepts workflows from a linked owner whose signer is allowlisted, so the + // deployer is set up as both before any workflow can be registered. + r.updateAllowedSigners([]common.Address{backend.transactionOpts.From}) + r.linkOwner(ctx, backend.transactionOpts.From) -func (r *WorkflowRegistry) UpdateWorkflow(input UpdatedWorkflow, donID uint32) { - updateWorkflow(r.t, r.backend, r.contract, input) + return r } -func (r *WorkflowRegistry) ComputeHashKey(owner string, field string) [32]byte { - return computeHashKey(r.t, r.contract, owner, field) +// UpdateAllowedDons sets the workflow limits for the given DON families. Families are the v2 +// registry's equivalent of the v1 allowlist of DON IDs, and match the families the DON is +// registered under in the capabilities registry. +func (r *WorkflowRegistry) UpdateAllowedDons(donFamilies []string) { + for _, donFamily := range donFamilies { + _, err := r.contract.SetDONLimit(r.backend.transactionOpts, donFamily, workflowRegistryDONLimit, + workflowRegistryUserDefaultLimit) + require.NoError(r.t, err, "failed to set limits for DON family %s", donFamily) + r.commit() + + limits, err := r.contract.GetMaxWorkflowsPerDON(&bind.CallOpts{From: r.backend.transactionOpts.From}, donFamily) + require.NoError(r.t, err) + require.Equal(r.t, workflowRegistryDONLimit, limits.MaxWorkflows) + } } -func updateAuthorizedAddress( - t *testing.T, - th *EthBlockchain, - wfRegC *workflow_registry_wrapper.WorkflowRegistry, - addresses []common.Address, - _ bool, -) { - t.Helper() - _, err := wfRegC.UpdateAuthorizedAddresses(th.transactionOpts, addresses, true) - require.NoError(t, err, "failed to update authorised addresses") - th.Commit() - th.Commit() - th.Commit() - gotAddresses, err := wfRegC.GetAllAuthorizedAddresses(&bind.CallOpts{ - From: th.transactionOpts.From, - }) - require.NoError(t, err) - require.ElementsMatch(t, addresses, gotAddresses) +func (r *WorkflowRegistry) RegisterWorkflow(input Workflow, donFamily string) { + r.upsertWorkflow(input, donFamily) } -func updateAllowedDONs( - t *testing.T, - th *EthBlockchain, - wfRegC *workflow_registry_wrapper.WorkflowRegistry, - donIDs []uint32, - allowed bool, -) { - t.Helper() - _, err := wfRegC.UpdateAllowedDONs(th.transactionOpts, donIDs, allowed) - require.NoError(t, err, "failed to update DONs") - th.Commit() - th.Commit() - th.Commit() - gotDons, err := wfRegC.GetAllAllowedDONs(&bind.CallOpts{ - From: th.transactionOpts.From, - }) - require.NoError(t, err) - require.ElementsMatch(t, donIDs, gotDons) +// UpdateWorkflow updates a previously registered workflow. The v2 registry has a single upsert +// entrypoint, and rejects updates that change the status or the DON family of a workflow. +func (r *WorkflowRegistry) UpdateWorkflow(input Workflow, donFamily string) { + r.upsertWorkflow(input, donFamily) } type Workflow struct { Name string + Tag string ID [32]byte Status uint8 BinaryURL string ConfigURL string - SecretsURL string + Attributes []byte + KeepAlive bool +} + +func (r *WorkflowRegistry) upsertWorkflow(input Workflow, donFamily string) { + r.t.Helper() + _, err := r.contract.UpsertWorkflow(r.backend.transactionOpts, input.Name, input.Tag, input.ID, input.Status, + donFamily, input.BinaryURL, input.ConfigURL, input.Attributes, input.KeepAlive) + require.NoError(r.t, err, "failed to upsert workflow") + r.commit() +} + +func (r *WorkflowRegistry) updateAllowedSigners(addresses []common.Address) { + r.t.Helper() + _, err := r.contract.UpdateAllowedSigners(r.backend.transactionOpts, addresses, true) + require.NoError(r.t, err, "failed to update allowed signers") + r.commit() + + for _, address := range addresses { + allowed, err := r.contract.IsAllowedSigner(&bind.CallOpts{From: r.backend.transactionOpts.From}, address) + require.NoError(r.t, err) + require.True(r.t, allowed, "signer %s was not allowlisted", address) + } } -func registerWorkflow( - t *testing.T, - th *EthBlockchain, - wfRegC *workflow_registry_wrapper.WorkflowRegistry, - input Workflow, - donID uint32, -) { - t.Helper() - _, err := wfRegC.RegisterWorkflow(th.transactionOpts, input.Name, input.ID, donID, - input.Status, input.BinaryURL, input.ConfigURL, input.SecretsURL) - require.NoError(t, err, "failed to register workflow") - th.Commit() - th.Commit() - th.Commit() +// linkOwner proves ownership of the given address to the registry, which the v2 contract requires +// before that address can register any workflow. +func (r *WorkflowRegistry) linkOwner(ctx context.Context, owner common.Address) { + r.t.Helper() + + typeAndVersion, err := r.contract.TypeAndVersion(&bind.CallOpts{From: r.backend.transactionOpts.From}) + require.NoError(r.t, err) + + chainID, err := r.backend.Client().ChainID(ctx) + require.NoError(r.t, err) + + validityTimestamp := big.NewInt(time.Now().UTC().Add(workflowRegistryOwnerLinkValidity).Unix()) + proof := ownershipProofHash(owner.String(), "integration-tests", "1") + + arguments, err := ownershipLinkABIArguments() + require.NoError(r.t, err) + + packed, err := arguments.Pack(workflowRegistryLinkRequestType, owner, chainID, r.addr, typeAndVersion, + validityTimestamp, proof) + require.NoError(r.t, err) + + hash := crypto.Keccak256(packed) + + // The contract recovers the signer using EIP-191, so the digest has to carry the same prefix. + prefixedMessage := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(hash), hash) + signature, err := r.backend.SignHash(crypto.Keccak256([]byte(prefixedMessage))) + require.NoError(r.t, err) + signature[64] += 27 + + _, err = r.contract.LinkOwner(r.backend.transactionOpts, validityTimestamp, proof, signature) + require.NoError(r.t, err, "failed to link owner") + r.commit() + + linked, err := r.contract.IsOwnerLinked(&bind.CallOpts{From: r.backend.transactionOpts.From}, owner) + require.NoError(r.t, err) + require.True(r.t, linked, "owner %s was not linked", owner) } -type UpdatedWorkflow struct { - WorkflowKey [32]byte - ID [32]byte - BinaryURL string - ConfigURL string - SecretsURL string +func ownershipProofHash(workflowOwnerAddress, organizationID, nonce string) [32]byte { + return sha256.Sum256([]byte(workflowOwnerAddress + organizationID + nonce)) } -func updateWorkflow( - t *testing.T, - th *EthBlockchain, - wfRegC *workflow_registry_wrapper.WorkflowRegistry, - input UpdatedWorkflow, -) { - t.Helper() - _, err := wfRegC.UpdateWorkflow(th.transactionOpts, input.WorkflowKey, input.ID, input.BinaryURL, input.ConfigURL, input.SecretsURL) - require.NoError(t, err, "failed to update workflow") - th.Commit() - th.Commit() - th.Commit() +// ownershipLinkABIArguments describes, in order, the payload the registry hashes when verifying an +// owner link signature. +func ownershipLinkABIArguments() (abi.Arguments, error) { + uint8Type, err := abi.NewType("uint8", "", nil) + if err != nil { + return nil, fmt.Errorf("failed to create uint8 type: %w", err) + } + + addressType, err := abi.NewType("address", "", nil) + if err != nil { + return nil, fmt.Errorf("failed to create address type: %w", err) + } + + bytes32Type, err := abi.NewType("bytes32", "", nil) + if err != nil { + return nil, fmt.Errorf("failed to create bytes32 type: %w", err) + } + + uint256Type, err := abi.NewType("uint256", "", nil) + if err != nil { + return nil, fmt.Errorf("failed to create uint256 type: %w", err) + } + + stringType, err := abi.NewType("string", "", nil) + if err != nil { + return nil, fmt.Errorf("failed to create string type: %w", err) + } + + return abi.Arguments{ + {Type: uint8Type}, // request type + {Type: addressType}, // owner address + {Type: uint256Type}, // chain ID + {Type: addressType}, // address of the contract + {Type: stringType}, // type and version string + {Type: uint256Type}, // validity timestamp + {Type: bytes32Type}, // ownership proof hash + }, nil } -func computeHashKey( - t *testing.T, - wfRegC *workflow_registry_wrapper.WorkflowRegistry, - owner string, - field string, -) [32]byte { - t.Helper() - hashKey, err := wfRegC.ComputeHashKey(&bind.CallOpts{}, common.HexToAddress(owner), field) - require.NoError(t, err, "failed to compute hash key") - return hashKey +// commit mines enough blocks for the writes above to be visible to finalized reads. +func (r *WorkflowRegistry) commit() { + r.backend.Commit() + r.backend.Commit() + r.backend.Commit() }