1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package testutils
import (
"context"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum-optimism/optimism/op-node/client"
)
// RPCErrFaker implements an RPC by wrapping one, but returns an error when prepared with one, to test RPC error handling.
type RPCErrFaker struct {
// RPC to call when no ErrFn is set, or the ErrFn does not return an error
RPC client.RPC
// ErrFn returns an error when the RPC needs to return error upon a call, batch call or subscription.
// The RPC operates without fake errors if the ErrFn is nil, or returns nil.
ErrFn func() error
}
func (r RPCErrFaker) Close() {
r.RPC.Close()
}
func (r RPCErrFaker) CallContext(ctx context.Context, result any, method string, args ...any) error {
if r.ErrFn != nil {
if err := r.ErrFn(); err != nil {
return err
}
}
return r.RPC.CallContext(ctx, result, method, args...)
}
func (r RPCErrFaker) BatchCallContext(ctx context.Context, b []rpc.BatchElem) error {
if r.ErrFn != nil {
if err := r.ErrFn(); err != nil {
return err
}
}
return r.RPC.BatchCallContext(ctx, b)
}
func (r RPCErrFaker) EthSubscribe(ctx context.Context, channel any, args ...any) (ethereum.Subscription, error) {
if r.ErrFn != nil {
if err := r.ErrFn(); err != nil {
return nil, err
}
}
return r.RPC.EthSubscribe(ctx, channel, args...)
}
var _ client.RPC = (*RPCErrFaker)(nil)