-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
huangzhen1997
merged 39 commits into
solana-offchain-plugin
from
NONEVM-1163/implement-extra-args-codec
Jan 30, 2025
Merged
Implement extra args codec #16016
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 7253bba
goimport
huangzhen1997 0ec8119
update
huangzhen1997 82a162f
rename
huangzhen1997 2c7bd05
Merge branch 'solana-offchain-plugin' into NONEVM-1163/implement-extr…
huangzhen1997 e8bbe9a
tidy
huangzhen1997 36a5ad1
refactor
huangzhen1997 3fa671b
fix lint
huangzhen1997 af29932
fix
huangzhen1997 2597b48
update
huangzhen1997 a99bdcc
update
huangzhen1997 5fe01d2
address Makram comments
huangzhen1997 c49c04b
lint
huangzhen1997 7204973
Merge branch 'solana-offchain-plugin' into NONEVM-1163/implement-extr…
huangzhen1997 a903310
mod tidy
huangzhen1997 80b3e3b
gomd
huangzhen1997 afcd808
fix import
huangzhen1997 6bc6b29
fix broken integration test
huangzhen1997 a5f84ac
update
huangzhen1997 d6af37b
fix test
huangzhen1997 f9489ae
fix test
huangzhen1997 cf28c84
update source chain
huangzhen1997 67e0038
update test
huangzhen1997 a20765b
minor
huangzhen1997 ee10849
merge develop
huangzhen1997 f2952a0
implement and add test
huangzhen1997 a1eacf6
update comments
huangzhen1997 0b15808
goimport
huangzhen1997 55b33e3
fix conflicts
huangzhen1997 8d4d702
update
huangzhen1997 fc7f0f7
update
huangzhen1997 9939559
add comment
huangzhen1997 6e6441d
refactor
huangzhen1997 7e96d52
update comment
huangzhen1997 2e8de08
add unit test
huangzhen1997 5c76148
update
huangzhen1997 5bcf509
lint
huangzhen1997 8af2a50
mod tidy
huangzhen1997 aa9841d
update
huangzhen1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
core/capabilities/ccip/ccipevm/extradatadecoder_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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