-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
transmitchecker.go
288 lines (261 loc) · 8.99 KB
/
transmitchecker.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package txmgr
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common/hexutil"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
evmclient "github.com/smartcontractkit/chainlink/core/chains/evm/client"
v1 "github.com/smartcontractkit/chainlink/core/internal/gethwrappers/generated/solidity_vrf_coordinator_interface"
v2 "github.com/smartcontractkit/chainlink/core/internal/gethwrappers/generated/vrf_coordinator_v2"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/utils"
bigmath "github.com/smartcontractkit/chainlink/core/utils/big_math"
)
var (
// NoChecker is a TransmitChecker that always determines a transaction should be submitted.
NoChecker TransmitChecker = noChecker{}
_ TransmitCheckerFactory = &CheckerFactory{}
_ TransmitChecker = &SimulateChecker{}
_ TransmitChecker = &VRFV1Checker{}
_ TransmitChecker = &VRFV2Checker{}
)
// CheckerFactory is a real implementation of TransmitCheckerFactory.
type CheckerFactory struct {
Client evmclient.Client
}
// BuildChecker satisfies the TransmitCheckerFactory interface.
func (c *CheckerFactory) BuildChecker(spec TransmitCheckerSpec) (TransmitChecker, error) {
switch spec.CheckerType {
case TransmitCheckerTypeSimulate:
return &SimulateChecker{c.Client}, nil
case TransmitCheckerTypeVRFV1:
if spec.VRFCoordinatorAddress == nil {
return nil, errors.Errorf("malformed checker, expected non-nil VRFCoordinatorAddress, got: %v", spec)
}
coord, err := v1.NewVRFCoordinator(*spec.VRFCoordinatorAddress, c.Client)
if err != nil {
return nil, errors.Wrapf(err,
"failed to create VRF V1 coordinator at address %v", spec.VRFCoordinatorAddress)
}
return &VRFV1Checker{coord.Callbacks}, nil
case TransmitCheckerTypeVRFV2:
if spec.VRFCoordinatorAddress == nil {
return nil, errors.Errorf("malformed checker, expected non-nil VRFCoordinatorAddress, got: %v", spec)
}
coord, err := v2.NewVRFCoordinatorV2(*spec.VRFCoordinatorAddress, c.Client)
if err != nil {
return nil, errors.Wrapf(err,
"failed to create VRF V2 coordinator at address %v", spec.VRFCoordinatorAddress)
}
if spec.VRFRequestBlockNumber == nil {
return nil, errors.New("VRFRequestBlockNumber parameter must be non-nil")
}
return &VRFV2Checker{
GetCommitment: coord.GetCommitment,
HeaderByNumber: c.Client.HeaderByNumber,
RequestBlockNumber: spec.VRFRequestBlockNumber,
}, nil
case "":
return NoChecker, nil
default:
return nil, errors.Errorf("unrecognized checker type: %s", spec.CheckerType)
}
}
type noChecker struct{}
// Check satisfies the TransmitChecker interface.
func (noChecker) Check(
_ context.Context,
_ logger.Logger,
_ EthTx,
_ EthTxAttempt,
) error {
return nil
}
// SimulateChecker simulates transactions, producing an error if they revert on chain.
type SimulateChecker struct {
Client evmclient.Client
}
// Check satisfies the TransmitChecker interface.
func (s *SimulateChecker) Check(
ctx context.Context,
l logger.Logger,
tx EthTx,
a EthTxAttempt,
) error {
// See: https://github.com/ethereum/go-ethereum/blob/acdf9238fb03d79c9b1c20c2fa476a7e6f4ac2ac/ethclient/gethclient/gethclient.go#L193
callArg := map[string]interface{}{
"from": tx.FromAddress,
"to": &tx.ToAddress,
"gas": hexutil.Uint64(a.ChainSpecificGasLimit),
// NOTE: Deliberately do not include gas prices. We never want to fatally error a
// transaction just because the wallet has insufficient eth.
// Relevant info regarding EIP1559 transactions: https://github.com/ethereum/go-ethereum/pull/23027
"gasPrice": nil,
"maxFeePerGas": nil,
"maxPriorityFeePerGas": nil,
"value": (*hexutil.Big)(tx.Value.ToInt()),
"data": hexutil.Bytes(tx.EncodedPayload),
}
var b hexutil.Bytes
// always run simulation on "latest" block
err := s.Client.CallContext(ctx, &b, "eth_call", callArg, evmclient.ToBlockNumArg(nil))
if err != nil {
if jErr := evmclient.ExtractRPCError(err); jErr != nil {
l.Criticalw("Transaction reverted during simulation",
"ethTxAttemptID", a.ID, "txHash", a.Hash, "err", err, "rpcErr", jErr.String(), "returnValue", b.String())
return errors.Errorf("transaction reverted during simulation: %s", jErr.String())
}
l.Warnw("Transaction simulation failed, will attempt to send anyway",
"ethTxAttemptID", a.ID, "txHash", a.Hash, "err", err, "returnValue", b.String())
} else {
l.Debugw("Transaction simulation succeeded",
"ethTxAttemptID", a.ID, "txHash", a.Hash, "returnValue", b.String())
}
return nil
}
// VRFV1Checker is an implementation of TransmitChecker that checks whether a VRF V1 fulfillment
// has already been fulfilled.
type VRFV1Checker struct {
// Callbacks checks whether a VRF V1 request has already been fulfilled on the VRFCoordinator
// Solidity contract
Callbacks func(opts *bind.CallOpts, reqID [32]byte) (v1.Callbacks, error)
}
// Check satisfies the TransmitChecker interface.
func (v *VRFV1Checker) Check(
ctx context.Context,
l logger.Logger,
tx EthTx,
_ EthTxAttempt,
) error {
meta, err := tx.GetMeta()
if err != nil {
l.Errorw("Failed to parse transaction meta. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta)
return nil
}
if meta == nil {
l.Errorw("Expected a non-nil meta for a VRF transaction. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta)
return nil
}
if len(meta.RequestID.Bytes()) != 32 {
l.Errorw("Unexpected request ID. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta)
return nil
}
var reqID [32]byte
copy(reqID[:], meta.RequestID.Bytes())
callback, err := v.Callbacks(&bind.CallOpts{Context: ctx}, reqID)
if err != nil {
l.Errorw("Unable to check if already fulfilled. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta,
"reqID", reqID)
return nil
} else if utils.IsEmpty(callback.SeedAndBlockNum[:]) {
// Request already fulfilled
l.Infow("Request already fulfilled",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta,
"reqID", reqID)
return errors.New("request already fulfilled")
} else {
// Request not fulfilled
return nil
}
}
// VRFV2Checker is an implementation of TransmitChecker that checks whether a VRF V2 fulfillment
// has already been fulfilled.
type VRFV2Checker struct {
// GetCommitment checks whether a VRF V2 request has been fulfilled on the VRFCoordinatorV2
// Solidity contract.
GetCommitment func(opts *bind.CallOpts, requestID *big.Int) ([32]byte, error)
// HeaderByNumber fetches the header given the number. If nil is provided,
// the latest header is fetched.
HeaderByNumber func(ctx context.Context, n *big.Int) (*gethtypes.Header, error)
// RequestBlockNumber is the block number of the VRFV2 request.
RequestBlockNumber *big.Int
}
// Check satisfies the TransmitChecker interface.
func (v *VRFV2Checker) Check(
ctx context.Context,
l logger.Logger,
tx EthTx,
_ EthTxAttempt,
) error {
meta, err := tx.GetMeta()
if err != nil {
l.Errorw("Failed to parse transaction meta. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta)
return nil
}
if meta == nil {
l.Errorw("Expected a non-nil meta for a VRF transaction. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta)
return nil
}
h, err := v.HeaderByNumber(ctx, nil)
if err != nil {
l.Errorw("Failed to fetch latest header. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta,
)
return nil
}
// If the request block number is not provided, transmit anyway just to be safe.
// Worst we can do is revert due to the request already being fulfilled.
if v.RequestBlockNumber == nil {
l.Errorw("Was provided with a nil request block number. Attempting to transmit anyway.",
"ethTxID", tx.ID,
"meta", tx.Meta,
)
return nil
}
vrfRequestID := meta.RequestID.Big()
// Subtract 5 since the newest block likely isn't indexed yet and will cause "header not found"
// errors.
latest := new(big.Int).Sub(h.Number, big.NewInt(5))
blockNumber := bigmath.Max(latest, v.RequestBlockNumber)
callback, err := v.GetCommitment(&bind.CallOpts{
Context: ctx,
BlockNumber: blockNumber,
}, vrfRequestID)
if err != nil {
l.Errorw("Failed to check request fulfillment status, error calling GetCommitment. Attempting to transmit anyway.",
"err", err,
"ethTxID", tx.ID,
"meta", tx.Meta,
"vrfRequestId", vrfRequestID,
"blockNumber", h.Number,
)
return nil
} else if utils.IsEmpty(callback[:]) {
// If seedAndBlockNumber is zero then the response has been fulfilled and we should skip it.
l.Infow("Request already fulfilled.",
"ethTxID", tx.ID,
"meta", tx.Meta,
"vrfRequestId", vrfRequestID)
return errors.New("request already fulfilled")
} else {
l.Debugw("Request not yet fulfilled",
"ethTxID", tx.ID,
"meta", tx.Meta,
"vrfRequestId", vrfRequestID)
return nil
}
}