Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json schema for the webapi trigger #14627

Merged
merged 9 commits into from
Oct 3, 2024
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
28 changes: 14 additions & 14 deletions core/capabilities/webapi/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/types/core"
"github.com/smartcontractkit/chainlink-common/pkg/values"

"github.com/smartcontractkit/chainlink/v2/core/capabilities/webapi/webapicap"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/api"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/connector"
Expand All @@ -30,29 +32,19 @@ var webapiTriggerInfo = capabilities.MustNewCapabilityInfo(
"A trigger to start workflow execution from a web api call",
)

type Input struct {
}
type Config struct {
AllowedSenders []string `toml:"allowedSenders"`
AllowedTopics []string `toml:"allowedTopics"`
RateLimiter common.RateLimiterConfig `toml:"rateLimiter"`
// RequiredParams is advisory to the web trigger message sender it is not enforced.
RequiredParams []string `toml:"requiredParams"`
}

type webapiTrigger struct {
allowedSenders map[string]bool
allowedTopics map[string]bool
ch chan<- capabilities.TriggerResponse
config Config
config webapicap.TriggerConfig
rateLimiter *common.RateLimiter
}

type triggerConnectorHandler struct {
services.StateMachine

capabilities.CapabilityInfo
capabilities.Validator[Config, Input, capabilities.TriggerResponse]
capabilities.Validator[webapicap.TriggerConfig, struct{}, capabilities.TriggerResponse]
connector connector.GatewayConnector
lggr logger.Logger
mu sync.Mutex
Expand All @@ -67,7 +59,7 @@ func NewTrigger(config string, registry core.CapabilitiesRegistry, connector con
return nil, errors.New("missing connector")
}
handler := &triggerConnectorHandler{
Validator: capabilities.NewValidator[Config, Input, capabilities.TriggerResponse](capabilities.ValidatorArgs{Info: webapiTriggerInfo}),
Validator: capabilities.NewValidator[webapicap.TriggerConfig, struct{}, capabilities.TriggerResponse](capabilities.ValidatorArgs{Info: webapiTriggerInfo}),
connector: connector,
registeredWorkflows: map[string]webapiTrigger{},
lggr: lggr.Named("WorkflowConnectorHandler"),
Expand Down Expand Up @@ -198,7 +190,15 @@ func (h *triggerConnectorHandler) RegisterTrigger(ctx context.Context, req capab
return nil, fmt.Errorf("triggerId %s already registered", req.TriggerID)
}

rateLimiter, err := common.NewRateLimiter(reqConfig.RateLimiter)
rateLimiterConfig := reqConfig.RateLimiter
commonRateLimiter := common.RateLimiterConfig{
GlobalRPS: rateLimiterConfig.GlobalRPS,
GlobalBurst: int(rateLimiterConfig.GlobalBurst),
PerSenderRPS: rateLimiterConfig.PerSenderRPS,
PerSenderBurst: int(rateLimiterConfig.PerSenderBurst),
}

rateLimiter, err := common.NewRateLimiter(commonRateLimiter)
if err != nil {
return nil, err
}
Expand Down
89 changes: 89 additions & 0 deletions core/capabilities/webapi/webapicap/event_trigger-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/smartcontractkit/chainlink/v2/core/capabilities/webapi/webapicap/web-trigger@1.0.0",
"$defs": {
"TriggerConfig": {
"type": "object",
"properties": {
"allowedSenders": {
"type": "array",
"items": {
"type": "string"
}
},
"allowedTopics": {
"type": "array",
"items": {
"type": "string"
}
},
"rateLimiter": {
"$ref": "#/$defs/RateLimiterConfig"
},
"requiredParams": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["allowedSenders", "allowedTopics", "rateLimiter", "requiredParams"],
"additionalProperties": false
},
"RateLimiterConfig": {
"type": "object",
"properties": {
"globalRPS": {
"type": "number"
},
"globalBurst": {
"type": "integer"
},
"perSenderRPS": {
"type": "number"
},
"perSenderBurst": {
"type": "integer"
}
},
"required": ["globalRPS", "globalBurst", "perSenderRPS", "perSenderBurst"],
"additionalProperties": false
},
"TriggerRequestPayload": {
"type": "object",
"properties": {
"trigger_id": {
"type": "string"
},
"trigger_event_id": {
"type": "string"
},
"timestamp": {
"type": "integer",
"format": "int64"
},
"topics": {
"type": "array",
"items": {
"type": "string"
}
},
"params": {
"type": "object",
"additionalProperties": true
}
},
"required": ["trigger_id", "trigger_event_id", "timestamp", "topics", "params"],
"additionalProperties": false
}
},
"type": "object",
"properties": {
"Config": {
"$ref": "#/$defs/TriggerConfig"
},
"Outputs": {
"$ref": "#/$defs/TriggerRequestPayload"
}
}
}
147 changes: 147 additions & 0 deletions core/capabilities/webapi/webapicap/event_trigger_generated.go

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

5 changes: 5 additions & 0 deletions core/capabilities/webapi/webapicap/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package webapicap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be the package for all the webapi capabilities, notably the trigger and the target? We seem to have 3 different package names in the handler now.
webapicap
trigger
target
And also webapicapabilities in gateway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know who uses webapicapabilities, but if it's only for these capabilities, it should have the duplicated types removed in favour of these ones.

webapicap should hold the wasm builders for both the trigger and the target IMO.

The trigger and target can either be in the webapi folder, or subfolders called trigger and target. I'm less opinonated about that one. @DeividasK has probably put more thought into it.


import _ "github.com/smartcontractkit/chainlink-common/pkg/capabilities/cli/cmd" // Required so that the tool is available to be run in go generate below.

//go:generate go run github.com/smartcontractkit/chainlink-common/pkg/capabilities/cli/cmd/generate-types --dir $GOFILE
Loading
Loading