-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add wasm spec factory #14458
base: develop
Are you sure you want to change the base?
Add wasm spec factory #14458
Changes from 4 commits
3be5957
028ada8
8cd4100
cfcfaf6
a0cd4bd
aef4662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build wasip1 | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/wasm" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/capabilities/cli/cmd/testdata/fixtures/capabilities/basictrigger" | ||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk" | ||
) | ||
|
||
func BuildWorkflow(config []byte) *sdk.WorkflowSpecFactory { | ||
params := sdk.NewWorkflowParams{} | ||
if err := json.Unmarshal(config, ¶ms); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
workflow := sdk.NewWorkflowSpecFactory(params) | ||
|
||
triggerCfg := basictrigger.TriggerConfig{Name: "trigger", Number: 100} | ||
_ = triggerCfg.New(workflow) | ||
|
||
return workflow | ||
} | ||
|
||
func main() { | ||
runner := wasm.NewRunner() | ||
workflow := BuildWorkflow(runner.Config()) | ||
runner.Run(workflow) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package job | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk" | ||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/wasm/host" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
) | ||
|
||
type WasmFileSpecFactory struct{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For visibility to reviewers: In a side-channel discussion with Cedric, he mentioned that he thinks this should support URLs as well. I commented that I thought by the time the workflow runs, it would have already ensured that the file is on the box locally. I was thinking that if it needs to use HTTP to fetch the file, that could be done just once. The HTTP invoker could have a deterministic location to save the workflow file. It could then check if the file exists and download it if it doesn’t. Once the file is on disk, it would invoke the WASM file spec. I’m open to hearing what others think as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One other potential use case, if we end up having any pre-build WASMs that users can run by providing their own config, we could leave them in fixed locations |
||
|
||
func (w WasmFileSpecFactory) Spec(ctx context.Context, lggr logger.Logger, rawSpec, config []byte) (sdk.WorkflowSpec, error) { | ||
moduleConfig := &host.ModuleConfig{Logger: lggr} | ||
spec, err := host.GetWorkflowSpec(moduleConfig, rawSpec, config) | ||
if err != nil { | ||
return sdk.WorkflowSpec{}, err | ||
} else if spec == nil { | ||
return sdk.WorkflowSpec{}, errors.New("workflow spec not found when running wasm") | ||
} | ||
|
||
return *spec, nil | ||
} | ||
|
||
func (w WasmFileSpecFactory) RawSpec(_ context.Context, wf string) ([]byte, error) { | ||
return os.ReadFile(wf) | ||
} | ||
|
||
var _ SDKWorkflowSpecFactory = (*WasmFileSpecFactory)(nil) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package job_test | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/sdk" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/workflows/wasm/host" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils" | ||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/job" | ||
) | ||
|
||
func TestWasmFileSpecFactory(t *testing.T) { | ||
binaryLocation := createTestBinary(t) | ||
config, err := json.Marshal(sdk.NewWorkflowParams{ | ||
Owner: "owner", | ||
Name: "name", | ||
}) | ||
require.NoError(t, err) | ||
|
||
factory := job.WasmFileSpecFactory{} | ||
rawSpec, err := factory.RawSpec(testutils.Context(t), binaryLocation) | ||
require.NoError(t, err) | ||
actual, err := factory.Spec(testutils.Context(t), logger.NullLogger, rawSpec, config) | ||
require.NoError(t, err) | ||
|
||
rawBinary, err := os.ReadFile(binaryLocation) | ||
require.NoError(t, err) | ||
expected, err := host.GetWorkflowSpec(&host.ModuleConfig{Logger: logger.NullLogger}, rawBinary, config) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, *expected, actual) | ||
} | ||
|
||
func createTestBinary(t *testing.T) string { | ||
const testBinaryLocation = "testdata/wasm/testmodule.wasm" | ||
|
||
cmd := exec.Command("go", "build", "-o", testBinaryLocation, "github.com/smartcontractkit/chainlink/v2/core/services/job/testdata/wasm") | ||
cmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm") | ||
|
||
output, err := cmd.CombinedOutput() | ||
require.NoError(t, err, string(output)) | ||
|
||
return testBinaryLocation | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a logger for validation? Typically we want to either log something, (x)or return an error to be logged - not both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed it to call into the WASM creation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather change the WASM runner to not need it if we're not going to use it.