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

Correct Read Error Format #15223

Merged
merged 4 commits into from
Nov 19, 2024
Merged
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
24 changes: 12 additions & 12 deletions core/services/relay/evm/read/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func newDefaultEvmBatchCaller(
}

// batchCall formats a batch, calls the rpc client, and unpacks results.
// this function only returns errors of type ErrRead which should wrap lower errors.
// this function only returns errors of type Error which should wrap lower errors.
func (c *defaultEvmBatchCaller) batchCall(ctx context.Context, blockNumber uint64, batchCall BatchCall) ([]dataAndErr, error) {
if len(batchCall) == 0 {
return nil, nil
Expand All @@ -147,9 +147,9 @@ func (c *defaultEvmBatchCaller) batchCall(ctx context.Context, blockNumber uint6
if err = c.evmClient.BatchCallContext(ctx, rpcBatchCalls); err != nil {
// return a basic read error with no detail or result since this is a general client
// error instead of an error for a specific batch call.
return nil, ErrRead{
Err: fmt.Errorf("%w: batch call context: %s", types.ErrInternal, err.Error()),
Batch: true,
return nil, Error{
Err: fmt.Errorf("%w: batch call context: %s", types.ErrInternal, err.Error()),
Type: batchReadType,
}
}

Expand All @@ -176,7 +176,7 @@ func (c *defaultEvmBatchCaller) createBatchCalls(
fmt.Errorf("%w: encode params: %s", types.ErrInvalidConfig, err.Error()),
call,
block,
true,
batchReadType,
)
}

Expand Down Expand Up @@ -217,7 +217,7 @@ func (c *defaultEvmBatchCaller) unpackBatchResults(
if rpcBatchCalls[idx].Error != nil {
results[idx].err = newErrorFromCall(
fmt.Errorf("%w: rpc call error: %w", types.ErrInternal, rpcBatchCalls[idx].Error),
call, block, true,
call, block, batchReadType,
)

continue
Expand All @@ -233,7 +233,7 @@ func (c *defaultEvmBatchCaller) unpackBatchResults(
if err != nil {
callErr := newErrorFromCall(
fmt.Errorf("%w: hex decode result: %s", types.ErrInternal, err.Error()),
call, block, true,
call, block, batchReadType,
)

callErr.Result = &hexEncodedOutputs[idx]
Expand All @@ -250,7 +250,7 @@ func (c *defaultEvmBatchCaller) unpackBatchResults(
if len(packedBytes) == 0 {
callErr := newErrorFromCall(
fmt.Errorf("%w: %w: %s", types.ErrInternal, errEmptyOutput, err.Error()),
call, block, true,
call, block, batchReadType,
)

callErr.Result = &hexEncodedOutputs[idx]
Expand All @@ -259,7 +259,7 @@ func (c *defaultEvmBatchCaller) unpackBatchResults(
} else {
callErr := newErrorFromCall(
fmt.Errorf("%w: codec decode result: %s", types.ErrInvalidType, err.Error()),
call, block, true,
call, block, batchReadType,
)

callErr.Result = &hexEncodedOutputs[idx]
Expand Down Expand Up @@ -290,9 +290,9 @@ func (c *defaultEvmBatchCaller) batchCallDynamicLimitRetries(ctx context.Context
}

if lim <= 1 {
return nil, ErrRead{
Err: fmt.Errorf("%w: limited call: call data: %+v", err, calls),
Batch: true,
return nil, Error{
Err: fmt.Errorf("%w: limited call: call data: %+v", err, calls),
Type: batchReadType,
}
}

Expand Down
28 changes: 18 additions & 10 deletions core/services/relay/evm/read/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
)

type ErrRead struct {
type readType string

const (
batchReadType readType = "BatchGetLatestValue"
singleReadType readType = "GetLatestValue"
EasterTheBunny marked this conversation as resolved.
Show resolved Hide resolved
eventReadType readType = "QueryKey"
)

type Error struct {
Err error
Batch bool
Type readType
Detail *readDetail
Result *string
}
Expand All @@ -25,10 +33,10 @@ type readDetail struct {
Block string
}

func newErrorFromCall(err error, call Call, block string, batch bool) ErrRead {
return ErrRead{
Err: err,
Batch: batch,
func newErrorFromCall(err error, call Call, block string, tp readType) Error {
return Error{
Err: err,
Type: tp,
Detail: &readDetail{
Address: call.ContractAddress.Hex(),
Contract: call.ContractName,
Expand All @@ -40,12 +48,12 @@ func newErrorFromCall(err error, call Call, block string, batch bool) ErrRead {
}
}

func (e ErrRead) Error() string {
func (e Error) Error() string {
var builder strings.Builder

builder.WriteString("[rpc error]")
builder.WriteString(fmt.Sprintf(" batch: %T;", e.Batch))
builder.WriteString("[read error]")
builder.WriteString(fmt.Sprintf(" err: %s;", e.Err.Error()))
builder.WriteString(fmt.Sprintf(" type: %s;", e.Type))

if e.Detail != nil {
builder.WriteString(fmt.Sprintf(" block: %s;", e.Detail.Block))
Expand All @@ -63,7 +71,7 @@ func (e ErrRead) Error() string {
return builder.String()
}

func (e ErrRead) Unwrap() error {
func (e Error) Unwrap() error {
return e.Err
}

Expand Down
4 changes: 2 additions & 2 deletions core/services/relay/evm/read/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (b *EventBinding) GetLatestValueWithHeadData(ctx context.Context, address c
ReadName: b.eventName,
Params: params,
ReturnVal: into,
}, strconv.Itoa(int(confs)), false)
}, strconv.Itoa(int(confs)), eventReadType)

callErr.Result = result

Expand Down Expand Up @@ -315,7 +315,7 @@ func (b *EventBinding) QueryKey(ctx context.Context, address common.Address, fil
ContractName: b.contractName,
ReadName: b.eventName,
ReturnVal: sequenceDataType,
}, "", false)
}, "", eventReadType)
}
}()

Expand Down
11 changes: 6 additions & 5 deletions core/services/relay/evm/read/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func (b *MethodBinding) Bind(ctx context.Context, bindings ...common.Address) er
// check for contract byte code at the latest block and provided address
byteCode, err := b.client.CodeAt(ctx, binding, nil)
if err != nil {
return ErrRead{
Err: fmt.Errorf("%w: code at call failure: %s", commontypes.ErrInternal, err.Error()),
return Error{
Err: fmt.Errorf("%w: code at call failure: %s", commontypes.ErrInternal, err.Error()),
Type: singleReadType,
Detail: &readDetail{
Address: binding.Hex(),
Contract: b.contractName,
Expand Down Expand Up @@ -146,7 +147,7 @@ func (b *MethodBinding) GetLatestValueWithHeadData(ctx context.Context, addr com
ReadName: b.method,
Params: params,
ReturnVal: returnVal,
}, blockNum.String(), false)
}, blockNum.String(), singleReadType)

return nil, callErr
}
Expand All @@ -167,7 +168,7 @@ func (b *MethodBinding) GetLatestValueWithHeadData(ctx context.Context, addr com
ReadName: b.method,
Params: params,
ReturnVal: returnVal,
}, blockNum.String(), false)
}, blockNum.String(), singleReadType)

return nil, callErr
}
Expand All @@ -181,7 +182,7 @@ func (b *MethodBinding) GetLatestValueWithHeadData(ctx context.Context, addr com
ReadName: b.method,
Params: params,
ReturnVal: returnVal,
}, blockNum.String(), false)
}, blockNum.String(), singleReadType)

strResult := hexutil.Encode(bytes)
callErr.Result = &strResult
Expand Down
Loading