Skip to content

Commit

Permalink
Capability Encoder changes to use PreCodec (#15538)
Browse files Browse the repository at this point in the history
* Draft: Capability Encoder changes to use PreCodec

* (refactor): Changes for config changing from codecFactory -> codecs

* Bump chainlink-common

* Soothe linter
  • Loading branch information
justinkaseman authored Dec 16, 2024
1 parent b1c3395 commit 0fb9d70
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 18 deletions.
2 changes: 1 addition & 1 deletion core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/prometheus/client_golang v1.20.5
github.com/shopspring/decimal v1.4.0
github.com/smartcontractkit/chainlink-automation v0.8.1
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand Down
4 changes: 2 additions & 2 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1150,8 +1150,8 @@ github.com/smartcontractkit/chainlink-automation v0.8.1 h1:sTc9LKpBvcKPc1JDYAmgB
github.com/smartcontractkit/chainlink-automation v0.8.1/go.mod h1:Iij36PvWZ6blrdC5A/nrQUBuf3MH3JvsBB9sSyc9W08=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b h1:iSQJ6ng4FhEswf8SXunGkaJlVP3E3JlgLB8Oo2f3Ud4=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b/go.mod h1:F8xQAIW0ymb2BZhqn89sWZLXreJhM5KDVF6Qb4y44N0=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd h1:lbq/4m6g1OjtBXSu4Tg3qFwu5zlWeUUu1INQOxDlNxA=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805 h1:Pz8jB/6qe10xT10h2S3LFYJrnebNpG5rJ/w16HZGwPQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e h1:PRoeby6ZlTuTkv2f+7tVU4+zboTfRzI+beECynF4JQ0=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e/go.mod h1:mUh5/woemsVaHgTorA080hrYmO3syBCmPdnWc/5dOqk=
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20241202141438-a90db35252db h1:N1RH1hSr2ACzOFc9hkCcjE8pRBTdcU3p8nsTJByaLes=
Expand Down
58 changes: 55 additions & 3 deletions core/services/relay/evm/cap_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

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

Expand All @@ -17,8 +18,9 @@ import (
)

const (
abiConfigFieldName = "abi"
encoderName = "user"
abiConfigFieldName = "abi"
subabiConfigFieldName = "subabi"
encoderName = "user"
)

type capEncoder struct {
Expand Down Expand Up @@ -46,9 +48,33 @@ func NewEVMEncoder(config *values.Map) (consensustypes.Encoder, error) {
return nil, err
}

chainCodecConfig := types.ChainCodecConfig{
TypeABI: string(jsonSelector),
}

var subabi map[string]string
subabiConfig, ok := config.Underlying[subabiConfigFieldName]
if ok {
err2 := subabiConfig.UnwrapTo(&subabi)
if err2 != nil {
return nil, err2
}
codecs, err2 := makePreCodecModifierCodecs(subabi)
if err2 != nil {
return nil, err2
}
chainCodecConfig.ModifierConfigs = commoncodec.ModifiersConfig{
&commoncodec.PreCodecModifierConfig{
Fields: subabi,
Codecs: codecs,
},
}
}

codecConfig := types.CodecConfig{Configs: map[string]types.ChainCodecConfig{
encoderName: {TypeABI: string(jsonSelector)},
encoderName: chainCodecConfig,
}}

c, err := codec.NewCodec(codecConfig)
if err != nil {
return nil, err
Expand All @@ -57,6 +83,32 @@ func NewEVMEncoder(config *values.Map) (consensustypes.Encoder, error) {
return &capEncoder{codec: c}, nil
}

func makePreCodecModifierCodecs(subabi map[string]string) (map[string]commontypes.RemoteCodec, error) {
codecs := map[string]commontypes.RemoteCodec{}
for _, abi := range subabi {
selector, err := abiutil.ParseSelector("inner(" + abi + ")")
if err != nil {
return nil, err
}
jsonSelector, err := json.Marshal(selector.Inputs)
if err != nil {
return nil, err
}
emptyName := ""
codecConfig := types.CodecConfig{Configs: map[string]types.ChainCodecConfig{
emptyName: {
TypeABI: string(jsonSelector),
},
}}
codec, err := codec.NewCodec(codecConfig)
if err != nil {
return nil, err
}
codecs[abi] = codec
}
return codecs, nil
}

func (c *capEncoder) Encode(ctx context.Context, input values.Map) ([]byte, error) {
unwrappedInput, err := input.Unwrap()
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions core/services/relay/evm/cap_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,78 @@ func TestEVMEncoder_InvalidIDs(t *testing.T) {
assert.ErrorContains(t, err, "incorrect length for id")
}

func TestEVMEncoder_SubABI(t *testing.T) {
config := map[string]any{
"abi": "(bytes32 FeedID, bytes Bundle, uint32 Timestamp)[] Reports",
"subabi": map[string]string{
"Reports.Bundle": "uint256 Ask, uint256 Bid",
},
}
wrapped, err := values.NewMap(config)
require.NoError(t, err)
enc, err := evm.NewEVMEncoder(wrapped)
require.NoError(t, err)

type SubReport struct {
Ask int
Bid int
}
type ReportStruct struct {
FeedID [32]byte
Bundle SubReport
Timestamp uint32
}
reportOne := ReportStruct{
FeedID: [32]byte{1},
Bundle: SubReport{
Ask: 5,
Bid: 6,
},
Timestamp: 47890122,
}
reportTwo := ReportStruct{
FeedID: [32]byte{2},
Bundle: SubReport{
Ask: 7,
Bid: 8,
},
Timestamp: 47890122,
}

// output of a reduce aggregator + metadata fields appended by OCR
input := map[string]any{
"Reports": []any{reportOne, reportTwo},
consensustypes.MetadataFieldName: getMetadata(workflowID),
}
wrapped, err = values.NewMap(input)
require.NoError(t, err)
encoded, err := enc.Encode(testutils.Context(t), *wrapped)
require.NoError(t, err)

expected :=
// start of the outer tuple
getHexMetadata() +
// start of the inner tuple (user_fields)
"0000000000000000000000000000000000000000000000000000000000000020" + // offset of Reports array
"0000000000000000000000000000000000000000000000000000000000000002" + // length of Reports array
"0000000000000000000000000000000000000000000000000000000000000040" + // offset of ReportOne
"0000000000000000000000000000000000000000000000000000000000000100" + // offset of ReportTwo
"0100000000000000000000000000000000000000000000000000000000000000" + // ReportOne FeedID
"0000000000000000000000000000000000000000000000000000000000000060" + // offset of ReportOne Bundle
"0000000000000000000000000000000000000000000000000000000002dabeca" + // ReportOne Timestamp
"0000000000000000000000000000000000000000000000000000000000000040" + // length of ReportOne Bundle
"0000000000000000000000000000000000000000000000000000000000000005" + // ReportOne Ask
"0000000000000000000000000000000000000000000000000000000000000006" + // ReportOne Bid
"0200000000000000000000000000000000000000000000000000000000000000" + // ReportTwo FeedID
"0000000000000000000000000000000000000000000000000000000000000060" + // offset of ReportTwo Bundle
"0000000000000000000000000000000000000000000000000000000002dabeca" + // ReportTwo Timestamp
"0000000000000000000000000000000000000000000000000000000000000040" + // length of ReportTwo Bundle
"0000000000000000000000000000000000000000000000000000000000000007" + // ReportTwo Ask
"0000000000000000000000000000000000000000000000000000000000000008" // ReportTwo Bid

require.Equal(t, expected, hex.EncodeToString(encoded))
}

func getHexMetadata() string {
return "01" + executionID + timestampHex + donIDHex + configVersionHex + workflowID + workflowName + workflowOwnerID + reportID
}
Expand Down
2 changes: 1 addition & 1 deletion deployment/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/smartcontractkit/ccip-owner-contracts v0.0.0-salt-fix
github.com/smartcontractkit/chain-selectors v1.0.34
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805
github.com/smartcontractkit/chainlink-protos/job-distributor v0.6.0
github.com/smartcontractkit/chainlink-testing-framework/lib v1.50.13
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
Expand Down
4 changes: 2 additions & 2 deletions deployment/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1417,8 +1417,8 @@ github.com/smartcontractkit/chainlink-automation v0.8.1 h1:sTc9LKpBvcKPc1JDYAmgB
github.com/smartcontractkit/chainlink-automation v0.8.1/go.mod h1:Iij36PvWZ6blrdC5A/nrQUBuf3MH3JvsBB9sSyc9W08=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b h1:iSQJ6ng4FhEswf8SXunGkaJlVP3E3JlgLB8Oo2f3Ud4=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b/go.mod h1:F8xQAIW0ymb2BZhqn89sWZLXreJhM5KDVF6Qb4y44N0=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd h1:lbq/4m6g1OjtBXSu4Tg3qFwu5zlWeUUu1INQOxDlNxA=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805 h1:Pz8jB/6qe10xT10h2S3LFYJrnebNpG5rJ/w16HZGwPQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e h1:PRoeby6ZlTuTkv2f+7tVU4+zboTfRzI+beECynF4JQ0=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e/go.mod h1:mUh5/woemsVaHgTorA080hrYmO3syBCmPdnWc/5dOqk=
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20241202141438-a90db35252db h1:N1RH1hSr2ACzOFc9hkCcjE8pRBTdcU3p8nsTJByaLes=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ require (
github.com/smartcontractkit/chain-selectors v1.0.34
github.com/smartcontractkit/chainlink-automation v0.8.1
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20241202141438-a90db35252db
github.com/smartcontractkit/chainlink-feeds v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,8 @@ github.com/smartcontractkit/chainlink-automation v0.8.1 h1:sTc9LKpBvcKPc1JDYAmgB
github.com/smartcontractkit/chainlink-automation v0.8.1/go.mod h1:Iij36PvWZ6blrdC5A/nrQUBuf3MH3JvsBB9sSyc9W08=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b h1:iSQJ6ng4FhEswf8SXunGkaJlVP3E3JlgLB8Oo2f3Ud4=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b/go.mod h1:F8xQAIW0ymb2BZhqn89sWZLXreJhM5KDVF6Qb4y44N0=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd h1:lbq/4m6g1OjtBXSu4Tg3qFwu5zlWeUUu1INQOxDlNxA=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805 h1:Pz8jB/6qe10xT10h2S3LFYJrnebNpG5rJ/w16HZGwPQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e h1:PRoeby6ZlTuTkv2f+7tVU4+zboTfRzI+beECynF4JQ0=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e/go.mod h1:mUh5/woemsVaHgTorA080hrYmO3syBCmPdnWc/5dOqk=
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20241202141438-a90db35252db h1:N1RH1hSr2ACzOFc9hkCcjE8pRBTdcU3p8nsTJByaLes=
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ require (
github.com/smartcontractkit/chain-selectors v1.0.34
github.com/smartcontractkit/chainlink-automation v0.8.1
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805
github.com/smartcontractkit/chainlink-protos/job-distributor v0.6.0
github.com/smartcontractkit/chainlink-testing-framework/havoc v1.50.2
github.com/smartcontractkit/chainlink-testing-framework/lib v1.50.19
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1438,8 +1438,8 @@ github.com/smartcontractkit/chainlink-automation v0.8.1 h1:sTc9LKpBvcKPc1JDYAmgB
github.com/smartcontractkit/chainlink-automation v0.8.1/go.mod h1:Iij36PvWZ6blrdC5A/nrQUBuf3MH3JvsBB9sSyc9W08=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b h1:iSQJ6ng4FhEswf8SXunGkaJlVP3E3JlgLB8Oo2f3Ud4=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b/go.mod h1:F8xQAIW0ymb2BZhqn89sWZLXreJhM5KDVF6Qb4y44N0=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd h1:lbq/4m6g1OjtBXSu4Tg3qFwu5zlWeUUu1INQOxDlNxA=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805 h1:Pz8jB/6qe10xT10h2S3LFYJrnebNpG5rJ/w16HZGwPQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e h1:PRoeby6ZlTuTkv2f+7tVU4+zboTfRzI+beECynF4JQ0=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e/go.mod h1:mUh5/woemsVaHgTorA080hrYmO3syBCmPdnWc/5dOqk=
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20241202141438-a90db35252db h1:N1RH1hSr2ACzOFc9hkCcjE8pRBTdcU3p8nsTJByaLes=
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/load/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
github.com/slack-go/slack v0.15.0
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805
github.com/smartcontractkit/chainlink-testing-framework/lib v1.50.19
github.com/smartcontractkit/chainlink-testing-framework/seth v1.50.9
github.com/smartcontractkit/chainlink-testing-framework/wasp v1.50.2
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/load/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1429,8 +1429,8 @@ github.com/smartcontractkit/chainlink-automation v0.8.1 h1:sTc9LKpBvcKPc1JDYAmgB
github.com/smartcontractkit/chainlink-automation v0.8.1/go.mod h1:Iij36PvWZ6blrdC5A/nrQUBuf3MH3JvsBB9sSyc9W08=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b h1:iSQJ6ng4FhEswf8SXunGkaJlVP3E3JlgLB8Oo2f3Ud4=
github.com/smartcontractkit/chainlink-ccip v0.0.0-20241213122413-5e8f65dd6b1b/go.mod h1:F8xQAIW0ymb2BZhqn89sWZLXreJhM5KDVF6Qb4y44N0=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd h1:lbq/4m6g1OjtBXSu4Tg3qFwu5zlWeUUu1INQOxDlNxA=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241213210522-edc5deed9ffd/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805 h1:Pz8jB/6qe10xT10h2S3LFYJrnebNpG5rJ/w16HZGwPQ=
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241214155818-b403079b2805/go.mod h1:yti7e1+G9hhkYhj+L5sVUULn9Bn3bBL5/AxaNqdJ5YQ=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e h1:PRoeby6ZlTuTkv2f+7tVU4+zboTfRzI+beECynF4JQ0=
github.com/smartcontractkit/chainlink-cosmos v0.5.2-0.20241202195413-82468150ac1e/go.mod h1:mUh5/woemsVaHgTorA080hrYmO3syBCmPdnWc/5dOqk=
github.com/smartcontractkit/chainlink-data-streams v0.1.1-0.20241202141438-a90db35252db h1:N1RH1hSr2ACzOFc9hkCcjE8pRBTdcU3p8nsTJByaLes=
Expand Down

0 comments on commit 0fb9d70

Please sign in to comment.