-
Notifications
You must be signed in to change notification settings - Fork 375
/
membstore.gno
190 lines (150 loc) · 4.33 KB
/
membstore.gno
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package membstore
import (
"errors"
"std"
"gno.land/p/demo/avl"
)
var (
ErrAlreadyMember = errors.New("address is already a member")
ErrMissingMember = errors.New("address is not a member")
ErrInvalidAddressUpdate = errors.New("invalid address update")
ErrNotGovDAO = errors.New("caller not correct govdao instance")
)
// maxRequestMembers is the maximum number of
// paginated members that can be requested
const maxRequestMembers = 50
type Option func(*MembStore)
// WithInitialMembers initializes the member store
// with an initial member list
func WithInitialMembers(members []Member) Option {
return func(store *MembStore) {
for _, m := range members {
store.members.Set(m.Address.String(), m)
store.totalVotingPower += m.VotingPower
}
}
}
// WithDAOPkgPath initializes the member store
// with a dao package path guard
func WithDAOPkgPath(daoPkgPath string) Option {
return func(store *MembStore) {
store.daoPkgPath = daoPkgPath
}
}
// MembStore implements the dao.MembStore abstraction
type MembStore struct {
daoPkgPath string // active dao pkg path, if any
members *avl.Tree // std.Address -> Member
totalVotingPower uint64 // cached value for quick lookups
}
// NewMembStore creates a new member store
func NewMembStore(opts ...Option) *MembStore {
m := &MembStore{
members: avl.NewTree(), // empty set
daoPkgPath: "", // no dao guard
totalVotingPower: 0,
}
// Apply the options
for _, opt := range opts {
opt(m)
}
return m
}
func (m *MembStore) AddMember(member Member) error {
if !m.isCallerDAORealm() {
return ErrNotGovDAO
}
// Check if the member exists
if m.IsMember(member.Address) {
return ErrAlreadyMember
}
// Add the member
m.members.Set(member.Address.String(), member)
// Update the total voting power
m.totalVotingPower += member.VotingPower
return nil
}
func (m *MembStore) UpdateMember(address std.Address, member Member) error {
if !m.isCallerDAORealm() {
return ErrNotGovDAO
}
// Get the member
oldMember, err := m.Member(address)
if err != nil {
return err
}
// Check if this is a removal request
if member.VotingPower == 0 {
m.members.Remove(address.String())
// Update the total voting power
m.totalVotingPower -= oldMember.VotingPower
return nil
}
// Check that the member wouldn't be
// overwriting an existing one
isAddressUpdate := address != member.Address
if isAddressUpdate && m.IsMember(member.Address) {
return ErrInvalidAddressUpdate
}
// Remove the old member info
// in case the address changed
if address != member.Address {
m.members.Remove(address.String())
}
// Save the new member info
m.members.Set(member.Address.String(), member)
// Update the total voting power
difference := member.VotingPower - oldMember.VotingPower
m.totalVotingPower += difference
return nil
}
func (m *MembStore) IsMember(address std.Address) bool {
_, exists := m.members.Get(address.String())
return exists
}
func (m *MembStore) Member(address std.Address) (Member, error) {
member, exists := m.members.Get(address.String())
if !exists {
return Member{}, ErrMissingMember
}
return member.(Member), nil
}
func (m *MembStore) Members(offset, count uint64) []Member {
// Calculate the left and right bounds
if count < 1 || offset >= uint64(m.members.Size()) {
return []Member{}
}
// Limit the maximum number of returned members
if count > maxRequestMembers {
count = maxRequestMembers
}
// Gather the members
members := make([]Member, 0)
m.members.IterateByOffset(
int(offset),
int(count),
func(_ string, val interface{}) bool {
member := val.(Member)
// Save the member
members = append(members, member)
return false
})
return members
}
func (m *MembStore) Size() int {
return m.members.Size()
}
// TotalPower returns the total voting power
// of the member store
func (m *MembStore) TotalPower() uint64 {
return m.totalVotingPower
}
// isCallerDAORealm returns a flag indicating if the
// current caller context is the active DAO Realm.
// We need to include a dao guard, even if the
// executor guarantees it, because
// the API of the member store is public and callable
// by anyone who has a reference to the member store instance.
func (m *MembStore) isCallerDAORealm() bool {
return m.daoPkgPath == "" || std.CurrentRealm().PkgPath() == m.daoPkgPath
}