Skip to content

Commit

Permalink
Rename type -> id
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryNguyen5 committed Apr 30, 2024
1 parent 270fe6b commit 1da5e47
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 137 deletions.
28 changes: 15 additions & 13 deletions contracts/src/v0.8/keystone/CapabilityRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ contract CapabilityRegistry is OwnerIsCreator, TypeAndVersionInterface {
event NodeOperatorUpdated(uint256 nodeOperatorId, address indexed admin, string name);

/// @notice This event is emitted when a new capability is added
/// @param capabilityId The ID of the newly added capability
event CapabilityAdded(bytes32 indexed capabilityId);
/// @param compressedCapabilityId The ID of the newly added capability
event CapabilityAdded(bytes32 indexed compressedCapabilityId);

mapping(bytes32 => Capability) private s_capabilities;
EnumerableSet.Bytes32Set private s_capabilityIds;
/// @notice Set of compressed capability IDs,
/// a compressed ID is created by the function `getCompressedCapabilityID`.
EnumerableSet.Bytes32Set private s_compressedCapabilityIds;

/// @notice Mapping of node operators
mapping(uint256 nodeOperatorId => NodeOperator) private s_nodeOperators;
Expand Down Expand Up @@ -170,9 +172,8 @@ contract CapabilityRegistry is OwnerIsCreator, TypeAndVersionInterface {
}

function addCapability(Capability calldata capability) external onlyOwner {
bytes32 capabilityId = getCapabilityID(capability.capabilityType, capability.version);

if (s_capabilityIds.contains(capabilityId)) revert CapabilityAlreadyExists();
bytes32 compressedId = getCompressedCapabilityID(capability.labelledName, capability.version);
if (s_compressedCapabilityIds.contains(compressedId)) revert CapabilityAlreadyExists();

if (capability.configurationContract != address(0)) {
if (
Expand All @@ -183,19 +184,20 @@ contract CapabilityRegistry is OwnerIsCreator, TypeAndVersionInterface {
) revert InvalidCapabilityConfigurationContractInterface(capability.configurationContract);
}

s_capabilityIds.add(capabilityId);
s_capabilities[capabilityId] = capability;
s_compressedCapabilityIds.add(compressedId);
s_capabilities[compressedId] = capability;

emit CapabilityAdded(capabilityId);
emit CapabilityAdded(compressedId);
}

function getCapability(bytes32 capabilityID) public view returns (Capability memory) {
return s_capabilities[capabilityID];
/// @notice This function returns a Capability by its compressed ID. Use `getCompressedCapabilityID` to get the compressed ID.
function getCapability(bytes32 compressedId) public view returns (Capability memory) {
return s_capabilities[compressedId];
}

/// @notice This functions returns a Capability ID packed into a bytes32 for cheaper access
/// @return bytes32 A unique identifier for the capability
function getCapabilityID(bytes32 capabilityType, bytes32 version) public pure returns (bytes32) {
return keccak256(abi.encodePacked(capabilityType, version));
function getCompressedCapabilityID(bytes32 labeledName, bytes32 version) public pure returns (bytes32) {
return keccak256(abi.encodePacked(labeledName, version));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contract CapabilityRegistry_AddCapabilityTest is BaseTest {
function test_AddCapability_NoConfigurationContract() public {
s_capabilityRegistry.addCapability(basicCapability);

bytes32 capabilityId = s_capabilityRegistry.getCapabilityID(bytes32("data-streams-reports"), bytes32("1.0.0"));
bytes32 capabilityId = s_capabilityRegistry.getCompressedCapabilityID(bytes32("data-streams-reports"), bytes32("1.0.0"));
CapabilityRegistry.Capability memory storedCapability = s_capabilityRegistry.getCapability(capabilityId);

assertEq(storedCapability.capabilityType, basicCapability.capabilityType);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions core/services/workflows/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (e *Engine) resolveWorkflowCapabilities(ctx context.Context) error {
//
triggersInitialized := true
for _, t := range e.workflow.triggers {
tg, err := e.registry.GetTrigger(ctx, t.Type)
tg, err := e.registry.GetTrigger(ctx, t.ID)
if err != nil {
e.logger.Errorf("failed to get trigger capability: %s", err)
// we don't immediately return here, since we want to retry all triggers
Expand Down Expand Up @@ -123,16 +123,16 @@ func (e *Engine) initializeCapability(ctx context.Context, s *step) error {
return nil
}

cp, err := e.registry.Get(ctx, s.Type)
cp, err := e.registry.Get(ctx, s.ID)
if err != nil {
return fmt.Errorf("failed to get capability with ref %s: %s", s.Type, err)
return fmt.Errorf("failed to get capability with ref %s: %s", s.ID, err)
}

// We configure actions, consensus and targets here, and
// they all satisfy the `CallbackCapability` interface
cc, ok := cp.(capabilities.CallbackCapability)
if !ok {
return fmt.Errorf("could not coerce capability %s to CallbackCapability", s.Type)
return fmt.Errorf("could not coerce capability %s to CallbackCapability", s.ID)
}

if s.config == nil {
Expand Down Expand Up @@ -275,7 +275,7 @@ func (e *Engine) registerTrigger(ctx context.Context, t *triggerCapability) erro
}
eventsCh, err := t.trigger.RegisterTrigger(ctx, triggerRegRequest)
if err != nil {
return fmt.Errorf("failed to instantiate trigger %s, %s", t.Type, err)
return fmt.Errorf("failed to instantiate trigger %s, %s", t.ID, err)
}

go func() {
Expand Down
22 changes: 11 additions & 11 deletions core/services/workflows/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (

const hardcodedWorkflow = `
triggers:
- type: "mercury-trigger"
- id: "mercury-trigger"
config:
feedIds:
- "0x1111111111111111111100000000000000000000000000000000000000000000"
- "0x2222222222222222222200000000000000000000000000000000000000000000"
- "0x3333333333333333333300000000000000000000000000000000000000000000"
consensus:
- type: "offchain_reporting"
- id: "offchain_reporting"
ref: "evm_median"
inputs:
observations:
Expand All @@ -49,14 +49,14 @@ consensus:
abi: "mercury_reports bytes[]"
targets:
- type: "write_polygon-testnet-mumbai"
- id: "write_polygon-testnet-mumbai"
inputs:
report: "$(evm_median.outputs.report)"
config:
address: "0x3F3554832c636721F1fD1822Ccca0354576741Ef"
params: ["$(report)"]
abi: "receive(report bytes)"
- type: "write_ethereum-testnet-sepolia"
- id: "write_ethereum-testnet-sepolia"
inputs:
report: "$(evm_median.outputs.report)"
config:
Expand Down Expand Up @@ -205,15 +205,15 @@ func TestEngineWithHardcodedWorkflow(t *testing.T) {
const (
simpleWorkflow = `
triggers:
- type: "mercury-trigger"
- id: "mercury-trigger"
config:
feedlist:
- "0x1111111111111111111100000000000000000000000000000000000000000000" # ETHUSD
- "0x2222222222222222222200000000000000000000000000000000000000000000" # LINKUSD
- "0x3333333333333333333300000000000000000000000000000000000000000000" # BTCUSD
consensus:
- type: "offchain_reporting"
- id: "offchain_reporting"
ref: "evm_median"
inputs:
observations:
Expand All @@ -235,7 +235,7 @@ consensus:
abi: "mercury_reports bytes[]"
targets:
- type: "write_polygon-testnet-mumbai"
- id: "write_polygon-testnet-mumbai"
inputs:
report: "$(evm_median.outputs.report)"
config:
Expand Down Expand Up @@ -358,22 +358,22 @@ func TestEngine_ErrorsTheWorkflowIfAStepErrors(t *testing.T) {
const (
multiStepWorkflow = `
triggers:
- type: "mercury-trigger"
- id: "mercury-trigger"
config:
feedlist:
- "0x1111111111111111111100000000000000000000000000000000000000000000" # ETHUSD
- "0x2222222222222222222200000000000000000000000000000000000000000000" # LINKUSD
- "0x3333333333333333333300000000000000000000000000000000000000000000" # BTCUSD
actions:
- type: "read_chain_action"
- id: "read_chain_action"
ref: "read_chain_action"
inputs:
action:
- "$(trigger.outputs)"
consensus:
- type: "offchain_reporting"
- id: "offchain_reporting"
ref: "evm_median"
inputs:
observations:
Expand All @@ -396,7 +396,7 @@ consensus:
abi: "mercury_reports bytes[]"
targets:
- type: "write_polygon-testnet-mumbai"
- id: "write_polygon-testnet-mumbai"
inputs:
report: "$(evm_median.outputs.report)"
config:
Expand Down
5 changes: 2 additions & 3 deletions core/services/workflows/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ type stepRequest struct {
//
// Within the workflow spec, they are called "Capability Properties".
type stepDefinition struct {
// TODO: Rename this, type here refers to the capability ID, not its type.
Type string `json:"type" jsonschema:"required"`
ID string `json:"id" jsonschema:"required"`
Ref string `json:"ref,omitempty" jsonschema:"pattern=^[a-z0-9_]+$"`
Inputs map[string]any `json:"inputs,omitempty"`
Config map[string]any `json:"config" jsonschema:"required"`
Expand Down Expand Up @@ -160,7 +159,7 @@ func Parse(yamlWorkflow string) (*workflow, error) {
// To handle this, we default the `Ref` to the type, but ideally we
// should find a better long-term way to handle this.
if s.Ref == "" {
s.Ref = s.Type
s.Ref = s.ID
}

innerErr := g.AddVertex(&step{stepDefinition: s})
Expand Down

0 comments on commit 1da5e47

Please sign in to comment.