Skip to content

Commit

Permalink
Merge branch 'develop' into keystone-bytes32-secrets-pub-key
Browse files Browse the repository at this point in the history
  • Loading branch information
KuphJr authored Oct 10, 2024
2 parents f29ebcd + c7f2fdf commit 45ad475
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 30 deletions.
28 changes: 18 additions & 10 deletions core/capabilities/webapi/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

const defaultSendChannelBufferSize = 1000

const TriggerType = "web-trigger@1.0.0"
const TriggerType = "web-api-trigger@1.0.0"

var webapiTriggerInfo = capabilities.MustNewCapabilityInfo(
TriggerType,
Expand All @@ -49,6 +49,7 @@ type triggerConnectorHandler struct {
lggr logger.Logger
mu sync.Mutex
registeredWorkflows map[string]webapiTrigger
registry core.CapabilitiesRegistry
}

var _ capabilities.TriggerCapability = (*triggerConnectorHandler)(nil)
Expand All @@ -59,9 +60,11 @@ func NewTrigger(config string, registry core.CapabilitiesRegistry, connector con
return nil, errors.New("missing connector")
}
handler := &triggerConnectorHandler{
CapabilityInfo: webapiTriggerInfo,
Validator: capabilities.NewValidator[webapicap.TriggerConfig, struct{}, capabilities.TriggerResponse](capabilities.ValidatorArgs{Info: webapiTriggerInfo}),
connector: connector,
registeredWorkflows: map[string]webapiTrigger{},
registry: registry,
lggr: lggr.Named("WorkflowConnectorHandler"),
}

Expand Down Expand Up @@ -239,7 +242,14 @@ func (h *triggerConnectorHandler) UnregisterTrigger(ctx context.Context, req cap
return nil
}

func (h *triggerConnectorHandler) Info(ctx context.Context) (capabilities.CapabilityInfo, error) {
return h.CapabilityInfo, nil
}

func (h *triggerConnectorHandler) Start(ctx context.Context) error {
if err := h.registry.Add(ctx, h); err != nil {
return err
}
return h.StartOnce("GatewayConnectorServiceWrapper", func() error {
return h.connector.AddHandler([]string{"web_api_trigger"}, h)
})
Expand All @@ -265,15 +275,13 @@ func (h *triggerConnectorHandler) sendResponse(ctx context.Context, gatewayID st
payloadJSON, _ = json.Marshal(webapicapabilities.TriggerResponsePayload{Status: "ERROR", ErrorMessage: fmt.Errorf("error %s marshalling payload", err.Error()).Error()})
}

msg := &api.Message{
Body: api.MessageBody{
MessageId: requestBody.MessageId,
DonId: requestBody.DonId,
Method: requestBody.Method,
Receiver: requestBody.Sender,
Payload: payloadJSON,
},
body := &api.MessageBody{
MessageId: requestBody.MessageId,
DonId: requestBody.DonId,
Method: requestBody.Method,
Receiver: requestBody.Sender,
Payload: payloadJSON,
}

return h.connector.SendToGateway(ctx, gatewayID, msg)
return h.connector.SignAndSendToGateway(ctx, gatewayID, body)
}
17 changes: 9 additions & 8 deletions core/capabilities/webapi/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func gatewayRequest(t *testing.T, privateKey string, topics string, methodName s

func getResponseFromArg(arg interface{}) (webapicapabilities.TriggerResponsePayload, error) {
var response webapicapabilities.TriggerResponsePayload
err := json.Unmarshal((&(arg.(*api.Message)).Body).Payload, &response)
msgBody := arg.(*api.MessageBody)
err := json.Unmarshal(msgBody.Payload, &response)
return response, err
}

Expand Down Expand Up @@ -177,7 +178,7 @@ func TestTriggerExecute(t *testing.T) {
t.Run("happy case single topic to single workflow", func(t *testing.T) {
gatewayRequest := gatewayRequest(t, privateKey1, `["daily_price_update"]`, "")

th.connector.On("SendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
th.connector.On("SignAndSendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
resp, _ := getResponseFromArg(args.Get(2))
require.Equal(t, webapicapabilities.TriggerResponsePayload{Status: "ACCEPTED"}, resp)
}).Return(nil).Once()
Expand All @@ -199,7 +200,7 @@ func TestTriggerExecute(t *testing.T) {
t.Run("happy case single different topic 2 workflows.", func(t *testing.T) {
gatewayRequest := gatewayRequest(t, privateKey1, `["ad_hoc_price_update"]`, "")

th.connector.On("SendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
th.connector.On("SignAndSendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
resp, _ := getResponseFromArg(args.Get(2))
require.Equal(t, webapicapabilities.TriggerResponsePayload{Status: "ACCEPTED"}, resp)
}).Return(nil).Once()
Expand All @@ -226,7 +227,7 @@ func TestTriggerExecute(t *testing.T) {
t.Run("sad case empty topic 2 workflows", func(t *testing.T) {
gatewayRequest := gatewayRequest(t, privateKey1, `[]`, "")

th.connector.On("SendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
th.connector.On("SignAndSendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
resp, _ := getResponseFromArg(args.Get(2))
require.Equal(t, webapicapabilities.TriggerResponsePayload{Status: "ERROR", ErrorMessage: "empty Workflow Topics"}, resp)
}).Return(nil).Once()
Expand All @@ -239,7 +240,7 @@ func TestTriggerExecute(t *testing.T) {

t.Run("sad case topic with no workflows", func(t *testing.T) {
gatewayRequest := gatewayRequest(t, privateKey1, `["foo"]`, "")
th.connector.On("SendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
th.connector.On("SignAndSendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
resp, _ := getResponseFromArg(args.Get(2))
require.Equal(t, webapicapabilities.TriggerResponsePayload{Status: "ERROR", ErrorMessage: "no Matching Workflow Topics"}, resp)
}).Return(nil).Once()
Expand All @@ -251,7 +252,7 @@ func TestTriggerExecute(t *testing.T) {

t.Run("sad case Not Allowed Sender", func(t *testing.T) {
gatewayRequest := gatewayRequest(t, privateKey2, `["ad_hoc_price_update"]`, "")
th.connector.On("SendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
th.connector.On("SignAndSendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
resp, _ := getResponseFromArg(args.Get(2))

require.Equal(t, webapicapabilities.TriggerResponsePayload{Status: "ERROR", ErrorMessage: "unauthorized Sender 0x2dAC9f74Ee66e2D55ea1B8BE284caFedE048dB3A, messageID 12345"}, resp)
Expand All @@ -264,7 +265,7 @@ func TestTriggerExecute(t *testing.T) {

t.Run("sad case Invalid Method", func(t *testing.T) {
gatewayRequest := gatewayRequest(t, privateKey2, `["ad_hoc_price_update"]`, "boo")
th.connector.On("SendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
th.connector.On("SignAndSendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
resp, _ := getResponseFromArg(args.Get(2))
require.Equal(t, webapicapabilities.TriggerResponsePayload{Status: "ERROR", ErrorMessage: "unsupported method boo"}, resp)
}).Return(nil).Once()
Expand Down Expand Up @@ -333,7 +334,7 @@ func TestTriggerExecute2WorkflowsSameTopicDifferentAllowLists(t *testing.T) {
t.Run("happy case single topic to single workflow", func(t *testing.T) {
gatewayRequest := gatewayRequest(t, privateKey1, `["daily_price_update"]`, "")

th.connector.On("SendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
th.connector.On("SignAndSendToGateway", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
resp, _ := getResponseFromArg(args.Get(2))
require.Equal(t, webapicapabilities.TriggerResponsePayload{Status: "ACCEPTED"}, resp)
}).Return(nil).Once()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$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",
"$id": "https://github.com/smartcontractkit/chainlink/v2/core/capabilities/webapi/webapicap/web-api-trigger@1.0.0",
"$defs": {
"TriggerConfig": {
"type": "object",
Expand Down

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

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

8 changes: 4 additions & 4 deletions core/scripts/gateway/web_api_trigger/invoke_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/api"
)

// https://gateway-us-1.chain.link/web-trigger
// https://gateway-us-1.chain.link/web-api-trigger
// {
// jsonrpc: "2.0",
// id: "...",
// method: "web-trigger",
// method: "web-api-trigger",
// params: {
// signature: "...",
// body: {
// don_id: "workflow_123",
// payload: {
// trigger_id: "web-trigger@1.0.0",
// trigger_id: "web-api-trigger@1.0.0",
// trigger_event_id: "action_1234567890",
// timestamp: 1234567890,
// sub-events: [
Expand Down Expand Up @@ -78,7 +78,7 @@ func main() {
}

payload := `{
"trigger_id": "web-trigger@1.0.0",
"trigger_id": "web-api-trigger@1.0.0",
"trigger_event_id": "action_1234567890",
"timestamp": ` + strconv.Itoa(int(time.Now().Unix())) + `,
"topics": ["daily_price_update"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func triggerRequest(t *testing.T, privateKey string, topics string, methodName s
require.NoError(t, err)
if payload == "" {
payload = `{
"trigger_id": "web-trigger@1.0.0",
"trigger_id": "web-api-trigger@1.0.0",
"trigger_event_id": "action_1234567890",
"timestamp": ` + timestamp + `,
"topics": ` + topics + `,
Expand Down
6 changes: 3 additions & 3 deletions core/services/gateway/handlers/webapicapabilities/webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ type TargetResponsePayload struct {
Body []byte `json:"body,omitempty"` // HTTP response body
}

// https://gateway-us-1.chain.link/web-trigger
// https://gateway-us-1.chain.link/web-api-trigger
//
// {
// jsonrpc: "2.0",
// id: "...",
// method: "web-trigger",
// method: "web-api-trigger",
// params: {
// signature: "...",
// body: {
// don_id: "workflow_123",
// payload: {
// trigger_id: "web-trigger@1.0.0",
// trigger_id: "web-api-trigger@1.0.0",
// trigger_event_id: "action_1234567890",
// timestamp: 1234567890,
// topics: ["daily_price_update"],
Expand Down

0 comments on commit 45ad475

Please sign in to comment.