Skip to content

Commit

Permalink
convert batch boolean to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Nov 14, 2024
1 parent 1fa4974 commit 36bbebc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 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 ReadError 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, ReadError{
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, ReadError{
Err: fmt.Errorf("%w: limited call: call data: %+v", err, calls),
Type: batchReadType,
}
}

Expand Down
29 changes: 17 additions & 12 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 ReadError 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) ReadError {
return ReadError{
Err: err,
Type: tp,
Detail: &readDetail{
Address: call.ContractAddress.Hex(),
Contract: call.ContractName,
Expand All @@ -40,15 +48,12 @@ func newErrorFromCall(err error, call Call, block string, batch bool) ErrRead {
}
}

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

builder.WriteString("[read error]")
builder.WriteString(fmt.Sprintf(" err: %s;", e.Err.Error()))

if e.Batch {
builder.WriteString(" batch;")
}
builder.WriteString(fmt.Sprintf(" type: %s;", e.Type))

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

func (e ErrRead) Unwrap() error {
func (e ReadError) 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) GetLatestValue(ctx context.Context, address common.Addres
ReadName: b.eventName,
Params: params,
ReturnVal: into,
}, strconv.Itoa(int(confs)), false)
}, strconv.Itoa(int(confs)), eventReadType)

callErr.Result = result

Expand Down Expand Up @@ -311,7 +311,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 ReadError{
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 @@ -141,7 +142,7 @@ func (b *MethodBinding) GetLatestValue(ctx context.Context, addr common.Address,
ReadName: b.method,
Params: params,
ReturnVal: returnVal,
}, block.String(), false)
}, block.String(), singleReadType)

return callErr
}
Expand All @@ -162,7 +163,7 @@ func (b *MethodBinding) GetLatestValue(ctx context.Context, addr common.Address,
ReadName: b.method,
Params: params,
ReturnVal: returnVal,
}, block.String(), false)
}, block.String(), singleReadType)

return callErr
}
Expand All @@ -176,7 +177,7 @@ func (b *MethodBinding) GetLatestValue(ctx context.Context, addr common.Address,
ReadName: b.method,
Params: params,
ReturnVal: returnVal,
}, block.String(), false)
}, block.String(), singleReadType)

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

0 comments on commit 36bbebc

Please sign in to comment.