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

Implement extra args codec #16016

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a757a6e
Use SVM ABI, needs to check test
huangzhen1997 Jan 21, 2025
7253bba
goimport
huangzhen1997 Jan 21, 2025
0ec8119
update
huangzhen1997 Jan 21, 2025
82a162f
rename
huangzhen1997 Jan 21, 2025
2c7bd05
Merge branch 'solana-offchain-plugin' into NONEVM-1163/implement-extr…
huangzhen1997 Jan 21, 2025
e8bbe9a
tidy
huangzhen1997 Jan 21, 2025
36a5ad1
refactor
huangzhen1997 Jan 21, 2025
3fa671b
fix lint
huangzhen1997 Jan 22, 2025
af29932
fix
huangzhen1997 Jan 22, 2025
2597b48
update
huangzhen1997 Jan 22, 2025
a99bdcc
update
huangzhen1997 Jan 22, 2025
5fe01d2
address Makram comments
huangzhen1997 Jan 23, 2025
c49c04b
lint
huangzhen1997 Jan 23, 2025
7204973
Merge branch 'solana-offchain-plugin' into NONEVM-1163/implement-extr…
huangzhen1997 Jan 23, 2025
a903310
mod tidy
huangzhen1997 Jan 23, 2025
80b3e3b
gomd
huangzhen1997 Jan 23, 2025
afcd808
fix import
huangzhen1997 Jan 24, 2025
6bc6b29
fix broken integration test
huangzhen1997 Jan 24, 2025
a5f84ac
update
huangzhen1997 Jan 24, 2025
d6af37b
fix test
huangzhen1997 Jan 27, 2025
f9489ae
fix test
huangzhen1997 Jan 27, 2025
cf28c84
update source chain
huangzhen1997 Jan 27, 2025
67e0038
update test
huangzhen1997 Jan 27, 2025
a20765b
minor
huangzhen1997 Jan 27, 2025
ee10849
merge develop
huangzhen1997 Jan 28, 2025
f2952a0
implement and add test
huangzhen1997 Jan 28, 2025
a1eacf6
update comments
huangzhen1997 Jan 28, 2025
0b15808
goimport
huangzhen1997 Jan 28, 2025
55b33e3
fix conflicts
huangzhen1997 Jan 28, 2025
8d4d702
update
huangzhen1997 Jan 29, 2025
fc7f0f7
update
huangzhen1997 Jan 29, 2025
9939559
add comment
huangzhen1997 Jan 29, 2025
6e6441d
refactor
huangzhen1997 Jan 29, 2025
7e96d52
update comment
huangzhen1997 Jan 30, 2025
2e8de08
add unit test
huangzhen1997 Jan 30, 2025
5c76148
update
huangzhen1997 Jan 30, 2025
5bcf509
lint
huangzhen1997 Jan 30, 2025
8af2a50
mod tidy
huangzhen1997 Jan 30, 2025
aa9841d
update
huangzhen1997 Jan 30, 2025
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
56 changes: 56 additions & 0 deletions core/capabilities/ccip/ccipevm/extradatadecoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ccipevm

import (
"fmt"

cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
)

func DecodeDestExecDataToMap(DestExecData cciptypes.Bytes) (map[string]interface{}, error) {
destGasAmount, err := abiDecodeUint32(DestExecData)
if err != nil {
return nil, fmt.Errorf("decode dest gas amount: %w", err)
}

return map[string]interface{}{
evmDestExecDataKey: destGasAmount,
}, nil
}

func DecodeExtraArgsToMap(extraArgs []byte) (map[string]any, error) {
if len(extraArgs) < 4 {
return nil, fmt.Errorf("extra args too short: %d, should be at least 4 (i.e the extraArgs tag)", len(extraArgs))
}

var method string
var extraByteOffset int
switch string(extraArgs[:4]) {
case string(evmExtraArgsV1Tag):
// for EVMExtraArgs, the first four bytes is the method name
method = evmV1DecodeName
extraByteOffset = 4
case string(evmExtraArgsV2Tag):
method = evmV2DecodeName
extraByteOffset = 4
case string(svmExtraArgsV1Tag):
// for SVMExtraArgs there's the four bytes plus another 32 bytes padding for the dynamic array
// TODO this is a temporary solution, the evm on-chain side will fix it, so the offset should just be 4 instead of 36
// https://smartcontract-it.atlassian.net/browse/BCI-4180
method = svmV1DecodeName
extraByteOffset = 36
default:
return nil, fmt.Errorf("unknown extra args tag: %x", extraArgs)
}

output := make(map[string]any)
args := make(map[string]interface{})
err := messageHasherABI.Methods[method].Inputs.UnpackIntoMap(args, extraArgs[extraByteOffset:])
if err != nil {
return nil, fmt.Errorf("abi decode extra args %v: %w", method, err)
}

for k, val := range args {
output[k] = val
}
return output, nil
}
105 changes: 105 additions & 0 deletions core/capabilities/ccip/ccipevm/extradatadecoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package ccipevm

