-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
chain.go
461 lines (391 loc) · 12.1 KB
/
chain.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package solana
import (
"context"
"fmt"
"math/big"
"math/rand"
"strings"
"sync"
"time"
solanago "github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/system"
"github.com/gagliardetto/solana-go/rpc"
"github.com/pkg/errors"
"go.uber.org/multierr"
"golang.org/x/exp/maps"
"github.com/smartcontractkit/chainlink-relay/pkg/logger"
"github.com/smartcontractkit/chainlink-relay/pkg/loop"
relaytypes "github.com/smartcontractkit/chainlink-relay/pkg/types"
"github.com/smartcontractkit/chainlink-solana/pkg/solana"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/config"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/db"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/txm"
"github.com/smartcontractkit/chainlink/v2/core/chains"
"github.com/smartcontractkit/chainlink/v2/core/chains/internal"
"github.com/smartcontractkit/chainlink/v2/core/chains/solana/monitor"
"github.com/smartcontractkit/chainlink/v2/core/services"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
// DefaultRequestTimeout is the default Solana client timeout.
const DefaultRequestTimeout = 30 * time.Second
// ChainOpts holds options for configuring a Chain.
type ChainOpts struct {
Logger logger.Logger
KeyStore loop.Keystore
}
func (o *ChainOpts) Validate() (err error) {
required := func(s string) error {
return errors.Errorf("%s is required", s)
}
if o.Logger == nil {
err = multierr.Append(err, required("Logger"))
}
if o.KeyStore == nil {
err = multierr.Append(err, required("KeyStore"))
}
return
}
func (o *ChainOpts) GetLogger() logger.Logger {
return o.Logger
}
func NewChain(cfg *SolanaConfig, opts ChainOpts) (solana.Chain, error) {
if !cfg.IsEnabled() {
return nil, fmt.Errorf("cannot create new chain with ID %s: %w", *cfg.ChainID, chains.ErrChainDisabled)
}
c, err := newChain(*cfg.ChainID, cfg, opts.KeyStore, opts.Logger)
if err != nil {
return nil, err
}
return c, nil
}
var _ solana.Chain = (*chain)(nil)
type chain struct {
utils.StartStopOnce
id string
cfg *SolanaConfig
txm *txm.Txm
balanceMonitor services.ServiceCtx
lggr logger.Logger
// tracking node chain id for verification
clientCache map[string]*verifiedCachedClient // map URL -> {client, chainId} [mainnet/testnet/devnet/localnet]
clientLock sync.RWMutex
}
type verifiedCachedClient struct {
chainID string
expectedChainID string
nodeURL string
chainIDVerified bool
chainIDVerifiedLock sync.RWMutex
client.ReaderWriter
}
func (v *verifiedCachedClient) verifyChainID() (bool, error) {
v.chainIDVerifiedLock.RLock()
if v.chainIDVerified {
v.chainIDVerifiedLock.RUnlock()
return true, nil
}
v.chainIDVerifiedLock.RUnlock()
var err error
v.chainIDVerifiedLock.Lock()
defer v.chainIDVerifiedLock.Unlock()
v.chainID, err = v.ReaderWriter.ChainID()
if err != nil {
v.chainIDVerified = false
return v.chainIDVerified, errors.Wrap(err, "failed to fetch ChainID in verifiedCachedClient")
}
// check chainID matches expected chainID
expectedChainID := strings.ToLower(v.expectedChainID)
if v.chainID != expectedChainID {
v.chainIDVerified = false
return v.chainIDVerified, errors.Errorf("client returned mismatched chain id (expected: %s, got: %s): %s", expectedChainID, v.chainID, v.nodeURL)
}
v.chainIDVerified = true
return v.chainIDVerified, nil
}
func (v *verifiedCachedClient) SendTx(ctx context.Context, tx *solanago.Transaction) (solanago.Signature, error) {
verified, err := v.verifyChainID()
if !verified {
return [64]byte{}, err
}
return v.ReaderWriter.SendTx(ctx, tx)
}
func (v *verifiedCachedClient) SimulateTx(ctx context.Context, tx *solanago.Transaction, opts *rpc.SimulateTransactionOpts) (*rpc.SimulateTransactionResult, error) {
verified, err := v.verifyChainID()
if !verified {
return nil, err
}
return v.ReaderWriter.SimulateTx(ctx, tx, opts)
}
func (v *verifiedCachedClient) SignatureStatuses(ctx context.Context, sigs []solanago.Signature) ([]*rpc.SignatureStatusesResult, error) {
verified, err := v.verifyChainID()
if !verified {
return nil, err
}
return v.ReaderWriter.SignatureStatuses(ctx, sigs)
}
func (v *verifiedCachedClient) Balance(addr solanago.PublicKey) (uint64, error) {
verified, err := v.verifyChainID()
if !verified {
return 0, err
}
return v.ReaderWriter.Balance(addr)
}
func (v *verifiedCachedClient) SlotHeight() (uint64, error) {
verified, err := v.verifyChainID()
if !verified {
return 0, err
}
return v.ReaderWriter.SlotHeight()
}
func (v *verifiedCachedClient) LatestBlockhash() (*rpc.GetLatestBlockhashResult, error) {
verified, err := v.verifyChainID()
if !verified {
return nil, err
}
return v.ReaderWriter.LatestBlockhash()
}
func (v *verifiedCachedClient) ChainID() (string, error) {
verified, err := v.verifyChainID()
if !verified {
return "", err
}
return v.chainID, nil
}
func (v *verifiedCachedClient) GetFeeForMessage(msg string) (uint64, error) {
verified, err := v.verifyChainID()
if !verified {
return 0, err
}
return v.ReaderWriter.GetFeeForMessage(msg)
}
func (v *verifiedCachedClient) GetAccountInfoWithOpts(ctx context.Context, addr solanago.PublicKey, opts *rpc.GetAccountInfoOpts) (*rpc.GetAccountInfoResult, error) {
verified, err := v.verifyChainID()
if !verified {
return nil, err
}
return v.ReaderWriter.GetAccountInfoWithOpts(ctx, addr, opts)
}
func newChain(id string, cfg *SolanaConfig, ks loop.Keystore, lggr logger.Logger) (*chain, error) {
lggr = logger.With(lggr, "chainID", id, "chain", "solana")
var ch = chain{
id: id,
cfg: cfg,
lggr: logger.Named(lggr, "Chain"),
clientCache: map[string]*verifiedCachedClient{},
}
tc := func() (client.ReaderWriter, error) {
return ch.getClient()
}
ch.txm = txm.NewTxm(ch.id, tc, cfg, ks, lggr)
ch.balanceMonitor = monitor.NewBalanceMonitor(ch.id, cfg, lggr, ks, ch.Reader)
return &ch, nil
}
// ChainService interface
func (c *chain) GetChainStatus(ctx context.Context) (relaytypes.ChainStatus, error) {
toml, err := c.cfg.TOMLString()
if err != nil {
return relaytypes.ChainStatus{}, err
}
return relaytypes.ChainStatus{
ID: c.id,
Enabled: c.cfg.IsEnabled(),
Config: toml,
}, nil
}
func (c *chain) ListNodeStatuses(ctx context.Context, pageSize int32, pageToken string) (stats []relaytypes.NodeStatus, nextPageToken string, total int, err error) {
return internal.ListNodeStatuses(int(pageSize), pageToken, c.listNodeStatuses)
}
func (c *chain) Transact(ctx context.Context, from, to string, amount *big.Int, balanceCheck bool) error {
return c.sendTx(ctx, from, to, amount, balanceCheck)
}
func (c *chain) listNodeStatuses(start, end int) ([]relaytypes.NodeStatus, int, error) {
stats := make([]relaytypes.NodeStatus, 0)
total := len(c.cfg.Nodes)
if start >= total {
return stats, total, internal.ErrOutOfRange
}
if end > total {
end = total
}
nodes := c.cfg.Nodes[start:end]
for _, node := range nodes {
stat, err := nodeStatus(node, c.ChainID())
if err != nil {
return stats, total, err
}
stats = append(stats, stat)
}
return stats, total, nil
}
func (c *chain) Name() string {
return c.lggr.Name()
}
func (c *chain) ID() string {
return c.id
}
func (c *chain) Config() config.Config {
return c.cfg
}
func (c *chain) TxManager() solana.TxManager {
return c.txm
}
func (c *chain) Reader() (client.Reader, error) {
return c.getClient()
}
func (c *chain) ChainID() relay.ChainID {
return relay.ChainID(c.id)
}
// getClient returns a client, randomly selecting one from available and valid nodes
func (c *chain) getClient() (client.ReaderWriter, error) {
var node db.Node
var client client.ReaderWriter
nodes, err := c.cfg.ListNodes()
if err != nil {
return nil, errors.Wrap(err, "failed to get nodes")
}
if len(nodes) == 0 {
return nil, errors.New("no nodes available")
}
// #nosec
index := rand.Perm(len(nodes)) // list of node indexes to try
for _, i := range index {
node = nodes[i]
// create client and check
client, err = c.verifiedClient(node)
// if error, try another node
if err != nil {
c.lggr.Warnw("failed to create node", "name", node.Name, "solana-url", node.SolanaURL, "err", err.Error())
continue
}
// if all checks passed, mark found and break loop
break
}
// if no valid node found, exit with error
if client == nil {
return nil, errors.New("no node valid nodes available")
}
c.lggr.Debugw("Created client", "name", node.Name, "solana-url", node.SolanaURL)
return client, nil
}
// verifiedClient returns a client for node or an error if fails to create the client.
// The client will still be returned if the nodes are not valid, or the chain id doesn't match.
// Further client calls will try and verify the client, and fail if the client is still not valid.
func (c *chain) verifiedClient(node db.Node) (client.ReaderWriter, error) {
url := node.SolanaURL
var err error
// check if cached client exists
c.clientLock.RLock()
cl, exists := c.clientCache[url]
c.clientLock.RUnlock()
if !exists {
cl = &verifiedCachedClient{
nodeURL: url,
expectedChainID: c.id,
}
// create client
cl.ReaderWriter, err = client.NewClient(url, c.cfg, DefaultRequestTimeout, logger.Named(c.lggr, "Client."+node.Name))
if err != nil {
return nil, errors.Wrap(err, "failed to create client")
}
c.clientLock.Lock()
// recheck when writing to prevent parallel writes (discard duplicate if exists)
if cached, exists := c.clientCache[url]; !exists {
c.clientCache[url] = cl
} else {
cl = cached
}
c.clientLock.Unlock()
}
return cl, nil
}
func (c *chain) Start(ctx context.Context) error {
return c.StartOnce("Chain", func() error {
c.lggr.Debug("Starting")
c.lggr.Debug("Starting txm")
c.lggr.Debug("Starting balance monitor")
var ms services.MultiStart
return ms.Start(ctx, c.txm, c.balanceMonitor)
})
}
func (c *chain) Close() error {
return c.StopOnce("Chain", func() error {
c.lggr.Debug("Stopping")
c.lggr.Debug("Stopping txm")
c.lggr.Debug("Stopping balance monitor")
return services.CloseAll(c.txm, c.balanceMonitor)
})
}
func (c *chain) Ready() error {
return multierr.Combine(
c.StartStopOnce.Ready(),
c.txm.Ready(),
)
}
func (c *chain) HealthReport() map[string]error {
report := map[string]error{c.Name(): c.StartStopOnce.Healthy()}
maps.Copy(report, c.txm.HealthReport())
return report
}
func (c *chain) sendTx(ctx context.Context, from, to string, amount *big.Int, balanceCheck bool) error {
reader, err := c.Reader()
if err != nil {
return fmt.Errorf("chain unreachable: %w", err)
}
fromKey, err := solanago.PublicKeyFromBase58(from)
if err != nil {
return fmt.Errorf("failed to parse from key: %w", err)
}
toKey, err := solanago.PublicKeyFromBase58(to)
if err != nil {
return fmt.Errorf("failed to parse to key: %w", err)
}
if !amount.IsUint64() {
return fmt.Errorf("amount %s overflows uint64", amount)
}
amountI := amount.Uint64()
blockhash, err := reader.LatestBlockhash()
if err != nil {
return fmt.Errorf("failed to get latest block hash: %w", err)
}
tx, err := solanago.NewTransaction(
[]solanago.Instruction{
system.NewTransferInstruction(
amountI,
fromKey,
toKey,
).Build(),
},
blockhash.Value.Blockhash,
solanago.TransactionPayer(fromKey),
)
if err != nil {
return fmt.Errorf("failed to create tx: %w", err)
}
if balanceCheck {
if err = solanaValidateBalance(reader, fromKey, amountI, tx.Message.ToBase64()); err != nil {
return fmt.Errorf("failed to validate balance: %w", err)
}
}
txm := c.TxManager()
err = txm.Enqueue("", tx)
if err != nil {
return fmt.Errorf("transaction failed: %w", err)
}
return nil
}
func solanaValidateBalance(reader client.Reader, from solanago.PublicKey, amount uint64, msg string) error {
balance, err := reader.Balance(from)
if err != nil {
return err
}
fee, err := reader.GetFeeForMessage(msg)
if err != nil {
return err
}
if balance < (amount + fee) {
return fmt.Errorf("balance %d is too low for this transaction to be executed: amount %d + fee %d", balance, amount, fee)
}
return nil
}