Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passthrough encoder #942

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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 @@ -22,6 +22,7 @@ func (c IdenticalConsensusConfig[T]) New(w *sdk.WorkflowSpecFactory, ref string,
Config: map[string]any{
"encoder": c.Encoder,
"encoder_config": c.EncoderConfig,
"aggregation_config": make(map[string]interface{}),
"aggregation_method": "identical",
"report_id": c.ReportID,
"key_id": c.KeyID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestIdenticalConsensus(t *testing.T) {
Config: map[string]any{
"encoder": "EVM",
"encoder_config": map[string]any{},
"aggregation_config": map[string]any{},
"aggregation_method": "identical",
"report_id": "0001",
"key_id": "evm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"encoder": {
"type": "string",
"enum": ["EVM", "ValueMap"]
"enum": ["EVM", "ValueMap", "Passthrough"]
},
"encoder_config": {
"type": "object",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 140 additions & 0 deletions pkg/capabilities/consensus/ocr3/passthrough_encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package ocr3

import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"fmt"

consensustypes "github.com/smartcontractkit/chainlink-common/pkg/capabilities/consensus/ocr3/types"
"github.com/smartcontractkit/chainlink-common/pkg/values"
)

type PassthroughEncoder struct{}

// Brought in from CL core.
type ReportV1Metadata struct {
Version uint8
WorkflowExecutionID [32]byte
Timestamp uint32
DonID uint32
DonConfigVersion uint32
WorkflowCID [32]byte
WorkflowName [10]byte
WorkflowOwner [20]byte
ReportID [2]byte
}

func (rm ReportV1Metadata) Encode() ([]byte, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, rm)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (rm ReportV1Metadata) Length() int {
bytes, err := rm.Encode()
if err != nil {
return 0
}
return len(bytes)
}

func (v PassthroughEncoder) Encode(ctx context.Context, input values.Map) ([]byte, error) {
metaMap, ok := input.Underlying[consensustypes.MetadataFieldName]
if !ok {
return nil, fmt.Errorf("expected metadata field to be present: %s", consensustypes.MetadataFieldName)
}

dataMap, ok := input.Underlying["0"]
if !ok {
return nil, fmt.Errorf("expected metadata field to be present: %s", "0")
}

var meta consensustypes.Metadata
err := metaMap.UnwrapTo(&meta)
if err != nil {
return nil, err
}

var data []byte
err = dataMap.UnwrapTo(&data)
if err != nil {
return nil, err
}

return prependMetadataFields(meta, data)
}

func prependMetadataFields(meta consensustypes.Metadata, userPayload []byte) ([]byte, error) {
var err error
var result []byte

// 1. Version (1 byte)
if meta.Version > 255 {
return nil, fmt.Errorf("version must be between 0 and 255")
}
result = append(result, byte(meta.Version))

// 2. Execution ID (32 bytes)
if result, err = decodeAndAppend(meta.ExecutionID, 32, result, "ExecutionID"); err != nil {
return nil, err
}

// 3. Timestamp (4 bytes)
tsBytes := make([]byte, 4)
binary.BigEndian.PutUint32(tsBytes, meta.Timestamp)
result = append(result, tsBytes...)

// 4. DON ID (4 bytes)
donIDBytes := make([]byte, 4)
binary.BigEndian.PutUint32(donIDBytes, meta.DONID)
result = append(result, donIDBytes...)

// 5. DON config version (4 bytes)
cfgVersionBytes := make([]byte, 4)
binary.BigEndian.PutUint32(cfgVersionBytes, meta.DONConfigVersion)
result = append(result, cfgVersionBytes...)

// 6. Workflow ID / spec hash (32 bytes)
if result, err = decodeAndAppend(meta.WorkflowID, 32, result, "WorkflowID"); err != nil {
return nil, err
}

// 7. Workflow Name (7 bytes but we pad to 10)
if result, err = decodeAndAppend(meta.WorkflowName, 10, result, "WorkflowName"); err != nil {
return nil, err
}

// 8. Workflow Owner (20 bytes)
if result, err = decodeAndAppend(meta.WorkflowOwner, 20, result, "WorkflowOwner"); err != nil {
return nil, err
}

// 9. Report ID (2 bytes)
if result, err = decodeAndAppend(meta.ReportID, 2, result, "ReportID"); err != nil {
return nil, err
}

return append(result, userPayload...), nil
}

func decodeAndAppend(id string, expectedLen int, prevResult []byte, logName string) ([]byte, error) {
b, err := hex.DecodeString(id)
if err != nil {
return nil, fmt.Errorf("failed to hex-decode %s (%s): %w", logName, id, err)
}
if len(b) > expectedLen {
return nil, fmt.Errorf("incorrect length for id %s (%s), expected at most %d bytes, got %d", logName, id, expectedLen, len(b))
}
if len(b) < expectedLen {
padding := make([]byte, expectedLen-len(b))
b = append(b, padding...)
}
return append(prevResult, b...), nil
}

var _ consensustypes.Encoder = (*PassthroughEncoder)(nil)
60 changes: 60 additions & 0 deletions pkg/capabilities/consensus/ocr3/passthrough_encoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ocr3_test

import (
"bytes"
"encoding/binary"
"encoding/hex"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/capabilities/consensus/ocr3"
consensustypes "github.com/smartcontractkit/chainlink-common/pkg/capabilities/consensus/ocr3/types"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
"github.com/smartcontractkit/chainlink-common/pkg/values"
)

func Test_PassthroughEncoder_Encode(t *testing.T) {
t.Parallel()
input := map[string]any{
"INTERNAL_METADATA": consensustypes.Metadata{
Version: 1,
ExecutionID: "9916550055460393550154715607595304997184366551966969077087191279",
Timestamp: 1000000000,
DONID: 1,
DONConfigVersion: 1,
WorkflowID: "7558258820343239239009088930242061323966339210999574814231444361",
WorkflowName: "11111111111111",
WorkflowOwner: "5c7cb2d007218404a2f38ade9738735faf56a8c6",
ReportID: "0001",
},
"0": []byte{0x01, 0x02, 0x03},
}
inputWrapped, err := values.NewMap(input)
require.NoError(t, err)

encoder := ocr3.PassthroughEncoder{}
actual, err := encoder.Encode(tests.Context(t), *inputWrapped)
require.NoError(t, err)

assert.True(t, bytes.Equal([]byte{0x01, 0x02, 0x03}, actual[len(actual)-3:]))

metadata, err := decodeReportMetadata(t, actual)
require.NoError(t, err)

assert.Equal(t, uint8(1), metadata.Version)
assert.Equal(t, "9916550055460393550154715607595304997184366551966969077087191279", hex.EncodeToString(metadata.WorkflowExecutionID[:]))
assert.Equal(t, uint32(1000000000), metadata.Timestamp)
assert.Equal(t, uint32(1), metadata.DonID)
assert.Equal(t, uint32(1), metadata.DonConfigVersion)
assert.Equal(t, "7558258820343239239009088930242061323966339210999574814231444361", hex.EncodeToString(metadata.WorkflowCID[:]))
assert.Equal(t, "11111111111111000000", hex.EncodeToString(metadata.WorkflowName[:])) // padded by 3 bytes
assert.Equal(t, "5c7cb2d007218404a2f38ade9738735faf56a8c6", hex.EncodeToString(metadata.WorkflowOwner[:]))
assert.Equal(t, []byte{0x00, 0x01}, metadata.ReportID[:])
}

func decodeReportMetadata(t *testing.T, data []byte) (metadata ocr3.ReportV1Metadata, err error) {
require.True(t, len(data) >= metadata.Length())
return metadata, binary.Read(bytes.NewReader(data[:metadata.Length()]), binary.BigEndian, &metadata)
}
Loading