diff --git a/core/services/workflows/syncer/v2/contract_workflow_source_test.go b/core/services/workflows/syncer/v2/contract_workflow_source_test.go index f0d1ebbde5d..196e9c5f528 100644 --- a/core/services/workflows/syncer/v2/contract_workflow_source_test.go +++ b/core/services/workflows/syncer/v2/contract_workflow_source_test.go @@ -3,6 +3,8 @@ package v2 import ( "context" "errors" + "fmt" + "math/big" "reflect" "testing" @@ -37,6 +39,9 @@ var ( type mockWorkflowContractReader struct { commontypes.ContractReader workflowList []workflow_registry_wrapper_v2.WorkflowRegistryWorkflowMetadataView + // onGet, when set, returns the workflow list for a specific query (e.g. one page). + onGet func(params GetWorkflowListByDONParams) []workflow_registry_wrapper_v2.WorkflowRegistryWorkflowMetadataView + calls []GetWorkflowListByDONParams head *commontypes.Head getLatestErr error bindErr error @@ -47,17 +52,30 @@ func (m *mockWorkflowContractReader) GetLatestValueWithHeadData( _ context.Context, _ string, _ primitives.ConfidenceLevel, - _ any, + params any, result any, ) (*commontypes.Head, error) { if m.getLatestErr != nil { return nil, m.getLatestErr } + list := m.workflowList + if p, ok := params.(GetWorkflowListByDONParams); ok { + // The caller mutates Start between pages, so snapshot it for assertions. + m.calls = append(m.calls, GetWorkflowListByDONParams{ + DonFamily: p.DonFamily, + Start: new(big.Int).Set(p.Start), + Limit: new(big.Int).Set(p.Limit), + }) + if m.onGet != nil { + list = m.onGet(p) + } + } + resultVal := reflect.ValueOf(result).Elem() listField := resultVal.FieldByName("List") if listField.IsValid() && listField.CanSet() { - listField.Set(reflect.ValueOf(m.workflowList)) + listField.Set(reflect.ValueOf(list)) } return m.head, nil @@ -134,6 +152,56 @@ func TestContractWorkflowSource_ListWorkflowMetadata_Success(t *testing.T) { assert.Equal(t, "workflow-2", wfs[1].WorkflowName) } +func TestContractWorkflowSource_ListWorkflowMetadata_PaginatesAcrossPages(t *testing.T) { + t.Parallel() + lggr := logger.TestLogger(t) + ctx := t.Context() + + // One full page plus a short page so the loop is exercised end to end. + total := int(MaxResultsPerQuery) + 3 + all := make([]workflow_registry_wrapper_v2.WorkflowRegistryWorkflowMetadataView, 0, total) + for i := range total { + all = append(all, createTestWorkflowMetadata(fmt.Sprintf("workflow-%d", i), "family-a")) + } + + mockReader := &mockWorkflowContractReader{ + onGet: func(params GetWorkflowListByDONParams) []workflow_registry_wrapper_v2.WorkflowRegistryWorkflowMetadataView { + start := int(params.Start.Int64()) + if start >= len(all) { + return nil + } + end := min(start+int(params.Limit.Int64()), len(all)) + return all[start:end] + }, + head: &commontypes.Head{Height: "100"}, + } + + source := NewContractWorkflowSource( + lggr, + func(_ context.Context, _ []byte) (commontypes.ContractReader, error) { + return mockReader, nil + }, + testOwnerAddress, + "test-chain-selector", + ) + source.contractReader = mockReader + + don := capabilities.DON{ + ID: 1, + Families: []string{"family-a"}, + } + + wfs, _, err := source.ListWorkflowMetadata(ctx, don) + require.NoError(t, err) + assert.Len(t, wfs, total) + + require.Len(t, mockReader.calls, 2) + assert.Equal(t, int64(0), mockReader.calls[0].Start.Int64()) + assert.Equal(t, MaxResultsPerQuery, mockReader.calls[0].Limit.Int64()) + assert.Equal(t, MaxResultsPerQuery, mockReader.calls[1].Start.Int64()) + assert.Equal(t, MaxResultsPerQuery, mockReader.calls[1].Limit.Int64()) +} + func TestContractWorkflowSource_ListWorkflowMetadata_MultipleDONFamilies(t *testing.T) { t.Parallel() lggr := logger.TestLogger(t) diff --git a/core/services/workflows/syncer/v2/workflow_registry.go b/core/services/workflows/syncer/v2/workflow_registry.go index 70929e42260..a8e39a1ab2e 100644 --- a/core/services/workflows/syncer/v2/workflow_registry.go +++ b/core/services/workflows/syncer/v2/workflow_registry.go @@ -53,8 +53,10 @@ var ( defaultTickIntervalForAllowlistedRequests = 5 * time.Second // MaxResultsPerQuery defines the maximum number of results that can be queried in a single request. - // The default value of 1,000 was chosen based on expected system performance and typical use cases. - MaxResultsPerQuery = int64(1_000) + // getWorkflowListByDON costs roughly 50k gas per returned workflow, so the page size must keep a + // single eth_call well under the RPC node's gas cap (commonly 16,777,216). At 1,000 the call needs + // >30M gas once ~600 workflows are indexed and is rejected outright; 250 keeps a page around 12.5M. + MaxResultsPerQuery = int64(250) ) // WorkflowRegistrySyncer is the public interface of the package.