code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/network_limits.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 NetworkLimits struct { 28 *ConnectionSource 29 } 30 31 func NewNetworkLimits(connectionSource *ConnectionSource) *NetworkLimits { 32 return &NetworkLimits{ConnectionSource: connectionSource} 33 } 34 35 // Add inserts a row into the network limits table. If a row with the same vega time 36 // exists, that row is updated instead. (i.e. there are multiple updates of the limits 37 // in one block, does occur). 38 func (nl *NetworkLimits) Add(ctx context.Context, limits entities.NetworkLimits) error { 39 defer metrics.StartSQLQuery("NetworkLimits", "Add")() 40 _, err := nl.Exec(ctx, ` 41 INSERT INTO network_limits( 42 tx_hash, 43 vega_time, 44 can_propose_market, 45 can_propose_asset, 46 propose_market_enabled, 47 propose_asset_enabled, 48 genesis_loaded, 49 propose_market_enabled_from, 50 propose_asset_enabled_from) 51 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) 52 ON CONFLICT (vega_time) DO UPDATE SET 53 can_propose_market=EXCLUDED.can_propose_market, 54 can_propose_asset=EXCLUDED.can_propose_asset, 55 propose_market_enabled=EXCLUDED.propose_market_enabled, 56 propose_asset_enabled=EXCLUDED.propose_asset_enabled, 57 genesis_loaded=EXCLUDED.genesis_loaded, 58 propose_market_enabled_from=EXCLUDED.propose_market_enabled_from, 59 propose_asset_enabled_from=EXCLUDED.propose_asset_enabled_from 60 `, 61 limits.TxHash, 62 limits.VegaTime, 63 limits.CanProposeMarket, 64 limits.CanProposeAsset, 65 limits.ProposeMarketEnabled, 66 limits.ProposeAssetEnabled, 67 limits.GenesisLoaded, 68 limits.ProposeMarketEnabledFrom, 69 limits.ProposeAssetEnabledFrom) 70 return err 71 } 72 73 // GetLatest returns the most recent network limits. 74 func (nl *NetworkLimits) GetLatest(ctx context.Context) (entities.NetworkLimits, error) { 75 networkLimits := entities.NetworkLimits{} 76 defer metrics.StartSQLQuery("NetworkLimits", "GetLatest")() 77 return networkLimits, nl.wrapE(pgxscan.Get(ctx, nl.ConnectionSource, &networkLimits, 78 `SELECT * FROM network_limits ORDER BY vega_time DESC limit 1;`)) 79 }