From 64695945ad49ea3b15227d2fe93bef6b8decc901 Mon Sep 17 00:00:00 2001 From: Pablo La Greca Date: Wed, 2 Oct 2024 16:15:27 -0300 Subject: [PATCH] BCFR-967 - EVM Chain bindings for CR/CW - Basic support for method write/read --- .../evm/bindings/chain_config_factory.go | 102 ++++ .../relay/evm/bindings/chain_reader_tester.go | 186 +++++++ .../relay/evm/chain_components_test.go | 7 +- .../chain_writer_historical_wrapper_test.go | 21 +- .../evm/evmtesting/bindings_test_adapter.go | 497 ++++++++++++++++++ .../chain_components_interface_tester.go | 211 ++++---- .../relay/evm/evmtesting/run_tests.go | 418 ++++++++------- go.mod | 2 +- go.sum | 4 +- 9 files changed, 1169 insertions(+), 279 deletions(-) create mode 100644 core/services/relay/evm/bindings/chain_config_factory.go create mode 100644 core/services/relay/evm/bindings/chain_reader_tester.go create mode 100644 core/services/relay/evm/evmtesting/bindings_test_adapter.go diff --git a/core/services/relay/evm/bindings/chain_config_factory.go b/core/services/relay/evm/bindings/chain_config_factory.go new file mode 100644 index 00000000000..6312f244ebd --- /dev/null +++ b/core/services/relay/evm/bindings/chain_config_factory.go @@ -0,0 +1,102 @@ +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" +) + +func NewChainReaderConfig() types.ChainReaderConfig { + chainReaderConfig := types.ChainReaderConfig{ + Contracts: map[string]types.ChainContractReader{ + "ChainReaderTester": types.ChainContractReader{ + Configs: map[string]*types.ChainReaderDefinition{ + "GetAlterablePrimitiveValue": &types.ChainReaderDefinition{ + CacheEnabled: false, + ChainSpecificName: "getAlterablePrimitiveValue", + ReadType: 0, + }, "GetDifferentPrimitiveValue": &types.ChainReaderDefinition{ + CacheEnabled: false, + ChainSpecificName: "getDifferentPrimitiveValue", + ReadType: 0, + }, "GetElementAtIndex": &types.ChainReaderDefinition{ + CacheEnabled: false, + ChainSpecificName: "getElementAtIndex", + ReadType: 0, + }, "GetPrimitiveValue": &types.ChainReaderDefinition{ + CacheEnabled: false, + ChainSpecificName: "getPrimitiveValue", + ReadType: 0, + }, "GetSliceValue": &types.ChainReaderDefinition{ + CacheEnabled: false, + ChainSpecificName: "getSliceValue", + ReadType: 0, + }, "ReturnSeen": &types.ChainReaderDefinition{ + CacheEnabled: false, + ChainSpecificName: "returnSeen", + ReadType: 0, + }, + }, + ContractABI: "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"indexed\":false,\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"indexed\":false,\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"},{\"indexed\":false,\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"Account\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address[]\",\"name\":\"Accounts\",\"type\":\"address[]\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"indexed\":false,\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"}],\"name\":\"Triggered\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"string\",\"name\":\"fieldHash\",\"type\":\"string\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"field\",\"type\":\"string\"}],\"name\":\"TriggeredEventWithDynamicTopic\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field1\",\"type\":\"int32\"},{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field2\",\"type\":\"int32\"},{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field3\",\"type\":\"int32\"}],\"name\":\"TriggeredWithFourTopics\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"string\",\"name\":\"field1\",\"type\":\"string\"},{\"indexed\":true,\"internalType\":\"uint8[32]\",\"name\":\"field2\",\"type\":\"uint8[32]\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"field3\",\"type\":\"bytes32\"}],\"name\":\"TriggeredWithFourTopicsWithHashed\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"}],\"name\":\"addTestStruct\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getAlterablePrimitiveValue\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getDifferentPrimitiveValue\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"i\",\"type\":\"uint256\"}],\"name\":\"getElementAtIndex\",\"outputs\":[{\"components\":[{\"internalType\":\"int32\",\"name\":\"Field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"DifferentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"OracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"OracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"Account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"Accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"BigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"NestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"NestedStaticStruct\",\"type\":\"tuple\"}],\"internalType\":\"struct TestStruct\",\"name\":\"\",\"type\":\"tuple\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getPrimitiveValue\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getSliceValue\",\"outputs\":[{\"internalType\":\"uint64[]\",\"name\":\"\",\"type\":\"uint64[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"}],\"name\":\"returnSeen\",\"outputs\":[{\"components\":[{\"internalType\":\"int32\",\"name\":\"Field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"DifferentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"OracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"OracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"Account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"Accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"BigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"NestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"NestedStaticStruct\",\"type\":\"tuple\"}],\"internalType\":\"struct TestStruct\",\"name\":\"\",\"type\":\"tuple\"}],\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint64\",\"name\":\"value\",\"type\":\"uint64\"}],\"name\":\"setAlterablePrimitiveValue\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"},{\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"}],\"name\":\"triggerEvent\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"field\",\"type\":\"string\"}],\"name\":\"triggerEventWithDynamicTopic\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field1\",\"type\":\"int32\"},{\"internalType\":\"int32\",\"name\":\"field2\",\"type\":\"int32\"},{\"internalType\":\"int32\",\"name\":\"field3\",\"type\":\"int32\"}],\"name\":\"triggerWithFourTopics\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"field1\",\"type\":\"string\"},{\"internalType\":\"uint8[32]\",\"name\":\"field2\",\"type\":\"uint8[32]\"},{\"internalType\":\"bytes32\",\"name\":\"field3\",\"type\":\"bytes32\"}],\"name\":\"triggerWithFourTopicsWithHashed\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", + ContractPollingFilter: types.ContractPollingFilter{ + PollingFilter: types.PollingFilter{ + LogsPerBlock: 0, + MaxLogsKept: 0, + Retention: 0, + }, + }, + }, + }, + } + return chainReaderConfig +} + +func NewChainWriterConfig(maxGasPrice assets.Wei, defaultGasPrice uint64, fromAddress common.Address) types.ChainWriterConfig { + chainWriterConfig := types.ChainWriterConfig{ + Contracts: map[string]*types.ContractConfig{ + "ChainReaderTester": &types.ContractConfig{ + Configs: map[string]*types.ChainWriterDefinition{ + "AddTestStruct": &types.ChainWriterDefinition{ + ChainSpecificName: "addTestStruct", + Checker: "simulate", + FromAddress: fromAddress, + GasLimit: 0, + }, "SetAlterablePrimitiveValue": &types.ChainWriterDefinition{ + ChainSpecificName: "setAlterablePrimitiveValue", + Checker: "simulate", + FromAddress: fromAddress, + GasLimit: 0, + }, "TriggerEvent": &types.ChainWriterDefinition{ + ChainSpecificName: "triggerEvent", + Checker: "simulate", + FromAddress: fromAddress, + GasLimit: 0, + }, "TriggerEventWithDynamicTopic": &types.ChainWriterDefinition{ + ChainSpecificName: "triggerEventWithDynamicTopic", + Checker: "simulate", + FromAddress: fromAddress, + GasLimit: 0, + }, "TriggerWithFourTopics": &types.ChainWriterDefinition{ + ChainSpecificName: "triggerWithFourTopics", + Checker: "simulate", + FromAddress: fromAddress, + GasLimit: 0, + }, "TriggerWithFourTopicsWithHashed": &types.ChainWriterDefinition{ + ChainSpecificName: "triggerWithFourTopicsWithHashed", + Checker: "simulate", + FromAddress: fromAddress, + GasLimit: 0, + }, + }, + ContractABI: "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"indexed\":false,\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"indexed\":false,\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"},{\"indexed\":false,\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"Account\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address[]\",\"name\":\"Accounts\",\"type\":\"address[]\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"indexed\":false,\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"}],\"name\":\"Triggered\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"string\",\"name\":\"fieldHash\",\"type\":\"string\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"field\",\"type\":\"string\"}],\"name\":\"TriggeredEventWithDynamicTopic\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field1\",\"type\":\"int32\"},{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field2\",\"type\":\"int32\"},{\"indexed\":true,\"internalType\":\"int32\",\"name\":\"field3\",\"type\":\"int32\"}],\"name\":\"TriggeredWithFourTopics\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"string\",\"name\":\"field1\",\"type\":\"string\"},{\"indexed\":true,\"internalType\":\"uint8[32]\",\"name\":\"field2\",\"type\":\"uint8[32]\"},{\"indexed\":true,\"internalType\":\"bytes32\",\"name\":\"field3\",\"type\":\"bytes32\"}],\"name\":\"TriggeredWithFourTopicsWithHashed\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"}],\"name\":\"addTestStruct\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getAlterablePrimitiveValue\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getDifferentPrimitiveValue\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"i\",\"type\":\"uint256\"}],\"name\":\"getElementAtIndex\",\"outputs\":[{\"components\":[{\"internalType\":\"int32\",\"name\":\"Field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"DifferentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"OracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"OracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"Account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"Accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"BigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"NestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"NestedStaticStruct\",\"type\":\"tuple\"}],\"internalType\":\"struct TestStruct\",\"name\":\"\",\"type\":\"tuple\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getPrimitiveValue\",\"outputs\":[{\"internalType\":\"uint64\",\"name\":\"\",\"type\":\"uint64\"}],\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getSliceValue\",\"outputs\":[{\"internalType\":\"uint64[]\",\"name\":\"\",\"type\":\"uint64[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"}],\"name\":\"returnSeen\",\"outputs\":[{\"components\":[{\"internalType\":\"int32\",\"name\":\"Field\",\"type\":\"int32\"},{\"internalType\":\"string\",\"name\":\"DifferentField\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"OracleId\",\"type\":\"uint8\"},{\"internalType\":\"uint8[32]\",\"name\":\"OracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"Account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"Accounts\",\"type\":\"address[]\"},{\"internalType\":\"int192\",\"name\":\"BigField\",\"type\":\"int192\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"NestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"NestedStaticStruct\",\"type\":\"tuple\"}],\"internalType\":\"struct TestStruct\",\"name\":\"\",\"type\":\"tuple\"}],\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint64\",\"name\":\"value\",\"type\":\"uint64\"}],\"name\":\"setAlterablePrimitiveValue\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field\",\"type\":\"int32\"},{\"internalType\":\"uint8\",\"name\":\"oracleId\",\"type\":\"uint8\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"string\",\"name\":\"S\",\"type\":\"string\"}],\"internalType\":\"struct InnerDynamicTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelDynamicTestStruct\",\"name\":\"nestedDynamicStruct\",\"type\":\"tuple\"},{\"components\":[{\"internalType\":\"bytes2\",\"name\":\"FixedBytes\",\"type\":\"bytes2\"},{\"components\":[{\"internalType\":\"int64\",\"name\":\"IntVal\",\"type\":\"int64\"},{\"internalType\":\"address\",\"name\":\"A\",\"type\":\"address\"}],\"internalType\":\"struct InnerStaticTestStruct\",\"name\":\"Inner\",\"type\":\"tuple\"}],\"internalType\":\"struct MidLevelStaticTestStruct\",\"name\":\"nestedStaticStruct\",\"type\":\"tuple\"},{\"internalType\":\"uint8[32]\",\"name\":\"oracleIds\",\"type\":\"uint8[32]\"},{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"string\",\"name\":\"differentField\",\"type\":\"string\"},{\"internalType\":\"int192\",\"name\":\"bigField\",\"type\":\"int192\"}],\"name\":\"triggerEvent\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"field\",\"type\":\"string\"}],\"name\":\"triggerEventWithDynamicTopic\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"int32\",\"name\":\"field1\",\"type\":\"int32\"},{\"internalType\":\"int32\",\"name\":\"field2\",\"type\":\"int32\"},{\"internalType\":\"int32\",\"name\":\"field3\",\"type\":\"int32\"}],\"name\":\"triggerWithFourTopics\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"string\",\"name\":\"field1\",\"type\":\"string\"},{\"internalType\":\"uint8[32]\",\"name\":\"field2\",\"type\":\"uint8[32]\"},{\"internalType\":\"bytes32\",\"name\":\"field3\",\"type\":\"bytes32\"}],\"name\":\"triggerWithFourTopicsWithHashed\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", + }, + }, + } + chainWriterConfig.MaxGasPrice = &maxGasPrice + for _, contract := range chainWriterConfig.Contracts { + for _, chainWriterDefinition := range contract.Configs { + chainWriterDefinition.GasLimit = defaultGasPrice + } + } + return chainWriterConfig +} diff --git a/core/services/relay/evm/bindings/chain_reader_tester.go b/core/services/relay/evm/bindings/chain_reader_tester.go new file mode 100644 index 00000000000..f047c0d3d2c --- /dev/null +++ b/core/services/relay/evm/bindings/chain_reader_tester.go @@ -0,0 +1,186 @@ +// Code generated evm-bindings; DO NOT EDIT. + +package bindings + +import ( + "context" + "github.com/ethereum/go-ethereum/common" + "github.com/smartcontractkit/chainlink-common/pkg/types" + "github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives" + "math/big" +) + +// CodeDetails methods inputs and outputs structs +type ChainReaderTester struct { + BoundContract types.BoundContract + ContractReader types.ContractReader + ChainWriter types.ChainWriter +} + +type AddTestStructInput struct { + Field int32 + DifferentField string + OracleId uint8 + OracleIds [32]uint8 + Account common.Address + Accounts []common.Address + BigField *big.Int + NestedDynamicStruct MidLevelDynamicTestStruct + NestedStaticStruct MidLevelStaticTestStruct +} + +type GetAlterablePrimitiveValueOutput struct { + Value uint64 +} + +type GetDifferentPrimitiveValueOutput struct { + Value uint64 +} + +type GetElementAtIndexInput struct { + I *big.Int +} + +type GetPrimitiveValueOutput struct { + Value uint64 +} + +type GetSliceValueOutput struct { + Value []uint64 +} + +type InnerDynamicTestStruct struct { + IntVal int64 + S string +} + +type InnerStaticTestStruct struct { + IntVal int64 + A common.Address +} + +type MidLevelDynamicTestStruct struct { + FixedBytes [2]uint8 + Inner InnerDynamicTestStruct +} + +type MidLevelStaticTestStruct struct { + FixedBytes [2]uint8 + Inner InnerStaticTestStruct +} + +type ReturnSeenInput struct { + Field int32 + DifferentField string + OracleId uint8 + OracleIds [32]uint8 + Account common.Address + Accounts []common.Address + BigField *big.Int + NestedDynamicStruct MidLevelDynamicTestStruct + NestedStaticStruct MidLevelStaticTestStruct +} + +type SetAlterablePrimitiveValueInput struct { + Value uint64 +} + +type TestStruct struct { + Field int32 + DifferentField string + OracleId uint8 + OracleIds [32]uint8 + Account common.Address + Accounts []common.Address + BigField *big.Int + NestedDynamicStruct MidLevelDynamicTestStruct + NestedStaticStruct MidLevelStaticTestStruct +} + +type TriggerEventInput struct { + Field int32 + OracleId uint8 + NestedDynamicStruct MidLevelDynamicTestStruct + NestedStaticStruct MidLevelStaticTestStruct + OracleIds [32]uint8 + Account common.Address + Accounts []common.Address + DifferentField string + BigField *big.Int +} + +type TriggerEventWithDynamicTopicInput struct { + Field string +} + +type TriggerWithFourTopicsInput struct { + Field1 int32 + Field2 int32 + Field3 int32 +} + +type TriggerWithFourTopicsWithHashedInput struct { + Field1 string + Field2 [32]uint8 + Field3 [32]uint8 +} + +func (b ChainReaderTester) GetPrimitiveValue(ctx context.Context, confidence primitives.ConfidenceLevel) (uint64, error) { + var output uint64 + err := b.ContractReader.GetLatestValue(ctx, b.BoundContract.ReadIdentifier("GetPrimitiveValue"), confidence, nil, &output) + return output, err +} + +func (b ChainReaderTester) GetSliceValue(ctx context.Context, confidence primitives.ConfidenceLevel) ([]uint64, error) { + var output []uint64 + err := b.ContractReader.GetLatestValue(ctx, b.BoundContract.ReadIdentifier("GetSliceValue"), confidence, nil, &output) + return output, err +} + +func (b ChainReaderTester) ReturnSeen(ctx context.Context, input ReturnSeenInput, confidence primitives.ConfidenceLevel) (TestStruct, error) { + output := TestStruct{} + err := b.ContractReader.GetLatestValue(ctx, b.BoundContract.ReadIdentifier("ReturnSeen"), confidence, input, &output) + return output, err +} + +func (b ChainReaderTester) TriggerEvent(ctx context.Context, input TriggerEventInput, txId string, toAddress string, meta *types.TxMeta) error { + return b.ChainWriter.SubmitTransaction(ctx, "ChainReaderTester", "TriggerEvent", input, txId, toAddress, meta, big.NewInt(0)) +} + +func (b ChainReaderTester) GetDifferentPrimitiveValue(ctx context.Context, confidence primitives.ConfidenceLevel) (uint64, error) { + var output uint64 + err := b.ContractReader.GetLatestValue(ctx, b.BoundContract.ReadIdentifier("GetDifferentPrimitiveValue"), confidence, nil, &output) + return output, err +} + +func (b ChainReaderTester) GetElementAtIndex(ctx context.Context, input GetElementAtIndexInput, confidence primitives.ConfidenceLevel) (TestStruct, error) { + output := TestStruct{} + err := b.ContractReader.GetLatestValue(ctx, b.BoundContract.ReadIdentifier("GetElementAtIndex"), confidence, input, &output) + return output, err +} + +func (b ChainReaderTester) SetAlterablePrimitiveValue(ctx context.Context, input SetAlterablePrimitiveValueInput, txId string, toAddress string, meta *types.TxMeta) error { + return b.ChainWriter.SubmitTransaction(ctx, "ChainReaderTester", "SetAlterablePrimitiveValue", input, txId, toAddress, meta, big.NewInt(0)) +} + +func (b ChainReaderTester) TriggerEventWithDynamicTopic(ctx context.Context, input TriggerEventWithDynamicTopicInput, txId string, toAddress string, meta *types.TxMeta) error { + return b.ChainWriter.SubmitTransaction(ctx, "ChainReaderTester", "TriggerEventWithDynamicTopic", input, txId, toAddress, meta, big.NewInt(0)) +} + +func (b ChainReaderTester) TriggerWithFourTopics(ctx context.Context, input TriggerWithFourTopicsInput, txId string, toAddress string, meta *types.TxMeta) error { + return b.ChainWriter.SubmitTransaction(ctx, "ChainReaderTester", "TriggerWithFourTopics", input, txId, toAddress, meta, big.NewInt(0)) +} + +func (b ChainReaderTester) TriggerWithFourTopicsWithHashed(ctx context.Context, input TriggerWithFourTopicsWithHashedInput, txId string, toAddress string, meta *types.TxMeta) error { + return b.ChainWriter.SubmitTransaction(ctx, "ChainReaderTester", "TriggerWithFourTopicsWithHashed", input, txId, toAddress, meta, big.NewInt(0)) +} + +func (b ChainReaderTester) AddTestStruct(ctx context.Context, input AddTestStructInput, txId string, toAddress string, meta *types.TxMeta) error { + return b.ChainWriter.SubmitTransaction(ctx, "ChainReaderTester", "AddTestStruct", input, txId, toAddress, meta, big.NewInt(0)) +} + +func (b ChainReaderTester) GetAlterablePrimitiveValue(ctx context.Context, confidence primitives.ConfidenceLevel) (uint64, error) { + var output uint64 + err := b.ContractReader.GetLatestValue(ctx, b.BoundContract.ReadIdentifier("GetAlterablePrimitiveValue"), confidence, nil, &output) + return output, err +} diff --git a/core/services/relay/evm/chain_components_test.go b/core/services/relay/evm/chain_components_test.go index 50c530904dd..ab256e10fbd 100644 --- a/core/services/relay/evm/chain_components_test.go +++ b/core/services/relay/evm/chain_components_test.go @@ -4,6 +4,7 @@ import ( "context" "crypto/ecdsa" "fmt" + commontestutils "github.com/smartcontractkit/chainlink-common/pkg/loop/testutils" "math" "math/big" "os" @@ -20,7 +21,6 @@ import ( "github.com/stretchr/testify/require" commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config" - commontestutils "github.com/smartcontractkit/chainlink-common/pkg/loop/testutils" clcommontypes "github.com/smartcontractkit/chainlink-common/pkg/types" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey" @@ -149,15 +149,16 @@ func TestContractReaderEventsInitValidation(t *testing.T) { } } +//go:generate evm-chain-bindings -contracts contracts/src/v0.8/shared/test/helpers -output core/services/relay/evm/bindings func TestChainComponents(t *testing.T) { t.Parallel() it := &EVMChainComponentsInterfaceTester[*testing.T]{Helper: &helper{}} - - it.Helper.Init(t) + it.Init(t) // add new subtests here so that it can be run on real chains too RunChainComponentsEvmTests(t, it) RunChainComponentsInLoopEvmTests[*testing.T](t, commontestutils.WrapContractReaderTesterForLoop(it)) + RunChainComponentsInLoopEvmTests(t, WrapContractReaderTesterWithBindings(t, it)) } type helper struct { diff --git a/core/services/relay/evm/chain_writer_historical_wrapper_test.go b/core/services/relay/evm/chain_writer_historical_wrapper_test.go index c849d1f3d57..669ec9e7953 100644 --- a/core/services/relay/evm/chain_writer_historical_wrapper_test.go +++ b/core/services/relay/evm/chain_writer_historical_wrapper_test.go @@ -2,6 +2,7 @@ package evm import ( "context" + "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/bindings" "math/big" commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" @@ -22,7 +23,8 @@ func NewChainWriterHistoricalWrapper(cw commontypes.ChainWriter, cwh *ClientWith } func (cwhw *ChainWriterHistoricalWrapper) SubmitTransaction(ctx context.Context, contractName, method string, args any, transactionID string, toAddress string, meta *commontypes.TxMeta, value *big.Int) error { - if primArgs, ok := args.(interfacetesttypes.PrimitiveArgs); ok { + alterablePrimitiveCall, newValue := cwhw.getPrimitiveValueIfPossible(args) + if alterablePrimitiveCall { callArgs := interfacetesttypes.ExpectedGetLatestValueArgs{ ContractName: contractName, ReadName: "GetAlterablePrimitiveValue", @@ -30,10 +32,25 @@ func (cwhw *ChainWriterHistoricalWrapper) SubmitTransaction(ctx context.Context, Params: nil, ReturnVal: nil, } - err := cwhw.cwh.SetUintLatestValue(ctx, primArgs.Value, callArgs) + err := cwhw.cwh.SetUintLatestValue(ctx, newValue, callArgs) if err != nil { return err } } return cwhw.ChainWriter.SubmitTransaction(ctx, contractName, method, args, transactionID, toAddress, meta, value) } + +func (cwhw *ChainWriterHistoricalWrapper) getPrimitiveValueIfPossible(args any) (bool, uint64) { + primitiveArgs, alterablePrimitiveCall := args.(interfacetesttypes.PrimitiveArgs) + var newValue uint64 + var alterablePrimitiveValue bindings.SetAlterablePrimitiveValueInput + if alterablePrimitiveCall { + newValue = primitiveArgs.Value + } else { + alterablePrimitiveValue, alterablePrimitiveCall = args.(bindings.SetAlterablePrimitiveValueInput) + if alterablePrimitiveCall { + newValue = alterablePrimitiveValue.Value + } + } + return alterablePrimitiveCall, newValue +} diff --git a/core/services/relay/evm/evmtesting/bindings_test_adapter.go b/core/services/relay/evm/evmtesting/bindings_test_adapter.go new file mode 100644 index 00000000000..4cc5d9d7d0f --- /dev/null +++ b/core/services/relay/evm/evmtesting/bindings_test_adapter.go @@ -0,0 +1,497 @@ +package evmtesting + +import ( + "context" + "errors" + "fmt" + "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" + "github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" + "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/bindings" + "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. + wrapped.DisableTests([]string{ + interfacetests.GetLatestValueAsValuesDotValue, interfacetests.GetLatestValueNoArgumentsAndPrimitiveReturnAsValuesDotValue, interfacetests.GetLatestValueNoArgumentsAndSliceReturnAsValueDotValue, + interfacetests.GetLatestValueGetsLatestForEvent, interfacetests.GetLatestValueBasedOnConfidenceLevelForEvent, + interfacetests.GetLatestValueReturnsNotFoundWhenNotTriggeredForEvent, interfacetests.GetLatestValueWithFilteringForEvent, interfacetests.BatchGetLatestValue, interfacetests.BatchGetLatestValueMultipleContractNamesSameFunction, + interfacetests.BatchGetLatestValueDifferentParamsResultsRetainOrder, interfacetests.BatchGetLatestValueDifferentParamsResultsRetainOrderMultipleContracts, interfacetests.BatchGetLatestValueNoArgumentsPrimitiveReturn, + interfacetests.BatchGetLatestValueSetsErrorsProperly, interfacetests.BatchGetLatestValueNoArgumentsWithSliceReturn, interfacetests.BatchGetLatestValueWithModifiersOwnMapstructureOverride, + interfacetests.QueryKeyNotFound, interfacetests.QueryKeyReturnsData, interfacetests.QueryKeyReturnsDataAsValuesDotValue, interfacetests.QueryKeyReturnsDataAsValuesDotValue, + interfacetests.QueryKeyCanFilterWithValueComparator, QueryKeyFilterOnDataWordsWithValueComparator, QueryKeyOnDataWordsWithValueComparatorOnNestedField, + QueryKeyFilterOnDataWordsWithValueComparatorOnDynamicField, + }) + wrapped.SetChainReaderConfigSupplier(func(t *testing.T) types.ChainReaderConfig { + return getChainReaderConfig(wrapped) + }) + wrapped.SetChainWriterConfigSupplier(func(t *testing.T) types.ChainWriterConfig { + return getChainWriterConfig(t, wrapped) + }) + return newBindingClientTester(wrapped) +} + +func newBindingClientTester(wrapped *EVMChainComponentsInterfaceTester[*testing.T]) bindingClientTester { + bindingsMapping := newBindingsMapping() + return bindingClientTester{ + ChainComponentsInterfaceTester: wrapped, + bindingsMapping: &bindingsMapping, + } +} + +func newBindingsMapping() bindingsMapping { + contractReaderProxy := bindingContractReaderProxy{} + chainWriterProxy := bindingChainWriterProxy{} + methodNameMappingByContract := make(map[string]map[string]string) + methodNameMappingByContract[interfacetests.AnyContractName] = map[string]string{ + interfacetests.MethodTakingLatestParamsReturningTestStruct: "GetElementAtIndex", + interfacetests.MethodReturningSeenStruct: "ReturnSeen", + interfacetests.MethodReturningAlterableUint64: "GetAlterablePrimitiveValue", + interfacetests.MethodReturningUint64: "GetPrimitiveValue", + interfacetests.MethodReturningUint64Slice: "getSliceValue", + interfacetests.MethodSettingStruct: "AddTestStruct", + interfacetests.MethodSettingUint64: "SetAlterablePrimitiveValue", + interfacetests.MethodTriggeringEvent: "TriggerEvent", + } + methodNameMappingByContract[interfacetests.AnySecondContractName] = map[string]string{ + interfacetests.MethodReturningUint64: "GetDifferentPrimitiveValue", + } + + bindingsMapping := bindingsMapping{ + contractNameMapping: map[string]string{ + interfacetests.AnyContractName: contractName, + interfacetests.AnySecondContractName: contractName, + }, + methodNameMappingByContract: methodNameMappingByContract, + contractReaderProxy: &contractReaderProxy, + chainWriterProxy: &chainWriterProxy, + chainReaderTesters: map[string]*bindings.ChainReaderTester{}, + } + contractReaderProxy.bm = &bindingsMapping + chainWriterProxy.bm = &bindingsMapping + bindingsMapping.createDelegates() + return bindingsMapping +} + +func getChainReaderConfig(wrapped *EVMChainComponentsInterfaceTester[*testing.T]) types.ChainReaderConfig { + testStruct := interfacetests.CreateTestStruct[*testing.T](0, wrapped) + chainReaderConfig := bindings.NewChainReaderConfig() + chainReaderConfig.Contracts["ChainReaderTester"].Configs["ReturnSeen"] = &types.ChainReaderDefinition{ + CacheEnabled: false, + ChainSpecificName: "returnSeen", + ReadType: 0, + InputModifications: codec.ModifiersConfig{ + &codec.HardCodeModifierConfig{ + OnChainValues: map[string]any{ + "BigField": testStruct.BigField.String(), + "Account": hexutil.Encode(testStruct.Account), + }, + }, + }, + OutputModifications: codec.ModifiersConfig{ + &codec.HardCodeModifierConfig{OffChainValues: map[string]any{"ExtraField": interfacetests.AnyExtraValue}}, + }, + } + return chainReaderConfig +} + +func getChainWriterConfig(t *testing.T, wrapped *EVMChainComponentsInterfaceTester[*testing.T]) types.ChainWriterConfig { + return bindings.NewChainWriterConfig(*assets.NewWei(big.NewInt(1000000000000000000)), 2_000_000, wrapped.Helper.Accounts(t)[1].From) +} + +func (b bindingClientTester) Name() string { + return "generated bindings" +} + +type bindingClientTester struct { + interfacetests.ChainComponentsInterfaceTester[*testing.T] + bindingsMapping *bindingsMapping +} + +func (b bindingClientTester) GetContractReader(t *testing.T) commontypes.ContractReader { + contractReader := b.ChainComponentsInterfaceTester.GetContractReader(t) + if b.bindingsMapping.contractReaderProxy.ContractReader == nil { + b.bindingsMapping.contractReaderProxy.ContractReader = contractReader + b.addDefaultBindings(t) + for _, tester := range b.bindingsMapping.chainReaderTesters { + tester.ContractReader = contractReader + } + } + return b.bindingsMapping.contractReaderProxy +} + +func (b bindingClientTester) addDefaultBindings(t *testing.T) { + defaultBindings := b.ChainComponentsInterfaceTester.GetBindings(t) + for _, binding := range defaultBindings { + chainReaderTester := b.bindingsMapping.chainReaderTesters[binding.Address] + if chainReaderTester == nil { + chainReaderTester = &bindings.ChainReaderTester{ + BoundContract: binding, + ChainWriter: b.bindingsMapping.chainWriterProxy.ChainWriter, + } + b.bindingsMapping.chainReaderTesters[binding.Address] = chainReaderTester + } else { + chainReaderTester.ChainWriter = b.bindingsMapping.chainWriterProxy.ChainWriter + } + } +} + +func (b bindingClientTester) GetChainWriter(t *testing.T) commontypes.ChainWriter { + chainWriter := b.ChainComponentsInterfaceTester.GetChainWriter(t) + if b.bindingsMapping.chainWriterProxy.ChainWriter == nil { + b.addDefaultBindings(t) + for _, tester := range b.bindingsMapping.chainReaderTesters { + tester.ChainWriter = chainWriter + } + b.bindingsMapping.chainWriterProxy.ChainWriter = chainWriter + } + return b.bindingsMapping.chainWriterProxy +} + +type bindingsMapping struct { + contractNameMapping map[string]string + methodNameMappingByContract map[string]map[string]string + delegates map[string]*Delegate + chainReaderTesters map[string]*bindings.ChainReaderTester + contractReaderProxy *bindingContractReaderProxy + chainWriterProxy *bindingChainWriterProxy +} + +type bindingContractReaderProxy struct { + commontypes.ContractReader + bm *bindingsMapping +} + +type bindingChainWriterProxy struct { + commontypes.ChainWriter + bm *bindingsMapping +} + +func (b bindingContractReaderProxy) Bind(ctx context.Context, boundContracts []commontypes.BoundContract) error { + updatedBindings := b.bm.translateContractNames(boundContracts) + for _, updatedBinding := range updatedBindings { + b.bm.chainReaderTesters[updatedBinding.Address] = &bindings.ChainReaderTester{ + BoundContract: updatedBinding, + ContractReader: b.ContractReader, + ChainWriter: b.bm.chainWriterProxy.ChainWriter, + } + } + return b.ContractReader.Bind(ctx, updatedBindings) +} + +func (b bindingsMapping) translateContractNames(boundContracts []commontypes.BoundContract) []commontypes.BoundContract { + updatedBindings := []commontypes.BoundContract{} + for _, boundContract := range boundContracts { + updatedBindings = append(updatedBindings, commontypes.BoundContract{ + boundContract.Address, + b.translateContractName(boundContract.Name), + }) + } + return updatedBindings +} + +func (b bindingContractReaderProxy) Close() error { + return b.ContractReader.Close() +} + +func (b bindingContractReaderProxy) GetLatestValue(ctx context.Context, readKey string, confidenceLevel primitives.ConfidenceLevel, params, returnVal any) error { + delegate, err := b.bm.getBindingDelegate(readKey) + if err != nil { + return err + } + output, err := delegate.apply(ctx, readKey, params, confidenceLevel) + if err != nil { + return err + } + if output == nil { + return nil + } + err = convertStruct(output, returnVal) + if err != nil { + return err + } + return nil +} + +func (b bindingChainWriterProxy) SubmitTransaction(ctx context.Context, contract, method string, args any, transactionID string, toAddress string, meta *commontypes.TxMeta, value *big.Int) error { + chainReaderTesters := b.bm.chainReaderTesters[toAddress] + switch contract { + case interfacetests.AnyContractName, interfacetests.AnySecondContractName: + switch method { + case interfacetests.MethodSettingStruct: + bindingsInput := bindings.AddTestStructInput{} + convertStruct(args, &bindingsInput) + return chainReaderTesters.AddTestStruct(ctx, bindingsInput, transactionID, toAddress, meta) + case interfacetests.MethodSettingUint64: + bindingsInput := bindings.SetAlterablePrimitiveValueInput{} + convertStruct(args, &bindingsInput) + return chainReaderTesters.SetAlterablePrimitiveValue(ctx, bindingsInput, transactionID, toAddress, meta) + case interfacetests.MethodTriggeringEvent: + bindingsInput := bindings.TriggerEventInput{} + convertStruct(args, &bindingsInput) + return chainReaderTesters.TriggerEvent(ctx, bindingsInput, transactionID, toAddress, meta) + default: + return errors.New("No logic implemented for method: " + method) + } + default: + return errors.New("contract with id not supported " + contract) + } +} + +func (b *bindingChainWriterProxy) GetTransactionStatus(ctx context.Context, transactionID string) (commontypes.TransactionStatus, error) { + return b.ChainWriter.GetTransactionStatus(ctx, transactionID) +} + +func removeAddressFromReadIdentifier(s string) string { + index := strings.Index(s, "-") + if index == -1 { + return s + } + return s[index+1:] +} + +func (b *bindingsMapping) createDelegates() { + delegates := make(map[string]*Delegate) + boundContract := commontypes.BoundContract{Address: "", Name: contractName} + methodTakingLatestParamsKey := removeAddressFromReadIdentifier(boundContract.ReadIdentifier(b.methodNameMappingByContract[interfacetests.AnyContractName][interfacetests.MethodTakingLatestParamsReturningTestStruct])) + delegates[methodTakingLatestParamsKey] = b.createDelegateForMethodTakingLatestParams() + methodReturningAlterableUint64Key := removeAddressFromReadIdentifier(boundContract.ReadIdentifier(b.methodNameMappingByContract[interfacetests.AnyContractName][interfacetests.MethodReturningAlterableUint64])) + delegates[methodReturningAlterableUint64Key] = b.createDelegateForMethodReturningAlterableUint64() + methodReturningSeenStructKey := removeAddressFromReadIdentifier(boundContract.ReadIdentifier(b.methodNameMappingByContract[interfacetests.AnyContractName][interfacetests.MethodReturningSeenStruct])) + delegates[methodReturningSeenStructKey] = b.createDelegateForMethodReturningSeenStruct() + methodReturningUint64Key := removeAddressFromReadIdentifier(boundContract.ReadIdentifier(b.methodNameMappingByContract[interfacetests.AnyContractName][interfacetests.MethodReturningUint64])) + delegates[methodReturningUint64Key] = b.createDelegateForMethodReturningUint64() + methodReturningUint64SliceKey := removeAddressFromReadIdentifier(boundContract.ReadIdentifier(b.methodNameMappingByContract[interfacetests.AnyContractName][interfacetests.MethodReturningUint64Slice])) + delegates[methodReturningUint64SliceKey] = b.createDelegateForMethodReturningUint64Slice() + methodReturningDifferentUint64Key := removeAddressFromReadIdentifier(boundContract.ReadIdentifier(b.methodNameMappingByContract[interfacetests.AnySecondContractName][interfacetests.MethodReturningUint64])) + delegates[methodReturningDifferentUint64Key] = b.createDelegateForSecondContractMethodReturningUint64() + b.delegates = delegates +} + +func (b *bindingsMapping) createDelegateForMethodTakingLatestParams() *Delegate { + delegate := Delegate{inputType: reflect.TypeOf(bindings.GetElementAtIndexInput{})} + delegate.delegateFunc = func(ctx context.Context, readyKey string, input *any, level primitives.ConfidenceLevel) (any, error) { + methodInvocation := func(ctx context.Context, readKey string, input *bindings.GetElementAtIndexInput, level primitives.ConfidenceLevel) (any, error) { + chainReaderTester := b.GetChainReaderTester(readKey) + return chainReaderTester.GetElementAtIndex(ctx, *input, level) + } + return invokeSpecificMethod(ctx, b.translateReadKey(readyKey), (*input).(*bindings.GetElementAtIndexInput), level, methodInvocation) + } + return &delegate +} + +func (b *bindingsMapping) createDelegateForMethodReturningAlterableUint64() *Delegate { + delegate := Delegate{} + delegate.delegateFunc = func(ctx context.Context, readyKey string, input *any, level primitives.ConfidenceLevel) (any, error) { + methodInvocation := func(ctx context.Context, readKey string, input any, level primitives.ConfidenceLevel) (any, error) { + chainReaderTester := b.GetChainReaderTester(readKey) + return chainReaderTester.GetAlterablePrimitiveValue(ctx, level) + } + return invokeSpecificMethod(ctx, b.translateReadKey(readyKey), nil, level, methodInvocation) + } + return &delegate +} + +func (b *bindingsMapping) createDelegateForMethodReturningSeenStruct() *Delegate { + delegate := Delegate{inputType: reflect.TypeOf(bindings.ReturnSeenInput{})} + delegate.delegateFunc = func(ctx context.Context, readyKey string, input *any, level primitives.ConfidenceLevel) (any, error) { + methodInvocation := func(ctx context.Context, readKey string, input *bindings.ReturnSeenInput, level primitives.ConfidenceLevel) (any, error) { + chainReaderTester := b.GetChainReaderTester(readKey) + return chainReaderTester.ReturnSeen(ctx, *input, level) + } + return invokeSpecificMethod(ctx, b.translateReadKey(readyKey), (*input).(*bindings.ReturnSeenInput), level, methodInvocation) + } + return &delegate +} + +func (b *bindingsMapping) createDelegateForMethodReturningUint64() *Delegate { + delegate := Delegate{} + delegate.delegateFunc = func(ctx context.Context, readyKey string, input *any, level primitives.ConfidenceLevel) (any, error) { + methodInvocation := func(ctx context.Context, readKey string, input any, level primitives.ConfidenceLevel) (any, error) { + chainReaderTester := b.GetChainReaderTester(readKey) + return chainReaderTester.GetPrimitiveValue(ctx, level) + } + return invokeSpecificMethod(ctx, b.translateReadKey(readyKey), nil, level, methodInvocation) + } + return &delegate +} + +func (b *bindingsMapping) createDelegateForMethodReturningUint64Slice() *Delegate { + delegate := Delegate{} + delegate.delegateFunc = func(ctx context.Context, readyKey string, input *any, level primitives.ConfidenceLevel) (any, error) { + methodInvocation := func(ctx context.Context, readKey string, input any, level primitives.ConfidenceLevel) (any, error) { + chainReaderTester := b.GetChainReaderTester(readKey) + return chainReaderTester.GetSliceValue(ctx, level) + } + return invokeSpecificMethod(ctx, b.translateReadKey(readyKey), nil, level, methodInvocation) + } + return &delegate +} + +func (b *bindingsMapping) createDelegateForSecondContractMethodReturningUint64() *Delegate { + delegate := Delegate{} + delegate.delegateFunc = func(ctx context.Context, readyKey string, input *any, level primitives.ConfidenceLevel) (any, error) { + methodInvocation := func(ctx context.Context, readKey string, input any, level primitives.ConfidenceLevel) (any, error) { + chainReaderTester := b.GetChainReaderTester(readKey) + return chainReaderTester.GetDifferentPrimitiveValue(ctx, level) + } + return invokeSpecificMethod(ctx, b.translateReadKey(readyKey), nil, level, methodInvocation) + } + return &delegate +} + +// Transforms a readKey from ChainReader using the generic testing config to the actual config being used with go bindings which is the auto-generated from the solidity contract. +func (b bindingsMapping) translateReadKey(key string) string { + var updatedKey = key + parts := strings.Split(key, "-") + contractName := parts[1] + methodName := parts[2] + for testConfigName, bindingsName := range b.contractNameMapping { + if contractName == testConfigName { + updatedKey = strings.Replace(updatedKey, testConfigName, bindingsName, 1) + } + } + for testConfigName, bindingsName := range b.methodNameMappingByContract[contractName] { + if methodName == testConfigName { + updatedKey = strings.Replace(updatedKey, testConfigName, bindingsName, 1) + } + } + return updatedKey +} + +// Transforms a readKey from ChainReader using the generic testing config to the actual config being used with go bindings which is the auto-generated from the solidity contract. +func (b bindingsMapping) translateContractName(contractName string) string { + for testContractName, bindingsName := range b.contractNameMapping { + if contractName == testContractName { + return bindingsName + } + } + return contractName +} + +func invokeSpecificMethod[T any](ctx context.Context, readKey string, input T, level primitives.ConfidenceLevel, methodInvocation func(ctx context.Context, readKey string, input T, level primitives.ConfidenceLevel) (any, error)) (any, error) { + return methodInvocation(ctx, readKey, input, level) +} + +func (b bindingsMapping) getBindingDelegate(readKey string) (*Delegate, error) { + translatedKey := removeAddressFromReadIdentifier(b.translateReadKey(readKey)) + delegate := b.delegates[translatedKey] + + if delegate == nil { + return nil, fmt.Errorf("delegate not found for readerKey %s", translatedKey) + } + return delegate, nil +} + +func (b bindingsMapping) GetChainReaderTester(key string) *bindings.ChainReaderTester { + address := key[0:strings.Index(key, "-")] + return b.chainReaderTesters[address] +} + +type Delegate struct { + inputType reflect.Type + delegateFunc func(context.Context, string, *any, primitives.ConfidenceLevel) (any, error) +} + +func (d Delegate) getInput(input any) (*any, error) { + if input == nil { + return nil, nil + } + adaptedInput := reflect.New(d.inputType).Interface() + err := convertStruct(input, adaptedInput) + if err != nil { + return nil, err + } + return &adaptedInput, nil +} + +func (d Delegate) apply(ctx context.Context, readKey string, input any, confidenceLevel primitives.ConfidenceLevel) (any, error) { + adaptedInput, err := d.getInput(input) + if err != nil { + return nil, err + } + output, err := d.delegateFunc(ctx, readKey, adaptedInput, confidenceLevel) + if err != nil { + return nil, err + } + return output, nil +} + +// 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 + dst.(*bindings.GetElementAtIndexInput).I = big.NewInt(int64(value)) + return nil + } + decoderConfig := &mapstructure.DecoderConfig{ + Result: dst, + } + decoder, err := mapstructure.NewDecoder(decoderConfig) + if err != nil { + return err + } + err = decoder.Decode(src) + if err != nil { + return err + } + if reflect.TypeOf(dst).Elem() == reflect.TypeOf(interfacetests.TestStructWithExtraField{}) { + destTestStruct := dst.(*interfacetests.TestStructWithExtraField) + if destTestStruct != nil { + auxTestStruct := &interfacetests.TestStruct{} + mapstructure.Decode(src, auxTestStruct) + destTestStruct.TestStruct = *auxTestStruct + sourceTestStruct := src.(bindings.TestStruct) + destTestStruct.BigField = sourceTestStruct.BigField + destTestStruct.NestedStaticStruct.Inner.I = int(sourceTestStruct.NestedStaticStruct.Inner.IntVal) + destTestStruct.NestedStaticStruct.FixedBytes = sourceTestStruct.NestedStaticStruct.FixedBytes + destTestStruct.NestedDynamicStruct.Inner.I = int(sourceTestStruct.NestedDynamicStruct.Inner.IntVal) + destTestStruct.NestedDynamicStruct.FixedBytes = sourceTestStruct.NestedDynamicStruct.FixedBytes + destTestStruct.ExtraField = interfacetests.AnyExtraValue + } + } else if reflect.TypeOf(dst).Elem() == reflect.TypeOf(interfacetests.TestStruct{}) { + destTestStruct := dst.(*interfacetests.TestStruct) + if destTestStruct != nil { + sourceTestStruct := src.(bindings.TestStruct) + destTestStruct.BigField = sourceTestStruct.BigField + destTestStruct.NestedStaticStruct.Inner.I = int(sourceTestStruct.NestedStaticStruct.Inner.IntVal) + destTestStruct.NestedStaticStruct.FixedBytes = sourceTestStruct.NestedStaticStruct.FixedBytes + 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{}) { + destTestStruct := dst.(*bindings.AddTestStructInput) + if destTestStruct != nil { + sourceTestStruct := src.(interfacetests.TestStruct) + destTestStruct.BigField = sourceTestStruct.BigField + destTestStruct.NestedStaticStruct.Inner.IntVal = int64(sourceTestStruct.NestedStaticStruct.Inner.I) + destTestStruct.NestedStaticStruct.FixedBytes = sourceTestStruct.NestedStaticStruct.FixedBytes + 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{}) { + destTestStruct := dst.(*bindings.ReturnSeenInput) + if destTestStruct != nil { + sourceTestStruct := src.(interfacetests.TestStruct) + destTestStruct.BigField = sourceTestStruct.BigField + destTestStruct.NestedStaticStruct.Inner.IntVal = int64(sourceTestStruct.NestedStaticStruct.Inner.I) + destTestStruct.NestedStaticStruct.FixedBytes = sourceTestStruct.NestedStaticStruct.FixedBytes + destTestStruct.NestedDynamicStruct.Inner.IntVal = int64(sourceTestStruct.NestedDynamicStruct.Inner.I) + destTestStruct.NestedDynamicStruct.FixedBytes = sourceTestStruct.NestedDynamicStruct.FixedBytes + } + } + return err +} diff --git a/core/services/relay/evm/evmtesting/chain_components_interface_tester.go b/core/services/relay/evm/evmtesting/chain_components_interface_tester.go index ac8ece9b37f..cb89d51e5af 100644 --- a/core/services/relay/evm/evmtesting/chain_components_interface_tester.go +++ b/core/services/relay/evm/evmtesting/chain_components_interface_tester.go @@ -59,20 +59,24 @@ type EVMChainComponentsInterfaceTesterHelper[T TestingT[T]] interface { } type EVMChainComponentsInterfaceTester[T TestingT[T]] struct { - Helper EVMChainComponentsInterfaceTesterHelper[T] - client client.Client - address string - address2 string - contractTesters map[string]*chain_reader_tester.ChainReaderTester - chainReaderConfig types.ChainReaderConfig - chainWriterConfig types.ChainWriterConfig - deployerAuth *bind.TransactOpts - senderAuth *bind.TransactOpts - cr evm.ChainReaderService - cw evm.ChainWriterService - dirtyContracts bool - txm evmtxmgr.TxManager - gasEstimator gas.EvmFeeEstimator + TestSelectionSupport + Helper EVMChainComponentsInterfaceTesterHelper[T] + client client.Client + address string + address2 string + contractTesters map[string]*chain_reader_tester.ChainReaderTester + chainReaderConfig types.ChainReaderConfig + chainWriterConfig types.ChainWriterConfig + deployerAuth *bind.TransactOpts + senderAuth *bind.TransactOpts + cr evm.ChainReaderService + cw evm.ChainWriterService + dirtyContracts bool + txm evmtxmgr.TxManager + gasEstimator gas.EvmFeeEstimator + chainReaderConfigSupplier func(t T) types.ChainReaderConfig + chainWriterConfigSupplier func(t T) types.ChainWriterConfig + dirtyConfig bool } func (it *EVMChainComponentsInterfaceTester[T]) Setup(t T) { @@ -94,7 +98,7 @@ func (it *EVMChainComponentsInterfaceTester[T]) Setup(t T) { }) // can re-use the same chain for tests, just make new contract for each test - if it.client != nil { + if it.client != nil && !it.dirtyConfig { it.deployNewContracts(t) return } @@ -105,8 +109,91 @@ func (it *EVMChainComponentsInterfaceTester[T]) Setup(t T) { it.deployerAuth = accounts[0] it.senderAuth = accounts[1] - testStruct := CreateTestStruct[T](0, it) + it.chainReaderConfig = it.chainReaderConfigSupplier(t) + + it.GetContractReader(t) + it.txm = it.Helper.TXM(t, it.client) + + it.chainWriterConfig = it.chainWriterConfigSupplier(t) + it.deployNewContracts(t) + it.dirtyConfig = false +} + +func (it *EVMChainComponentsInterfaceTester[T]) getChainWriterConfig(t T) types.ChainWriterConfig { + return types.ChainWriterConfig{ + Contracts: map[string]*types.ContractConfig{ + AnyContractName: { + ContractABI: chain_reader_tester.ChainReaderTesterMetaData.ABI, + Configs: map[string]*types.ChainWriterDefinition{ + "addTestStruct": { + ChainSpecificName: "addTestStruct", + FromAddress: it.Helper.Accounts(t)[1].From, + GasLimit: 2_000_000, + Checker: "simulate", + InputModifications: codec.ModifiersConfig{ + &codec.RenameModifierConfig{Fields: map[string]string{"NestedDynamicStruct.Inner.IntVal": "I"}}, + &codec.RenameModifierConfig{Fields: map[string]string{"NestedStaticStruct.Inner.IntVal": "I"}}, + }, + }, + "setAlterablePrimitiveValue": { + ChainSpecificName: "setAlterablePrimitiveValue", + FromAddress: it.Helper.Accounts(t)[1].From, + GasLimit: 2_000_000, + Checker: "simulate", + }, + "triggerEvent": { + ChainSpecificName: "triggerEvent", + FromAddress: it.Helper.Accounts(t)[1].From, + GasLimit: 2_000_000, + Checker: "simulate", + InputModifications: codec.ModifiersConfig{ + &codec.RenameModifierConfig{Fields: map[string]string{"NestedDynamicStruct.Inner.IntVal": "I"}}, + &codec.RenameModifierConfig{Fields: map[string]string{"NestedStaticStruct.Inner.IntVal": "I"}}, + }, + }, + "triggerEventWithDynamicTopic": { + ChainSpecificName: "triggerEventWithDynamicTopic", + FromAddress: it.Helper.Accounts(t)[1].From, + GasLimit: 2_000_000, + Checker: "simulate", + }, + "triggerWithFourTopics": { + ChainSpecificName: "triggerWithFourTopics", + FromAddress: it.Helper.Accounts(t)[1].From, + GasLimit: 2_000_000, + Checker: "simulate", + }, + "triggerWithFourTopicsWithHashed": { + ChainSpecificName: "triggerWithFourTopicsWithHashed", + FromAddress: it.Helper.Accounts(t)[1].From, + GasLimit: 2_000_000, + Checker: "simulate", + }, + }, + }, + AnySecondContractName: { + ContractABI: chain_reader_tester.ChainReaderTesterMetaData.ABI, + Configs: map[string]*types.ChainWriterDefinition{ + "addTestStruct": { + ChainSpecificName: "addTestStruct", + FromAddress: it.Helper.Accounts(t)[1].From, + GasLimit: 2_000_000, + Checker: "simulate", + InputModifications: codec.ModifiersConfig{ + &codec.RenameModifierConfig{Fields: map[string]string{"NestedDynamicStruct.Inner.IntVal": "I"}}, + &codec.RenameModifierConfig{Fields: map[string]string{"NestedStaticStruct.Inner.IntVal": "I"}}, + }, + }, + }, + }, + }, + MaxGasPrice: assets.NewWei(big.NewInt(1000000000000000000)), + } +} + +func (it *EVMChainComponentsInterfaceTester[T]) getChainReaderConfig(t T) types.ChainReaderConfig { + testStruct := CreateTestStruct[T](0, it) methodTakingLatestParamsReturningTestStructConfig := types.ChainReaderDefinition{ ChainSpecificName: "getElementAtIndex", OutputModifications: codec.ModifiersConfig{ @@ -114,8 +201,7 @@ func (it *EVMChainComponentsInterfaceTester[T]) Setup(t T) { &codec.RenameModifierConfig{Fields: map[string]string{"NestedStaticStruct.Inner.IntVal": "I"}}, }, } - - it.chainReaderConfig = types.ChainReaderConfig{ + return types.ChainReaderConfig{ Contracts: map[string]types.ChainContractReader{ AnyContractName: { ContractABI: chain_reader_tester.ChainReaderTesterMetaData.ABI, @@ -211,79 +297,6 @@ func (it *EVMChainComponentsInterfaceTester[T]) Setup(t T) { }, }, } - it.GetContractReader(t) - it.txm = it.Helper.TXM(t, it.client) - - it.chainWriterConfig = types.ChainWriterConfig{ - Contracts: map[string]*types.ContractConfig{ - AnyContractName: { - ContractABI: chain_reader_tester.ChainReaderTesterMetaData.ABI, - Configs: map[string]*types.ChainWriterDefinition{ - "addTestStruct": { - ChainSpecificName: "addTestStruct", - FromAddress: it.Helper.Accounts(t)[1].From, - GasLimit: 2_000_000, - Checker: "simulate", - InputModifications: codec.ModifiersConfig{ - &codec.RenameModifierConfig{Fields: map[string]string{"NestedDynamicStruct.Inner.IntVal": "I"}}, - &codec.RenameModifierConfig{Fields: map[string]string{"NestedStaticStruct.Inner.IntVal": "I"}}, - }, - }, - "setAlterablePrimitiveValue": { - ChainSpecificName: "setAlterablePrimitiveValue", - FromAddress: it.Helper.Accounts(t)[1].From, - GasLimit: 2_000_000, - Checker: "simulate", - }, - "triggerEvent": { - ChainSpecificName: "triggerEvent", - FromAddress: it.Helper.Accounts(t)[1].From, - GasLimit: 2_000_000, - Checker: "simulate", - InputModifications: codec.ModifiersConfig{ - &codec.RenameModifierConfig{Fields: map[string]string{"NestedDynamicStruct.Inner.IntVal": "I"}}, - &codec.RenameModifierConfig{Fields: map[string]string{"NestedStaticStruct.Inner.IntVal": "I"}}, - }, - }, - "triggerEventWithDynamicTopic": { - ChainSpecificName: "triggerEventWithDynamicTopic", - FromAddress: it.Helper.Accounts(t)[1].From, - GasLimit: 2_000_000, - Checker: "simulate", - }, - "triggerWithFourTopics": { - ChainSpecificName: "triggerWithFourTopics", - FromAddress: it.Helper.Accounts(t)[1].From, - GasLimit: 2_000_000, - Checker: "simulate", - }, - "triggerWithFourTopicsWithHashed": { - ChainSpecificName: "triggerWithFourTopicsWithHashed", - FromAddress: it.Helper.Accounts(t)[1].From, - GasLimit: 2_000_000, - Checker: "simulate", - }, - }, - }, - AnySecondContractName: { - ContractABI: chain_reader_tester.ChainReaderTesterMetaData.ABI, - Configs: map[string]*types.ChainWriterDefinition{ - "addTestStruct": { - ChainSpecificName: "addTestStruct", - FromAddress: it.Helper.Accounts(t)[1].From, - GasLimit: 2_000_000, - Checker: "simulate", - InputModifications: codec.ModifiersConfig{ - &codec.RenameModifierConfig{Fields: map[string]string{"NestedDynamicStruct.Inner.IntVal": "I"}}, - &codec.RenameModifierConfig{Fields: map[string]string{"NestedStaticStruct.Inner.IntVal": "I"}}, - }, - }, - }, - }, - }, - MaxGasPrice: assets.NewWei(big.NewInt(1000000000000000000)), - } - it.deployNewContracts(t) } func (it *EVMChainComponentsInterfaceTester[T]) Name() string { @@ -418,6 +431,22 @@ func (it *EVMChainComponentsInterfaceTester[T]) MaxWaitTimeForEvents() time.Dura return it.Helper.MaxWaitTimeForEvents() } +func (it *EVMChainComponentsInterfaceTester[T]) Init(t T) { + it.Helper.Init(t) + it.chainWriterConfigSupplier = func(t T) types.ChainWriterConfig { return it.getChainWriterConfig(t) } + it.chainReaderConfigSupplier = func(t T) types.ChainReaderConfig { return it.getChainReaderConfig(t) } +} + +func (it *EVMChainComponentsInterfaceTester[T]) SetChainReaderConfigSupplier(chainReaderConfigSupplier func(t T) types.ChainReaderConfig) { + it.dirtyConfig = true + it.chainReaderConfigSupplier = chainReaderConfigSupplier +} + +func (it *EVMChainComponentsInterfaceTester[T]) SetChainWriterConfigSupplier(chainWriterConfigSupplier func(t T) types.ChainWriterConfig) { + it.dirtyConfig = true + it.chainWriterConfigSupplier = chainWriterConfigSupplier +} + func OracleIDsToBytes(oracleIDs [32]commontypes.OracleID) [32]byte { convertedIDs := [32]byte{} for i, id := range oracleIDs { diff --git a/core/services/relay/evm/evmtesting/run_tests.go b/core/services/relay/evm/evmtesting/run_tests.go index 7e01cf2b657..ce67f6d0231 100644 --- a/core/services/relay/evm/evmtesting/run_tests.go +++ b/core/services/relay/evm/evmtesting/run_tests.go @@ -20,6 +20,16 @@ import ( . "github.com/smartcontractkit/chainlink-common/pkg/types/interfacetests" //nolint common practice to import test mods with . ) +const ( + QueryKeyFilterOnDataWordsWithValueComparator = "Filtering can be done on data words using value comparator" + QueryKeyOnDataWordsWithValueComparatorOnNestedField = "Filtering can be done on data words using value comparator on a nested field" + QueryKeyFilterOnDataWordsWithValueComparatorOnDynamicField = "Filtering can be done on data words using value comparator on field that follows a dynamic field" + DynamicTypedTopicsFilterAndCorrectReturn = "Dynamically typed topics can be used to filter and have type correct in return" + MultipleTopicCanFilterTogether = "Multiple topics can filter together" + FilteringCanBeDoneOnHashedIndexedTopics = "Filtering can be done on indexed topics that get hashed" + BindReturnsErrorOnMissingContractAtAddress = "Bind returns error on missing contract at address" +) + func RunChainComponentsEvmTests[T TestingT[T]](t T, it *EVMChainComponentsInterfaceTester[T]) { RunContractReaderEvmTests[T](t, it) // Add ChainWriter tests here @@ -33,194 +43,242 @@ func RunChainComponentsInLoopEvmTests[T TestingT[T]](t T, it ChainComponentsInte func RunContractReaderEvmTests[T TestingT[T]](t T, it *EVMChainComponentsInterfaceTester[T]) { RunContractReaderInterfaceTests[T](t, it, false) - t.Run("Dynamically typed topics can be used to filter and have type correct in return", func(t T) { - it.Setup(t) - - anyString := "foo" - ctx := it.Helper.Context(t) - - cr := it.GetContractReader(t) - bindings := it.GetBindings(t) - require.NoError(t, cr.Bind(ctx, bindings)) - - type DynamicEvent struct { - Field string - } - SubmitTransactionToCW(t, it, "triggerEventWithDynamicTopic", DynamicEvent{Field: anyString}, bindings[0], types.Unconfirmed) - - input := struct{ Field string }{Field: anyString} - tp := cr.(clcommontypes.ContractTypeProvider) - - readName := types.BoundContract{ - Address: bindings[0].Address, - Name: AnyContractName, - }.ReadIdentifier(triggerWithDynamicTopic) - - output, err := tp.CreateContractType(readName, false) - require.NoError(t, err) - rOutput := reflect.Indirect(reflect.ValueOf(output)) - - require.Eventually(t, func() bool { - return cr.GetLatestValue(ctx, readName, primitives.Unconfirmed, input, output) == nil - }, it.MaxWaitTimeForEvents(), 100*time.Millisecond) - - assert.Equal(t, &anyString, rOutput.FieldByName("Field").Interface()) - topic, err := abi.MakeTopics([]any{anyString}) - require.NoError(t, err) - assert.Equal(t, &topic[0][0], rOutput.FieldByName("FieldHash").Interface()) - }) - - t.Run("Multiple topics can filter together", func(t T) { - it.Setup(t) - ctx := it.Helper.Context(t) - cr := it.GetContractReader(t) - bindings := it.GetBindings(t) - - require.NoError(t, cr.Bind(ctx, bindings)) - - triggerFourTopics(t, it, int32(1), int32(2), int32(3)) - triggerFourTopics(t, it, int32(2), int32(2), int32(3)) - triggerFourTopics(t, it, int32(1), int32(3), int32(3)) - triggerFourTopics(t, it, int32(1), int32(2), int32(4)) - - var bound types.BoundContract - for idx := range bindings { - if bindings[idx].Name == AnyContractName { - bound = bindings[idx] - } - } - - var latest struct{ Field1, Field2, Field3 int32 } - params := struct{ Field1, Field2, Field3 int32 }{Field1: 1, Field2: 2, Field3: 3} - - time.Sleep(it.MaxWaitTimeForEvents()) - - require.NoError(t, cr.GetLatestValue(ctx, bound.ReadIdentifier(triggerWithAllTopics), primitives.Unconfirmed, params, &latest)) - assert.Equal(t, int32(1), latest.Field1) - assert.Equal(t, int32(2), latest.Field2) - assert.Equal(t, int32(3), latest.Field3) - }) - - t.Run("Filtering can be done on indexed topics that get hashed", func(t T) { - it.Setup(t) - - cr := it.GetContractReader(t) - ctx := it.Helper.Context(t) - bindings := it.GetBindings(t) - - require.NoError(t, cr.Bind(ctx, bindings)) - - triggerFourTopicsWithHashed(t, it, "1", [32]uint8{2}, [32]byte{5}) - triggerFourTopicsWithHashed(t, it, "2", [32]uint8{2}, [32]byte{3}) - triggerFourTopicsWithHashed(t, it, "1", [32]uint8{3}, [32]byte{3}) - - var bound types.BoundContract - for idx := range bindings { - if bindings[idx].Name == AnyContractName { - bound = bindings[idx] - } - } - - var latest struct { - Field3 [32]byte - } - params := struct { - Field1 string - Field2 [32]uint8 - Field3 [32]byte - }{Field1: "1", Field2: [32]uint8{2}, Field3: [32]byte{5}} - - time.Sleep(it.MaxWaitTimeForEvents()) - require.NoError(t, cr.GetLatestValue(ctx, bound.ReadIdentifier(triggerWithAllTopicsWithHashed), primitives.Unconfirmed, params, &latest)) - // only checking Field3 topic makes sense since it isn't hashed, to check other fields we'd have to replicate solidity encoding and hashing - assert.Equal(t, [32]uint8{5}, latest.Field3) - }) - - t.Run("Bind returns error on missing contract at address", func(t T) { - it.Setup(t) - - addr := common.BigToAddress(big.NewInt(42)) - reader := it.GetContractReader(t) - - ctx := it.Helper.Context(t) - err := reader.Bind(ctx, []clcommontypes.BoundContract{{Name: AnyContractName, Address: addr.Hex()}}) - - require.ErrorIs(t, err, read.NoContractExistsError{Address: addr}) - }) + testCases := []Testcase[T]{ + { + Name: DynamicTypedTopicsFilterAndCorrectReturn, + Test: func(t T) { + it.Setup(t) + + anyString := "foo" + ctx := it.Helper.Context(t) + + cr := it.GetContractReader(t) + bindings := it.GetBindings(t) + require.NoError(t, cr.Bind(ctx, bindings)) + + type DynamicEvent struct { + Field string + } + SubmitTransactionToCW(t, it, "triggerEventWithDynamicTopic", DynamicEvent{Field: anyString}, bindings[0], types.Unconfirmed) + + input := struct{ Field string }{Field: anyString} + tp := cr.(clcommontypes.ContractTypeProvider) + + readName := types.BoundContract{ + Address: bindings[0].Address, + Name: AnyContractName, + }.ReadIdentifier(triggerWithDynamicTopic) + + output, err := tp.CreateContractType(readName, false) + require.NoError(t, err) + rOutput := reflect.Indirect(reflect.ValueOf(output)) + + require.Eventually(t, func() bool { + return cr.GetLatestValue(ctx, readName, primitives.Unconfirmed, input, output) == nil + }, it.MaxWaitTimeForEvents(), 100*time.Millisecond) + + assert.Equal(t, &anyString, rOutput.FieldByName("Field").Interface()) + topic, err := abi.MakeTopics([]any{anyString}) + require.NoError(t, err) + assert.Equal(t, &topic[0][0], rOutput.FieldByName("FieldHash").Interface()) + }, + }, + { + Name: MultipleTopicCanFilterTogether, + Test: func(t T) { + it.Setup(t) + ctx := it.Helper.Context(t) + cr := it.GetContractReader(t) + bindings := it.GetBindings(t) + + require.NoError(t, cr.Bind(ctx, bindings)) + + triggerFourTopics(t, it, int32(1), int32(2), int32(3)) + triggerFourTopics(t, it, int32(2), int32(2), int32(3)) + triggerFourTopics(t, it, int32(1), int32(3), int32(3)) + triggerFourTopics(t, it, int32(1), int32(2), int32(4)) + + var bound types.BoundContract + for idx := range bindings { + if bindings[idx].Name == AnyContractName { + bound = bindings[idx] + } + } + + var latest struct{ Field1, Field2, Field3 int32 } + params := struct{ Field1, Field2, Field3 int32 }{Field1: 1, Field2: 2, Field3: 3} + + time.Sleep(it.MaxWaitTimeForEvents()) + + require.NoError(t, cr.GetLatestValue(ctx, bound.ReadIdentifier(triggerWithAllTopics), primitives.Unconfirmed, params, &latest)) + assert.Equal(t, int32(1), latest.Field1) + assert.Equal(t, int32(2), latest.Field2) + assert.Equal(t, int32(3), latest.Field3) + }, + }, + { + Name: FilteringCanBeDoneOnHashedIndexedTopics, + Test: func(t T) { + it.Setup(t) + + cr := it.GetContractReader(t) + ctx := it.Helper.Context(t) + bindings := it.GetBindings(t) + + require.NoError(t, cr.Bind(ctx, bindings)) + + triggerFourTopicsWithHashed(t, it, "1", [32]uint8{2}, [32]byte{5}) + triggerFourTopicsWithHashed(t, it, "2", [32]uint8{2}, [32]byte{3}) + triggerFourTopicsWithHashed(t, it, "1", [32]uint8{3}, [32]byte{3}) + + var bound types.BoundContract + for idx := range bindings { + if bindings[idx].Name == AnyContractName { + bound = bindings[idx] + } + } + + var latest struct { + Field3 [32]byte + } + params := struct { + Field1 string + Field2 [32]uint8 + Field3 [32]byte + }{Field1: "1", Field2: [32]uint8{2}, Field3: [32]byte{5}} + + time.Sleep(it.MaxWaitTimeForEvents()) + require.NoError(t, cr.GetLatestValue(ctx, bound.ReadIdentifier(triggerWithAllTopicsWithHashed), primitives.Unconfirmed, params, &latest)) + // only checking Field3 topic makes sense since it isn't hashed, to check other fields we'd have to replicate solidity encoding and hashing + assert.Equal(t, [32]uint8{5}, latest.Field3) + }, + }, + { + Name: BindReturnsErrorOnMissingContractAtAddress, + Test: func(t T) { + it.Setup(t) + + addr := common.BigToAddress(big.NewInt(42)) + reader := it.GetContractReader(t) + + ctx := it.Helper.Context(t) + err := reader.Bind(ctx, []clcommontypes.BoundContract{{Name: AnyContractName, Address: addr.Hex()}}) + + require.ErrorIs(t, err, read.NoContractExistsError{Address: addr}) + }, + }, + } + RunTests(t, it, testCases) } func RunContractReaderInLoopTests[T TestingT[T]](t T, it ChainComponentsInterfaceTester[T]) { RunContractReaderInterfaceTests[T](t, it, false) - it.Setup(t) - ctx := tests.Context(t) - cr := it.GetContractReader(t) - require.NoError(t, cr.Bind(ctx, it.GetBindings(t))) - bindings := it.GetBindings(t) - boundContract := BindingsByName(bindings, AnyContractName)[0] - require.NoError(t, cr.Bind(ctx, bindings)) - - ts1 := CreateTestStruct[T](0, it) - _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed) - ts2 := CreateTestStruct[T](15, it) - _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed) - ts3 := CreateTestStruct[T](35, it) - _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed) - - t.Run("Filtering can be done on data words using value comparator", func(t T) { - ts := &TestStruct{} - assert.Eventually(t, func() bool { - sequences, err := cr.QueryKey(ctx, boundContract, query.KeyFilter{Key: EventName, Expressions: []query.Expression{ - query.Comparator("OracleID", - primitives.ValueComparator{ - Value: uint8(ts2.OracleID), - Operator: primitives.Eq, - }), + testcases := []Testcase[T]{ + { + + Name: QueryKeyFilterOnDataWordsWithValueComparator, + Test: func(t T) { + ctx := tests.Context(t) + cr := it.GetContractReader(t) + require.NoError(t, cr.Bind(ctx, it.GetBindings(t))) + bindings := it.GetBindings(t) + boundContract := BindingsByName(bindings, AnyContractName)[0] + require.NoError(t, cr.Bind(ctx, bindings)) + + ts1 := CreateTestStruct[T](0, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed) + ts2 := CreateTestStruct[T](15, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed) + ts3 := CreateTestStruct[T](35, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed) + ts := &TestStruct{} + assert.Eventually(t, func() bool { + sequences, err := cr.QueryKey(ctx, boundContract, query.KeyFilter{Key: EventName, Expressions: []query.Expression{ + query.Comparator("OracleID", + primitives.ValueComparator{ + Value: uint8(ts2.OracleID), + Operator: primitives.Eq, + }), + }, + }, query.LimitAndSort{}, ts) + return err == nil && len(sequences) == 1 && reflect.DeepEqual(&ts2, sequences[0].Data) + }, it.MaxWaitTimeForEvents(), time.Millisecond*10) }, - }, query.LimitAndSort{}, ts) - return err == nil && len(sequences) == 1 && reflect.DeepEqual(&ts2, sequences[0].Data) - }, it.MaxWaitTimeForEvents(), time.Millisecond*10) - }) - - t.Run("Filtering can be done on data words using value comparator on a nested field", func(t T) { - ts := &TestStruct{} - assert.Eventually(t, func() bool { - sequences, err := cr.QueryKey(ctx, boundContract, query.KeyFilter{Key: EventName, Expressions: []query.Expression{ - query.Comparator("OracleID", - primitives.ValueComparator{ - Value: uint8(ts2.OracleID), - Operator: primitives.Eq, - }), - query.Comparator("NestedStaticStruct.Inner.IntVal", - primitives.ValueComparator{ - Value: ts2.NestedStaticStruct.Inner.I, - Operator: primitives.Eq, - }), + }, + { + Name: QueryKeyOnDataWordsWithValueComparatorOnNestedField, + Test: func(t T) { + ctx := tests.Context(t) + cr := it.GetContractReader(t) + require.NoError(t, cr.Bind(ctx, it.GetBindings(t))) + bindings := it.GetBindings(t) + boundContract := BindingsByName(bindings, AnyContractName)[0] + require.NoError(t, cr.Bind(ctx, bindings)) + + ts1 := CreateTestStruct[T](0, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed) + ts2 := CreateTestStruct[T](15, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed) + ts3 := CreateTestStruct[T](35, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed) + ts := &TestStruct{} + assert.Eventually(t, func() bool { + sequences, err := cr.QueryKey(ctx, boundContract, query.KeyFilter{Key: EventName, Expressions: []query.Expression{ + query.Comparator("OracleID", + primitives.ValueComparator{ + Value: uint8(ts2.OracleID), + Operator: primitives.Eq, + }), + query.Comparator("NestedStaticStruct.Inner.IntVal", + primitives.ValueComparator{ + Value: ts2.NestedStaticStruct.Inner.I, + Operator: primitives.Eq, + }), + }, + }, query.LimitAndSort{}, ts) + return err == nil && len(sequences) == 1 && reflect.DeepEqual(&ts2, sequences[0].Data) + }, it.MaxWaitTimeForEvents(), time.Millisecond*10) }, - }, query.LimitAndSort{}, ts) - return err == nil && len(sequences) == 1 && reflect.DeepEqual(&ts2, sequences[0].Data) - }, it.MaxWaitTimeForEvents(), time.Millisecond*10) - }) - - t.Run("Filtering can be done on data words using value comparator on field that follows a dynamic field", func(t T) { - ts := &TestStruct{} - assert.Eventually(t, func() bool { - sequences, err := cr.QueryKey(ctx, boundContract, query.KeyFilter{Key: EventName, Expressions: []query.Expression{ - query.Comparator("OracleID", - primitives.ValueComparator{ - Value: uint8(ts2.OracleID), - Operator: primitives.Eq, - }), - query.Comparator("BigField", - primitives.ValueComparator{ - Value: ts2.BigField, - Operator: primitives.Eq, - }), + }, + { + Name: QueryKeyFilterOnDataWordsWithValueComparatorOnDynamicField, + Test: func(t T) { + ctx := tests.Context(t) + cr := it.GetContractReader(t) + require.NoError(t, cr.Bind(ctx, it.GetBindings(t))) + bindings := it.GetBindings(t) + boundContract := BindingsByName(bindings, AnyContractName)[0] + require.NoError(t, cr.Bind(ctx, bindings)) + + ts1 := CreateTestStruct[T](0, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts1, boundContract, types.Unconfirmed) + ts2 := CreateTestStruct[T](15, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts2, boundContract, types.Unconfirmed) + ts3 := CreateTestStruct[T](35, it) + _ = SubmitTransactionToCW(t, it, MethodTriggeringEvent, ts3, boundContract, types.Unconfirmed) + ts := &TestStruct{} + assert.Eventually(t, func() bool { + sequences, err := cr.QueryKey(ctx, boundContract, query.KeyFilter{Key: EventName, Expressions: []query.Expression{ + query.Comparator("OracleID", + primitives.ValueComparator{ + Value: uint8(ts2.OracleID), + Operator: primitives.Eq, + }), + query.Comparator("BigField", + primitives.ValueComparator{ + Value: ts2.BigField, + Operator: primitives.Eq, + }), + }, + }, query.LimitAndSort{}, ts) + return err == nil && len(sequences) == 1 && reflect.DeepEqual(&ts2, sequences[0].Data) + }, it.MaxWaitTimeForEvents(), time.Millisecond*10) }, - }, query.LimitAndSort{}, ts) - return err == nil && len(sequences) == 1 && reflect.DeepEqual(&ts2, sequences[0].Data) - }, it.MaxWaitTimeForEvents(), time.Millisecond*10) - }) + }, + } + + RunTests(t, it, testcases) } func triggerFourTopics[T TestingT[T]](t T, it *EVMChainComponentsInterfaceTester[T], i1, i2, i3 int32) { diff --git a/go.mod b/go.mod index c192323e61d..6ec1a951c68 100644 --- a/go.mod +++ b/go.mod @@ -75,7 +75,7 @@ require ( github.com/smartcontractkit/chain-selectors v1.0.23 github.com/smartcontractkit/chainlink-automation v1.0.4 github.com/smartcontractkit/chainlink-ccip v0.0.0-20240930203817-424892240cd1 - github.com/smartcontractkit/chainlink-common v0.2.3-0.20241001140426-35be2fad06ec + github.com/smartcontractkit/chainlink-common v0.2.3-0.20241003131539-be5ef0b4b833 github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240911175228-daf2600bb7b7 github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240916152957-433914114bd2 github.com/smartcontractkit/chainlink-feeds v0.0.0-20240910155501-42f20443189f diff --git a/go.sum b/go.sum index 89a1b45d722..b33529443ab 100644 --- a/go.sum +++ b/go.sum @@ -1056,8 +1056,8 @@ github.com/smartcontractkit/chainlink-automation v1.0.4 h1:iyW181JjKHLNMnDleI8um github.com/smartcontractkit/chainlink-automation v1.0.4/go.mod h1:u4NbPZKJ5XiayfKHD/v3z3iflQWqvtdhj13jVZXj/cM= github.com/smartcontractkit/chainlink-ccip v0.0.0-20240930203817-424892240cd1 h1:X07jKFIakpR7UyG/BnQ8wKZz395LsEcowqyQgDKHcT8= github.com/smartcontractkit/chainlink-ccip v0.0.0-20240930203817-424892240cd1/go.mod h1:/nGkIe25kgtr+l6y30VH+aTVaxu0NjIEEEhtV1TDlaE= -github.com/smartcontractkit/chainlink-common v0.2.3-0.20241001140426-35be2fad06ec h1:zmLmKLCpoV73AaGU8YBc86YPLJBzoZFv9lPC677Eqcs= -github.com/smartcontractkit/chainlink-common v0.2.3-0.20241001140426-35be2fad06ec/go.mod h1:F6WUS6N4mP5ScwpwyTyAJc9/vjR+GXbMCRUOVekQi1g= +github.com/smartcontractkit/chainlink-common v0.2.3-0.20241003131539-be5ef0b4b833 h1:hfL6TrruENsMo4YTYNrH57colKH+bszWIQF6uI4mLAU= +github.com/smartcontractkit/chainlink-common v0.2.3-0.20241003131539-be5ef0b4b833/go.mod h1:F6WUS6N4mP5ScwpwyTyAJc9/vjR+GXbMCRUOVekQi1g= github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240911175228-daf2600bb7b7 h1:lTGIOQYLk1Ufn++X/AvZnt6VOcuhste5yp+C157No/Q= github.com/smartcontractkit/chainlink-cosmos v0.4.1-0.20240911175228-daf2600bb7b7/go.mod h1:BMYE1vC/pGmdFSsOJdPrAA0/4gZ0Xo0SxTMdGspBtRo= github.com/smartcontractkit/chainlink-data-streams v0.0.0-20240916152957-433914114bd2 h1:yRk4ektpx/UxwarqAfgxUXLrsYXlaNeP1NOwzHGrK2Q=