-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
chain.go
320 lines (267 loc) · 8.88 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
package solana
import (
"context"
"math"
"math/rand"
"strings"
"sync"
"time"
solanago "github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/pkg/errors"
"go.uber.org/multierr"
"github.com/smartcontractkit/chainlink-solana/pkg/solana"
solanaclient "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/sqlx"
"github.com/smartcontractkit/chainlink/core/chains/solana/monitor"
"github.com/smartcontractkit/chainlink/core/chains/solana/soltxm"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services"
"github.com/smartcontractkit/chainlink/core/services/keystore"
"github.com/smartcontractkit/chainlink/core/services/pg"
"github.com/smartcontractkit/chainlink/core/utils"
)
// DefaultRequestTimeout is the default Solana client timeout.
const DefaultRequestTimeout = 30 * time.Second
//go:generate mockery --name TxManager --srcpkg github.com/smartcontractkit/chainlink-solana/pkg/solana --output ./mocks/ --case=underscore
//go:generate mockery --name Reader --srcpkg github.com/smartcontractkit/chainlink-solana/pkg/solana/client --output ./mocks/ --case=underscore
//go:generate mockery --name Chain --srcpkg github.com/smartcontractkit/chainlink-solana/pkg/solana --output ./mocks/ --case=underscore
var _ solana.Chain = (*chain)(nil)
type chain struct {
utils.StartStopOnce
id string
cfg config.Config
txm *soltxm.Txm
balanceMonitor services.ServiceCtx
orm ORM
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
lggr logger.Logger
chainIDVerified bool
chainIDVerifiedLock sync.RWMutex
solanaclient.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)
}
// NewChain returns a new chain backed by node.
func NewChain(db *sqlx.DB, ks keystore.Solana, logCfg pg.LogConfig, eb pg.EventBroadcaster, dbchain DBChain, orm ORM, lggr logger.Logger) (*chain, error) {
cfg := config.NewConfig(*dbchain.Cfg, lggr)
lggr = lggr.With("chainID", dbchain.ID, "chainSet", "solana")
var ch = chain{
id: dbchain.ID,
cfg: cfg,
orm: orm,
lggr: lggr.Named("Chain"),
clientCache: map[string]*verifiedCachedClient{},
}
tc := func() (solanaclient.ReaderWriter, error) {
return ch.getClient()
}
ch.txm = soltxm.NewTxm(ch.id, tc, cfg, ks, lggr)
ch.balanceMonitor = monitor.NewBalanceMonitor(ch.id, cfg, lggr, ks, ch.Reader)
return &ch, nil
}
func (c *chain) ID() string {
return c.id
}
func (c *chain) Config() config.Config {
return c.cfg
}
func (c *chain) UpdateConfig(cfg *db.ChainCfg) {
c.cfg.Update(*cfg)
}
func (c *chain) TxManager() solana.TxManager {
return c.txm
}
func (c *chain) Reader() (solanaclient.Reader, error) {
return c.getClient()
}
// getClient returns a client, randomly selecting one from available and valid nodes
func (c *chain) getClient() (solanaclient.ReaderWriter, error) {
var node db.Node
var client solanaclient.ReaderWriter
nodes, cnt, err := c.orm.NodesForChain(c.id, 0, math.MaxInt)
if err != nil {
return nil, errors.Wrap(err, "failed to get nodes")
}
if cnt == 0 {
return nil, errors.New("no nodes available")
}
rand.Seed(time.Now().Unix()) // seed randomness otherwise it will return the same each time
// #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, "error", 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) (solanaclient.ReaderWriter, error) {
url := node.SolanaURL
var err error
// check if cached client exists
c.clientLock.RLock()
client, exists := c.clientCache[url]
c.clientLock.RUnlock()
if !exists {
client = &verifiedCachedClient{
nodeURL: url,
expectedChainID: c.id,
}
// create client
client.ReaderWriter, err = solanaclient.NewClient(url, c.cfg, DefaultRequestTimeout, c.lggr.Named("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] = client
} else {
client = cached
}
c.clientLock.Unlock()
}
return client, 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")
return multierr.Combine(
c.txm.Start(ctx),
c.balanceMonitor.Start(ctx))
})
}
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 multierr.Combine(c.txm.Close(),
c.balanceMonitor.Close())
})
}
func (c *chain) Ready() error {
return multierr.Combine(
c.StartStopOnce.Ready(),
c.txm.Ready(),
)
}
func (c *chain) Healthy() error {
return multierr.Combine(
c.StartStopOnce.Healthy(),
c.txm.Healthy(),
)
}