Skip to content

Commit

Permalink
fix commit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhen1997 committed Dec 2, 2024
1 parent 6a3c1d4 commit 3ea3a4f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 51 deletions.
24 changes: 24 additions & 0 deletions core/capabilities/ccip/ccipevm/commitcodecv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ccipevm
import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3"
Expand Down Expand Up @@ -66,6 +67,25 @@ func (c *CommitPluginCodecV2) Encode(ctx context.Context, report cciptypes.Commi
return cd.Encode(ctx, report, "CommitPluginReport")
}

func postProcess(report *cciptypes.CommitPluginReport) error {
for index, update := range report.PriceUpdates.TokenPriceUpdates {
if !common.IsHexAddress(string(update.TokenID)) {
return fmt.Errorf("invalid token address: %s", update.TokenID)
}
if update.Price.IsEmpty() || update.Price.Int64() == 0 {
report.PriceUpdates.TokenPriceUpdates[index].Price = cciptypes.NewBigInt(big.NewInt(0))
}
}

for idx, update := range report.PriceUpdates.GasPriceUpdates {
if update.GasPrice.IsEmpty() || update.GasPrice.Int64() == 0 {
report.PriceUpdates.GasPriceUpdates[idx].GasPrice = cciptypes.NewBigInt(big.NewInt(0))
}
}

return nil
}

func (c *CommitPluginCodecV2) Decode(ctx context.Context, bytes []byte) (cciptypes.CommitPluginReport, error) {
report := cciptypes.CommitPluginReport{}
cd, err := codec.NewCodec(commitCodecConfig)
Expand All @@ -78,6 +98,10 @@ func (c *CommitPluginCodecV2) Decode(ctx context.Context, bytes []byte) (cciptyp
return report, err
}

if err = postProcess(&report); err != nil {
return report, err
}

return report, nil
}

Expand Down
29 changes: 15 additions & 14 deletions core/capabilities/ccip/ccipevm/commitcodecv2_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ccipevm

import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -37,20 +38,20 @@ func TestCommitPluginCodecV2(t *testing.T) {
return report
},
},
//{
// name: "zero token price",
// report: func(report cciptypes.CommitPluginReport) cciptypes.CommitPluginReport {
// report.PriceUpdates.TokenPriceUpdates[0].Price = cciptypes.NewBigInt(big.NewInt(0))
// return report
// },
//},
//{
// name: "zero gas price",
// report: func(report cciptypes.CommitPluginReport) cciptypes.CommitPluginReport {
// report.PriceUpdates.GasPriceUpdates[0].GasPrice = cciptypes.NewBigInt(big.NewInt(0))
// return report
// },
//},
{
name: "zero token price",
report: func(report cciptypes.CommitPluginReport) cciptypes.CommitPluginReport {
report.PriceUpdates.TokenPriceUpdates[0].Price = cciptypes.NewBigInt(big.NewInt(0))
return report
},
},
{
name: "zero gas price",
report: func(report cciptypes.CommitPluginReport) cciptypes.CommitPluginReport {
report.PriceUpdates.GasPriceUpdates[0].GasPrice = cciptypes.NewBigInt(big.NewInt(0))
return report
},
},
}

for _, tc := range testCases {
Expand Down
37 changes: 0 additions & 37 deletions core/capabilities/ccip/ccipevm/executecodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
commoncodec "github.com/smartcontractkit/chainlink-common/pkg/codec"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/codec"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"

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

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
Expand Down Expand Up @@ -93,39 +89,6 @@ var randomExecuteReport = func(t *testing.T, d *testSetupData) cciptypes.Execute
return cciptypes.ExecutePluginReport{ChainReports: chainReports}
}

func TestCodec_ExecReport(t *testing.T) {
d := testSetup(t)
input := randomExecuteReport(t, d)
config := `[{"name":"chainReports","type":"tuple[]","components":[{"name":"sourceChainSelector","type":"uint64","internalType":"uint64"},{"name":"messages","type":"tuple[]","components":[{"name":"header","type":"tuple","components":[{"name":"messageID","type":"bytes32","internalType":"bytes32"},{"name":"sourceChainSelector","type":"uint64","internalType":"uint64"},{"name":"destChainSelector","type":"uint64","internalType":"uint64"},{"name":"sequenceNumber","type":"uint64","internalType":"uint64"},{"name":"nonce","type":"uint64","internalType":"uint64"},{"name":"msgHash","type":"bytes32","internalType":"bytes32"},{"name":"onRamp","type":"bytes","internalType":"bytes"}]},{"name":"data","type":"bytes","internalType":"bytes"},{"name":"sender","type":"bytes","internalType":"bytes"},{"name":"receiver","type":"bytes","internalType":"bytes"},{"name":"extraArgs","type":"bytes","internalType":"bytes"},{"name":"feeToken","type":"bytes","internalType":"bytes"},{"name":"feeTokenAmount","type":"uint256","internalType":"uint256"},{"name":"feeValueJuels","type":"uint256","internalType":"uint256"},{"name":"tokenAmounts","type":"tuple[]","components":[{"name":"sourcePoolAddress","type":"bytes","internalType":"bytes"},{"name":"destTokenAddress","type":"bytes","internalType":"bytes"},{"name":"extraData","type":"bytes","internalType":"bytes"},{"name":"amount","type":"uint256","internalType":"uint256"},{"name":"destExecData","type":"bytes","internalType":"bytes"}]}]},{"name":"offchainTokenData","type":"bytes[][]","internalType":"bytes[][]"},{"name":"proofs","type":"bytes32[]","internalType":"bytes32[]"},{"name":"proofFlagBits","type":"uint256","internalType":"uint256"}]}]`
codecConfig := types.CodecConfig{
Configs: map[string]types.ChainCodecConfig{
"ExecPluginReport": {
TypeABI: config,
ModifierConfigs: commoncodec.ModifiersConfig{
&commoncodec.WrapperModifierConfig{
Fields: map[string]string{
"ChainReports.Messages.FeeTokenAmount": "Int",
"ChainReports.Messages.FeeValueJuels": "Int",
"ChainReports.Messages.TokenAmounts.Amount": "Int",
"ChainReports.ProofFlagBits": "Int",
},
},
},
},
},
}
c, err := codec.NewCodec(codecConfig)
require.NoError(t, err)

result, err := c.Encode(testutils.Context(t), input, "ExecPluginReport")
require.NoError(t, err)
require.NotNil(t, result)
output := cciptypes.ExecutePluginReport{}
err = c.Decode(testutils.Context(t), result, &output, "ExecPluginReport")
require.NoError(t, err)
require.Equal(t, input, output)
}

func TestExecutePluginCodecV1(t *testing.T) {
d := testSetup(t)

Expand Down

0 comments on commit 3ea3a4f

Please sign in to comment.