/
backend.go
213 lines (179 loc) · 6.86 KB
/
backend.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package backendmock
import (
"context"
"errors"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethsana/sana/pkg/transaction"
)
type backendMock struct {
codeAt func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
sendTransaction func(ctx context.Context, tx *types.Transaction) error
suggestGasPrice func(ctx context.Context) (*big.Int, error)
estimateGas func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
transactionReceipt func(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
pendingNonceAt func(ctx context.Context, account common.Address) (uint64, error)
transactionByHash func(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error)
blockNumber func(ctx context.Context) (uint64, error)
blockByNumber func(ctx context.Context, number *big.Int) (*types.Block, error)
headerByNumber func(ctx context.Context, number *big.Int) (*types.Header, error)
balanceAt func(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)
nonceAt func(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
}
func (m *backendMock) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
if m.codeAt != nil {
return m.codeAt(ctx, contract, blockNumber)
}
return nil, errors.New("not implemented")
}
func (*backendMock) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
return nil, errors.New("not implemented")
}
func (*backendMock) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
return nil, errors.New("not implemented")
}
func (m *backendMock) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
if m.pendingNonceAt != nil {
return m.pendingNonceAt(ctx, account)
}
return 0, errors.New("not implemented")
}
func (m *backendMock) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
if m.suggestGasPrice != nil {
return m.suggestGasPrice(ctx)
}
return nil, errors.New("not implemented")
}
func (m *backendMock) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) {
if m.estimateGas != nil {
return m.estimateGas(ctx, call)
}
return 0, errors.New("not implemented")
}
func (m *backendMock) SendTransaction(ctx context.Context, tx *types.Transaction) error {
if m.sendTransaction != nil {
return m.sendTransaction(ctx, tx)
}
return errors.New("not implemented")
}
func (*backendMock) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) {
return nil, errors.New("not implemented")
}
func (*backendMock) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
return nil, errors.New("not implemented")
}
func (m *backendMock) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
if m.transactionReceipt != nil {
return m.transactionReceipt(ctx, txHash)
}
return nil, errors.New("not implemented")
}
func (m *backendMock) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
if m.transactionByHash != nil {
return m.transactionByHash(ctx, hash)
}
return nil, false, errors.New("not implemented")
}
func (m *backendMock) BlockNumber(ctx context.Context) (uint64, error) {
if m.blockNumber != nil {
return m.blockNumber(ctx)
}
return 0, errors.New("not implemented")
}
func (m *backendMock) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
if m.blockNumber != nil {
return m.blockByNumber(ctx, number)
}
return nil, errors.New("not implemented")
}
func (m *backendMock) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
if m.headerByNumber != nil {
return m.headerByNumber(ctx, number)
}
return nil, errors.New("not implemented")
}
func (m *backendMock) BalanceAt(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) {
if m.balanceAt != nil {
return m.balanceAt(ctx, address, block)
}
return nil, errors.New("not implemented")
}
func (m *backendMock) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
if m.nonceAt != nil {
return m.nonceAt(ctx, account, blockNumber)
}
return 0, errors.New("not implemented")
}
func New(opts ...Option) transaction.Backend {
mock := new(backendMock)
for _, o := range opts {
o.apply(mock)
}
return mock
}
// Option is the option passed to the mock Chequebook service
type Option interface {
apply(*backendMock)
}
type optionFunc func(*backendMock)
func (f optionFunc) apply(r *backendMock) { f(r) }
func WithCodeAtFunc(f func(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)) Option {
return optionFunc(func(s *backendMock) {
s.codeAt = f
})
}
func WithPendingNonceAtFunc(f func(ctx context.Context, account common.Address) (uint64, error)) Option {
return optionFunc(func(s *backendMock) {
s.pendingNonceAt = f
})
}
func WithSuggestGasPriceFunc(f func(ctx context.Context) (*big.Int, error)) Option {
return optionFunc(func(s *backendMock) {
s.suggestGasPrice = f
})
}
func WithEstimateGasFunc(f func(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)) Option {
return optionFunc(func(s *backendMock) {
s.estimateGas = f
})
}
func WithTransactionReceiptFunc(f func(ctx context.Context, txHash common.Hash) (*types.Receipt, error)) Option {
return optionFunc(func(s *backendMock) {
s.transactionReceipt = f
})
}
func WithTransactionByHashFunc(f func(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)) Option {
return optionFunc(func(s *backendMock) {
s.transactionByHash = f
})
}
func WithBlockByNumberFunc(f func(ctx context.Context, number *big.Int) (*types.Block, error)) Option {
return optionFunc(func(s *backendMock) {
s.blockByNumber = f
})
}
func WithSendTransactionFunc(f func(ctx context.Context, tx *types.Transaction) error) Option {
return optionFunc(func(s *backendMock) {
s.sendTransaction = f
})
}
func WithBlockNumberFunc(f func(context.Context) (uint64, error)) Option {
return optionFunc(func(s *backendMock) {
s.blockNumber = f
})
}
func WithHeaderbyNumberFunc(f func(ctx context.Context, number *big.Int) (*types.Header, error)) Option {
return optionFunc(func(s *backendMock) {
s.headerByNumber = f
})
}
func WithNonceAtFunc(f func(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)) Option {
return optionFunc(func(s *backendMock) {
s.nonceAt = f
})
}