This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
461 lines (417 loc) · 15.9 KB
/
dao.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package trustednode
import (
"fmt"
"math/big"
"sync"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/sync/errgroup"
"github.com/multisig-labs/gogopool-go/gogopool"
"github.com/multisig-labs/gogopool-go/utils/strings"
)
// Settings
const (
MemberAddressBatchSize = 50
MemberDetailsBatchSize = 20
)
// Proposal details
type MemberDetails struct {
Address common.Address `json:"address"`
Exists bool `json:"exists"`
ID string `json:"id"`
Url string `json:"url"`
JoinedTime uint64 `json:"joinedTime"`
LastProposalTime uint64 `json:"lastProposalTime"`
GGPBondAmount *big.Int `json:"ggpBondAmount"`
UnbondedValidatorCount uint64 `json:"unbondedValidatorCount"`
}
// Get all member details
func GetMembers(ggp *gogopool.GoGoPool, opts *bind.CallOpts) ([]MemberDetails, error) {
// Get member addresses
memberAddresses, err := GetMemberAddresses(ggp, opts)
if err != nil {
return []MemberDetails{}, err
}
// Load member details in batches
details := make([]MemberDetails, len(memberAddresses))
for bsi := 0; bsi < len(memberAddresses); bsi += MemberDetailsBatchSize {
// Get batch start & end index
msi := bsi
mei := bsi + MemberDetailsBatchSize
if mei > len(memberAddresses) {
mei = len(memberAddresses)
}
// Load details
var wg errgroup.Group
for mi := msi; mi < mei; mi++ {
mi := mi
wg.Go(func() error {
memberAddress := memberAddresses[mi]
memberDetails, err := GetMemberDetails(ggp, memberAddress, opts)
if err == nil {
details[mi] = memberDetails
}
return err
})
}
if err := wg.Wait(); err != nil {
return []MemberDetails{}, err
}
}
// Return
return details, nil
}
// Get all member addresses
func GetMemberAddresses(ggp *gogopool.GoGoPool, opts *bind.CallOpts) ([]common.Address, error) {
// Get member count
memberCount, err := GetMemberCount(ggp, opts)
if err != nil {
return []common.Address{}, err
}
// Load member addresses in batches
addresses := make([]common.Address, memberCount)
for bsi := uint64(0); bsi < memberCount; bsi += MemberAddressBatchSize {
// Get batch start & end index
msi := bsi
mei := bsi + MemberAddressBatchSize
if mei > memberCount {
mei = memberCount
}
// Load addresses
var wg errgroup.Group
for mi := msi; mi < mei; mi++ {
mi := mi
wg.Go(func() error {
address, err := GetMemberAt(ggp, mi, opts)
if err == nil {
addresses[mi] = address
}
return err
})
}
if err := wg.Wait(); err != nil {
return []common.Address{}, err
}
}
// Return
return addresses, nil
}
// Get a member's details
func GetMemberDetails(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (MemberDetails, error) {
// Data
var wg errgroup.Group
var exists bool
var id string
var url string
var joinedTime uint64
var lastProposalTime uint64
var ggpBondAmount *big.Int
var unbondedValidatorCount uint64
// Load data
wg.Go(func() error {
var err error
exists, err = GetMemberExists(ggp, memberAddress, opts)
return err
})
wg.Go(func() error {
var err error
id, err = GetMemberID(ggp, memberAddress, opts)
return err
})
wg.Go(func() error {
var err error
url, err = GetMemberUrl(ggp, memberAddress, opts)
return err
})
wg.Go(func() error {
var err error
joinedTime, err = GetMemberJoinedTime(ggp, memberAddress, opts)
return err
})
wg.Go(func() error {
var err error
lastProposalTime, err = GetMemberLastProposalTime(ggp, memberAddress, opts)
return err
})
wg.Go(func() error {
var err error
ggpBondAmount, err = GetMemberGGPBondAmount(ggp, memberAddress, opts)
return err
})
wg.Go(func() error {
var err error
unbondedValidatorCount, err = GetMemberUnbondedValidatorCount(ggp, memberAddress, opts)
return err
})
// Wait for data
if err := wg.Wait(); err != nil {
return MemberDetails{}, err
}
// Return
return MemberDetails{
Address: memberAddress,
Exists: exists,
ID: id,
Url: url,
JoinedTime: joinedTime,
LastProposalTime: lastProposalTime,
GGPBondAmount: ggpBondAmount,
UnbondedValidatorCount: unbondedValidatorCount,
}, nil
}
// Get the minimum member count
func GetMinimumMemberCount(ggp *gogopool.GoGoPool, opts *bind.CallOpts) (uint64, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return 0, err
}
minMemberCount := new(*big.Int)
if err := gogoDAONodeTrusted.Call(opts, minMemberCount, "getMemberMinRequired"); err != nil {
return 0, fmt.Errorf("Could not get trusted node DAO minimum member count: %w", err)
}
return (*minMemberCount).Uint64(), nil
}
// Get the member count
func GetMemberCount(ggp *gogopool.GoGoPool, opts *bind.CallOpts) (uint64, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return 0, err
}
memberCount := new(*big.Int)
if err := gogoDAONodeTrusted.Call(opts, memberCount, "getMemberCount"); err != nil {
return 0, fmt.Errorf("Could not get trusted node DAO member count: %w", err)
}
return (*memberCount).Uint64(), nil
}
// Get a member address by index
func GetMemberAt(ggp *gogopool.GoGoPool, index uint64, opts *bind.CallOpts) (common.Address, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return common.Address{}, err
}
memberAddress := new(common.Address)
if err := gogoDAONodeTrusted.Call(opts, memberAddress, "getMemberAt", big.NewInt(int64(index))); err != nil {
return common.Address{}, fmt.Errorf("Could not get trusted node DAO member %d address: %w", index, err)
}
return *memberAddress, nil
}
// Member details
func GetMemberExists(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (bool, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return false, err
}
exists := new(bool)
if err := gogoDAONodeTrusted.Call(opts, exists, "getMemberIsValid", memberAddress); err != nil {
return false, fmt.Errorf("Could not get trusted node DAO member %s exists status: %w", memberAddress.Hex(), err)
}
return *exists, nil
}
func GetMemberID(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (string, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return "", err
}
id := new(string)
if err := gogoDAONodeTrusted.Call(opts, id, "getMemberID", memberAddress); err != nil {
return "", fmt.Errorf("Could not get trusted node DAO member %s ID: %w", memberAddress.Hex(), err)
}
return strings.Sanitize(*id), nil
}
func GetMemberUrl(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (string, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return "", err
}
url := new(string)
if err := gogoDAONodeTrusted.Call(opts, url, "getMemberUrl", memberAddress); err != nil {
return "", fmt.Errorf("Could not get trusted node DAO member %s URL: %w", memberAddress.Hex(), err)
}
return strings.Sanitize(*url), nil
}
func GetMemberJoinedTime(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (uint64, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return 0, err
}
joinedTime := new(*big.Int)
if err := gogoDAONodeTrusted.Call(opts, joinedTime, "getMemberJoinedTime", memberAddress); err != nil {
return 0, fmt.Errorf("Could not get trusted node DAO member %s joined time: %w", memberAddress.Hex(), err)
}
return (*joinedTime).Uint64(), nil
}
func GetMemberLastProposalTime(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (uint64, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return 0, err
}
lastProposalTime := new(*big.Int)
if err := gogoDAONodeTrusted.Call(opts, lastProposalTime, "getMemberLastProposalTime", memberAddress); err != nil {
return 0, fmt.Errorf("Could not get trusted node DAO member %s last proposal time: %w", memberAddress.Hex(), err)
}
return (*lastProposalTime).Uint64(), nil
}
func GetMemberGGPBondAmount(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (*big.Int, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return nil, err
}
ggpBondAmount := new(*big.Int)
if err := gogoDAONodeTrusted.Call(opts, ggpBondAmount, "getMemberGGPBondAmount", memberAddress); err != nil {
return nil, fmt.Errorf("Could not get trusted node DAO member %s GGP bond amount: %w", memberAddress.Hex(), err)
}
return *ggpBondAmount, nil
}
func GetMemberUnbondedValidatorCount(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (uint64, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return 0, err
}
unbondedValidatorCount := new(*big.Int)
if err := gogoDAONodeTrusted.Call(opts, unbondedValidatorCount, "getMemberUnbondedValidatorCount", memberAddress); err != nil {
return 0, fmt.Errorf("Could not get trusted node DAO member %s unbonded validator count: %w", memberAddress.Hex(), err)
}
return (*unbondedValidatorCount).Uint64(), nil
}
// Get the time that a proposal for a member was executed at
func GetMemberInviteProposalExecutedTime(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (uint64, error) {
return GetMemberProposalExecutedTime(ggp, "invited", memberAddress, opts)
}
func GetMemberLeaveProposalExecutedTime(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (uint64, error) {
return GetMemberProposalExecutedTime(ggp, "leave", memberAddress, opts)
}
func GetMemberReplaceProposalExecutedTime(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (uint64, error) {
return GetMemberProposalExecutedTime(ggp, "replace", memberAddress, opts)
}
func GetMemberProposalExecutedTime(ggp *gogopool.GoGoPool, proposalType string, memberAddress common.Address, opts *bind.CallOpts) (uint64, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return 0, err
}
proposalExecutedTime := new(*big.Int)
if err := gogoDAONodeTrusted.Call(opts, proposalExecutedTime, "getMemberProposalExecutedTime", proposalType, memberAddress); err != nil {
return 0, fmt.Errorf("Could not get trusted node DAO %s proposal executed time for member %s: %w", proposalType, memberAddress.Hex(), err)
}
return (*proposalExecutedTime).Uint64(), nil
}
// Get a member's replacement address if being replaced
func GetMemberReplacementAddress(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (common.Address, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return common.Address{}, err
}
replacementAddress := new(common.Address)
if err := gogoDAONodeTrusted.Call(opts, replacementAddress, "getMemberReplacedAddress", "new", memberAddress); err != nil {
return common.Address{}, fmt.Errorf("Could not get trusted node DAO member %s replacement address: %w", memberAddress.Hex(), err)
}
return *replacementAddress, nil
}
// Get whether a member has an active challenge against them
func GetMemberIsChallenged(ggp *gogopool.GoGoPool, memberAddress common.Address, opts *bind.CallOpts) (bool, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return false, err
}
isChallenged := new(bool)
if err := gogoDAONodeTrusted.Call(opts, isChallenged, "getMemberIsChallenged", memberAddress); err != nil {
return false, fmt.Errorf("Could not get trusted node DAO member %s is challenged status: %w", memberAddress.Hex(), err)
}
return *isChallenged, nil
}
// Estimate the gas of BootstrapBool
func EstimateBootstrapBoolGas(ggp *gogopool.GoGoPool, contractName, settingPath string, value bool, opts *bind.TransactOpts) (gogopool.GasInfo, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return gogopool.GasInfo{}, err
}
return gogoDAONodeTrusted.GetTransactionGasInfo(opts, "bootstrapSettingBool", contractName, settingPath, value)
}
// Bootstrap a bool setting
func BootstrapBool(ggp *gogopool.GoGoPool, contractName, settingPath string, value bool, opts *bind.TransactOpts) (common.Hash, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return common.Hash{}, err
}
hash, err := gogoDAONodeTrusted.Transact(opts, "bootstrapSettingBool", contractName, settingPath, value)
if err != nil {
return common.Hash{}, fmt.Errorf("Could not bootstrap trusted node setting %s.%s: %w", contractName, settingPath, err)
}
return hash, nil
}
// Estimate the gas of BootstrapUint
func EstimateBootstrapUintGas(ggp *gogopool.GoGoPool, contractName, settingPath string, value *big.Int, opts *bind.TransactOpts) (gogopool.GasInfo, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return gogopool.GasInfo{}, err
}
return gogoDAONodeTrusted.GetTransactionGasInfo(opts, "bootstrapSettingUint", contractName, settingPath, value)
}
// Bootstrap a uint256 setting
func BootstrapUint(ggp *gogopool.GoGoPool, contractName, settingPath string, value *big.Int, opts *bind.TransactOpts) (common.Hash, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return common.Hash{}, err
}
hash, err := gogoDAONodeTrusted.Transact(opts, "bootstrapSettingUint", contractName, settingPath, value)
if err != nil {
return common.Hash{}, fmt.Errorf("Could not bootstrap trusted node setting %s.%s: %w", contractName, settingPath, err)
}
return hash, nil
}
// Estimate the gas of BootstrapMember
func EstimateBootstrapMemberGas(ggp *gogopool.GoGoPool, id, url string, nodeAddress common.Address, opts *bind.TransactOpts) (gogopool.GasInfo, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return gogopool.GasInfo{}, err
}
url = strings.Sanitize(url)
return gogoDAONodeTrusted.GetTransactionGasInfo(opts, "bootstrapMember", id, url, nodeAddress)
}
// Bootstrap a DAO member
func BootstrapMember(ggp *gogopool.GoGoPool, id, url string, nodeAddress common.Address, opts *bind.TransactOpts) (common.Hash, error) {
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return common.Hash{}, err
}
url = strings.Sanitize(url)
hash, err := gogoDAONodeTrusted.Transact(opts, "bootstrapMember", id, url, nodeAddress)
if err != nil {
return common.Hash{}, fmt.Errorf("Could not bootstrap trusted node member %s: %w", id, err)
}
return hash, nil
}
// Estimate the gas of BootstrapUpgrade
func EstimateBootstrapUpgradeGas(ggp *gogopool.GoGoPool, upgradeType, contractName, contractAbi string, contractAddress common.Address, opts *bind.TransactOpts) (gogopool.GasInfo, error) {
compressedAbi, err := gogopool.EncodeAbiStr(contractAbi)
if err != nil {
return gogopool.GasInfo{}, err
}
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return gogopool.GasInfo{}, err
}
return gogoDAONodeTrusted.GetTransactionGasInfo(opts, "bootstrapUpgrade", upgradeType, contractName, compressedAbi, contractAddress)
}
// Bootstrap a contract upgrade
func BootstrapUpgrade(ggp *gogopool.GoGoPool, upgradeType, contractName, contractAbi string, contractAddress common.Address, opts *bind.TransactOpts) (common.Hash, error) {
compressedAbi, err := gogopool.EncodeAbiStr(contractAbi)
if err != nil {
return common.Hash{}, err
}
gogoDAONodeTrusted, err := getGoGoDAONodeTrusted(ggp)
if err != nil {
return common.Hash{}, err
}
hash, err := gogoDAONodeTrusted.Transact(opts, "bootstrapUpgrade", upgradeType, contractName, compressedAbi, contractAddress)
if err != nil {
return common.Hash{}, fmt.Errorf("Could not bootstrap contract '%s' upgrade (%s): %w", contractName, upgradeType, err)
}
return hash, nil
}
// Get contracts
var gogoDAONodeTrustedLock sync.Mutex
func getGoGoDAONodeTrusted(ggp *gogopool.GoGoPool) (*gogopool.Contract, error) {
gogoDAONodeTrustedLock.Lock()
defer gogoDAONodeTrustedLock.Unlock()
return ggp.GetContract("rocketDAONodeTrusted")
}