ethclient: ensure returned subscription is nil on error (#26976)

This commit is contained in:
norwnd 2023-03-26 00:38:43 +03:00 committed by GitHub
parent 792d893ed0
commit df383addee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -320,7 +320,14 @@ func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, err
// SubscribeNewHead subscribes to notifications about the current blockchain head // SubscribeNewHead subscribes to notifications about the current blockchain head
// on the given channel. // on the given channel.
func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) { func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
return ec.c.EthSubscribe(ctx, ch, "newHeads") sub, err := ec.c.EthSubscribe(ctx, ch, "newHeads")
if err != nil {
// Defensively prefer returning nil interface explicitly on error-path, instead
// of letting default golang behavior wrap it with non-nil interface that stores
// nil concrete type value.
return nil, err
}
return sub, nil
} }
// State Access // State Access
@ -389,7 +396,14 @@ func (ec *Client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuer
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ec.c.EthSubscribe(ctx, ch, "logs", arg) sub, err := ec.c.EthSubscribe(ctx, ch, "logs", arg)
if err != nil {
// Defensively prefer returning nil interface explicitly on error-path, instead
// of letting default golang behavior wrap it with non-nil interface that stores
// nil concrete type value.
return nil, err
}
return sub, nil
} }
func toFilterArg(q ethereum.FilterQuery) (interface{}, error) { func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {