Skip to content

Commit

Permalink
Json schema for the webapi trigger (#14627)
Browse files Browse the repository at this point in the history
* Json schema for the webapi trigger

* fix go mod

* update common to try to fix the lint not finding a struct...?

* latest chainlink-common

* backout chainlink-common updates:

* PR review, combine 2 requires in integration-test/load/go.mod

* make gomodtidy

---------

Co-authored-by: David Orchard <[email protected]>
  • Loading branch information
nolag and DavidOrchard authored Oct 3, 2024
1 parent 0dba7fb commit fdbd9da
Show file tree
Hide file tree
Showing 12 changed files with 518 additions and 14 deletions.
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/[email protected]",
"$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

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

0 comments on commit fdbd9da

Please sign in to comment.