Skip to content

Commit

Permalink
Fixes padWorkflowName()
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzaldysanchez committed Dec 19, 2024
1 parent 5a0f919 commit 2665cb6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
9 changes: 6 additions & 3 deletions pkg/capabilities/consensus/ocr3/types/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ type Metadata struct {
}

// the contract requires exactly 10 bytes for the workflow name
// the json schema allows for a variable length string <= len(10)
// pad with trailing spaces to meet the contract requirements
// the resulting workflow name should be up to 10 bytes long
// so pad accordingly to meet the contract requirements
func (m *Metadata) padWorkflowName() {
b, err := hex.DecodeString(m.WorkflowName)
if err == nil && len(b) < 10 {
m.WorkflowName = hex.EncodeToString(append(b, make([]byte, 10-len(b))...))
// Each byte is 2 characters, so we need to pad with 0s
neededBytes := append(b, make([]byte, 10-len(b))...)
m.WorkflowName = hex.EncodeToString(neededBytes)
} else if len(m.WorkflowName) < 10 {
// Pad with spaces
suffix := strings.Repeat(" ", 10-len(m.WorkflowName))
m.WorkflowName += suffix
}
Expand Down
23 changes: 15 additions & 8 deletions pkg/capabilities/consensus/ocr3/types/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@ func TestMetadata_padWorkflowName(t *testing.T) {
want string
}{
{
name: "padWorkflowName 1",
name: "padWorkflowName hex with 9 bytes",
fields: fields{
WorkflowName: "123456789",
WorkflowName: "ABCD1234EF567890AB",
},
want: "123456789 ",
want: "abcd1234ef567890ab00",
},
{
name: "padWorkflowName 0",
name: "padWorkflowName hex with 5 bytes",
fields: fields{
WorkflowName: "1234567890",
WorkflowName: "1234ABCD56",
},
want: "1234567890",
want: "1234abcd560000000000",
},
{
name: "padWorkflowName 10",
name: "padWorkflowName empty",
fields: fields{
WorkflowName: "",
},
want: " ",
want: "00000000000000000000",
},
{
name: "padWorkflowName non-hex string",
fields: fields{
WorkflowName: "not-hex",
},
want: "not-hex ",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 2665cb6

Please sign in to comment.