Skip to content

Commit 1c45112

Browse files
committed
pkg/workflows/sdk: add WorkflowSpec.FormatChart for mermaid flowcharts
1 parent aded1b2 commit 1c45112

17 files changed

+886
-159
lines changed

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/smartcontractkit/chainlink-common
22

3-
go 1.22.0
4-
5-
toolchain go1.22.7
3+
go 1.23
64

75
require (
86
github.com/andybalholm/brotli v1.1.0

pkg/capabilities/capabilities.go

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package capabilities
22

33
import (
4+
"cmp"
45
"context"
56
"fmt"
67
"regexp"
@@ -53,6 +54,26 @@ func (c CapabilityType) IsValid() error {
5354
return fmt.Errorf("invalid capability type: %s", c)
5455
}
5556

57+
func (c CapabilityType) cmpOrder() int {
58+
switch c {
59+
case CapabilityTypeTrigger:
60+
return 0
61+
case CapabilityTypeAction:
62+
return 1
63+
case CapabilityTypeConsensus:
64+
return 2
65+
case CapabilityTypeTarget:
66+
return 3
67+
case CapabilityTypeUnknown:
68+
return 4
69+
default:
70+
return 5
71+
}
72+
}
73+
func (c CapabilityType) Compare(c2 CapabilityType) int {
74+
return cmp.Compare(c.cmpOrder(), c2.cmpOrder())
75+
}
76+
5677
// CapabilityResponse is a struct for the Execute response of a capability.
5778
type CapabilityResponse struct {
5879
Value *values.Map

pkg/workflows/models_yaml_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var transformJSON = cmp.FilterValues(func(x, y []byte) bool {
4949
return out
5050
}))
5151

52-
func TestWorkflowSpecMarshalling(t *testing.T) {
52+
func TestWorkflowSpecYamlMarshalling(t *testing.T) {
5353
t.Parallel()
5454
fixtureReader := yamlFixtureReaderBytes(t, "marshalling")
5555

pkg/workflows/sdk/builder.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ type WorkflowSpecFactory struct {
1515
emptyNames bool
1616
badCapTypes []string
1717
fns map[string]func(runtime Runtime, request capabilities.CapabilityRequest) (capabilities.CapabilityResponse, error)
18+
serialMode bool
19+
prevRefs []string
1820
}
1921

2022
func (w *WorkflowSpecFactory) GetFn(name string) func(sdk Runtime, request capabilities.CapabilityRequest) (capabilities.CapabilityResponse, error) {
2123
return w.fns[name]
2224
}
2325

26+
func (w *WorkflowSpecFactory) BeginSerial() {
27+
w.serialMode = true
28+
}
29+
30+
func (w *WorkflowSpecFactory) BeginAsync() {
31+
w.serialMode = false
32+
}
33+
2434
type CapDefinition[O any] interface {
2535
Ref() any
2636
self() CapDefinition[O]
@@ -98,6 +108,15 @@ type NewWorkflowParams struct {
98108
Name string
99109
}
100110

111+
// NewSerialWorkflowSpecFactory returns a new WorkflowSpecFactory in Serial mode.
112+
// This is the same as calling NewWorkflowSpecFactory then WorkflowSpecFactory.BeginSerial.
113+
func NewSerialWorkflowSpecFactory(params NewWorkflowParams) *WorkflowSpecFactory {
114+
f := NewWorkflowSpecFactory(params)
115+
f.BeginSerial()
116+
return f
117+
}
118+
119+
// NewWorkflowSpecFactory returns a new NewWorkflowSpecFactory.
101120
func NewWorkflowSpecFactory(
102121
params NewWorkflowParams,
103122
) *WorkflowSpecFactory {
@@ -119,6 +138,16 @@ func NewWorkflowSpecFactory(
119138
// AddTo is meant to be called by generated code
120139
func (step *Step[O]) AddTo(w *WorkflowSpecFactory) CapDefinition[O] {
121140
stepDefinition := step.Definition
141+
142+
if w.serialMode {
143+
// ensure we depend on each previous step
144+
for _, prevRef := range w.prevRefs {
145+
if !stepDefinition.Inputs.HasRef(prevRef) {
146+
stepDefinition.Condition = fmt.Sprintf("$(%s.success)", prevRef)
147+
}
148+
}
149+
}
150+
122151
stepRef := stepDefinition.Ref
123152
if w.names[stepRef] && stepDefinition.CapabilityType != capabilities.CapabilityTypeTarget {
124153
w.duplicateNames[stepRef] = true
@@ -143,7 +172,14 @@ func (step *Step[O]) AddTo(w *WorkflowSpecFactory) CapDefinition[O] {
143172
w.badCapTypes = append(w.badCapTypes, stepDefinition.ID)
144173
}
145174

146-
return &capDefinitionImpl[O]{ref: fmt.Sprintf("$(%s.outputs)", step.Definition.Ref)}
175+
c := &capDefinitionImpl[O]{ref: fmt.Sprintf("$(%s.outputs)", step.Definition.Ref)}
176+
177+
if w.serialMode {
178+
w.prevRefs = []string{step.Definition.Ref}
179+
} else {
180+
w.prevRefs = append(w.prevRefs, step.Definition.Ref)
181+
}
182+
return c
147183
}
148184

149185
// AccessField is meant to be used by generated code

pkg/workflows/sdk/builder_test.go

+1-76
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/stretchr/testify/require"
88
"sigs.k8s.io/yaml"
99

10-
"github.com/smartcontractkit/chainlink-common/pkg/capabilities"
1110
ocr3 "github.com/smartcontractkit/chainlink-common/pkg/capabilities/consensus/ocr3/ocr3cap"
1211
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/targets/chainwriter"
1312
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/triggers/streams"
@@ -205,81 +204,7 @@ func TestBuilder_ValidSpec(t *testing.T) {
205204
actual, err := factory.Spec()
206205
require.NoError(t, err)
207206

208-
expected := sdk.WorkflowSpec{
209-
Name: "notccipethsep",
210-
Owner: "0x00000000000000000000000000000000000000aa",
211-
Triggers: []sdk.StepDefinition{
212-
{
213-
214-
Ref: "trigger",
215-
Inputs: sdk.StepInputs{},
216-
Config: map[string]any{"maxFrequencyMs": 5000},
217-
CapabilityType: capabilities.CapabilityTypeTrigger,
218-
},
219-
},
220-
Actions: make([]sdk.StepDefinition, 0),
221-
Consensus: []sdk.StepDefinition{
222-
{
223-
224-
Ref: "data-feeds-report",
225-
Inputs: sdk.StepInputs{
226-
Mapping: map[string]any{"observations": []map[string]any{
227-
{
228-
"Metadata": map[string]any{
229-
"MinRequiredSignatures": 1,
230-
"Signers": []string{"$(trigger.outputs.Metadata.Signer)"},
231-
},
232-
"Payload": []map[string]any{
233-
{
234-
"BenchmarkPrice": "$(trigger.outputs.Payload.BuyPrice)",
235-
"FeedID": anyFakeFeedID,
236-
"FullReport": "$(trigger.outputs.Payload.FullReport)",
237-
"ObservationTimestamp": "$(trigger.outputs.Payload.ObservationTimestamp)",
238-
"ReportContext": "$(trigger.outputs.Payload.ReportContext)",
239-
"Signatures": []string{"$(trigger.outputs.Payload.Signature)"},
240-
},
241-
},
242-
"Timestamp": "$(trigger.outputs.Timestamp)",
243-
},
244-
}},
245-
},
246-
Config: map[string]any{
247-
"aggregation_config": ocr3.DataFeedsConsensusConfigAggregationConfig{
248-
AllowedPartialStaleness: "0.5",
249-
Feeds: map[string]ocr3.FeedValue{
250-
anyFakeFeedID: {
251-
Deviation: "0.5",
252-
Heartbeat: 3600,
253-
},
254-
},
255-
},
256-
"aggregation_method": "data_feeds",
257-
"encoder": "EVM",
258-
"encoder_config": ocr3.EncoderConfig{
259-
"Abi": "(bytes32 FeedID, uint224 Price, uint32 Timestamp)[] Reports",
260-
},
261-
"report_id": "0001",
262-
},
263-
CapabilityType: capabilities.CapabilityTypeConsensus,
264-
},
265-
},
266-
Targets: []sdk.StepDefinition{
267-
{
268-
269-
Inputs: sdk.StepInputs{
270-
Mapping: map[string]any{"signed_report": "$(data-feeds-report.outputs)"},
271-
},
272-
Config: map[string]any{
273-
"address": "0xE0082363396985ae2FdcC3a9F816A586Eed88416",
274-
"deltaStage": "45s",
275-
"schedule": "oneAtATime",
276-
},
277-
CapabilityType: capabilities.CapabilityTypeTarget,
278-
},
279-
},
280-
}
281-
282-
testutils.AssertWorkflowSpec(t, expected, actual)
207+
testutils.AssertWorkflowSpec(t, notStreamSepoliaWorkflowSpec, actual)
283208
})
284209

285210
t.Run("duplicate names causes errors", func(t *testing.T) {

pkg/workflows/sdk/compute_test.go

+2-68
Original file line numberDiff line numberDiff line change
@@ -39,74 +39,8 @@ func TestCompute(t *testing.T) {
3939

4040
spec, err2 := workflow.Spec()
4141
require.NoError(t, err2)
42-
expectedSpec := sdk.WorkflowSpec{
43-
Name: "name",
44-
Owner: "owner",
45-
Triggers: []sdk.StepDefinition{
46-
{
47-
48-
Ref: "trigger",
49-
Inputs: sdk.StepInputs{},
50-
Config: map[string]any{"maxFrequencyMs": 5000},
51-
CapabilityType: capabilities.CapabilityTypeTrigger,
52-
},
53-
},
54-
Actions: []sdk.StepDefinition{
55-
{
56-
57-
Ref: "Compute",
58-
Inputs: sdk.StepInputs{
59-
Mapping: map[string]any{"Arg0": "$(trigger.outputs)"},
60-
},
61-
Config: map[string]any{
62-
"binary": "$(ENV.binary)",
63-
"config": "$(ENV.config)",
64-
},
65-
CapabilityType: capabilities.CapabilityTypeAction,
66-
},
67-
},
68-
Consensus: []sdk.StepDefinition{
69-
{
70-
71-
Ref: "data-feeds-report",
72-
Inputs: sdk.StepInputs{
73-
Mapping: map[string]any{"observations": "$(Compute.outputs.Value)"},
74-
},
75-
Config: map[string]any{
76-
"aggregation_config": ocr3.DataFeedsConsensusConfigAggregationConfig{
77-
AllowedPartialStaleness: "false",
78-
Feeds: map[string]ocr3.FeedValue{
79-
anyFakeFeedID: {
80-
Deviation: "0.5",
81-
Heartbeat: 3600,
82-
},
83-
},
84-
},
85-
"aggregation_method": "data_feeds",
86-
"encoder": ocr3.EncoderEVM,
87-
"encoder_config": ocr3.EncoderConfig{},
88-
"report_id": "0001",
89-
},
90-
CapabilityType: capabilities.CapabilityTypeConsensus,
91-
},
92-
},
93-
Targets: []sdk.StepDefinition{
94-
{
95-
96-
Inputs: sdk.StepInputs{
97-
Mapping: map[string]any{"signed_report": "$(data-feeds-report.outputs)"},
98-
},
99-
Config: map[string]any{
100-
"address": "0xE0082363396985ae2FdcC3a9F816A586Eed88416",
101-
"deltaStage": "45s",
102-
"schedule": "oneAtATime",
103-
},
104-
CapabilityType: capabilities.CapabilityTypeTarget,
105-
},
106-
},
107-
}
10842

109-
testutils.AssertWorkflowSpec(t, expectedSpec, spec)
43+
testutils.AssertWorkflowSpec(t, serialWorkflowSpec, spec)
11044
})
11145

11246
t.Run("compute runs the function and returns the value", func(t *testing.T) {
@@ -133,7 +67,7 @@ func TestCompute(t *testing.T) {
13367
func createWorkflow(fn func(_ sdk.Runtime, inputFeed notstreams.Feed) ([]streams.Feed, error)) *sdk.WorkflowSpecFactory {
13468
workflow := sdk.NewWorkflowSpecFactory(sdk.NewWorkflowParams{
13569
Owner: "owner",
136-
Name: "name",
70+
Name: "serial",
13771
})
13872

13973
trigger := notstreams.TriggerConfig{MaxFrequencyMs: 5000}.New(workflow)

pkg/workflows/sdk/helper_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sdk
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func (w *WorkflowSpecFactory) MustSpec(t *testing.T) WorkflowSpec {
10+
t.Helper()
11+
s, err := w.Spec()
12+
require.NoError(t, err)
13+
return s
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
```mermaid
2+
flowchart
3+
4+
trigger[\"<b>trigger</b><br>trigger<br><i>(basic-test-trigger[at]1.0.0)</i>"/]
5+
6+
compute["<b>compute</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
7+
get-bar -- Value --> compute
8+
get-baz -- Value --> compute
9+
get-foo -- Value --> compute
10+
11+
get-bar["<b>get-bar</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
12+
trigger -- cool_output --> get-bar
13+
14+
get-baz["<b>get-baz</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
15+
trigger -- cool_output --> get-baz
16+
17+
get-foo["<b>get-foo</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
18+
trigger -- cool_output --> get-foo
19+
20+
consensus[["<b>consensus</b><br>consensus<br><i>(offchain_reporting[at]1.0.0)</i>"]]
21+
compute -- Value --> consensus
22+
23+
unnamed6[/"target<br><i>(id)</i>"\]
24+
consensus --> unnamed6
25+
26+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
```mermaid
2+
flowchart
3+
4+
trigger[\"<b>trigger</b><br>trigger<br><i>(basic-test-trigger[at]1.0.0)</i>"/]
5+
6+
compute["<b>compute</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
7+
get-bar -- Value --> compute
8+
get-baz -- Value --> compute
9+
get-foo -- Value --> compute
10+
11+
get-bar["<b>get-bar</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
12+
get-foo -..-> get-bar
13+
trigger -- cool_output --> get-bar
14+
15+
get-baz["<b>get-baz</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
16+
get-bar -..-> get-baz
17+
trigger -- cool_output --> get-baz
18+
19+
get-foo["<b>get-foo</b><br>action<br><i>(custom_compute[at]1.0.0)</i>"]
20+
trigger -- cool_output --> get-foo
21+
22+
consensus[["<b>consensus</b><br>consensus<br><i>(offchain_reporting[at]1.0.0)</i>"]]
23+
compute -- Value --> consensus
24+
25+
unnamed6[/"target<br><i>(id)</i>"\]
26+
consensus --> unnamed6
27+
28+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
```mermaid
2+
flowchart
3+
4+
trigger[\"<b>trigger</b><br>trigger<br><i>(notstreams[at]1.0.0)</i>"/]
5+
6+
data-feeds-report[["<b>data-feeds-report</b><br>consensus<br><i>(offchain_reporting[at]1.0.0)</i>"]]
7+
trigger -- Metadata.Signer<br>Payload.BuyPrice<br>Payload.FullReport<br>Payload.ObservationTimestamp<br>Payload.ReportContext<br>Payload.Signature<br>Timestamp --> data-feeds-report
8+
9+
unnamed2[/"target<br><i>(write_ethereum-testnet-sepolia[at]1.0.0)</i>"\]
10+
data-feeds-report --> unnamed2
11+
12+
```

0 commit comments

Comments
 (0)