code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/epochs.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package sqlstore 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/datanode/entities" 22 "code.vegaprotocol.io/vega/datanode/metrics" 23 24 "github.com/georgysavva/scany/pgxscan" 25 ) 26 27 type Epochs struct { 28 *ConnectionSource 29 } 30 31 func NewEpochs(connectionSource *ConnectionSource) *Epochs { 32 e := &Epochs{ 33 ConnectionSource: connectionSource, 34 } 35 return e 36 } 37 38 func (es *Epochs) Add(ctx context.Context, r entities.Epoch) error { 39 defer metrics.StartSQLQuery("Epochs", "Add")() 40 _, err := es.Exec(ctx, 41 `INSERT INTO epochs( 42 id, 43 start_time, 44 expire_time, 45 end_time, 46 tx_hash, 47 vega_time) 48 VALUES ($1, $2, $3, $4, $5, $6) 49 ON CONFLICT (id, vega_time) 50 DO UPDATE SET start_time=EXCLUDED.start_time, 51 expire_time=EXCLUDED.expire_time, 52 end_time=EXCLUDED.end_time, 53 tx_hash=EXCLUDED.tx_hash 54 ;`, 55 r.ID, r.StartTime, r.ExpireTime, r.EndTime, r.TxHash, r.VegaTime) 56 return err 57 } 58 59 func (es *Epochs) GetAll(ctx context.Context) ([]entities.Epoch, error) { 60 defer metrics.StartSQLQuery("Epochs", "GetAll")() 61 epochs := []entities.Epoch{} 62 query := `SELECT * FROM current_epochs order by id, vega_time desc;` 63 err := pgxscan.Select(ctx, es.ConnectionSource, &epochs, query) 64 return epochs, err 65 } 66 67 func (es *Epochs) Get(ctx context.Context, ID uint64) (entities.Epoch, error) { 68 defer metrics.StartSQLQuery("Epochs", "Get")() 69 query := `SELECT * FROM current_epochs WHERE id = $1;` 70 71 epoch := entities.Epoch{} 72 return epoch, es.wrapE(pgxscan.Get(ctx, es.ConnectionSource, &epoch, query, ID)) 73 } 74 75 func (es *Epochs) GetByBlock(ctx context.Context, height uint64) (entities.Epoch, error) { 76 defer metrics.StartSQLQuery("Epochs", "GetByBlock")() 77 query := `SELECT * FROM current_epochs WHERE first_block <= $1 AND (last_block > $1 or last_block is NULL) ORDER BY id, vega_time desc;` 78 79 epoch := entities.Epoch{} 80 return epoch, es.wrapE(pgxscan.Get(ctx, es.ConnectionSource, &epoch, query, height)) 81 } 82 83 func (es *Epochs) GetCurrent(ctx context.Context) (entities.Epoch, error) { 84 query := `SELECT * FROM current_epochs ORDER BY id DESC, vega_time DESC FETCH FIRST ROW ONLY;` 85 86 epoch := entities.Epoch{} 87 defer metrics.StartSQLQuery("Epochs", "GetCurrent")() 88 return epoch, es.wrapE(pgxscan.Get(ctx, es.ConnectionSource, &epoch, query)) 89 }