Skip to content

Commit

Permalink
Right-pad workflow name if it is too short
Browse files Browse the repository at this point in the history
  • Loading branch information
vreff committed Jan 9, 2025
1 parent 2450fff commit 7d3cfee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
8 changes: 7 additions & 1 deletion core/services/relay/evm/cap_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,14 @@ func decodeAndAppend(id string, expectedLen int, prevResult []byte, logName stri
if err != nil {
return nil, fmt.Errorf("failed to hex-decode %s (%s): %w", logName, id, err)
}
if len(b) != expectedLen {
if len(b) > expectedLen || (len(b) < expectedLen && logName != "WorkflowName") {
return nil, fmt.Errorf("incorrect length for id %s (%s), expected %d bytes, got %d", logName, id, expectedLen, len(b))
}

// Right-pad short workflow names.
if len(b) < expectedLen && logName == "WorkflowName" {
padding := make([]byte, expectedLen-len(b))
b = append(b, padding...)
}
return append(prevResult, b...), nil
}
25 changes: 15 additions & 10 deletions core/services/relay/evm/cap_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var (
reportB = []byte{0xaa, 0xbb, 0xcc, 0xdd}

workflowID = "15c631d295ef5e32deb99a10ee6804bc4af1385568f9b3363f6552ac6dbb2cef"
workflowName = "aabbccddeeaabbccddee"
workflowName = "aabbccddeeaabb000000"
workflowName2 = "aabbccddeeaabb" // 7 bytes, pads to aabbccddeeaabb000000
donID = uint32(2)
donIDHex = "00000002"
executionID = "8d4e66421db647dd916d3ec28d56188c8d7dae5f808e03d03339ed2562f13bb0"
Expand All @@ -46,7 +47,7 @@ func TestEVMEncoder_SingleField(t *testing.T) {
// output of a DF2.0 aggregator + metadata fields appended by OCR
input := map[string]any{
"Full_reports": []any{reportA, reportB},
consensustypes.MetadataFieldName: getMetadata(workflowID),
consensustypes.MetadataFieldName: getMetadata(workflowID, false),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestEVMEncoder_TwoFields(t *testing.T) {
input := map[string]any{
"Prices": []any{big.NewInt(234), big.NewInt(456)},
"Timestamps": []any{int64(111), int64(222)},
consensustypes.MetadataFieldName: getMetadata(workflowID),
consensustypes.MetadataFieldName: getMetadata(workflowID, true),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
Expand Down Expand Up @@ -120,7 +121,7 @@ func TestEVMEncoder_Tuple(t *testing.T) {
"Prices": []any{big.NewInt(234), big.NewInt(456)},
"Timestamps": []any{int64(111), int64(222)},
},
consensustypes.MetadataFieldName: getMetadata(workflowID),
consensustypes.MetadataFieldName: getMetadata(workflowID, false),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
Expand Down Expand Up @@ -165,7 +166,7 @@ func TestEVMEncoder_ListOfTuples(t *testing.T) {
"Timestamp": int64(222),
},
},
consensustypes.MetadataFieldName: getMetadata(workflowID),
consensustypes.MetadataFieldName: getMetadata(workflowID, false),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
Expand Down Expand Up @@ -199,7 +200,7 @@ func TestEVMEncoder_InvalidIDs(t *testing.T) {
// using an invalid ID
input := map[string]any{
"Full_reports": []any{reportA, reportB},
consensustypes.MetadataFieldName: getMetadata(invalidID),
consensustypes.MetadataFieldName: getMetadata(invalidID, false),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
Expand All @@ -209,7 +210,7 @@ func TestEVMEncoder_InvalidIDs(t *testing.T) {
// using valid hex string of wrong length
input = map[string]any{
"Full_reports": []any{reportA, reportB},
consensustypes.MetadataFieldName: getMetadata(wrongLength),
consensustypes.MetadataFieldName: getMetadata(wrongLength, false),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
Expand Down Expand Up @@ -258,7 +259,7 @@ func TestEVMEncoder_SubABI(t *testing.T) {
// output of a reduce aggregator + metadata fields appended by OCR
input := map[string]any{
"Reports": []any{reportOne, reportTwo},
consensustypes.MetadataFieldName: getMetadata(workflowID),
consensustypes.MetadataFieldName: getMetadata(workflowID, false),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
Expand Down Expand Up @@ -293,15 +294,19 @@ func getHexMetadata() string {
return "01" + executionID + timestampHex + donIDHex + configVersionHex + workflowID + workflowName + workflowOwnerID + reportID
}

func getMetadata(cid string) consensustypes.Metadata {
func getMetadata(cid string, useShortWorkflowName bool) consensustypes.Metadata {
wN := workflowName
if useShortWorkflowName {
wN = workflowName2
}
return consensustypes.Metadata{
Version: 1,
ExecutionID: executionID,
Timestamp: timestampInt,
DONID: donID,
DONConfigVersion: configVersionInt,
WorkflowID: cid,
WorkflowName: workflowName,
WorkflowName: wN,
WorkflowOwner: workflowOwnerID,
ReportID: reportID,
}
Expand Down

0 comments on commit 7d3cfee

Please sign in to comment.