2019-06-07 21:20:34 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-07-15 18:38:08 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-06-07 21:20:34 +01:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// ErrVouchers represents errors from the vouchers database.
|
|
|
|
var ErrVouchers = errs.Class("vouchersdb error")
|
2019-06-07 21:20:34 +01:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
type vouchersDB struct {
|
|
|
|
location string
|
|
|
|
SQLDB
|
|
|
|
}
|
2019-06-07 21:20:34 +01:00
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
// newVouchersDB returns a new instance of vouchersdb initialized with the specified database.
|
|
|
|
func newVouchersDB(db SQLDB, location string) *vouchersDB {
|
|
|
|
return &vouchersDB{
|
|
|
|
location: location,
|
|
|
|
SQLDB: db,
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 21:20:34 +01:00
|
|
|
|
|
|
|
// Put inserts or updates a voucher from a satellite
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *vouchersDB) Put(ctx context.Context, voucher *pb.Voucher) (err error) {
|
2019-06-07 21:20:34 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
voucherSerialized, err := proto.Marshal(voucher)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return ErrVouchers.Wrap(err)
|
2019-06-07 21:20:34 +01:00
|
|
|
}
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
_, err = db.Exec(`
|
2019-06-07 21:20:34 +01:00
|
|
|
INSERT INTO vouchers(
|
2019-07-15 18:38:08 +01:00
|
|
|
satellite_id,
|
|
|
|
voucher_serialized,
|
2019-06-07 21:20:34 +01:00
|
|
|
expiration
|
|
|
|
) VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT(satellite_id) DO UPDATE SET
|
|
|
|
voucher_serialized = ?,
|
|
|
|
expiration = ?
|
2019-07-15 18:38:08 +01:00
|
|
|
`, voucher.SatelliteId, voucherSerialized, voucher.Expiration.UTC(), voucherSerialized, voucher.Expiration.UTC())
|
2019-06-07 21:20:34 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-21 23:48:52 +01:00
|
|
|
// NeedVoucher returns true if a voucher from a particular satellite is expired, about to expire, or does not exist
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *vouchersDB) NeedVoucher(ctx context.Context, satelliteID storj.NodeID, expirationBuffer time.Duration) (need bool, err error) {
|
2019-06-07 21:20:34 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-07-15 18:38:08 +01:00
|
|
|
expiresBefore := time.Now().Add(expirationBuffer)
|
2019-06-21 23:48:52 +01:00
|
|
|
|
|
|
|
// query returns row if voucher is good. If not, it is either expiring or does not exist
|
2019-08-21 15:32:25 +01:00
|
|
|
row := db.QueryRow(`
|
2019-06-07 21:20:34 +01:00
|
|
|
SELECT satellite_id
|
|
|
|
FROM vouchers
|
2019-06-21 23:48:52 +01:00
|
|
|
WHERE satellite_id = ? AND expiration >= ?
|
2019-07-15 18:38:08 +01:00
|
|
|
`, satelliteID, expiresBefore.UTC())
|
2019-06-21 23:48:52 +01:00
|
|
|
|
|
|
|
var bytes []byte
|
|
|
|
err = row.Scan(&bytes)
|
2019-06-07 21:20:34 +01:00
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
2019-06-21 23:48:52 +01:00
|
|
|
return true, nil
|
2019-06-07 21:20:34 +01:00
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return false, ErrVouchers.Wrap(err)
|
2019-06-07 21:20:34 +01:00
|
|
|
}
|
2019-06-21 23:48:52 +01:00
|
|
|
return false, nil
|
2019-06-07 21:20:34 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 15:52:44 +01:00
|
|
|
// GetAll returns all vouchers in the table
|
2019-08-21 15:32:25 +01:00
|
|
|
func (db *vouchersDB) GetAll(ctx context.Context) (vouchers []*pb.Voucher, err error) {
|
2019-06-07 21:20:34 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-08-21 15:32:25 +01:00
|
|
|
rows, err := db.Query(`
|
2019-06-07 21:20:34 +01:00
|
|
|
SELECT voucher_serialized
|
|
|
|
FROM vouchers
|
2019-07-19 15:52:44 +01:00
|
|
|
`)
|
2019-06-07 21:20:34 +01:00
|
|
|
if err != nil {
|
2019-06-21 23:48:52 +01:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrVouchers.Wrap(err)
|
2019-06-07 21:20:34 +01:00
|
|
|
}
|
2019-07-19 15:52:44 +01:00
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
2019-06-07 21:20:34 +01:00
|
|
|
|
2019-07-19 15:52:44 +01:00
|
|
|
for rows.Next() {
|
|
|
|
var voucherSerialized []byte
|
|
|
|
err := rows.Scan(&voucherSerialized)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrVouchers.Wrap(err)
|
2019-07-19 15:52:44 +01:00
|
|
|
}
|
|
|
|
voucher := &pb.Voucher{}
|
|
|
|
err = proto.Unmarshal(voucherSerialized, voucher)
|
|
|
|
if err != nil {
|
2019-08-21 15:32:25 +01:00
|
|
|
return nil, ErrVouchers.Wrap(err)
|
2019-07-19 15:52:44 +01:00
|
|
|
}
|
|
|
|
vouchers = append(vouchers, voucher)
|
2019-06-07 21:20:34 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 15:52:44 +01:00
|
|
|
return vouchers, nil
|
2019-06-07 21:20:34 +01:00
|
|
|
}
|