From b0a48d6951ca408e3f2a08eafcd22ef07100644e Mon Sep 17 00:00:00 2001 From: Awbrey Hughlett Date: Tue, 18 Jun 2024 13:34:23 -0500 Subject: [PATCH] BCF-3242 Codec Float/Int Type Loss (#13414) * add new hook and make decode function * updated tester to encode/decode config * remove common replace * mod tidy --- core/services/relay/evm/codec.go | 8 +++++++- .../evmtesting/chain_reader_interface_tester.go | 10 +++++++++- core/services/relay/evm/types/types.go | 16 ++++++++++++++++ go.mod | 1 - 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/core/services/relay/evm/codec.go b/core/services/relay/evm/codec.go index a87976da54f..9636996c2ae 100644 --- a/core/services/relay/evm/codec.go +++ b/core/services/relay/evm/codec.go @@ -29,7 +29,13 @@ import ( // SliceToArrayVerifySizeHook verifies that slices have the correct size when converting to an array // sizeVerifyBigIntHook allows our custom types that verify the number fits in the on-chain type to be converted as-if // it was a *big.Int -var evmDecoderHooks = []mapstructure.DecodeHookFunc{decodeAccountAndAllowArraySliceHook, codec.BigIntHook, codec.SliceToArrayVerifySizeHook, sizeVerifyBigIntHook} +var evmDecoderHooks = []mapstructure.DecodeHookFunc{ + decodeAccountAndAllowArraySliceHook, + codec.BigIntHook, + codec.SliceToArrayVerifySizeHook, + sizeVerifyBigIntHook, + codec.NumberHook, +} // NewCodec creates a new [commontypes.RemoteCodec] for EVM. // Note that names in the ABI are converted to Go names using [abi.ToCamelCase], diff --git a/core/services/relay/evm/evmtesting/chain_reader_interface_tester.go b/core/services/relay/evm/evmtesting/chain_reader_interface_tester.go index 61a45996ac2..b9f141b2d0b 100644 --- a/core/services/relay/evm/evmtesting/chain_reader_interface_tester.go +++ b/core/services/relay/evm/evmtesting/chain_reader_interface_tester.go @@ -2,6 +2,7 @@ package evmtesting import ( "context" + "encoding/json" "math/big" "time" @@ -201,7 +202,14 @@ func (it *EVMChainReaderInterfaceTester[T]) GetChainReader(t T) clcommontypes.Co lp := logpoller.NewLogPoller(logpoller.NewORM(it.Helper.ChainID(), db, lggr), it.client, lggr, lpOpts) require.NoError(t, lp.Start(ctx)) - cr, err := evm.NewChainReaderService(ctx, lggr, lp, it.client, it.chainConfig) + // encode and decode the config to ensure the test covers type issues + confBytes, err := json.Marshal(it.chainConfig) + require.NoError(t, err) + + conf, err := types.ChainReaderConfigFromBytes(confBytes) + require.NoError(t, err) + + cr, err := evm.NewChainReaderService(ctx, lggr, lp, it.client, conf) require.NoError(t, err) require.NoError(t, cr.Start(ctx)) it.cr = cr diff --git a/core/services/relay/evm/types/types.go b/core/services/relay/evm/types/types.go index 0d508610389..e09f48f1337 100644 --- a/core/services/relay/evm/types/types.go +++ b/core/services/relay/evm/types/types.go @@ -274,3 +274,19 @@ type LogPollerWrapper interface { // TODO (FUN-668): Remove from the LOOP interface and only use internally within the EVM relayer SubscribeToUpdates(ctx context.Context, name string, subscriber RouteUpdateSubscriber) } + +// ChainReaderConfigFromBytes function applies json decoding on provided bytes to return a +// valid ChainReaderConfig. If other unmarshaling or parsing techniques are required, place it +// here. +func ChainReaderConfigFromBytes(bts []byte) (ChainReaderConfig, error) { + decoder := json.NewDecoder(bytes.NewBuffer(bts)) + + decoder.UseNumber() + + var cfg ChainReaderConfig + if err := decoder.Decode(&cfg); err != nil { + return cfg, fmt.Errorf("failed to unmarshal chain reader config err: %s", err) + } + + return cfg, nil +} diff --git a/go.mod b/go.mod index 14b2d0e0262..37619ec3665 100644 --- a/go.mod +++ b/go.mod @@ -348,5 +348,4 @@ replace ( // until merged upstream: https://github.com/mwitkow/grpc-proxy/pull/69 github.com/mwitkow/grpc-proxy => github.com/smartcontractkit/grpc-proxy v0.0.0-20230731113816-f1be6620749f - )