-
Notifications
You must be signed in to change notification settings - Fork 22
/
governance.go
140 lines (119 loc) · 5.18 KB
/
governance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package service
import (
"context"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/utils"
"code.vegaprotocol.io/vega/logging"
)
type ProposalStore interface {
Add(ctx context.Context, p entities.Proposal) error
GetByID(ctx context.Context, id string) (entities.Proposal, error)
GetByReference(ctx context.Context, ref string) (entities.Proposal, error)
Get(ctx context.Context, inState *entities.ProposalState, partyIDStr *string, proposalType *entities.ProposalType,
pagination entities.CursorPagination) ([]entities.Proposal, entities.PageInfo, error)
}
type VoteStore interface {
Add(ctx context.Context, v entities.Vote) error
GetYesVotesForProposal(ctx context.Context, proposalIDStr string) ([]entities.Vote, error)
GetNoVotesForProposal(ctx context.Context, proposalIDStr string) ([]entities.Vote, error)
GetByParty(ctx context.Context, partyIDStr string) ([]entities.Vote, error)
GetByPartyConnection(ctx context.Context, partyIDStr string, pagination entities.CursorPagination) ([]entities.Vote, entities.PageInfo, error)
Get(ctx context.Context, proposalID, partyID *string, value *entities.VoteValue) ([]entities.Vote, error)
GetConnection(
ctx context.Context,
proposalIDStr, partyIDStr *string,
pagination entities.CursorPagination,
) ([]entities.Vote, entities.PageInfo, error)
}
type Governance struct {
pStore ProposalStore
vStore VoteStore
pObserver utils.Observer[entities.Proposal]
vObserver utils.Observer[entities.Vote]
}
func NewGovernance(pStore ProposalStore, vStore VoteStore, log *logging.Logger) *Governance {
return &Governance{
pStore: pStore,
vStore: vStore,
pObserver: utils.NewObserver[entities.Proposal]("proposal", log, 0, 0),
vObserver: utils.NewObserver[entities.Vote]("vote", log, 0, 0),
}
}
func (g *Governance) AddProposal(ctx context.Context, p entities.Proposal) error {
err := g.pStore.Add(ctx, p)
if err != nil {
return err
}
g.pObserver.Notify([]entities.Proposal{p})
return nil
}
func (g *Governance) GetProposalByID(ctx context.Context, id string) (entities.Proposal, error) {
return g.pStore.GetByID(ctx, id)
}
func (g *Governance) GetProposalByReference(ctx context.Context, ref string) (entities.Proposal, error) {
return g.pStore.GetByReference(ctx, ref)
}
func (g *Governance) GetProposals(ctx context.Context, inState *entities.ProposalState, partyID *string, proposalType *entities.ProposalType,
pagination entities.CursorPagination,
) ([]entities.Proposal, entities.PageInfo, error) {
return g.pStore.Get(ctx, inState, partyID, proposalType, pagination)
}
func (g *Governance) ObserveProposals(ctx context.Context, retries int, partyID *string) (<-chan []entities.Proposal, uint64) {
ch, ref := g.pObserver.Observe(ctx,
retries,
func(o entities.Proposal) bool { return partyID == nil || o.PartyID.String() == *partyID })
return ch, ref
}
func (g *Governance) AddVote(ctx context.Context, v entities.Vote) error {
err := g.vStore.Add(ctx, v)
if err != nil {
return err
}
g.vObserver.Notify([]entities.Vote{v})
return nil
}
func (g *Governance) GetYesVotesForProposal(ctx context.Context, proposalID string) ([]entities.Vote, error) {
return g.vStore.GetYesVotesForProposal(ctx, proposalID)
}
func (g *Governance) GetNoVotesForProposal(ctx context.Context, proposalID string) ([]entities.Vote, error) {
return g.vStore.GetNoVotesForProposal(ctx, proposalID)
}
func (g *Governance) GetVotesByParty(ctx context.Context, partyID string) ([]entities.Vote, error) {
return g.vStore.GetByParty(ctx, partyID)
}
func (g *Governance) GetByPartyConnection(ctx context.Context, partyID string, pagination entities.CursorPagination) ([]entities.Vote, entities.PageInfo, error) {
return g.vStore.GetByPartyConnection(ctx, partyID, pagination)
}
func (g *Governance) GetConnection(
ctx context.Context,
proposalID, partyID *string,
pagination entities.CursorPagination,
) ([]entities.Vote, entities.PageInfo, error) {
return g.vStore.GetConnection(ctx, proposalID, partyID, pagination)
}
func (g *Governance) GetVotes(ctx context.Context, proposalID, partyID *string, value *entities.VoteValue) ([]entities.Vote, error) {
return g.vStore.Get(ctx, proposalID, partyID, value)
}
func (g *Governance) ObservePartyVotes(ctx context.Context, retries int, partyID string) (<-chan []entities.Vote, uint64) {
ch, ref := g.vObserver.Observe(ctx,
retries,
func(o entities.Vote) bool { return o.PartyID.String() == partyID })
return ch, ref
}
func (g *Governance) ObserveProposalVotes(ctx context.Context, retries int, proposalID string) (<-chan []entities.Vote, uint64) {
ch, ref := g.vObserver.Observe(ctx,
retries,
func(o entities.Vote) bool { return o.PartyID.String() == proposalID })
return ch, ref
}