/
store.go
274 lines (239 loc) · 7.35 KB
/
store.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
package store
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"sync"
"github.com/ethereum/go-ethereum/rpc"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/synchronization"
"github.com/smartcontractkit/chainlink/core/store/migrations"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
"github.com/smartcontractkit/chainlink/core/utils"
"github.com/tevino/abool"
"go.uber.org/multierr"
)
// Store contains fields for the database, Config, KeyStore, and TxManager
// for keeping the application state in sync with the database.
type Store struct {
*orm.ORM
Config Config
Clock utils.AfterNower
KeyStore *KeyStore
RunChannel RunChannel
TxManager TxManager
StatsPusher *synchronization.StatsPusher
}
type lazyRPCWrapper struct {
client *rpc.Client
url *url.URL
mutex *sync.Mutex
initialized *abool.AtomicBool
}
func newLazyRPCWrapper(urlString string) (CallerSubscriber, error) {
parsed, err := url.ParseRequestURI(urlString)
if err != nil {
return nil, err
}
if parsed.Scheme != "ws" && parsed.Scheme != "wss" {
return nil, fmt.Errorf("Ethereum url scheme must be websocket: %s", parsed.String())
}
return &lazyRPCWrapper{
url: parsed,
mutex: &sync.Mutex{},
initialized: abool.New(),
}, nil
}
// lazyDialInitializer initializes the Dial instance used to interact with
// an ethereum node using the Double-checked locking optimization:
// https://en.wikipedia.org/wiki/Double-checked_locking
func (wrapper *lazyRPCWrapper) lazyDialInitializer() error {
if wrapper.initialized.IsSet() {
return nil
}
wrapper.mutex.Lock()
defer wrapper.mutex.Unlock()
if wrapper.client == nil {
client, err := rpc.Dial(wrapper.url.String())
if err != nil {
return err
}
wrapper.client = client
wrapper.initialized.Set()
}
return nil
}
func (wrapper *lazyRPCWrapper) Call(result interface{}, method string, args ...interface{}) error {
err := wrapper.lazyDialInitializer()
if err != nil {
return err
}
return wrapper.client.Call(result, method, args...)
}
func (wrapper *lazyRPCWrapper) EthSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (models.EthSubscription, error) {
err := wrapper.lazyDialInitializer()
if err != nil {
return nil, err
}
return wrapper.client.EthSubscribe(ctx, channel, args...)
}
// Dialer implements Dial which is a function that creates a client for that url
type Dialer interface {
Dial(string) (CallerSubscriber, error)
}
// EthDialer is Dialer which accesses rpc urls
type EthDialer struct{}
// Dial will dial the given url and return a CallerSubscriber
func (ed *EthDialer) Dial(urlString string) (CallerSubscriber, error) {
return newLazyRPCWrapper(urlString)
}
// NewStore will create a new database file at the config's RootDir if
// it is not already present, otherwise it will use the existing db.sqlite3
// file.
func NewStore(config Config) *Store {
return NewStoreWithDialer(config, &EthDialer{})
}
// NewStoreWithDialer creates a new store with the given config and dialer
func NewStoreWithDialer(config Config, dialer Dialer) *Store {
err := os.MkdirAll(config.RootDir(), os.FileMode(0700))
if err != nil {
logger.Fatal(fmt.Sprintf("Unable to create project root dir: %+v", err))
}
orm, err := initializeORM(config)
if err != nil {
logger.Fatal(fmt.Sprintf("Unable to initialize ORM: %+v", err))
}
ethrpc, err := dialer.Dial(config.EthereumURL())
if err != nil {
logger.Fatal(fmt.Sprintf("Unable to dial ETH RPC port: %+v", err))
}
if err := orm.ClobberDiskKeyStoreWithDBKeys(config.KeysDir()); err != nil {
logger.Fatal(fmt.Sprintf("Unable to migrate key store to disk: %+v", err))
}
keyStore := NewKeyStore(config.KeysDir())
store := &Store{
Clock: utils.Clock{},
Config: config,
KeyStore: keyStore,
ORM: orm,
RunChannel: NewQueuedRunChannel(),
TxManager: NewEthTxManager(&EthClient{ethrpc}, config, keyStore, orm),
StatsPusher: synchronization.NewStatsPusher(orm, config.ExplorerURL(), config.ExplorerAccessKey(), config.ExplorerSecret()),
}
return store
}
// Start initiates all of Store's dependencies including the TxManager.
func (s *Store) Start() error {
s.TxManager.Register(s.KeyStore.Accounts())
return multierr.Combine(
s.SyncDiskKeyStoreToDB(),
s.StatsPusher.Start(),
)
}
// Close shuts down all of the working parts of the store.
func (s *Store) Close() error {
s.RunChannel.Close()
return multierr.Combine(
s.ORM.Close(),
s.StatsPusher.Close(),
)
}
// Unscoped returns a shallow copy of the store, with an unscoped ORM allowing
// one to work with soft deleted records.
func (s *Store) Unscoped() *Store {
cpy := *s
cpy.ORM = cpy.ORM.Unscoped()
return &cpy
}
// AuthorizedUserWithSession will return the one API user if the Session ID exists
// and hasn't expired, and update session's LastUsed field.
func (s *Store) AuthorizedUserWithSession(sessionID string) (models.User, error) {
return s.ORM.AuthorizedUserWithSession(sessionID, s.Config.SessionTimeout())
}
// SyncDiskKeyStoreToDB writes all keys in the keys directory to the underlying
// orm.
func (s *Store) SyncDiskKeyStoreToDB() error {
files, err := utils.FilesInDir(s.Config.KeysDir())
if err != nil {
return multierr.Append(errors.New("unable to sync disk keystore to db"), err)
}
var merr error
for _, f := range files {
key, err := models.NewKeyFromFile(filepath.Join(s.Config.KeysDir(), f))
if err != nil {
merr = multierr.Append(err, merr)
continue
}
err = s.FirstOrCreateKey(key)
if err != nil {
merr = multierr.Append(err, merr)
}
}
return merr
}
func initializeORM(config Config) (*orm.ORM, error) {
orm, err := orm.NewORM(config.NormalizedDatabaseURL(), config.DatabaseTimeout())
if err != nil {
return nil, err
}
if err = migrations.Migrate(orm.DB); err != nil {
return nil, err
}
orm.SetLogging(config.LogSQLStatements())
return orm, nil
}
// RunRequest is the type that the RunChannel uses to package all the necessary
// pieces to execute a Job Run.
type RunRequest struct {
ID string
}
// RunChannel manages and dispatches incoming runs.
type RunChannel interface {
Send(jobRunID string) error
Receive() <-chan RunRequest
Close()
}
// QueuedRunChannel manages incoming results and blocks by enqueuing them
// in a queue per run.
type QueuedRunChannel struct {
queue chan RunRequest
closed bool
mutex sync.Mutex
}
// NewQueuedRunChannel initializes a QueuedRunChannel.
func NewQueuedRunChannel() RunChannel {
return &QueuedRunChannel{
queue: make(chan RunRequest, 1000),
}
}
// Send adds another entry to the queue of runs.
func (rq *QueuedRunChannel) Send(jobRunID string) error {
rq.mutex.Lock()
defer rq.mutex.Unlock()
if rq.closed {
return errors.New("QueuedRunChannel.Add: cannot add to a closed QueuedRunChannel")
}
if jobRunID == "" {
return errors.New("QueuedRunChannel.Add: cannot add an empty jobRunID")
}
rq.queue <- RunRequest{ID: jobRunID}
return nil
}
// Receive returns a channel for listening to sent runs.
func (rq *QueuedRunChannel) Receive() <-chan RunRequest {
return rq.queue
}
// Close closes the QueuedRunChannel so that no runs can be added to it without
// throwing an error.
func (rq *QueuedRunChannel) Close() {
rq.mutex.Lock()
defer rq.mutex.Unlock()
if !rq.closed {
rq.closed = true
close(rq.queue)
}
}