Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract Reader Handle Empty Bytes #15688

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion core/services/relay/evm/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 29 additions & 25 deletions core/services/relay/evm/read/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions core/services/relay/evm/read/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package read

import (
"context"
"errors"
"fmt"
"math/big"
"sync"
Expand All @@ -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
Expand Down Expand Up @@ -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()),
Expand Down
Loading