-
Notifications
You must be signed in to change notification settings - Fork 390
/
vouchers.go
105 lines (86 loc) · 2.61 KB
/
vouchers.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package storagenodedb
import (
"context"
"database/sql"
"time"
"github.com/gogo/protobuf/proto"
"github.com/zeebo/errs"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
"storj.io/storj/storagenode/vouchers"
)
type vouchersdb struct{ *InfoDB }
// Vouchers returns database for storing vouchers
func (db *DB) Vouchers() vouchers.DB { return db.info.Vouchers() }
// Vouchers returns database for storing vouchers
func (db *InfoDB) Vouchers() vouchers.DB { return &vouchersdb{db} }
// Put inserts or updates a voucher from a satellite
func (db *vouchersdb) Put(ctx context.Context, voucher *pb.Voucher) (err error) {
defer mon.Task()(&ctx)(&err)
voucherSerialized, err := proto.Marshal(voucher)
if err != nil {
return ErrInfo.Wrap(err)
}
_, err = db.db.Exec(`
INSERT INTO vouchers(
satellite_id,
voucher_serialized,
expiration
) VALUES (?, ?, ?)
ON CONFLICT(satellite_id) DO UPDATE SET
voucher_serialized = ?,
expiration = ?
`, voucher.SatelliteId, voucherSerialized, voucher.Expiration.UTC(), voucherSerialized, voucher.Expiration.UTC())
return err
}
// NeedVoucher returns true if a voucher from a particular satellite is expired, about to expire, or does not exist
func (db *vouchersdb) NeedVoucher(ctx context.Context, satelliteID storj.NodeID, expirationBuffer time.Duration) (need bool, err error) {
defer mon.Task()(&ctx)(&err)
expiresBefore := time.Now().Add(expirationBuffer)
// query returns row if voucher is good. If not, it is either expiring or does not exist
row := db.db.QueryRow(`
SELECT satellite_id
FROM vouchers
WHERE satellite_id = ? AND expiration >= ?
`, satelliteID, expiresBefore.UTC())
var bytes []byte
err = row.Scan(&bytes)
if err != nil {
if err == sql.ErrNoRows {
return true, nil
}
return false, ErrInfo.Wrap(err)
}
return false, nil
}
// GetAll returns all vouchers in the table
func (db *vouchersdb) GetAll(ctx context.Context) (vouchers []*pb.Voucher, err error) {
defer mon.Task()(&ctx)(&err)
rows, err := db.db.Query(`
SELECT voucher_serialized
FROM vouchers
`)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, ErrInfo.Wrap(err)
}
defer func() { err = errs.Combine(err, rows.Close()) }()
for rows.Next() {
var voucherSerialized []byte
err := rows.Scan(&voucherSerialized)
if err != nil {
return nil, ErrInfo.Wrap(err)
}
voucher := &pb.Voucher{}
err = proto.Unmarshal(voucherSerialized, voucher)
if err != nil {
return nil, ErrInfo.Wrap(err)
}
vouchers = append(vouchers, voucher)
}
return vouchers, nil
}