Skip to content

Commit

Permalink
fixing linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pablolagreca committed Nov 6, 2024
1 parent 4006c5c commit 9340608
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 33 deletions.
1 change: 1 addition & 0 deletions core/services/relay/evm/bindings/chain_config_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bindings

import (
"github.com/ethereum/go-ethereum/common"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package evm

import (
"context"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/bindings"

"math/big"

commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"
interfacetesttypes "github.com/smartcontractkit/chainlink-common/pkg/types/interfacetests"
primitives "github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/bindings"
)

// This wrapper is required to enable the ChainReader to access historical data
Expand Down
4 changes: 3 additions & 1 deletion core/services/relay/evm/codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ func TestCodec_EncodeTupleWithLists(t *testing.T) {
require.Equal(t, expected, hexutil.Encode(result)[2:])
}

type codecInterfaceTester struct{}
type codecInterfaceTester struct {
TestSelectionSupport
}

func (it *codecInterfaceTester) Setup(_ *testing.T) {}

Expand Down
47 changes: 25 additions & 22 deletions core/services/relay/evm/evmtesting/bindings_test_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import (
"context"
"errors"
"fmt"
"math/big"
"reflect"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/go-viper/mapstructure/v2"

"github.com/smartcontractkit/chainlink-common/pkg/codec"
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink-common/pkg/types/interfacetests"
Expand All @@ -14,18 +20,14 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/bindings"
evmcodec "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/codec"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
"math/big"
"reflect"
"strings"
"testing"
)

const contractName = "ChainReaderTester"

// Wraps EVMChainComponentsInterfaceTester to rely on the EVM bindings generated for CR/CW instead of going directly to CR/CW. This way we can reuse all existing tests. Transformation between expected
// contract names and read keys will be done here as well as invocation delegation to generated code.
func WrapContractReaderTesterWithBindings(t *testing.T, wrapped *EVMChainComponentsInterfaceTester[*testing.T]) interfacetests.ChainComponentsInterfaceTester[*testing.T] {
//Tests not yet supported by EVM bindings.
// Tests not yet supported by EVM bindings.
wrapped.DisableTests([]string{
interfacetests.ContractReaderGetLatestValueAsValuesDotValue, interfacetests.ContractReaderGetLatestValueNoArgumentsAndPrimitiveReturnAsValuesDotValue, interfacetests.ContractReaderGetLatestValueNoArgumentsAndSliceReturnAsValueDotValue,
interfacetests.ContractReaderGetLatestValueGetsLatestForEvent, interfacetests.ContractReaderGetLatestValueBasedOnConfidenceLevelForEvent,
Expand Down Expand Up @@ -72,7 +74,7 @@ func newBindingsMapping() bindingsMapping {
interfacetests.MethodReturningUint64: "GetDifferentPrimitiveValue",
}

bindingsMapping := bindingsMapping{
bm := bindingsMapping{
contractNameMapping: map[string]string{
interfacetests.AnyContractName: contractName,
interfacetests.AnySecondContractName: contractName,
Expand All @@ -82,10 +84,10 @@ func newBindingsMapping() bindingsMapping {
chainWriterProxy: &chainWriterProxy,
chainReaderTesters: map[string]*bindings.ChainReaderTester{},
}
contractReaderProxy.bm = &bindingsMapping
chainWriterProxy.bm = &bindingsMapping
bindingsMapping.createDelegates()
return bindingsMapping
contractReaderProxy.bm = &bm
chainWriterProxy.bm = &bm
bm.createDelegates()
return bm
}

func getChainReaderConfig(wrapped *EVMChainComponentsInterfaceTester[*testing.T]) types.ChainReaderConfig {
Expand Down Expand Up @@ -195,11 +197,11 @@ func (b bindingContractReaderProxy) Bind(ctx context.Context, boundContracts []c
}

func (b bindingsMapping) translateContractNames(boundContracts []commontypes.BoundContract) []commontypes.BoundContract {
updatedBindings := []commontypes.BoundContract{}
var updatedBindings []commontypes.BoundContract
for _, boundContract := range boundContracts {
updatedBindings = append(updatedBindings, commontypes.BoundContract{
boundContract.Address,
b.translateContractName(boundContract.Name),
Address: boundContract.Address,
Name: b.translateContractName(boundContract.Name),
})
}
return updatedBindings
Expand Down Expand Up @@ -235,15 +237,15 @@ func (b bindingChainWriterProxy) SubmitTransaction(ctx context.Context, contract
switch method {
case interfacetests.MethodSettingStruct:
bindingsInput := bindings.AddTestStructInput{}
convertStruct(args, &bindingsInput)
_ = convertStruct(args, &bindingsInput)
return chainReaderTesters.AddTestStruct(ctx, bindingsInput, transactionID, toAddress, meta)
case interfacetests.MethodSettingUint64:
bindingsInput := bindings.SetAlterablePrimitiveValueInput{}
convertStruct(args, &bindingsInput)
_ = convertStruct(args, &bindingsInput)
return chainReaderTesters.SetAlterablePrimitiveValue(ctx, bindingsInput, transactionID, toAddress, meta)
case interfacetests.MethodTriggeringEvent:
bindingsInput := bindings.TriggerEventInput{}
convertStruct(args, &bindingsInput)
_ = convertStruct(args, &bindingsInput)
return chainReaderTesters.TriggerEvent(ctx, bindingsInput, transactionID, toAddress, meta)
default:
return errors.New("No logic implemented for method: " + method)
Expand Down Expand Up @@ -435,7 +437,7 @@ func (d Delegate) apply(ctx context.Context, readKey string, input any, confiden
// Utility function to converted original types from and to bindings expected types.
func convertStruct(src any, dst any) error {
if reflect.TypeOf(src).Kind() == reflect.Ptr && reflect.TypeOf(dst).Kind() == reflect.Ptr && reflect.TypeOf(src).Elem() == reflect.TypeOf(interfacetests.LatestParams{}) && reflect.TypeOf(dst).Elem() == reflect.TypeOf(bindings.GetElementAtIndexInput{}) {
value := (*src.(*interfacetests.LatestParams)).I
value := src.(*interfacetests.LatestParams).I
dst.(*bindings.GetElementAtIndexInput).I = big.NewInt(int64(value))
return nil
}
Expand All @@ -447,12 +449,13 @@ func convertStruct(src any, dst any) error {
if err != nil {
return err
}
if reflect.TypeOf(dst).Elem() == reflect.TypeOf(interfacetests.TestStructWithExtraField{}) {
switch {
case reflect.TypeOf(dst).Elem() == reflect.TypeOf(interfacetests.TestStructWithExtraField{}):
destTestStruct := dst.(*interfacetests.TestStructWithExtraField)
if destTestStruct != nil {
auxTestStruct := &interfacetests.TestStruct{}
decoder, _ := createDecoder(auxTestStruct)
decoder.Decode(src)
_ = decoder.Decode(src)
destTestStruct.TestStruct = *auxTestStruct
sourceTestStruct := src.(bindings.TestStruct)
destTestStruct.BigField = sourceTestStruct.BigField
Expand All @@ -462,7 +465,7 @@ func convertStruct(src any, dst any) error {
destTestStruct.NestedDynamicStruct.FixedBytes = sourceTestStruct.NestedDynamicStruct.FixedBytes
destTestStruct.ExtraField = interfacetests.AnyExtraValue
}
} else if reflect.TypeOf(dst).Elem() == reflect.TypeOf(interfacetests.TestStruct{}) {
case reflect.TypeOf(dst).Elem() == reflect.TypeOf(interfacetests.TestStruct{}):
destTestStruct := dst.(*interfacetests.TestStruct)
if destTestStruct != nil {
sourceTestStruct := src.(bindings.TestStruct)
Expand All @@ -472,7 +475,7 @@ func convertStruct(src any, dst any) error {
destTestStruct.NestedDynamicStruct.Inner.I = int(sourceTestStruct.NestedDynamicStruct.Inner.IntVal)
destTestStruct.NestedDynamicStruct.FixedBytes = sourceTestStruct.NestedDynamicStruct.FixedBytes
}
} else if reflect.TypeOf(src) == reflect.TypeOf(interfacetests.TestStruct{}) && reflect.TypeOf(dst) == reflect.TypeOf(&bindings.AddTestStructInput{}) {
case reflect.TypeOf(src) == reflect.TypeOf(interfacetests.TestStruct{}) && reflect.TypeOf(dst) == reflect.TypeOf(&bindings.AddTestStructInput{}):
destTestStruct := dst.(*bindings.AddTestStructInput)
if destTestStruct != nil {
sourceTestStruct := src.(interfacetests.TestStruct)
Expand All @@ -482,7 +485,7 @@ func convertStruct(src any, dst any) error {
destTestStruct.NestedDynamicStruct.Inner.IntVal = int64(sourceTestStruct.NestedDynamicStruct.Inner.I)
destTestStruct.NestedDynamicStruct.FixedBytes = sourceTestStruct.NestedDynamicStruct.FixedBytes
}
} else if reflect.TypeOf(src) == reflect.TypeOf(interfacetests.TestStruct{}) && reflect.TypeOf(dst) == reflect.TypeOf(&bindings.ReturnSeenInput{}) {
case reflect.TypeOf(src) == reflect.TypeOf(interfacetests.TestStruct{}) && reflect.TypeOf(dst) == reflect.TypeOf(&bindings.ReturnSeenInput{}):
destTestStruct := dst.(*bindings.ReturnSeenInput)
if destTestStruct != nil {
sourceTestStruct := src.(interfacetests.TestStruct)
Expand Down
16 changes: 8 additions & 8 deletions core/services/relay/evm/evmtesting/run_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ func RunContractReaderInLoopTests[T TestingT[T]](t T, it ChainComponentsInterfac
copy(val5[:], append(empty12Bytes[:], 5))
raw := []byte{9, 8}

var buf []byte
buf = binary.BigEndian.AppendUint32(buf, val1)
buf = binary.BigEndian.AppendUint32(buf, val2)
buf = binary.BigEndian.AppendUint32(buf, val3)
buf = binary.BigEndian.AppendUint64(buf, val4)
dataWordOnChainValueToQuery := buf[:]

resExpected := append(buf, common.LeftPadBytes(val5[:], 32)...)
var resExpected []byte
resExpected = binary.BigEndian.AppendUint32(resExpected, val1)
resExpected = binary.BigEndian.AppendUint32(resExpected, val2)
resExpected = binary.BigEndian.AppendUint32(resExpected, val3)
resExpected = binary.BigEndian.AppendUint64(resExpected, val4)
dataWordOnChainValueToQuery := resExpected

resExpected = append(resExpected, common.LeftPadBytes(val5[:], 32)...)
resExpected = append(resExpected, common.LeftPadBytes(val6[:], 32)...)
resExpected = append(resExpected, common.LeftPadBytes(val7[:], 32)...)
resExpected = append(resExpected, raw...)
Expand Down

0 comments on commit 9340608

Please sign in to comment.