import (
"math/big"
"math/rand"
"testing"

"github.com/gagliardetto/solana-go"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/message_hasher"
)

func Test_decodeExtraData(t *testing.T) {
d := testSetup(t)
gasLimit := big.NewInt(rand.Int63())

t.Run("decode extra args into map evm v1", func(t *testing.T) {
encoded, err := d.contract.EncodeEVMExtraArgsV1(nil, message_hasher.ClientEVMExtraArgsV1{
GasLimit: gasLimit,
})
require.NoError(t, err)

m, err := DecodeExtraArgsToMap(encoded)
require.NoError(t, err)
require.Len(t, m, 1)

gl, exist := m["gasLimit"]
require.True(t, exist)
require.Equal(t, gl, gasLimit)
})

t.Run("decode extra args into map evm v2", func(t *testing.T) {
encoded, err := d.contract.EncodeEVMExtraArgsV2(nil, message_hasher.ClientEVMExtraArgsV2{
GasLimit: gasLimit,
AllowOutOfOrderExecution: true,
})
require.NoError(t, err)

m, err := DecodeExtraArgsToMap(encoded)
require.NoError(t, err)
require.Len(t, m, 2)

gl, exist := m["gasLimit"]
require.True(t, exist)
require.Equal(t, gl, gasLimit)

ooe, exist := m["allowOutOfOrderExecution"]
require.True(t, exist)
require.Equal(t, true, ooe)
})

t.Run("decode extra args into map svm", func(t *testing.T) {
key, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
cu := uint32(10000)
bitmap := uint64(4)
ooe := false
tokenReceiver := [32]byte(key.PublicKey().Bytes())
accounts := [][32]byte{[32]byte(key.PublicKey().Bytes())}
decoded, err := d.contract.DecodeSVMExtraArgsV1(nil, cu, bitmap, ooe, tokenReceiver, accounts)
if err != nil {
return
}
encoded, err := d.contract.EncodeSVMExtraArgsV1(nil, decoded)
require.NoError(t, err)

m, err := DecodeExtraArgsToMap(encoded)
require.NoError(t, err)
require.Len(t, m, 5)

cuDecoded, exist := m["computeUnits"]
require.True(t, exist)
require.Equal(t, cuDecoded, cu)

bitmapDecoded, exist := m["accountIsWritableBitmap"]
require.True(t, exist)
require.Equal(t, bitmapDecoded, bitmap)

ooeDecoded, exist := m["allowOutOfOrderExecution"]
require.True(t, exist)
require.Equal(t, ooeDecoded, ooe)

tokenReceiverDecoded, exist := m["tokenReceiver"]
require.True(t, exist)
require.Equal(t, tokenReceiverDecoded, tokenReceiver)

accountsDecoded, exist := m["accounts"]
require.True(t, exist)
require.Equal(t, accountsDecoded, accounts)
})

t.Run("decode dest exec data into map", func(t *testing.T) {
destGasAmount := uint32(10000)
encoded, err := abiEncodeUint32(destGasAmount)
require.NoError(t, err)
m, err := DecodeDestExecDataToMap(encoded)
require.NoError(t, err)
require.Len(t, m, 1)

decoded, exist := m[evmDestExecDataKey]
require.True(t, exist)
require.Equal(t, destGasAmount, decoded)
})
}
11 changes: 9 additions & 2 deletions core/capabilities/ccip/ccipevm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi"
)

const (
svmV1DecodeName = "decodeSVMExtraArgsV1"
evmV1DecodeName = "decodeEVMExtraArgsV1"
evmV2DecodeName = "decodeEVMExtraArgsV2"
evmDestExecDataKey = "destGasAmount"
)

