code.vegaprotocol.io/vega@v0.79.0/core/coreapi/services/parties.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 services
    17  
    18  import (
    19  	"context"
    20  	"sync"
    21  
    22  	"code.vegaprotocol.io/vega/core/events"
    23  	"code.vegaprotocol.io/vega/core/subscribers"
    24  	"code.vegaprotocol.io/vega/core/types"
    25  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    26  )
    27  
    28  type partyE interface {
    29  	events.Event
    30  	Party() vegapb.Party
    31  }
    32  
    33  type Parties struct {
    34  	*subscribers.Base
    35  	ctx context.Context
    36  
    37  	mu      sync.RWMutex
    38  	parties map[string]vegapb.Party
    39  	ch      chan vegapb.Party
    40  }
    41  
    42  func NewParties(ctx context.Context) (parties *Parties) {
    43  	defer func() {
    44  		parties.parties[types.NetworkParty] = vegapb.Party{Id: types.NetworkParty}
    45  		go parties.consume()
    46  	}()
    47  	return &Parties{
    48  		Base:    subscribers.NewBase(ctx, 1000, true),
    49  		ctx:     ctx,
    50  		parties: map[string]vegapb.Party{},
    51  		ch:      make(chan vegapb.Party, 100),
    52  	}
    53  }
    54  
    55  func (a *Parties) consume() {
    56  	defer func() { close(a.ch) }()
    57  	for {
    58  		select {
    59  		case <-a.Closed():
    60  			return
    61  		case party, ok := <-a.ch:
    62  			if !ok {
    63  				// cleanup base
    64  				a.Halt()
    65  				// channel is closed
    66  				return
    67  			}
    68  			a.mu.Lock()
    69  			a.parties[party.Id] = party
    70  			a.mu.Unlock()
    71  		}
    72  	}
    73  }
    74  
    75  func (a *Parties) Push(evts ...events.Event) {
    76  	for _, e := range evts {
    77  		if ae, ok := e.(partyE); ok {
    78  			a.ch <- ae.Party()
    79  		}
    80  	}
    81  }
    82  
    83  func (a *Parties) List() []*vegapb.Party {
    84  	a.mu.RLock()
    85  	defer a.mu.RUnlock()
    86  	out := make([]*vegapb.Party, 0, len(a.parties))
    87  	for _, v := range a.parties {
    88  		v := v
    89  		out = append(out, &v)
    90  	}
    91  	return out
    92  }
    93  
    94  func (a *Parties) Types() []events.Type {
    95  	return []events.Type{
    96  		events.PartyEvent,
    97  	}
    98  }