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 26, 2024
1 parent e48986d commit c4adf17
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 134 deletions.
19 changes: 10 additions & 9 deletions contracts/src/v0.8/keystone/CapabilityRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ contract CapabilityRegistry is OwnerIsCreator, TypeAndVersionInterface {
event NodeOperatorRemoved(uint256 nodeOperatorId);

/// @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;

Expand Down Expand Up @@ -86,18 +86,19 @@ contract CapabilityRegistry is OwnerIsCreator, TypeAndVersionInterface {
}

function addCapability(Capability calldata capability) external onlyOwner {
bytes32 capabilityId = getCapabilityID(capability.capabilityType, capability.version);
s_capabilities[capabilityId] = capability;
emit CapabilityAdded(capabilityId);
bytes32 compressedId = getCompressedCapabilityID(capability.labelledName, capability.version);
s_capabilities[compressedId] = capability;
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 @@ -8,7 +8,7 @@ contract CapabilityRegistry_AddCapabilityTest is BaseTest {
function test_AddCapability() public {
s_capabilityRegistry.addCapability(CapabilityRegistry.Capability("data-streams-reports", "1.0.0"));

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 capability = s_capabilityRegistry.getCapability(capabilityId);

assertEq(capability.capabilityType, "data-streams-reports");
Expand Down

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions core/services/workflows/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ LOOP:
initSuccessful := true
// Resolve the underlying capability for each trigger
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 {
initSuccessful = false
e.logger.Errorf("failed to get trigger capability: %s, retrying in %d seconds", err, retrySec)
Expand Down Expand Up @@ -142,16 +142,16 @@ func (e *Engine) initializeCapability(ctx context.Context, s *step, retrySec int
return nil
}

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

// We only need to 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 @@ -258,7 +258,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 @@ -177,15 +177,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 @@ -207,7 +207,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 @@ -339,22 +339,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 @@ -377,7 +377,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
58 changes: 29 additions & 29 deletions core/services/workflows/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ func TestParse_Graph(t *testing.T) {
name: "basic example",
yaml: `
triggers:
- type: "a-trigger"
- id: "a-trigger"
actions:
- type: "an-action"
- id: "an-action"
ref: "an-action"
inputs:
trigger_output: $(trigger.outputs)
consensus:
- type: "a-consensus"
- id: "a-consensus"
ref: "a-consensus"
inputs:
trigger_output: $(trigger.outputs)
an-action_output: $(an-action.outputs)
targets:
- type: "a-target"
- id: "a-target"
ref: "a-target"
inputs:
consensus_output: $(a-consensus.outputs)
Expand All @@ -58,28 +58,28 @@ targets:
name: "circular relationship",
yaml: `
triggers:
- type: "a-trigger"
- id: "a-trigger"
actions:
- type: "an-action"
- id: "an-action"
ref: "an-action"
inputs:
trigger_output: $(trigger.outputs)
output: $(a-second-action.outputs)
- type: "a-second-action"
- id: "a-second-action"
ref: "a-second-action"
inputs:
output: $(an-action.outputs)
consensus:
- type: "a-consensus"
- id: "a-consensus"
ref: "a-consensus"
inputs:
trigger_output: $(trigger.outputs)
an-action_output: $(an-action.outputs)
targets:
- type: "a-target"
- id: "a-target"
ref: "a-target"
inputs:
consensus_output: $(a-consensus.outputs)
Expand All @@ -90,32 +90,32 @@ targets:
name: "indirect circular relationship",
yaml: `
triggers:
- type: "a-trigger"
- id: "a-trigger"
actions:
- type: "an-action"
- id: "an-action"
ref: "an-action"
inputs:
trigger_output: $(trigger.outputs)
action_output: $(a-third-action.outputs)
- type: "a-second-action"
- id: "a-second-action"
ref: "a-second-action"
inputs:
output: $(an-action.outputs)
- type: "a-third-action"
- id: "a-third-action"
ref: "a-third-action"
inputs:
output: $(a-second-action.outputs)
consensus:
- type: "a-consensus"
- id: "a-consensus"
ref: "a-consensus"
inputs:
trigger_output: $(trigger.outputs)
an-action_output: $(an-action.outputs)
targets:
- type: "a-target"
- id: "a-target"
ref: "a-target"
inputs:
consensus_output: $(a-consensus.outputs)
Expand All @@ -126,23 +126,23 @@ targets:
name: "relationship doesn't exist",
yaml: `
triggers:
- type: "a-trigger"
- id: "a-trigger"
actions:
- type: "an-action"
- id: "an-action"
ref: "an-action"
inputs:
trigger_output: $(trigger.outputs)
action_output: $(missing-action.outputs)
consensus:
- type: "a-consensus"
- id: "a-consensus"
ref: "a-consensus"
inputs:
an-action_output: $(an-action.outputs)
targets:
- type: "a-target"
- id: "a-target"
ref: "a-target"
inputs:
consensus_output: $(a-consensus.outputs)
Expand All @@ -153,23 +153,23 @@ targets:
name: "two trigger nodes",
yaml: `
triggers:
- type: "a-trigger"
- type: "a-second-trigger"
- id: "a-trigger"
- id: "a-second-trigger"
actions:
- type: "an-action"
- id: "an-action"
ref: "an-action"
inputs:
trigger_output: $(trigger.outputs)
consensus:
- type: "a-consensus"
- id: "a-consensus"
ref: "a-consensus"
inputs:
an-action_output: $(an-action.outputs)
targets:
- type: "a-target"
- id: "a-target"
ref: "a-target"
inputs:
consensus_output: $(a-consensus.outputs)
Expand All @@ -191,21 +191,21 @@ targets:
name: "non-trigger step with no dependent refs",
yaml: `
triggers:
- type: "a-trigger"
- type: "a-second-trigger"
- id: "a-trigger"
- id: "a-second-trigger"
actions:
- type: "an-action"
- id: "an-action"
ref: "an-action"
inputs:
hello: "world"
consensus:
- type: "a-consensus"
- id: "a-consensus"
ref: "a-consensus"
inputs:
trigger_output: $(trigger.outputs)
action_output: $(an-action.outputs)
targets:
- type: "a-target"
- id: "a-target"
ref: "a-target"
inputs:
consensus_output: $(a-consensus.outputs)
Expand Down

0 comments on commit c4adf17

Please sign in to comment.