Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v2
import (
"context"
"errors"
"fmt"
"math/big"
"reflect"
"testing"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions core/services/workflows/syncer/v2/workflow_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding it isn't the right solution.
We at the least need to change it to a cresetting. But also need to write code to smartly reduce it if gas errors are seen

)

// WorkflowRegistrySyncer is the public interface of the package.
Expand Down
Loading