/
db.go
678 lines (609 loc) · 18.4 KB
/
db.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// Generated by github.com/kr/genbolt. DO NOT EDIT.
// See schema/schema.go.
package db
import bytes "bytes"
import encoding "encoding"
import binary "encoding/binary"
import json "encoding/json"
import bolt "github.com/coreos/bbolt"
import fsm "github.com/interstellar/starlight/starlight/fsm"
import message "github.com/interstellar/starlight/starlight/internal/message"
import update "github.com/interstellar/starlight/starlight/internal/update"
const _ = binary.MaxVarintLen16
const _ = bolt.MaxKeySize
const _ = bytes.MinRead
// Root is a bucket with a static set of elements.
//
// Root is the type of the root bucket, as required by genbolt.
//
// Accessor methods read and write records
// and open child buckets.
// See functions View and
// Update to open transactions.
type Root struct {
db *bolt.Tx
}
// NewRoot returns a new Root for tx.
//
// Root is the type of the root bucket, as required by genbolt.
func NewRoot(tx *bolt.Tx) *Root {
return &Root{tx}
}
// View opens a read-only transaction
// and calls f with an instance of Root as the root bucket.
// It returns the error returned by f.
func View(db *bolt.DB, f func(*Root) error) error {
return db.View(func(tx *bolt.Tx) error {
return f(&Root{tx})
})
}
// Update opens a writable transaction
// and calls f with an instance of Root as the root bucket,
// then it commits the transaction.
// It returns the error returned by f,
// or any error committing to the database, if f was successful.
func Update(db *bolt.DB, f func(*Root) error) error {
return db.Update(func(tx *bolt.Tx) error {
return f(&Root{tx})
})
}
// Tx returns o's underlying *bolt.Tx object.
// This can be useful to access low-level database functions
// or other features not exposed by this generated code.
func (o *Root) Tx() *bolt.Tx {
return o.db
}
// Agent is a bucket with a static set of elements.
//
// Agent is the db layout for a Starlight agent.
//
// Accessor methods read and write records
// and open child buckets.
type Agent struct {
db *bolt.Bucket
}
// Bucket returns o's underlying *bolt.Bucket object.
// This can be useful to access low-level database functions
// or other features not exposed by this generated code.
//
// Note, if o's transaction is read-only and the underlying
// bucket has not previously been created in a writable
// transaction, Bucket returns nil.
func (o *Agent) Bucket() *bolt.Bucket {
return o.db
}
// Config is a bucket with a static set of elements.
//
// Config is the db layout for Starlight agent-level configuration.
//
// Accessor methods read and write records
// and open child buckets.
type Config struct {
db *bolt.Bucket
}
// Bucket returns o's underlying *bolt.Bucket object.
// This can be useful to access low-level database functions
// or other features not exposed by this generated code.
//
// Note, if o's transaction is read-only and the underlying
// bucket has not previously been created in a writable
// transaction, Bucket returns nil.
func (o *Config) Bucket() *bolt.Bucket {
return o.db
}
// Agent gets the child bucket with key "Agent" from o.
//
// Agent creates a new bucket if none exists
// and o's transaction is writable.
// Regardless, it always returns a non-nil *Agent;
// if the bucket doesn't exist
// and o's transaction is read-only, the returned value
// represents an empty bucket.
func (o *Root) Agent() *Agent {
return &Agent{bucket(o.db, keyAgent)}
}
// Config gets the child bucket with key "Config" from o.
//
// Config creates a new bucket if none exists
// and o's transaction is writable.
// Regardless, it always returns a non-nil *Config;
// if the bucket doesn't exist
// and o's transaction is read-only, the returned value
// represents an empty bucket.
func (o *Agent) Config() *Config {
return &Config{bucket(o.db, keyConfig)}
}
// Updates gets the child bucket with key "Updates" from o.
//
// Updates creates a new bucket if none exists
// and o's transaction is writable.
// Regardless, it always returns a non-nil *SeqOfUpdateUpdate;
// if the bucket doesn't exist
// and o's transaction is read-only, the returned value
// represents an empty bucket.
func (o *Agent) Updates() *SeqOfUpdateUpdate {
return &SeqOfUpdateUpdate{bucket(o.db, keyUpdates)}
}
// Channels gets the child bucket with key "Channels" from o.
//
// Channels holds the state of all open channels. Closed channels
// are deleted. (Their history is still available in Updates.)
//
// Channels creates a new bucket if none exists
// and o's transaction is writable.
// Regardless, it always returns a non-nil *MapOfFsmChannel;
// if the bucket doesn't exist
// and o's transaction is read-only, the returned value
// represents an empty bucket.
func (o *Agent) Channels() *MapOfFsmChannel {
return &MapOfFsmChannel{bucket(o.db, keyChannels)}
}
// Messages gets the child bucket with key "Messages" from o.
//
// Messages creates a new bucket if none exists
// and o's transaction is writable.
// Regardless, it always returns a non-nil *MapOfMessageMessage;
// if the bucket doesn't exist
// and o's transaction is read-only, the returned value
// represents an empty bucket.
func (o *Agent) Messages() *MapOfMessageMessage {
return &MapOfMessageMessage{bucket(o.db, keyMessages)}
}
// Ready reads the record stored under key "Ready".
//
// Ready indicates whether or not the Agent is ready to accept
// and process new commands. The Agent is only in a not-ready
// state when it is closing.
//
// If no record has been stored, Ready returns
// the zero value.
func (o *Agent) Ready() bool {
rec := get(o.db, keyReady)
return len(rec) > 0 && rec[0] != 0
}
// PutReady stores v as a record under the key "Ready".
//
// Ready indicates whether or not the Agent is ready to accept
// and process new commands. The Agent is only in a not-ready
// state when it is closing.
func (o *Agent) PutReady(v bool) {
rec := make([]byte, 1)
if v {
rec[0] = 1
}
put(o.db, keyReady, rec)
}
// EncryptedSeed reads the record stored under key "EncryptedSeed".
// If no record has been stored, EncryptedSeed returns
// the zero value.
func (o *Agent) EncryptedSeed() []byte {
rec := get(o.db, keyEncryptedSeed)
v := make([]byte, len(rec))
copy(v, rec)
return v
}
// PutEncryptedSeed stores v as a record under the key "EncryptedSeed".
func (o *Agent) PutEncryptedSeed(v []byte) {
rec := v
put(o.db, keyEncryptedSeed, rec)
}
// NextKeypathIndex reads the record stored under key "NextKeypathIndex".
// If no record has been stored, NextKeypathIndex returns
// the zero value.
func (o *Agent) NextKeypathIndex() uint32 {
rec := get(o.db, keyNextKeypathIndex)
if rec == nil {
return 0
}
return binary.BigEndian.Uint32(rec)
}
// PutNextKeypathIndex stores v as a record under the key "NextKeypathIndex".
func (o *Agent) PutNextKeypathIndex(v uint32) {
rec := make([]byte, 4)
binary.BigEndian.PutUint32(rec, v)
put(o.db, keyNextKeypathIndex, rec)
}
// PrimaryAcct reads the record stored under key "PrimaryAcct".
// If no record has been stored, PrimaryAcct returns
// a pointer to
// the zero value.
func (o *Agent) PrimaryAcct() *fsm.AccountID {
rec := get(o.db, keyPrimaryAcct)
v := new(fsm.AccountID)
if rec == nil {
return v
}
err := encoding.BinaryUnmarshaler(v).UnmarshalBinary(rec)
if err != nil {
panic(err)
}
return v
}
// PutPrimaryAcct stores v as a record under the key "PrimaryAcct".
func (o *Agent) PutPrimaryAcct(v *fsm.AccountID) {
rec, err := encoding.BinaryMarshaler(v).MarshalBinary()
if err != nil {
panic(err)
}
put(o.db, keyPrimaryAcct, rec)
}
// Wallet reads the record stored under key "Wallet".
// If no record has been stored, Wallet returns
// a pointer to
// the zero value.
func (o *Agent) Wallet() *fsm.WalletAcct {
rec := get(o.db, keyWallet)
v := new(fsm.WalletAcct)
if rec == nil {
return v
}
err := json.Unmarshal(rec, json.Unmarshaler(v))
if err != nil {
panic(err)
}
return v
}
// PutWallet stores v as a record under the key "Wallet".
func (o *Agent) PutWallet(v *fsm.WalletAcct) {
rec, err := json.Marshal(json.Marshaler(v))
if err != nil {
panic(err)
}
put(o.db, keyWallet, rec)
}
// HorizonURL reads the record stored under key "HorizonURL".
// If no record has been stored, HorizonURL returns
// the zero value.
func (o *Config) HorizonURL() string {
rec := get(o.db, keyHorizonURL)
return string(rec)
}
// PutHorizonURL stores v as a record under the key "HorizonURL".
func (o *Config) PutHorizonURL(v string) {
rec := []byte(v)
put(o.db, keyHorizonURL, rec)
}
// Username reads the record stored under key "Username".
// If no record has been stored, Username returns
// the zero value.
func (o *Config) Username() string {
rec := get(o.db, keyUsername)
return string(rec)
}
// PutUsername stores v as a record under the key "Username".
func (o *Config) PutUsername(v string) {
rec := []byte(v)
put(o.db, keyUsername, rec)
}
// PwType reads the record stored under key "PwType".
//
// PwType records which hashing function was used for PwHash.
// Currently, it's always "bcrypt".
//
// If no record has been stored, PwType returns
// the zero value.
func (o *Config) PwType() string {
rec := get(o.db, keyPwType)
return string(rec)
}
// PutPwType stores v as a record under the key "PwType".
//
// PwType records which hashing function was used for PwHash.
// Currently, it's always "bcrypt".
func (o *Config) PutPwType(v string) {
rec := []byte(v)
put(o.db, keyPwType, rec)
}
// PwHash reads the record stored under key "PwHash".
// If no record has been stored, PwHash returns
// the zero value.
func (o *Config) PwHash() []byte {
rec := get(o.db, keyPwHash)
v := make([]byte, len(rec))
copy(v, rec)
return v
}
// PutPwHash stores v as a record under the key "PwHash".
func (o *Config) PutPwHash(v []byte) {
rec := v
put(o.db, keyPwHash, rec)
}
// MaxRoundDurMins reads the record stored under key "MaxRoundDurMins".
// If no record has been stored, MaxRoundDurMins returns
// the zero value.
func (o *Config) MaxRoundDurMins() int64 {
rec := get(o.db, keyMaxRoundDurMins)
if rec == nil {
return 0
}
return int64(binary.BigEndian.Uint64(rec))
}
// PutMaxRoundDurMins stores v as a record under the key "MaxRoundDurMins".
func (o *Config) PutMaxRoundDurMins(v int64) {
rec := make([]byte, 8)
binary.BigEndian.PutUint64(rec, uint64(v))
put(o.db, keyMaxRoundDurMins, rec)
}
// FinalityDelayMins reads the record stored under key "FinalityDelayMins".
// If no record has been stored, FinalityDelayMins returns
// the zero value.
func (o *Config) FinalityDelayMins() int64 {
rec := get(o.db, keyFinalityDelayMins)
if rec == nil {
return 0
}
return int64(binary.BigEndian.Uint64(rec))
}
// PutFinalityDelayMins stores v as a record under the key "FinalityDelayMins".
func (o *Config) PutFinalityDelayMins(v int64) {
rec := make([]byte, 8)
binary.BigEndian.PutUint64(rec, uint64(v))
put(o.db, keyFinalityDelayMins, rec)
}
// ChannelFeerate reads the record stored under key "ChannelFeerate".
// If no record has been stored, ChannelFeerate returns
// the zero value.
func (o *Config) ChannelFeerate() int64 {
rec := get(o.db, keyChannelFeerate)
if rec == nil {
return 0
}
return int64(binary.BigEndian.Uint64(rec))
}
// PutChannelFeerate stores v as a record under the key "ChannelFeerate".
func (o *Config) PutChannelFeerate(v int64) {
rec := make([]byte, 8)
binary.BigEndian.PutUint64(rec, uint64(v))
put(o.db, keyChannelFeerate, rec)
}
// HostFeerate reads the record stored under key "HostFeerate".
// If no record has been stored, HostFeerate returns
// the zero value.
func (o *Config) HostFeerate() int64 {
rec := get(o.db, keyHostFeerate)
if rec == nil {
return 0
}
return int64(binary.BigEndian.Uint64(rec))
}
// PutHostFeerate stores v as a record under the key "HostFeerate".
func (o *Config) PutHostFeerate(v int64) {
rec := make([]byte, 8)
binary.BigEndian.PutUint64(rec, uint64(v))
put(o.db, keyHostFeerate, rec)
}
// KeepAlive reads the record stored under key "KeepAlive".
// If no record has been stored, KeepAlive returns
// the zero value.
func (o *Config) KeepAlive() bool {
rec := get(o.db, keyKeepAlive)
return len(rec) > 0 && rec[0] != 0
}
// PutKeepAlive stores v as a record under the key "KeepAlive".
func (o *Config) PutKeepAlive(v bool) {
rec := make([]byte, 1)
if v {
rec[0] = 1
}
put(o.db, keyKeepAlive, rec)
}
// Public reads the record stored under key "Public".
// If no record has been stored, Public returns
// the zero value.
func (o *Config) Public() bool {
rec := get(o.db, keyPublic)
return len(rec) > 0 && rec[0] != 0
}
// PutPublic stores v as a record under the key "Public".
func (o *Config) PutPublic(v bool) {
rec := make([]byte, 1)
if v {
rec[0] = 1
}
put(o.db, keyPublic, rec)
}
// MapOfFsmChannel is a bucket with arbitrary keys,
// holding records of type *fsm.Channel.
type MapOfFsmChannel struct {
db *bolt.Bucket
}
// Bucket returns o's underlying *bolt.Bucket object.
// This can be useful to access low-level database functions
// or other features not exposed by this generated code.
//
// Note, if o's transaction is read-only and the underlying
// bucket has not previously been created in a writable
// transaction, Bucket returns nil.
func (o *MapOfFsmChannel) Bucket() *bolt.Bucket {
return o.db
}
// Get reads the record stored in o under the given key.
//
// If no record has been stored, it returns
// a pointer to
// the zero value.
func (o *MapOfFsmChannel) Get(key []byte) *fsm.Channel {
rec := get(o.db, key)
v := new(fsm.Channel)
if rec == nil {
return v
}
err := json.Unmarshal(rec, json.Unmarshaler(v))
if err != nil {
panic(err)
}
return v
}
// GetByString is equivalent to o.Get([]byte(key)).
func (o *MapOfFsmChannel) GetByString(key string) *fsm.Channel {
return o.Get([]byte(key))
}
// Put stores v in o as a record under the given key.
func (o *MapOfFsmChannel) Put(key []byte, v *fsm.Channel) {
rec, err := json.Marshal(json.Marshaler(v))
if err != nil {
panic(err)
}
put(o.db, key, rec)
}
// PutByString is equivalent to o.Put([]byte(key), v).
func (o *MapOfFsmChannel) PutByString(key string, v *fsm.Channel) {
o.Put([]byte(key), v)
}
// MapOfMessageMessage is a bucket with arbitrary keys,
// holding records of type *message.Message.
type MapOfMessageMessage struct {
db *bolt.Bucket
}
// Bucket returns o's underlying *bolt.Bucket object.
// This can be useful to access low-level database functions
// or other features not exposed by this generated code.
//
// Note, if o's transaction is read-only and the underlying
// bucket has not previously been created in a writable
// transaction, Bucket returns nil.
func (o *MapOfMessageMessage) Bucket() *bolt.Bucket {
return o.db
}
// Get reads the record stored in o under the given key.
//
// If no record has been stored, it returns
// a pointer to
// the zero value.
func (o *MapOfMessageMessage) Get(key []byte) *message.Message {
rec := get(o.db, key)
v := new(message.Message)
if rec == nil {
return v
}
err := json.Unmarshal(rec, json.Unmarshaler(v))
if err != nil {
panic(err)
}
return v
}
// GetByString is equivalent to o.Get([]byte(key)).
func (o *MapOfMessageMessage) GetByString(key string) *message.Message {
return o.Get([]byte(key))
}
// Put stores v in o as a record under the given key.
func (o *MapOfMessageMessage) Put(key []byte, v *message.Message) {
rec, err := json.Marshal(json.Marshaler(v))
if err != nil {
panic(err)
}
put(o.db, key, rec)
}
// PutByString is equivalent to o.Put([]byte(key), v).
func (o *MapOfMessageMessage) PutByString(key string, v *message.Message) {
o.Put([]byte(key), v)
}
// SeqOfUpdateUpdate is a bucket with sequential numeric keys,
// holding records of type *update.Update.
type SeqOfUpdateUpdate struct {
db *bolt.Bucket
}
// Bucket returns o's underlying *bolt.Bucket object.
// This can be useful to access low-level database functions
// or other features not exposed by this generated code.
//
// Note, if o's transaction is read-only and the underlying
// bucket has not previously been created in a writable
// transaction, Bucket returns nil.
func (o *SeqOfUpdateUpdate) Bucket() *bolt.Bucket {
return o.db
}
// Get reads the record stored in o under sequence number n.
//
// If no record has been stored, it returns
// a pointer to
// the zero value.
func (o *SeqOfUpdateUpdate) Get(n uint64) *update.Update {
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, n)
rec := get(o.db, key)
v := new(update.Update)
if rec == nil {
return v
}
err := json.Unmarshal(rec, json.Unmarshaler(v))
if err != nil {
panic(err)
}
return v
}
// Add stores v in o under a new sequence number.
// It writes the new sequence number to *np
// before marshaling v. It is okay for
// np to point to a field inside v, to store
// the sequence number in the new record.
func (o *SeqOfUpdateUpdate) Add(v *update.Update, np *uint64) {
n, err := o.db.NextSequence()
if err != nil {
panic(err)
}
*np = n
o.Put(n, v)
}
// Put stores v in o as a record under sequence number n.
func (o *SeqOfUpdateUpdate) Put(n uint64, v *update.Update) {
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, n)
rec, err := json.Marshal(json.Marshaler(v))
if err != nil {
panic(err)
}
put(o.db, key, rec)
}
var (
keyAgent = []byte("Agent")
keyChannelFeerate = []byte("ChannelFeerate")
keyChannels = []byte("Channels")
keyConfig = []byte("Config")
keyEncryptedSeed = []byte("EncryptedSeed")
keyFinalityDelayMins = []byte("FinalityDelayMins")
keyHorizonURL = []byte("HorizonURL")
keyHostFeerate = []byte("HostFeerate")
keyKeepAlive = []byte("KeepAlive")
keyMaxRoundDurMins = []byte("MaxRoundDurMins")
keyMessages = []byte("Messages")
keyNextKeypathIndex = []byte("NextKeypathIndex")
keyPrimaryAcct = []byte("PrimaryAcct")
keyPublic = []byte("Public")
keyPwHash = []byte("PwHash")
keyPwType = []byte("PwType")
keyReady = []byte("Ready")
keyUpdates = []byte("Updates")
keyUsername = []byte("Username")
keyWallet = []byte("Wallet")
)
type db interface {
Writable() bool
CreateBucketIfNotExists([]byte) (*bolt.Bucket, error)
Bucket([]byte) *bolt.Bucket
}
func bucket(db db, key []byte) *bolt.Bucket {
if bu, ok := db.(*bolt.Bucket); ok && bu == nil {
return nil // can happen in read-only txs
}
if !db.Writable() {
return db.Bucket(key)
}
b, err := db.CreateBucketIfNotExists(key)
if err != nil {
panic(err)
}
return b
}
func get(b *bolt.Bucket, key []byte) []byte {
if b == nil {
return nil
}
return b.Get(key)
}
func put(b *bolt.Bucket, key, value []byte) {
err := b.Put(key, value)
if err != nil {
panic(err)
}
}