Skip to content

Commit

Permalink
BCF-3242 Codec Float/Int Type Loss (#13414)
Browse files Browse the repository at this point in the history
* add new hook and make decode function

* updated tester to encode/decode config

* remove common replace

* mod tidy
  • Loading branch information
EasterTheBunny authored Jun 18, 2024
1 parent 9810856 commit b0a48d6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
8 changes: 7 additions & 1 deletion core/services/relay/evm/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evmtesting

import (
"context"
"encoding/json"
"math/big"
"time"

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions core/services/relay/evm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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

)

0 comments on commit b0a48d6

Please sign in to comment.