Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down Expand Up @@ -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 "<name>@<version>" 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
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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()

Expand Down
24 changes: 11 additions & 13 deletions core/capabilities/integration_tests/framework/don.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 19 additions & 2 deletions core/capabilities/integration_tests/framework/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package framework

import (
"context"
"crypto/ecdsa"
"os"
"sync"
"testing"
"time"

"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"
Expand All @@ -23,6 +26,7 @@ type EthBlockchain struct {
services.StateMachine
evmtypes.Backend
transactionOpts *bind.TransactOpts
ownerKey *ecdsa.PrivateKey

blockTimeProcessingTime time.Duration

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
25 changes: 18 additions & 7 deletions core/capabilities/integration_tests/framework/fake_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -133,6 +133,7 @@ func (a *FakeRageP2PNetwork) registerReceiverNode(nodePeerID p2ptypes.PeerID, ca
receiverKey: receiverKey{
capabilityID: capabilityID,
donID: capabilityDonID,
method: method,
},
receiver: receiver,
}
Expand Down Expand Up @@ -164,6 +165,7 @@ type brokerNode struct {
type receiverKey struct {
capabilityID string
donID uint32
method string
}

type registerReceiverRequest struct {
Expand All @@ -187,6 +189,7 @@ func (a *FakeRageP2PNetwork) newNode() *brokerNode {
k := receiverKey{
capabilityID: msg.CapabilityId,
donID: msg.CapabilityDonId,
method: msg.CapabilityMethod,
}

r, ok := receivers[k]
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
}

Expand Down
3 changes: 2 additions & 1 deletion core/capabilities/integration_tests/framework/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -35,6 +35,7 @@ func peerToNode(nopID uint32, p peerIDAndOCRSigner) (kcr.CapabilitiesRegistryNod
P2pId: p.PeerID,
Signer: sigb,
EncryptionPublicKey: testutils.Random32Byte(),
CsaKey: testutils.Random32Byte(),
}, nil
}

Expand Down
Loading
Loading