Skip to content

Commit

Permalink
Fix wasm workflow toml fields (#16206)
Browse files Browse the repository at this point in the history
* Fix model fields to parse correctly for a wasm workflow

* casing

* changeset

* add comment

---------

Co-authored-by: Bartek Tofel <tofel.b@gmail.com>
  • Loading branch information
vreff and Tofel authored Feb 5, 2025
1 parent 5e9d9f2 commit 5d635c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-files-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

Fix wasm workflow toml fields
16 changes: 11 additions & 5 deletions core/services/job/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,9 +900,9 @@ type WorkflowSpec struct {
Config string `toml:"config" db:"config"` // the raw representation of the config
// fields derived from the yaml spec, used for indexing the database
// note: i tried to make these private, but translating them to the database seems to require them to be public
WorkflowID string `toml:"-" db:"workflow_id"` // Derived. Do not modify. the CID of the workflow.
WorkflowOwner string `toml:"-" db:"workflow_owner"` // Derived. Do not modify. the owner of the workflow.
WorkflowName string `toml:"-" db:"workflow_name"` // Derived. Do not modify. the name of the workflow.
WorkflowID string `toml:"-" db:"workflow_id"` // Derived. Do not modify. the CID of the workflow.
WorkflowOwner string `toml:"workflow_owner" db:"workflow_owner"`
WorkflowName string `toml:"workflow_name" db:"workflow_name"`
Status WorkflowSpecStatus `db:"status"`
BinaryURL string `db:"binary_url"`
ConfigURL string `db:"config_url"`
Expand Down Expand Up @@ -931,8 +931,14 @@ func (w *WorkflowSpec) Validate(ctx context.Context) error {
return err
}

w.WorkflowOwner = strings.TrimPrefix(s.Owner, "0x") // the json schema validation ensures it is a hex string with 0x prefix, but the database does not store the prefix
w.WorkflowName = s.Name
// For yaml-based workflow specs, use the owner & name fields defined there.
// For wasm workflows, use the `workflow_name` & `workflow_owner` fields directly from the job spec.
if s.Owner+s.Name != "" {
w.WorkflowOwner = strings.TrimPrefix(s.Owner, "0x") // the json schema validation ensures it is a hex string with 0x prefix, but the database does not store the prefix
w.WorkflowName = s.Name
} else {
w.WorkflowOwner = strings.TrimPrefix(w.WorkflowOwner, "0x")
}

if len(w.WorkflowID) != workflowIDLen {
return fmt.Errorf("%w: incorrect length for id %s: expected %d, got %d", ErrInvalidWorkflowID, w.WorkflowID, workflowIDLen, len(w.WorkflowID))
Expand Down
28 changes: 28 additions & 0 deletions core/services/job/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package job_test

import (
_ "embed"
"fmt"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -347,4 +348,31 @@ func TestWorkflowSpec_Validate(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, w.WorkflowID)
})

t.Run("WASM can validate from TOML", func(t *testing.T) {
const wasmWorkfowTomlTemplate = `
workflow_owner = "%s"
workflow_name = "%s"
spec_type = "%s"
workflow = "%s"
config = "%s"
`
configLocation := "testdata/config.json"
tomlSpec := fmt.Sprintf(wasmWorkfowTomlTemplate,
"0x0123456789012345678901234567890123456788",
"wf-2",
job.WASMFile,
createTestBinary(t),
configLocation,
)
var w job.WorkflowSpec
err := toml.Unmarshal([]byte(tomlSpec), &w)
require.NoError(t, err)

err = w.Validate(testutils.Context(t))
require.NoError(t, err)
require.NotEmpty(t, w.WorkflowID)
assert.Equal(t, "0123456789012345678901234567890123456788", w.WorkflowOwner)
assert.Equal(t, "wf-2", w.WorkflowName)
})
}

0 comments on commit 5d635c4

Please sign in to comment.