Skip to content

Commit 79bfde4

Browse files
Merge pull request #102 from smartcontractkit/jh/update-extraArgs-binding-and-extraArgsCodec
Refactor extraArgs and extraDataCodec
2 parents 17ee57f + e2c89b8 commit 79bfde4

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

pkg/ccip/bindings/onramp/onramp.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"math/big"
55

66
"github.com/xssnick/tonutils-go/address"
7+
"github.com/xssnick/tonutils-go/tlb"
78
"github.com/xssnick/tonutils-go/tvm/cell"
89

910
"github.com/smartcontractkit/chainlink-ton/pkg/ccip/bindings/common"
@@ -17,12 +18,14 @@ type CCIPMessageSent struct {
1718

1819
// GenericExtraArgsV2 represents generic extra arguments for transactions.
1920
type GenericExtraArgsV2 struct {
20-
GasLimit *big.Int `tlb:"## 256"`
21-
AllowOutOfOrderExecution bool `tlb:"bool"`
21+
_ tlb.Magic `tlb:"#181dcf10"` //nolint:revive // Ignore opcode tag // hex encoded bytes4(keccak256("CCIP EVMExtraArgsV2")), can be verified with hexutil.MustDecode("0x181dcf10")
22+
GasLimit *big.Int `tlb:"## 256"`
23+
AllowOutOfOrderExecution bool `tlb:"bool"`
2224
}
2325

2426
// SVMExtraArgsV1 represents extra arguments for SVM transactions.
2527
type SVMExtraArgsV1 struct {
28+
_ tlb.Magic `tlb:"#1f3b3aba"` //nolint:revive // Ignore opcode tag // hex encoded bytes4(keccak256("CCIP SVMExtraArgsV1")), can be verified with hexutil.MustDecode("0x1f3b3aba")
2629
ComputeUnits uint32 `tlb:"## 32"`
2730
AccountIsWritableBitmap uint64 `tlb:"## 64"`
2831
AllowOutOfOrderExecution bool `tlb:"bool"`

pkg/ccip/codec/extradatacodec.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,41 @@ func (d ExtraDataDecoder) DecodeExtraArgsToMap(extraArgs ccipocr3.Bytes) (map[st
3838
var val reflect.Value
3939
var typ reflect.Type
4040
outputMap := make(map[string]any)
41-
switch string(extraArgs[:4]) {
42-
case string(evmExtraArgsV2Tag):
43-
var args onramp.GenericExtraArgsV2
44-
c, err := cell.FromBOC(extraArgs[4:])
45-
if err != nil {
46-
return outputMap, fmt.Errorf("decode BOC: %w", err)
47-
}
41+
c, err := cell.FromBOC(extraArgs)
42+
if err != nil {
43+
return outputMap, fmt.Errorf("failed to decode BOC: %w", err)
44+
}
45+
tag, err := c.BeginParse().LoadSlice(32)
46+
if err != nil {
47+
return outputMap, fmt.Errorf("failed to load tag from cell: %w", err)
48+
}
4849

50+
switch hexutil.Encode(tag) {
51+
case hexutil.Encode(evmExtraArgsV2Tag):
52+
var args onramp.GenericExtraArgsV2
4953
if err = tlb.LoadFromCell(&args, c.BeginParse()); err != nil {
5054
return nil, fmt.Errorf("failed to tlb load extra args from cell: %w", err)
5155
}
52-
5356
val = reflect.ValueOf(args)
5457
typ = reflect.TypeOf(args)
55-
case string(svmExtraArgsV1Tag):
56-
var tlbArgs onramp.SVMExtraArgsV1
57-
c, err := cell.FromBOC(extraArgs[4:])
58-
if err != nil {
59-
return outputMap, fmt.Errorf("decode BOC: %w", err)
60-
}
6158

59+
case hexutil.Encode(svmExtraArgsV1Tag):
60+
var tlbArgs onramp.SVMExtraArgsV1
6261
if err = tlb.LoadFromCell(&tlbArgs, c.BeginParse()); err != nil {
6362
return nil, fmt.Errorf("failed to tlb load extra args from cell: %w", err)
6463
}
65-
6664
val = reflect.ValueOf(tlbArgs)
6765
typ = reflect.TypeOf(tlbArgs)
66+
6867
default:
69-
return nil, fmt.Errorf("unknown extra args tag: %x", extraArgs[:4])
68+
return nil, fmt.Errorf("unknown extra args tag: %x", tag)
7069
}
7170

7271
for i := 0; i < val.NumField(); i++ {
7372
field := typ.Field(i)
73+
if !field.IsExported() {
74+
continue
75+
}
7476
fieldValue := val.Field(i).Interface()
7577
outputMap[field.Name] = fieldValue
7678
}

pkg/ccip/codec/extradatacodec_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Test_decodeExtraArgs(t *testing.T) {
2929

3030
t.Run("decode extra args into map svm", func(t *testing.T) {
3131
destGasAmount := uint32(10000)
32-
bitmap := uint64(0)
32+
bitmap := uint64(5)
3333
extraArgs := onramp.SVMExtraArgsV1{
3434
ComputeUnits: destGasAmount,
3535
AccountIsWritableBitmap: bitmap,
@@ -44,7 +44,7 @@ func Test_decodeExtraArgs(t *testing.T) {
4444
c, err := tlb.ToCell(extraArgs)
4545
require.NoError(t, err)
4646

47-
output, err := extraDataDecoder.DecodeExtraArgsToMap(append(svmExtraArgsV1Tag, c.ToBOC()...))
47+
output, err := extraDataDecoder.DecodeExtraArgsToMap(c.ToBOC())
4848
require.NoError(t, err)
4949
require.Len(t, output, 5)
5050

@@ -70,7 +70,7 @@ func Test_decodeExtraArgs(t *testing.T) {
7070
c, err := tlb.ToCell(extraArgs)
7171
require.NoError(t, err)
7272

73-
output, err := extraDataDecoder.DecodeExtraArgsToMap(append(evmExtraArgsV2Tag, c.ToBOC()...))
73+
output, err := extraDataDecoder.DecodeExtraArgsToMap(c.ToBOC())
7474
require.NoError(t, err)
7575
require.Len(t, output, 2)
7676

0 commit comments

Comments
 (0)