code.vegaprotocol.io/vega@v0.79.0/core/coreapi/service.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 coreapi 17 18 import ( 19 "context" 20 "errors" 21 "sync" 22 23 "code.vegaprotocol.io/vega/core/broker" 24 "code.vegaprotocol.io/vega/core/coreapi/services" 25 lb "code.vegaprotocol.io/vega/libs/broker" 26 "code.vegaprotocol.io/vega/logging" 27 apipb "code.vegaprotocol.io/vega/protos/vega/api/v1" 28 ) 29 30 var ErrServiceDisabled = errors.New("service disabled") 31 32 type Service struct { 33 apipb.UnimplementedCoreStateServiceServer 34 ctx context.Context 35 cfg Config 36 log *logging.Logger 37 38 bmu sync.RWMutex 39 broker broker.Interface 40 41 accounts *services.Accounts 42 assets *services.Assets 43 netparams *services.NetParams 44 netlimits *services.NetLimits 45 parties *services.Parties 46 validators *services.Validators 47 markets *services.Markets 48 proposals *services.Proposals 49 votes *services.Votes 50 marketsData *services.MarketsData 51 partiesStake *services.PartiesStake 52 delegations *services.Delegations 53 } 54 55 func NewService( 56 ctx context.Context, log *logging.Logger, cfg Config, broker broker.Interface, 57 ) *Service { 58 log = log.Named(namedLogger) 59 log.SetLevel(cfg.LogLevel.Get()) 60 svc := &Service{ 61 broker: broker, 62 cfg: cfg, 63 ctx: ctx, 64 log: log, 65 } 66 67 if cfg.Accounts { 68 log.Info("starting accounts core api") 69 svc.accounts = services.NewAccounts(ctx) 70 } 71 72 if cfg.Assets { 73 log.Info("starting assets core api") 74 svc.assets = services.NewAssets(ctx) 75 } 76 77 if cfg.NetworkParameters { 78 log.Info("starting network parameters core api") 79 svc.netparams = services.NewNetParams(ctx) 80 } 81 82 if cfg.NetworkLimits { 83 log.Info("starting network limits core api") 84 svc.netlimits = services.NewNetLimits(ctx) 85 } 86 87 if cfg.Parties { 88 log.Info("starting parties core api") 89 svc.parties = services.NewParties(ctx) 90 } 91 92 if cfg.Validators { 93 log.Info("starting validators core api") 94 svc.validators = services.NewValidators(ctx) 95 } 96 97 if cfg.Markets { 98 log.Info("starting markets core api") 99 svc.markets = services.NewMarkets(ctx) 100 } 101 102 if cfg.Proposals { 103 log.Info("starting proposals core api") 104 svc.proposals = services.NewProposals(ctx) 105 } 106 107 if cfg.MarketsData { 108 log.Info("starting marketsData core api") 109 svc.marketsData = services.NewMarketsData(ctx) 110 } 111 112 if cfg.Votes { 113 log.Info("starting votes core api") 114 svc.votes = services.NewVotes(ctx) 115 } 116 117 if cfg.PartiesStake { 118 log.Info("starting parties stake core api") 119 svc.partiesStake = services.NewPartiesStake(ctx, log) 120 } 121 122 if cfg.Delegations { 123 log.Info("starting delegations core api") 124 svc.delegations = services.NewDelegations(ctx) 125 } 126 127 svc.subscribeAll() 128 129 return svc 130 } 131 132 func (s *Service) UpdateBroker(broker broker.Interface) { 133 s.bmu.Lock() 134 defer s.bmu.Unlock() 135 s.broker = broker 136 s.subscribeAll() 137 } 138 139 func (s *Service) ListAccounts( 140 ctx context.Context, in *apipb.ListAccountsRequest, 141 ) (*apipb.ListAccountsResponse, error) { 142 if !s.cfg.Accounts { 143 return nil, ErrServiceDisabled 144 } 145 s.bmu.RLock() 146 defer s.bmu.RUnlock() 147 return &apipb.ListAccountsResponse{ 148 Accounts: s.accounts.List(in.Party, in.Market), 149 }, nil 150 } 151 152 func (s *Service) ListAssets( 153 ctx context.Context, in *apipb.ListAssetsRequest, 154 ) (*apipb.ListAssetsResponse, error) { 155 if !s.cfg.Assets { 156 return nil, ErrServiceDisabled 157 } 158 s.bmu.RLock() 159 defer s.bmu.RUnlock() 160 return &apipb.ListAssetsResponse{ 161 Assets: s.assets.List(in.Asset), 162 }, nil 163 } 164 165 func (s *Service) ListParties( 166 ctx context.Context, in *apipb.ListPartiesRequest, 167 ) (*apipb.ListPartiesResponse, error) { 168 if !s.cfg.Parties { 169 return nil, ErrServiceDisabled 170 } 171 s.bmu.RLock() 172 defer s.bmu.RUnlock() 173 return &apipb.ListPartiesResponse{ 174 Parties: s.parties.List(), 175 }, nil 176 } 177 178 func (s *Service) ListNetworkParameters( 179 ctx context.Context, in *apipb.ListNetworkParametersRequest, 180 ) (*apipb.ListNetworkParametersResponse, error) { 181 if !s.cfg.NetworkParameters { 182 return nil, ErrServiceDisabled 183 } 184 s.bmu.RLock() 185 defer s.bmu.RUnlock() 186 return &apipb.ListNetworkParametersResponse{ 187 NetworkParameters: s.netparams.List(in.NetworkParameterKey), 188 }, nil 189 } 190 191 func (s *Service) ListNetworkLimits( 192 ctx context.Context, in *apipb.ListNetworkLimitsRequest, 193 ) (*apipb.ListNetworkLimitsResponse, error) { 194 if !s.cfg.NetworkLimits { 195 return nil, ErrServiceDisabled 196 } 197 s.bmu.RLock() 198 defer s.bmu.RUnlock() 199 return &apipb.ListNetworkLimitsResponse{ 200 NetworkLimits: s.netlimits.Get(), 201 }, nil 202 } 203 204 func (s *Service) ListValidators( 205 ctx context.Context, in *apipb.ListValidatorsRequest, 206 ) (*apipb.ListValidatorsResponse, error) { 207 if !s.cfg.Validators { 208 return nil, ErrServiceDisabled 209 } 210 s.bmu.RLock() 211 defer s.bmu.RUnlock() 212 return &apipb.ListValidatorsResponse{ 213 Validators: s.validators.List(), 214 }, nil 215 } 216 217 func (s *Service) ListMarkets( 218 ctx context.Context, in *apipb.ListMarketsRequest, 219 ) (*apipb.ListMarketsResponse, error) { 220 if !s.cfg.Markets { 221 return nil, ErrServiceDisabled 222 } 223 s.bmu.RLock() 224 defer s.bmu.RUnlock() 225 return &apipb.ListMarketsResponse{ 226 Markets: s.markets.List(in.Market), 227 }, nil 228 } 229 230 func (s *Service) ListProposals( 231 ctx context.Context, in *apipb.ListProposalsRequest, 232 ) (*apipb.ListProposalsResponse, error) { 233 if !s.cfg.Proposals { 234 return nil, ErrServiceDisabled 235 } 236 s.bmu.RLock() 237 defer s.bmu.RUnlock() 238 return &apipb.ListProposalsResponse{ 239 Proposals: s.proposals.List(in.Proposal, in.Proposer), 240 }, nil 241 } 242 243 func (s *Service) ListVotes( 244 ctx context.Context, in *apipb.ListVotesRequest, 245 ) (*apipb.ListVotesResponse, error) { 246 if !s.cfg.Votes { 247 return nil, ErrServiceDisabled 248 } 249 s.bmu.RLock() 250 defer s.bmu.RUnlock() 251 votes, err := s.votes.List(in.Proposal, in.Party) 252 return &apipb.ListVotesResponse{ 253 Votes: votes, 254 }, err 255 } 256 257 func (s *Service) ListMarketsData( 258 ctx context.Context, in *apipb.ListMarketsDataRequest, 259 ) (*apipb.ListMarketsDataResponse, error) { 260 if !s.cfg.MarketsData { 261 return nil, ErrServiceDisabled 262 } 263 s.bmu.RLock() 264 defer s.bmu.RUnlock() 265 return &apipb.ListMarketsDataResponse{ 266 MarketsData: s.marketsData.List(in.Market), 267 }, nil 268 } 269 270 func (s *Service) ListPartiesStake( 271 ctx context.Context, in *apipb.ListPartiesStakeRequest, 272 ) (*apipb.ListPartiesStakeResponse, error) { 273 if !s.cfg.PartiesStake { 274 return nil, ErrServiceDisabled 275 } 276 s.bmu.RLock() 277 defer s.bmu.RUnlock() 278 return &apipb.ListPartiesStakeResponse{ 279 PartiesStake: s.partiesStake.List(in.Party), 280 }, nil 281 } 282 283 func (s *Service) ListDelegations( 284 ctx context.Context, in *apipb.ListDelegationsRequest, 285 ) (*apipb.ListDelegationsResponse, error) { 286 if !s.cfg.Delegations { 287 return nil, ErrServiceDisabled 288 } 289 s.bmu.RLock() 290 defer s.bmu.RUnlock() 291 return &apipb.ListDelegationsResponse{ 292 Delegations: s.delegations.List(in.Party, in.Node, in.EpochSeq), 293 }, nil 294 } 295 296 func (s *Service) subscribeAll() { 297 subscribers := []lb.Subscriber{} 298 299 if s.cfg.Accounts { 300 subscribers = append(subscribers, s.accounts) 301 } 302 303 if s.cfg.Assets { 304 subscribers = append(subscribers, s.assets) 305 } 306 307 if s.cfg.NetworkParameters { 308 subscribers = append(subscribers, s.netparams) 309 } 310 311 if s.cfg.NetworkLimits { 312 subscribers = append(subscribers, s.netlimits) 313 } 314 315 if s.cfg.Parties { 316 subscribers = append(subscribers, s.parties) 317 } 318 319 if s.cfg.Validators { 320 subscribers = append(subscribers, s.validators) 321 } 322 323 if s.cfg.Markets { 324 subscribers = append(subscribers, s.markets) 325 } 326 327 if s.cfg.Proposals { 328 subscribers = append(subscribers, s.proposals) 329 } 330 331 if s.cfg.MarketsData { 332 subscribers = append(subscribers, s.marketsData) 333 } 334 335 if s.cfg.Votes { 336 subscribers = append(subscribers, s.votes) 337 } 338 339 if s.cfg.PartiesStake { 340 subscribers = append(subscribers, s.partiesStake) 341 } 342 343 if s.cfg.Delegations { 344 subscribers = append(subscribers, s.delegations) 345 } 346 347 s.broker.SubscribeBatch(subscribers...) 348 }