Skip to content

Commit

Permalink
Dedupe webapi trigger structs (#14733)
Browse files Browse the repository at this point in the history
* Dedupe webapi trigger structs

* Re-generate event trigger to get comments
  • Loading branch information
nolag authored Oct 15, 2024
1 parent 4b21c32 commit a2b8566
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 65 deletions.
10 changes: 5 additions & 5 deletions core/capabilities/webapi/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type triggerConnectorHandler struct {
services.StateMachine

capabilities.CapabilityInfo
capabilities.Validator[webapicap.TriggerConfig, struct{}, capabilities.TriggerResponse]
capabilities.Validator[webapicap.TriggerConfig, struct{}, webapicap.TriggerRequestPayload]
connector connector.GatewayConnector
lggr logger.Logger
mu sync.Mutex
Expand All @@ -61,7 +61,7 @@ func NewTrigger(config string, registry core.CapabilitiesRegistry, connector con
}
handler := &triggerConnectorHandler{
CapabilityInfo: webapiTriggerInfo,
Validator: capabilities.NewValidator[webapicap.TriggerConfig, struct{}, capabilities.TriggerResponse](capabilities.ValidatorArgs{Info: webapiTriggerInfo}),
Validator: capabilities.NewValidator[webapicap.TriggerConfig, struct{}, webapicap.TriggerRequestPayload](capabilities.ValidatorArgs{Info: webapiTriggerInfo}),
connector: connector,
registeredWorkflows: map[string]webapiTrigger{},
registry: registry,
Expand All @@ -72,7 +72,7 @@ func NewTrigger(config string, registry core.CapabilitiesRegistry, connector con
}

// processTrigger iterates over each topic, checking against senders and rateLimits, then starting event processing and responding
func (h *triggerConnectorHandler) processTrigger(ctx context.Context, gatewayID string, body *api.MessageBody, sender ethCommon.Address, payload webapicapabilities.TriggerRequestPayload) error {
func (h *triggerConnectorHandler) processTrigger(ctx context.Context, gatewayID string, body *api.MessageBody, sender ethCommon.Address, payload webapicap.TriggerRequestPayload) error {
// Pass on the payload with the expectation that it's in an acceptable format for the executor
wrappedPayload, err := values.WrapMap(payload)
if err != nil {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (h *triggerConnectorHandler) processTrigger(ctx context.Context, gatewayID
continue
}
fullyMatchedWorkflows++
TriggerEventID := body.Sender + payload.TriggerEventID
TriggerEventID := body.Sender + payload.TriggerEventId
tr := capabilities.TriggerResponse{
Event: capabilities.TriggerEvent{
TriggerType: TriggerType,
Expand Down Expand Up @@ -135,7 +135,7 @@ func (h *triggerConnectorHandler) HandleGatewayMessage(ctx context.Context, gate
// TODO: Validate Signature
body := &msg.Body
sender := ethCommon.HexToAddress(body.Sender)
var payload webapicapabilities.TriggerRequestPayload
var payload webapicap.TriggerRequestPayload
err := json.Unmarshal(body.Payload, &payload)
if err != nil {
h.lggr.Errorw("error decoding payload", "err", err)
Expand Down
10 changes: 6 additions & 4 deletions core/capabilities/webapi/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/logger"
registrymock "github.com/smartcontractkit/chainlink-common/pkg/types/core/mocks"
"github.com/smartcontractkit/chainlink-common/pkg/values"

"github.com/smartcontractkit/chainlink/v2/core/capabilities/webapi/webapicap"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
corelogger "github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/api"
Expand Down Expand Up @@ -191,7 +193,7 @@ func TestTriggerExecute(t *testing.T) {

requireNoChanMsg(t, channel2)
data := received.Event.Outputs
var payload webapicapabilities.TriggerRequestPayload
var payload webapicap.TriggerRequestPayload
unwrapErr := data.UnwrapTo(&payload)
require.NoError(t, unwrapErr)
require.Equal(t, payload.Topics, []string{"daily_price_update"})
Expand All @@ -210,15 +212,15 @@ func TestTriggerExecute(t *testing.T) {
sent := <-channel
require.Equal(t, sent.Event.TriggerType, TriggerType)
data := sent.Event.Outputs
var payload webapicapabilities.TriggerRequestPayload
var payload webapicap.TriggerRequestPayload
unwrapErr := data.UnwrapTo(&payload)
require.NoError(t, unwrapErr)
require.Equal(t, payload.Topics, []string{"ad_hoc_price_update"})

sent2 := <-channel2
require.Equal(t, sent2.Event.TriggerType, TriggerType)
data2 := sent2.Event.Outputs
var payload2 webapicapabilities.TriggerRequestPayload
var payload2 webapicap.TriggerRequestPayload
err2 := data2.UnwrapTo(&payload2)
require.NoError(t, err2)
require.Equal(t, payload2.Topics, []string{"ad_hoc_price_update"})
Expand Down Expand Up @@ -346,7 +348,7 @@ func TestTriggerExecute2WorkflowsSameTopicDifferentAllowLists(t *testing.T) {
require.Equal(t, received.Event.TriggerType, TriggerType)
require.NoError(t, chanErr)
data := received.Event.Outputs
var payload webapicapabilities.TriggerRequestPayload
var payload webapicap.TriggerRequestPayload
unwrapErr := data.UnwrapTo(&payload)
require.NoError(t, unwrapErr)
require.Equal(t, payload.Topics, []string{"daily_price_update"})
Expand Down
16 changes: 11 additions & 5 deletions core/capabilities/webapi/webapicap/event_trigger-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"$id": "https://github.com/smartcontractkit/chainlink/v2/core/capabilities/webapi/webapicap/[email protected]",
"$defs": {
"TriggerConfig": {
"description": "See https://gateway-us-1.chain.link/web-api-trigger",
"type": "object",
"properties": {
"allowedSenders": {
Expand Down Expand Up @@ -53,24 +54,29 @@
"type": "object",
"properties": {
"trigger_id": {
"type": "string"
"type": "string",
"description": "ID of the trigger corresponding to the capability ID."
},
"trigger_event_id": {
"type": "string"
"type": "string",
"description": "Uniquely identifies generated event (scoped to trigger_id and sender)."
},
"timestamp": {
"type": "integer",
"format": "int64"
"format": "int64",
"description": "Timestamp of the event (unix time), needs to be within certain freshness to be processed."
},
"topics": {
"type": "array",
"items": {
"type": "string"
"type": "string",
"description" : "An array of a single topic (string) to be started by this event."
}
},
"params": {
"type": "object",
"additionalProperties": true
"additionalProperties": true,
"description": "Key-value pairs for the workflow engine, untranslated."
}
},
"required": ["trigger_id", "trigger_event_id", "timestamp", "topics", "params"],
Expand Down
11 changes: 7 additions & 4 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.

3 changes: 2 additions & 1 deletion core/services/gateway/handlers/webapicapabilities/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"go.uber.org/multierr"

"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/config"
Expand Down Expand Up @@ -202,7 +203,7 @@ func (h *handler) HandleUserMessage(ctx context.Context, msg *api.Message, callb
don := h.don
h.mu.Unlock()
body := msg.Body
var payload TriggerRequestPayload
var payload webapicap.TriggerRequestPayload
err := json.Unmarshal(body.Payload, &payload)
if err != nil {
h.lggr.Errorw("error decoding payload", "err", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package webapicapabilities
import (
"encoding/json"
"fmt"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"strconv"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -294,7 +293,7 @@ func TestHandlerReceiveHTTPMessageFromClient(t *testing.T) {
err := handler.HandleUserMessage(ctx, invalidMsg, ch)
require.NoError(t, err)
resp := <-ch
require.Equal(t, handlers.UserCallbackPayload{Msg: invalidMsg, ErrCode: api.UserMessageParseError, ErrMsg: "error decoding payload"}, resp)
require.Equal(t, handlers.UserCallbackPayload{Msg: invalidMsg, ErrCode: api.UserMessageParseError, ErrMsg: "error decoding payload field params in TriggerRequestPayload: required"}, resp)
_, open := <-ch
require.Equal(t, open, false)
})
Expand All @@ -305,7 +304,7 @@ func TestHandlerReceiveHTTPMessageFromClient(t *testing.T) {
err := handler.HandleUserMessage(ctx, invalidMsg, ch)
require.NoError(t, err)
resp := <-ch
require.Equal(t, handlers.UserCallbackPayload{Msg: invalidMsg, ErrCode: api.UserMessageParseError, ErrMsg: "error decoding payload"}, resp)
require.Equal(t, handlers.UserCallbackPayload{Msg: invalidMsg, ErrCode: api.UserMessageParseError, ErrMsg: "error decoding payload field params in TriggerRequestPayload: required"}, resp)
_, open := <-ch
require.Equal(t, open, false)
})
Expand Down
42 changes: 0 additions & 42 deletions core/services/gateway/handlers/webapicapabilities/webapi.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package webapicapabilities

import (
"github.com/smartcontractkit/chainlink-common/pkg/values"
)

type TargetRequestPayload struct {
URL string `json:"url"` // URL to query, only http and https protocols are supported.
Method string `json:"method,omitempty"` // HTTP verb, defaults to GET.
Expand All @@ -20,44 +16,6 @@ type TargetResponsePayload struct {
Body []byte `json:"body,omitempty"` // HTTP response body
}

// https://gateway-us-1.chain.link/web-api-trigger
//
// {
// jsonrpc: "2.0",
// id: "...",
// method: "web-api-trigger",
// params: {
// signature: "...",
// body: {
// don_id: "workflow_123",
// payload: {
// trigger_id: "[email protected]",
// trigger_event_id: "action_1234567890",
// timestamp: 1234567890,
// topics: ["daily_price_update"],
// params: {
// bid: "101",
// ask: "102"
// }
// }
// }
// }
// }
//
// from Web API Trigger Doc, with modifications.
// trigger_id - ID of the trigger corresponding to the capability ID
// trigger_event_id - uniquely identifies generated event (scoped to trigger_id and sender)
// timestamp - timestamp of the event (unix time), needs to be within certain freshness to be processed
// topics - an array of a single topic (string) to be started by this event
// params - key-value pairs for the workflow engine, untranslated.
type TriggerRequestPayload struct {
TriggerID string `json:"trigger_id"`
TriggerEventID string `json:"trigger_event_id"`
Timestamp int64 `json:"timestamp"`
Topics []string `json:"topics"`
Params values.Map `json:"params"`
}

type TriggerResponsePayload struct {
ErrorMessage string `json:"error_message,omitempty"`
// ERROR, ACCEPTED, PENDING, COMPLETED
Expand Down

0 comments on commit a2b8566

Please sign in to comment.