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 18a382a commit 1b64f68
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
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
36 changes: 19 additions & 17 deletions core/services/relay/evm/evmtesting/bindings_test_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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"
Expand All @@ -14,18 +19,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 @@ -195,11 +196,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

Check failure on line 199 in core/services/relay/evm/evmtesting/bindings_test_adapter.go

View workflow job for this annotation

GitHub Actions / lint

Consider pre-allocating `updatedBindings` (prealloc)
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 +236,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 +436,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 +448,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 +464,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 +474,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 +484,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 1b64f68

Please sign in to comment.