var (
abiUint32 = ABITypeOrPanic("uint32")
TokenDestGasOverheadABI = abi.Arguments{
Expand All @@ -24,9 +31,9 @@ func decodeExtraArgsV1V2(extraArgs []byte) (gasLimit *big.Int, err error) {

var method string
if bytes.Equal(extraArgs[:4], evmExtraArgsV1Tag) {
method = "decodeEVMExtraArgsV1"
method = evmV1DecodeName
} else if bytes.Equal(extraArgs[:4], evmExtraArgsV2Tag) {
method = "decodeEVMExtraArgsV2"
method = evmV2DecodeName
} else {
return nil, fmt.Errorf("unknown extra args tag: %x", extraArgs)
}
Expand Down
35 changes: 35 additions & 0 deletions core/capabilities/ccip/ccipevm/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,39 @@ func Test_decodeExtraArgs(t *testing.T) {

require.Equal(t, gasLimit, decodedGasLimit)
})

t.Run("decode extra args into map evm v1", func(t *testing.T) {
encoded, err := d.contract.EncodeEVMExtraArgsV1(nil, message_hasher.ClientEVMExtraArgsV1{
GasLimit: gasLimit,
})
require.NoError(t, err)

m, err := DecodeExtraArgsToMap(encoded)
require.NoError(t, err)
require.Len(t, m, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets add require.Equal checks on the contents of the map.

Copy link
Contributor

Choose a reason for hiding this comment

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

The check for contents is already in the below 3 lines (53-55).

Copy link
Contributor

Choose a reason for hiding this comment

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

the comparison is between a map, vs fields of a struct, so require.Equals will likely not work


gl, exist := m["gasLimit"]
require.True(t, exist)
require.Equal(t, gl, gasLimit)
})

t.Run("decode extra args into map evm v2", func(t *testing.T) {
encoded, err := d.contract.EncodeEVMExtraArgsV2(nil, message_hasher.ClientEVMExtraArgsV2{
GasLimit: gasLimit,
AllowOutOfOrderExecution: true,
})
require.NoError(t, err)

m, err := DecodeExtraArgsToMap(encoded)
require.NoError(t, err)
require.Len(t, m, 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto here re: equality checks.


gl, exist := m["gasLimit"]
require.True(t, exist)
require.Equal(t, gl, gasLimit)

ooe, exist := m["allowOutOfOrderExecution"]
require.True(t, exist)
require.Equal(t, true, ooe)
})
}
3 changes: 3 additions & 0 deletions core/capabilities/ccip/ccipevm/msghasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var (

// bytes4 public constant EVM_EXTRA_ARGS_V2_TAG = 0x181dcf10;
evmExtraArgsV2Tag = hexutil.MustDecode("0x181dcf10")

// bytes4 public constant SVM_EXTRA_EXTRA_ARGS_V1_TAG = 0x1f3b3aba
svmExtraArgsV1Tag = hexutil.MustDecode("0x1f3b3aba")
)

// MessageHasherV1 implements the MessageHasher interface.
Expand Down
84 changes: 84 additions & 0 deletions core/capabilities/ccip/ccipsolana/extradatadecoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ccipsolana

import (
"encoding/binary"
"fmt"
"reflect"

"github.com/ethereum/go-ethereum/common/hexutil"
agbinary "github.com/gagliardetto/binary"

"github.com/smartcontractkit/chainlink-ccip/chains/solana/gobindings/ccip_router"
)

const (
svmDestExecDataKey = "destGasAmount"
)

var (
// tag definition https://github.com/smartcontractkit/chainlink-ccip/blob/1b2ee24da54bddef8f3943dc84102686f2890f87/chains/solana/contracts/programs/ccip-router/src/extra_args.rs#L8C21-L11C45
// this should be moved to msghasher.go once merged

// bytes4(keccak256("CCIP SVMExtraArgsV1"));
svmExtraArgsV1Tag = hexutil.MustDecode("0x1f3b3aba")

// bytes4(keccak256("CCIP EVMExtraArgsV2"));
evmExtraArgsV2Tag = hexutil.MustDecode("0x181dcf10")
)

// DecodeExtraArgsToMap is a helper function for converting Borsh encoded extra args bytes into map[string]any, which will be saved in ocr report.message.ExtraArgsDecoded
func DecodeExtraArgsToMap(extraArgs []byte) (map[string]any, error) {
if len(extraArgs) < 4 {
return nil, fmt.Errorf("extra args too short: %d, should be at least 4 (i.e the extraArgs tag)", len(extraArgs))
}

var val reflect.Value
var typ reflect.Type
outputMap := make(map[string]any)
switch string(extraArgs[:4]) {
case string(evmExtraArgsV2Tag):
var args ccip_router.EVMExtraArgsV2
decoder := agbinary.NewBorshDecoder(extraArgs[4:])
err := args.UnmarshalWithDecoder(decoder)
if err != nil {
return outputMap, fmt.Errorf("failed to decode extra args: %w", err)
}
val = reflect.ValueOf(args)
typ = reflect.TypeOf(args)
case string(svmExtraArgsV1Tag):
var args ccip_router.SVMExtraArgsV1
decoder := agbinary.NewBorshDecoder(extraArgs[4:])
err := args.UnmarshalWithDecoder(decoder)
if err != nil {
return outputMap, fmt.Errorf("failed to decode extra args: %w", err)
}
val = reflect.ValueOf(args)
typ = reflect.TypeOf(args)
default:
return nil, fmt.Errorf("unknown extra args tag: %x", extraArgs)
}

for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
fieldValue := val.Field(i).Interface()
outputMap[field.Name] = fieldValue
}

return outputMap, nil
}

func DecodeDestExecDataToMap(destExecData []byte) (map[string]any, error) {
return map[string]interface{}{
svmDestExecDataKey: bytesToUint32LE(destExecData),
}, nil
}

func bytesToUint32LE(b []byte) uint32 {
if len(b) < 4 {
var padded [4]byte
copy(padded[:len(b)], b) // Pad from the right for little-endian
return binary.LittleEndian.Uint32(padded[:])
}

return binary.LittleEndian.Uint32(b)
}
Loading
Loading