Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
connorwstein committed Sep 25, 2023
1 parent a8885dd commit 53bd56b
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 209 deletions.
112 changes: 0 additions & 112 deletions core/services/ocr2/plugins/ccip/abihelpers/abi_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/commit_store"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_offramp"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/price_registry"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
Expand Down Expand Up @@ -65,26 +64,7 @@ func GetIDOrPanic(name string, abi2 abi.ABI) common.Hash {
return event.ID
}

func getTupleNamedElem(name string, arg abi.Argument) *abi.Type {
if arg.Type.T != abi.TupleTy {
return nil
}
for i, elem := range arg.Type.TupleElems {
if arg.Type.TupleRawNames[i] == name {
return elem
}
}
return nil
}

func init() {
//onRampABI, err := abi.JSON(strings.NewReader(evm_2_evm_onramp.EVM2EVMOnRampABI))
//if err != nil {
// panic(err)
//}
//EventSignatures.SendRequested = GetIDOrPanic("CCIPSendRequested", onRampABI)
//EventSignatures.SendRequestedSequenceNumberWord = 4

commitStoreABI, err := abi.JSON(strings.NewReader(commit_store.CommitStoreABI))
if err != nil {
panic(err)
Expand All @@ -110,23 +90,13 @@ func init() {
EventSignatures.FeeTokenAdded = GetIDOrPanic("FeeTokenAdded", priceRegistryABI)
EventSignatures.FeeTokenRemoved = GetIDOrPanic("FeeTokenRemoved", priceRegistryABI)

// arguments
//MessageArgs = onRampABI.Events["CCIPSendRequested"].Inputs
//tokenAmountsTy := getTupleNamedElem("tokenAmounts", MessageArgs[0])
//if tokenAmountsTy == nil {
// panic(fmt.Sprintf("missing component '%s' in tuple %+v", "tokenAmounts", MessageArgs))
//}
//TokenAmountsArgs = abi.Arguments{{Type: *tokenAmountsTy, Name: "tokenAmounts"}}

CommitReportArgs = commitStoreABI.Events["ReportAccepted"].Inputs

manuallyExecuteMethod, ok := offRampABI.Methods["manuallyExecute"]
if !ok {
panic("missing event 'manuallyExecute'")
}
ExecutionReportArgs = manuallyExecuteMethod.Inputs[:1]

//EventSignatures.USDCMessageSent = utils.Keccak256Fixed([]byte("MessageSent(bytes)"))
}

func MessagesFromExecutionReport(report types.Report) ([]evm_2_evm_offramp.InternalEVM2EVMMessage, error) {
Expand All @@ -137,88 +107,6 @@ func MessagesFromExecutionReport(report types.Report) ([]evm_2_evm_offramp.Inter
return decodedExecutionReport.Messages, nil
}

func DecodeOffRampMessage(b []byte) (*evm_2_evm_offramp.InternalEVM2EVMMessage, error) {
unpacked, err := MessageArgs.Unpack(b)
if err != nil {
return nil, err
}
if len(unpacked) == 0 {
return nil, fmt.Errorf("no message found when unpacking")
}

// Note must use unnamed type here
receivedCp, ok := unpacked[0].(struct {
SourceChainSelector uint64 `json:"sourceChainSelector"`
Sender common.Address `json:"sender"`
Receiver common.Address `json:"receiver"`
SequenceNumber uint64 `json:"sequenceNumber"`
GasLimit *big.Int `json:"gasLimit"`
Strict bool `json:"strict"`
Nonce uint64 `json:"nonce"`
FeeToken common.Address `json:"feeToken"`
FeeTokenAmount *big.Int `json:"feeTokenAmount"`
Data []uint8 `json:"data"`
TokenAmounts []struct {
Token common.Address `json:"token"`
Amount *big.Int `json:"amount"`
} `json:"tokenAmounts"`
SourceTokenData [][]byte `json:"sourceTokenData"`
MessageId [32]byte `json:"messageId"`
})
if !ok {
return nil, fmt.Errorf("invalid format have %T want %T", unpacked[0], receivedCp)
}
var tokensAndAmounts []evm_2_evm_offramp.ClientEVMTokenAmount
for _, tokenAndAmount := range receivedCp.TokenAmounts {
tokensAndAmounts = append(tokensAndAmounts, evm_2_evm_offramp.ClientEVMTokenAmount{
Token: tokenAndAmount.Token,
Amount: tokenAndAmount.Amount,
})
}

return &evm_2_evm_offramp.InternalEVM2EVMMessage{
SourceChainSelector: receivedCp.SourceChainSelector,
Sender: receivedCp.Sender,
Receiver: receivedCp.Receiver,
SequenceNumber: receivedCp.SequenceNumber,
GasLimit: receivedCp.GasLimit,
Strict: receivedCp.Strict,
Nonce: receivedCp.Nonce,
FeeToken: receivedCp.FeeToken,
FeeTokenAmount: receivedCp.FeeTokenAmount,
Data: receivedCp.Data,
TokenAmounts: tokensAndAmounts,
SourceTokenData: receivedCp.SourceTokenData,
MessageId: receivedCp.MessageId,
}, nil
}

func OnRampMessageToOffRampMessage(msg evm_2_evm_onramp.InternalEVM2EVMMessage) evm_2_evm_offramp.InternalEVM2EVMMessage {
tokensAndAmounts := make([]evm_2_evm_offramp.ClientEVMTokenAmount, len(msg.TokenAmounts))
for i, tokenAndAmount := range msg.TokenAmounts {
tokensAndAmounts[i] = evm_2_evm_offramp.ClientEVMTokenAmount{
Token: tokenAndAmount.Token,
Amount: tokenAndAmount.Amount,
}
}

return evm_2_evm_offramp.InternalEVM2EVMMessage{
SourceChainSelector: msg.SourceChainSelector,
Sender: msg.Sender,
Receiver: msg.Receiver,
SequenceNumber: msg.SequenceNumber,
GasLimit: msg.GasLimit,
Strict: msg.Strict,
Nonce: msg.Nonce,
FeeToken: msg.FeeToken,
FeeTokenAmount: msg.FeeTokenAmount,
Data: msg.Data,
TokenAmounts: tokensAndAmounts,
SourceTokenData: msg.SourceTokenData,
MessageId: msg.MessageId,
}
}

// ProofFlagsToBits transforms a list of boolean proof flags to a *big.Int
// encoded number.
func ProofFlagsToBits(proofFlags []bool) *big.Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (t *LeafHasherV1_0_0) HashLeaf(log types.Log) ([32]byte, error) {
}
encodedTokens, err := utils.ABIEncode(
`[
{"components": [{"name":"token","type":"address"},{"name":"amount","type":"uint256"}], "type":"tuple"}]`, message.Message.TokenAmounts)
{"components": [{"name":"token","type":"address"},{"name":"amount","type":"uint256"}], "type":"tuple[]"}]`, message.Message.TokenAmounts)
if err != nil {
return [32]byte{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/hashlib"
)

func TestHasher(t *testing.T) {
func TestHasherV1_0_0(t *testing.T) {
sourceChainSelector, destChainSelector := uint64(1), uint64(4)
onRampAddress := common.HexToAddress("0x5550000000000000000000000000000000000001")
onRampABI, err := abi.JSON(strings.NewReader(evm_2_evm_onramp_1_0_0.EVM2EVMOnRampABI))
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestHasher(t *testing.T) {
require.NoError(t, err)

// NOTE: Must match spec
require.Equal(t, "46ad031bfb052db2e4a2514fed8dc480b98e5ce4acb55d5640d91407e0d8a3e9", hex.EncodeToString(hash[:]))
require.Equal(t, "26f282c6ac8231933b1799648d01ff6cec792a33fb37408b4d135968f9168ace", hex.EncodeToString(hash[:]))

message = evm_2_evm_onramp_1_0_0.InternalEVM2EVMMessage{
SourceChainSelector: sourceChainSelector,
Expand All @@ -74,7 +74,7 @@ func TestHasher(t *testing.T) {
require.NoError(t, err)

// NOTE: Must match spec
require.Equal(t, "4362a13a42e52ff5ce4324e7184dc7aa41704c3146bc842d35d95b94b32a78b6", hex.EncodeToString(hash[:]))
require.Equal(t, "05cee92e7cb86a37b6536554828a5b21ff20ac3d4ef821ec47056f1d963313de", hex.EncodeToString(hash[:]))
}

func TestMetaDataHash(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ccipdata
import (
"context"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -37,6 +38,71 @@ func init() {
CCIPSendRequestEventSigV1_2_0 = abihelpers.GetIDOrPanic("CCIPSendRequested", onRampABI)
}

// Backwards compat for integration tests
func DecodeOffRampMessageV1_2_0(b []byte) (*evm_2_evm_offramp.InternalEVM2EVMMessage, error) {
offRampABI, err := abi.JSON(strings.NewReader(evm_2_evm_offramp.EVM2EVMOffRampABI))
if err != nil {
panic(err)
}
event, ok := offRampABI.Events["EVM2EVMMessage"]
if !ok {
panic("no such event")
}
unpacked, err := event.Inputs.Unpack(b)
if err != nil {
return nil, err
}
if len(unpacked) == 0 {
return nil, fmt.Errorf("no message found when unpacking")
}

// Note must use unnamed type here
receivedCp, ok := unpacked[0].(struct {
SourceChainSelector uint64 `json:"sourceChainSelector"`
Sender common.Address `json:"sender"`
Receiver common.Address `json:"receiver"`
SequenceNumber uint64 `json:"sequenceNumber"`
GasLimit *big.Int `json:"gasLimit"`
Strict bool `json:"strict"`
Nonce uint64 `json:"nonce"`
FeeToken common.Address `json:"feeToken"`
FeeTokenAmount *big.Int `json:"feeTokenAmount"`
Data []uint8 `json:"data"`
TokenAmounts []struct {
Token common.Address `json:"token"`
Amount *big.Int `json:"amount"`
} `json:"tokenAmounts"`
SourceTokenData [][]byte `json:"sourceTokenData"`
MessageId [32]byte `json:"messageId"`
})
if !ok {
return nil, fmt.Errorf("invalid format have %T want %T", unpacked[0], receivedCp)
}
var tokensAndAmounts []evm_2_evm_offramp.ClientEVMTokenAmount
for _, tokenAndAmount := range receivedCp.TokenAmounts {
tokensAndAmounts = append(tokensAndAmounts, evm_2_evm_offramp.ClientEVMTokenAmount{
Token: tokenAndAmount.Token,
Amount: tokenAndAmount.Amount,
})
}

return &evm_2_evm_offramp.InternalEVM2EVMMessage{
SourceChainSelector: receivedCp.SourceChainSelector,
Sender: receivedCp.Sender,
Receiver: receivedCp.Receiver,
SequenceNumber: receivedCp.SequenceNumber,
GasLimit: receivedCp.GasLimit,
Strict: receivedCp.Strict,
Nonce: receivedCp.Nonce,
FeeToken: receivedCp.FeeToken,
FeeTokenAmount: receivedCp.FeeTokenAmount,
Data: receivedCp.Data,
TokenAmounts: tokensAndAmounts,
SourceTokenData: receivedCp.SourceTokenData,
MessageId: receivedCp.MessageId,
}, nil
}

type LeafHasherV1_2_0 struct {
metaDataHash [32]byte
ctx hashlib.Ctx[[32]byte]
Expand All @@ -57,7 +123,9 @@ func (t *LeafHasherV1_2_0) HashLeaf(log types.Log) ([32]byte, error) {
return [32]byte{}, err
}
message := msg.Message
encodedTokens, err := abihelpers.TokenAmountsArgs.PackValues([]interface{}{message.TokenAmounts})
encodedTokens, err := utils.ABIEncode(
`[
{"components": [{"name":"token","type":"address"},{"name":"amount","type":"uint256"}], "type":"tuple[]"}]`, message.TokenAmounts)
if err != nil {
return [32]byte{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package ccipdata

import (
"context"
"encoding/hex"
"math/big"
"strings"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -13,11 +17,76 @@ import (
evmClientMocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller/mocks"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/hashlib"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)

func TestHasherV1_2_0(t *testing.T) {
sourceChainSelector, destChainSelector := uint64(1), uint64(4)
onRampAddress := common.HexToAddress("0x5550000000000000000000000000000000000001")
onRampABI, err := abi.JSON(strings.NewReader(evm_2_evm_onramp.EVM2EVMOnRampABI))
require.NoError(t, err)

hashingCtx := hashlib.NewKeccakCtx()
ramp, err := evm_2_evm_onramp.NewEVM2EVMOnRamp(onRampAddress, nil)
require.NoError(t, err)
hasher := NewLeafHasherV1_2_0(sourceChainSelector, destChainSelector, onRampAddress, hashingCtx, ramp)

message := evm_2_evm_onramp.InternalEVM2EVMMessage{
SourceChainSelector: sourceChainSelector,
Sender: common.HexToAddress("0x1110000000000000000000000000000000000001"),
Receiver: common.HexToAddress("0x2220000000000000000000000000000000000001"),
SequenceNumber: 1337,
GasLimit: big.NewInt(100),
Strict: false,
Nonce: 1337,
FeeToken: common.Address{},
FeeTokenAmount: big.NewInt(1),
Data: []byte{},
TokenAmounts: []evm_2_evm_onramp.ClientEVMTokenAmount{{Token: common.HexToAddress("0x4440000000000000000000000000000000000001"), Amount: big.NewInt(12345678900)}},
SourceTokenData: [][]byte{},
MessageId: [32]byte{},
}

data, err := onRampABI.Events["CCIPSendRequested"].Inputs.Pack(message)
require.NoError(t, err)
hash, err := hasher.HashLeaf(types.Log{Topics: []common.Hash{CCIPSendRequestEventSigV1_2_0}, Data: data})
require.NoError(t, err)

// NOTE: Must match spec
require.Equal(t, "46ad031bfb052db2e4a2514fed8dc480b98e5ce4acb55d5640d91407e0d8a3e9", hex.EncodeToString(hash[:]))

message = evm_2_evm_onramp.InternalEVM2EVMMessage{
SourceChainSelector: sourceChainSelector,
Sender: common.HexToAddress("0x1110000000000000000000000000000000000001"),
Receiver: common.HexToAddress("0x2220000000000000000000000000000000000001"),
SequenceNumber: 1337,
GasLimit: big.NewInt(100),
Strict: false,
Nonce: 1337,
FeeToken: common.Address{},
FeeTokenAmount: big.NewInt(1e12),
Data: []byte("foo bar baz"),
TokenAmounts: []evm_2_evm_onramp.ClientEVMTokenAmount{
{Token: common.HexToAddress("0x4440000000000000000000000000000000000001"), Amount: big.NewInt(12345678900)},
{Token: common.HexToAddress("0x6660000000000000000000000000000000000001"), Amount: big.NewInt(4204242)},
},
SourceTokenData: [][]byte{{0x2, 0x1}},
MessageId: [32]byte{},
}

data, err = onRampABI.Events["CCIPSendRequested"].Inputs.Pack(message)
require.NoError(t, err)
hash, err = hasher.HashLeaf(types.Log{Topics: []common.Hash{CCIPSendRequestEventSigV1_2_0}, Data: data})
require.NoError(t, err)

// NOTE: Must match spec
require.Equal(t, "4362a13a42e52ff5ce4324e7184dc7aa41704c3146bc842d35d95b94b32a78b6", hex.EncodeToString(hash[:]))
}

func TestLogPollerClient_GetSendRequestsGteSeqNum(t *testing.T) {
onRampAddr := utils.RandomAddress()
seqNum := uint64(100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (d usdcPayload) Validate() error {
return nil
}

func parseUSDCMessageSent(logData []byte) ([]byte, error) {
func ParseUSDCMessageSent(logData []byte) ([]byte, error) {
decodeAbiStruct, err := abihelpers.DecodeAbiStruct[usdcPayload](logData)
if err != nil {
return nil, err
Expand All @@ -72,7 +72,7 @@ func (u *USDCReaderImpl) GetLastUSDCMessagePriorToLogIndexInTx(ctx context.Conte
current := logs[len(logs)-i-1]
if current.LogIndex < logIndex {
u.lggr.Infow("Found USDC message", "logIndex", current.LogIndex, "txHash", current.TxHash.Hex(), "data", hexutil.Encode(current.Data))
return parseUSDCMessageSent(current.Data)
return ParseUSDCMessageSent(current.Data)
}
}
return nil, errors.Errorf("no USDC message found prior to log index %d in tx %s", logIndex, txHash.Hex())
Expand Down
Loading

0 comments on commit 53bd56b

Please sign in to comment.