diff --git a/core/services/cron/cron.go b/core/services/cron/cron.go index 500192554fb..aa4d1d782f1 100644 --- a/core/services/cron/cron.go +++ b/core/services/cron/cron.go @@ -32,6 +32,9 @@ func NewCronFromJobSpec( "jobID", jobSpec.ID, "schedule", jobSpec.CronSpec.CronSchedule, ) + if id := jobSpec.CronSpec.EVMChainID; id != nil { + cronLogger = logger.With("evmChainID", id) + } return &Cron{ cronRunner: cronRunner(), @@ -48,7 +51,7 @@ func (cr *Cron) Start(context.Context) error { _, err := cr.cronRunner.AddFunc(cr.jobSpec.CronSpec.CronSchedule, cr.runPipeline) if err != nil { - cr.logger.Errorw(fmt.Sprintf("Error running cron job %d", cr.jobSpec.ID), "err", err, "schedule", cr.jobSpec.CronSpec.CronSchedule, "jobID", cr.jobSpec.ID) + cr.logger.Errorw(fmt.Sprintf("Error running cron job %d", cr.jobSpec.ID), "err", err) return err } cr.cronRunner.Start() @@ -67,12 +70,17 @@ func (cr *Cron) runPipeline() { ctx, cancel := cr.chStop.NewCtx() defer cancel() + jobSpec := map[string]interface{}{ + "databaseID": cr.jobSpec.ID, + "externalJobID": cr.jobSpec.ExternalJobID, + "name": cr.jobSpec.Name.ValueOrZero(), + } + if id := cr.jobSpec.CronSpec.EVMChainID; id != nil { + jobSpec["evmChainID"] = id.String() + } + vars := pipeline.NewVarsFrom(map[string]interface{}{ - "jobSpec": map[string]interface{}{ - "databaseID": cr.jobSpec.ID, - "externalJobID": cr.jobSpec.ExternalJobID, - "name": cr.jobSpec.Name.ValueOrZero(), - }, + "jobSpec": jobSpec, "jobRun": map[string]interface{}{ "meta": map[string]interface{}{}, }, diff --git a/core/services/job/models.go b/core/services/job/models.go index 5457768141d..a8c12cbece9 100644 --- a/core/services/job/models.go +++ b/core/services/job/models.go @@ -490,6 +490,7 @@ type DirectRequestSpec struct { type CronSpec struct { ID int32 `toml:"-"` CronSchedule string `toml:"schedule"` + EVMChainID *big.Big `toml:"evmChainID"` CreatedAt time.Time `toml:"-"` UpdatedAt time.Time `toml:"-"` } diff --git a/core/services/job/orm.go b/core/services/job/orm.go index efcb0882840..de531985465 100644 --- a/core/services/job/orm.go +++ b/core/services/job/orm.go @@ -502,8 +502,8 @@ func (o *orm) insertKeeperSpec(ctx context.Context, spec *KeeperSpec) (specID in } func (o *orm) insertCronSpec(ctx context.Context, spec *CronSpec) (specID int32, err error) { - return o.prepareQuerySpecID(ctx, `INSERT INTO cron_specs (cron_schedule, created_at, updated_at) - VALUES (:cron_schedule, NOW(), NOW()) + return o.prepareQuerySpecID(ctx, `INSERT INTO cron_specs (cron_schedule, evm_chain_id, created_at, updated_at) + VALUES (:cron_schedule, :evm_chain_id, NOW(), NOW()) RETURNING id;`, spec) } diff --git a/core/store/migrate/migrations/0246_cron_spec_evm_chain_id.sql b/core/store/migrate/migrations/0246_cron_spec_evm_chain_id.sql new file mode 100644 index 00000000000..d2c0b1582e0 --- /dev/null +++ b/core/store/migrate/migrations/0246_cron_spec_evm_chain_id.sql @@ -0,0 +1,5 @@ +-- +goose Up +ALTER TABLE cron_specs ADD COLUMN evm_chain_id numeric(78,0); + +-- +goose Down +ALTER TABLE cron_specs DROP COLUMN evm_chain_id; diff --git a/core/testdata/testspecs/v2_specs.go b/core/testdata/testspecs/v2_specs.go index 949515b3357..3a1798ab5ad 100644 --- a/core/testdata/testspecs/v2_specs.go +++ b/core/testdata/testspecs/v2_specs.go @@ -24,7 +24,7 @@ var ( type = "cron" schemaVersion = 1 schedule = "CRON_TZ=UTC * 0 0 1 1 *" -externalJobID = "%s" +externalJobID = "%s" observationSource = """ ds [type=http method=GET url="https://chain.link/ETH-USD"]; ds_parse [type=jsonparse path="data,price"]; @@ -36,7 +36,20 @@ ds -> ds_parse -> ds_multiply; type = "cron" schemaVersion = 1 schedule = "CRON_TZ=UTC * 0 0 1 1 *" -externalJobID = "%s" +externalJobID = "%s" +observationSource = """ +ds [type=http method=GET url="https://chain.link/ETH-USD"]; +ds_parse [type=jsonparse path="data.price" separator="."]; +ds_multiply [type=multiply times=100]; +ds -> ds_parse -> ds_multiply; +""" +` + CronSpecEVMChainIDTemplate = ` +type = "cron" +schemaVersion = 1 +schedule = "CRON_TZ=UTC * 0 0 1 1 *" +externalJobID = "%s" +evmChainID = "42" observationSource = """ ds [type=http method=GET url="https://chain.link/ETH-USD"]; ds_parse [type=jsonparse path="data.price" separator="."]; @@ -62,7 +75,7 @@ type = "directrequest" schemaVersion = 1 name = "%s" contractAddress = "0x613a38AC1659769640aaE063C651F48E0250454C" -externalJobID = "%s" +externalJobID = "%s" evmChainID = "0" observationSource = """ ds1 [type=http method=GET url="http://example.com" allowunrestrictednetworkaccess="true"]; diff --git a/core/web/jobs_controller_test.go b/core/web/jobs_controller_test.go index 0401dbdb8c8..60abe61537f 100644 --- a/core/web/jobs_controller_test.go +++ b/core/web/jobs_controller_test.go @@ -28,6 +28,7 @@ import ( "github.com/smartcontractkit/chainlink-common/pkg/utils" evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" + ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest" @@ -270,6 +271,25 @@ func TestJobController_Create_HappyPath(t *testing.T) { require.Equal(t, "CRON_TZ=UTC * 0 0 1 1 *", jb.CronSpec.CronSchedule) }, }, + { + name: "cron-evm-chain-id", + tomlTemplate: func(nameAndExternalJobID string) string { + return fmt.Sprintf(testspecs.CronSpecEVMChainIDTemplate, nameAndExternalJobID) + }, + assertion: func(t *testing.T, nameAndExternalJobID string, r *http.Response) { + require.Equal(t, http.StatusOK, r.StatusCode) + resource := presenters.JobResource{} + err := web.ParseJSONAPIResponse(cltest.ParseResponseBody(t, r), &resource) + assert.NoError(t, err) + + jb, err := jorm.FindJob(testutils.Context(t), mustInt32FromString(t, resource.ID)) + require.NoError(t, err) + require.NotNil(t, jb.CronSpec) + + assert.NotNil(t, resource.PipelineSpec.DotDAGSource) + require.Equal(t, ubig.NewI(42), jb.CronSpec.EVMChainID) + }, + }, { name: "directrequest", tomlTemplate: func(nameAndExternalJobID string) string { @@ -464,6 +484,7 @@ targets: t.Run(c.name, func(t *testing.T) { nameAndExternalJobID := uuid.New().String() toml := c.tomlTemplate(nameAndExternalJobID) + t.Log("Job toml:", toml) body, err := json.Marshal(web.CreateJobRequest{ TOML: toml, }) diff --git a/core/web/presenters/job.go b/core/web/presenters/job.go index 8d7dab626cb..ad6bf617a82 100644 --- a/core/web/presenters/job.go +++ b/core/web/presenters/job.go @@ -252,9 +252,10 @@ func NewWebhookSpec(spec *job.WebhookSpec) *WebhookSpec { // CronSpec defines the spec details of a Cron Job type CronSpec struct { - CronSchedule string `json:"schedule" tom:"schedule"` + CronSchedule string `json:"schedule"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` + EVMChainID *big.Big `json:"evmChainID"` } // NewCronSpec generates a new CronSpec from a job.CronSpec @@ -263,6 +264,7 @@ func NewCronSpec(spec *job.CronSpec) *CronSpec { CronSchedule: spec.CronSchedule, CreatedAt: spec.CreatedAt, UpdatedAt: spec.UpdatedAt, + EVMChainID: spec.EVMChainID, } } diff --git a/core/web/presenters/job_test.go b/core/web/presenters/job_test.go index 80856daa8ef..5de71f918e3 100644 --- a/core/web/presenters/job_test.go +++ b/core/web/presenters/job_test.go @@ -374,6 +374,7 @@ func TestJob(t *testing.T) { CronSchedule: cronSchedule, CreatedAt: timestamp, UpdatedAt: timestamp, + EVMChainID: evmChainID, }, ExternalJobID: uuid.MustParse("0EEC7E1D-D0D2-476C-A1A8-72DFB6633F46"), PipelineSpec: &pipeline.Spec{ @@ -404,7 +405,8 @@ func TestJob(t *testing.T) { "cronSpec": { "schedule": "%s", "createdAt":"2000-01-01T00:00:00Z", - "updatedAt":"2000-01-01T00:00:00Z" + "updatedAt":"2000-01-01T00:00:00Z", + "evmChainID":"42" }, "fluxMonitorSpec": null, "gasLimit": null, diff --git a/core/web/resolver/spec.go b/core/web/resolver/spec.go index b3c22b3b342..00b2442acab 100644 --- a/core/web/resolver/spec.go +++ b/core/web/resolver/spec.go @@ -141,6 +141,17 @@ func (r *CronSpecResolver) Schedule() string { return r.spec.CronSchedule } +// EVMChainID resolves the spec's evm chain id. +func (r *CronSpecResolver) EVMChainID() *string { + if r.spec.EVMChainID == nil { + return nil + } + + chainID := r.spec.EVMChainID.String() + + return &chainID +} + // CreatedAt resolves the spec's created at timestamp. func (r *CronSpecResolver) CreatedAt() graphql.Time { return graphql.Time{Time: r.spec.CreatedAt} diff --git a/core/web/resolver/spec_test.go b/core/web/resolver/spec_test.go index 2d5dcc71d1f..69d6a56509c 100644 --- a/core/web/resolver/spec_test.go +++ b/core/web/resolver/spec_test.go @@ -42,6 +42,7 @@ func TestResolver_CronSpec(t *testing.T) { Type: job.Cron, CronSpec: &job.CronSpec{ CronSchedule: "CRON_TZ=UTC 0 0 1 1 *", + EVMChainID: ubig.NewI(42), CreatedAt: f.Timestamp(), }, }, nil) @@ -54,6 +55,7 @@ func TestResolver_CronSpec(t *testing.T) { __typename ... on CronSpec { schedule + evmChainID createdAt } } @@ -67,6 +69,7 @@ func TestResolver_CronSpec(t *testing.T) { "spec": { "__typename": "CronSpec", "schedule": "CRON_TZ=UTC 0 0 1 1 *", + "evmChainID": "42", "createdAt": "2021-01-01T00:00:00Z" } } diff --git a/core/web/schema/type/spec.graphql b/core/web/schema/type/spec.graphql index db33dd14ee2..5a803e2f8ee 100644 --- a/core/web/schema/type/spec.graphql +++ b/core/web/schema/type/spec.graphql @@ -16,6 +16,7 @@ union JobSpec = type CronSpec { schedule: String! + evmChainID: String createdAt: Time! }