Skip to content

Commit

Permalink
Correct Read Error Format (#15223)
Browse files Browse the repository at this point in the history
* Correct Read Error Format

Changed error text to be `read error` instead of `rpc error` to limit confusion.
Batch flag to string corrected to show `true|false` instead of bool type.

* only write batch for batch calls

* convert batch boolean to enum

* fixed linting by changing to
  • Loading branch information
EasterTheBunny authored Nov 19, 2024
1 parent 6c4f1b9 commit 7a5d460
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
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"
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

0 comments on commit 7a5d460

Please sign in to comment.