diff --git a/core/services/relay/evm/chain_reader.go b/core/services/relay/evm/chain_reader.go index 99be89eae17..afe3b53902a 100644 --- a/core/services/relay/evm/chain_reader.go +++ b/core/services/relay/evm/chain_reader.go @@ -199,7 +199,11 @@ func (cr *chainReader) GetLatestValue(ctx context.Context, readName string, conf ptrToValue, isValue := returnVal.(*values.Value) if !isValue { _, err = binding.GetLatestValueWithHeadData(ctx, common.HexToAddress(address), confidenceLevel, params, returnVal) - return err + if err != nil { + return err + } + + return nil } contractType, err := cr.CreateContractType(readName, false) diff --git a/core/services/relay/evm/read/batch.go b/core/services/relay/evm/read/batch.go index 16333149f11..ce1c546ad73 100644 --- a/core/services/relay/evm/read/batch.go +++ b/core/services/relay/evm/read/batch.go @@ -241,32 +241,36 @@ func (c *defaultEvmBatchCaller) unpackBatchResults( return nil, callErr } - if err = c.codec.Decode( - ctx, - packedBytes, - call.ReturnVal, - codec.WrapItemType(call.ContractName, call.ReadName, false), - ); err != nil { - if len(packedBytes) == 0 { - callErr := newErrorFromCall( - fmt.Errorf("%w: %w: %s", types.ErrInternal, errEmptyOutput, err.Error()), - call, block, batchReadType, - ) - - callErr.Result = &hexEncodedOutputs[idx] - - results[idx].err = callErr - } else { - callErr := newErrorFromCall( - fmt.Errorf("%w: codec decode result: %s", types.ErrInvalidType, err.Error()), - call, block, batchReadType, - ) - - callErr.Result = &hexEncodedOutputs[idx] - results[idx].err = callErr - } + // the codec can't do anything with no bytes, so skip decoding and allow + // the result to be the empty struct or value + if len(packedBytes) > 0 { + if err = c.codec.Decode( + ctx, + packedBytes, + call.ReturnVal, + codec.WrapItemType(call.ContractName, call.ReadName, false), + ); err != nil { + if len(packedBytes) == 0 { + callErr := newErrorFromCall( + fmt.Errorf("%w: %w: %s", types.ErrInternal, errEmptyOutput, err.Error()), + call, block, batchReadType, + ) + + callErr.Result = &hexEncodedOutputs[idx] + + results[idx].err = callErr + } else { + callErr := newErrorFromCall( + fmt.Errorf("%w: codec decode result: %s", types.ErrInvalidType, err.Error()), + call, block, batchReadType, + ) + + callErr.Result = &hexEncodedOutputs[idx] + results[idx].err = callErr + } - continue + continue + } } results[idx].returnVal = call.ReturnVal diff --git a/core/services/relay/evm/read/method.go b/core/services/relay/evm/read/method.go index e988e4352f7..ed44e1aa9ca 100644 --- a/core/services/relay/evm/read/method.go +++ b/core/services/relay/evm/read/method.go @@ -2,6 +2,7 @@ package read import ( "context" + "errors" "fmt" "math/big" "sync" @@ -22,6 +23,8 @@ import ( evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" ) +var ErrEmptyContractReturnValue = errors.New("the contract return value was empty") + type MethodBinding struct { // read-only properties contractName string @@ -173,6 +176,12 @@ func (b *MethodBinding) GetLatestValueWithHeadData(ctx context.Context, addr com return nil, callErr } + // there may be cases where the contract value has not been set and the RPC returns with a value of 0x + // which is a set of empty bytes. there is no need for the codec to run in this case. + if len(bytes) == 0 { + return block.ToChainAgnosticHead(), nil + } + if err = b.codec.Decode(ctx, bytes, returnVal, codec.WrapItemType(b.contractName, b.method, false)); err != nil { callErr := newErrorFromCall( fmt.Errorf("%w: decode return data: %s", commontypes.ErrInvalidType, err.Error()),