-
Notifications
You must be signed in to change notification settings - Fork 110
/
DaoRegistry.sol
860 lines (757 loc) · 28.8 KB
/
DaoRegistry.sol
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
import "../guards/AdapterGuard.sol";
import "../guards/MemberGuard.sol";
import "../extensions/IExtension.sol";
import "../helpers/DaoHelper.sol";
/**
MIT License
Copyright (c) 2020 Openlaw
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
contract DaoRegistry is MemberGuard, AdapterGuard {
/**
* EVENTS
*/
event SubmittedProposal(bytes32 proposalId, uint256 flags);
event SponsoredProposal(
bytes32 proposalId,
uint256 flags,
address votingAdapter
);
event ProcessedProposal(bytes32 proposalId, uint256 flags);
event AdapterAdded(
bytes32 adapterId,
address adapterAddress,
uint256 flags
);
event AdapterRemoved(bytes32 adapterId);
event ExtensionAdded(bytes32 extensionId, address extensionAddress);
event ExtensionRemoved(bytes32 extensionId);
event UpdateDelegateKey(address memberAddress, address newDelegateKey);
event ConfigurationUpdated(bytes32 key, uint256 value);
event AddressConfigurationUpdated(bytes32 key, address value);
enum DaoState {
CREATION,
READY
}
enum MemberFlag {
EXISTS,
JAILED
}
enum ProposalFlag {
EXISTS,
SPONSORED,
PROCESSED
}
enum AclFlag {
REPLACE_ADAPTER,
SUBMIT_PROPOSAL,
UPDATE_DELEGATE_KEY,
SET_CONFIGURATION,
ADD_EXTENSION,
REMOVE_EXTENSION,
NEW_MEMBER,
JAIL_MEMBER
}
/**
* STRUCTURES
*/
struct Proposal {
/// the structure to track all the proposals in the DAO
address adapterAddress; /// the adapter address that called the functions to change the DAO state
uint256 flags; /// flags to track the state of the proposal: exist, sponsored, processed, canceled, etc.
}
struct Member {
/// the structure to track all the members in the DAO
uint256 flags; /// flags to track the state of the member: exists, etc
}
struct Checkpoint {
/// A checkpoint for marking number of votes from a given block
uint96 fromBlock;
uint160 amount;
}
struct DelegateCheckpoint {
/// A checkpoint for marking the delegate key for a member from a given block
uint96 fromBlock;
address delegateKey;
}
struct AdapterEntry {
bytes32 id;
uint256 acl;
}
struct ExtensionEntry {
bytes32 id;
mapping(address => uint256) acl;
bool deleted;
}
/**
* PUBLIC VARIABLES
*/
/// @notice internally tracks deployment under eip-1167 proxy pattern
bool public initialized = false;
/// @notice The dao state starts as CREATION and is changed to READY after the finalizeDao call
DaoState public state;
/// @notice The map to track all members of the DAO with their existing flags
mapping(address => Member) public members;
/// @notice The list of members
address[] private _members;
/// @notice delegate key => member address mapping
mapping(address => address) public memberAddressesByDelegatedKey;
/// @notice The map that keeps track of all proposasls submitted to the DAO
mapping(bytes32 => Proposal) public proposals;
/// @notice The map that tracks the voting adapter address per proposalId: proposalId => adapterAddress
mapping(bytes32 => address) public votingAdapter;
/// @notice The map that keeps track of all adapters registered in the DAO: sha3(adapterId) => adapterAddress
mapping(bytes32 => address) public adapters;
/// @notice The inverse map to get the adapter id based on its address
mapping(address => AdapterEntry) public inverseAdapters;
/// @notice The map that keeps track of all extensions registered in the DAO: sha3(extId) => extAddress
mapping(bytes32 => address) public extensions;
/// @notice The inverse map to get the extension id based on its address
mapping(address => ExtensionEntry) public inverseExtensions;
/// @notice The map that keeps track of configuration parameters for the DAO and adapters: sha3(configId) => numericValue
mapping(bytes32 => uint256) public mainConfiguration;
/// @notice The map to track all the configuration of type Address: sha3(configId) => addressValue
mapping(bytes32 => address) public addressConfiguration;
/// @notice controls the lock mechanism using the block.number
uint256 public lockedAt;
/**
* INTERNAL VARIABLES
*/
/// @notice memberAddress => checkpointNum => DelegateCheckpoint
mapping(address => mapping(uint32 => DelegateCheckpoint)) _checkpoints;
/// @notice memberAddress => numDelegateCheckpoints
mapping(address => uint32) _numCheckpoints;
/// @notice Clonable contract must have an empty constructor
constructor() {}
/**
* @notice Initialises the DAO
* @dev Involves initialising available tokens, checkpoints, and membership of creator
* @dev Can only be called once
* @param creator The DAO's creator, who will be an initial member
* @param payer The account which paid for the transaction to create the DAO, who will be an initial member
*/
//slither-disable-next-line reentrancy-no-eth
function initialize(address creator, address payer) external {
require(!initialized, "dao already initialized");
initialized = true;
potentialNewMember(msg.sender);
potentialNewMember(creator);
potentialNewMember(payer);
}
/**
* ACCESS CONTROL
*/
/**
* @dev Sets the state of the dao to READY
*/
function finalizeDao() external {
require(
isActiveMember(this, msg.sender) || isAdapter(msg.sender),
"not allowed to finalize"
);
state = DaoState.READY;
}
/**
* @notice Contract lock strategy to lock only the caller is an adapter or extension.
*/
function lockSession() external {
if (isAdapter(msg.sender) || isExtension(msg.sender)) {
lockedAt = block.number;
}
}
/**
* @notice Contract lock strategy to release the lock only the caller is an adapter or extension.
*/
function unlockSession() external {
if (isAdapter(msg.sender) || isExtension(msg.sender)) {
lockedAt = 0;
}
}
/**
* CONFIGURATIONS
*/
/**
* @notice Sets a configuration value
* @dev Changes the value of a key in the configuration mapping
* @param key The configuration key for which the value will be set
* @param value The value to set the key
*/
function setConfiguration(
bytes32 key,
uint256 value
) external hasAccess(this, AclFlag.SET_CONFIGURATION) {
mainConfiguration[key] = value;
emit ConfigurationUpdated(key, value);
}
/**
* @notice Sets an configuration value
* @dev Changes the value of a key in the configuration mapping
* @param key The configuration key for which the value will be set
* @param value The value to set the key
*/
function setAddressConfiguration(
bytes32 key,
address value
) external hasAccess(this, AclFlag.SET_CONFIGURATION) {
addressConfiguration[key] = value;
emit AddressConfigurationUpdated(key, value);
}
/**
* @return The configuration value of a particular key
* @param key The key to look up in the configuration mapping
*/
function getConfiguration(bytes32 key) external view returns (uint256) {
return mainConfiguration[key];
}
/**
* @return The configuration value of a particular key
* @param key The key to look up in the configuration mapping
*/
function getAddressConfiguration(
bytes32 key
) external view returns (address) {
return addressConfiguration[key];
}
/**
* ADAPTERS
*/
/**
* @notice Replaces an adapter in the registry in a single step.
* @notice It handles addition and removal of adapters as special cases.
* @dev It removes the current adapter if the adapterId maps to an existing adapter address.
* @dev It adds an adapter if the adapterAddress parameter is not zeroed.
* @param adapterId The unique identifier of the adapter
* @param adapterAddress The address of the new adapter or zero if it is a removal operation
* @param acl The flags indicating the access control layer or permissions of the new adapter
* @param keys The keys indicating the adapter configuration names.
* @param values The values indicating the adapter configuration values.
*/
function replaceAdapter(
bytes32 adapterId,
address adapterAddress,
uint128 acl,
bytes32[] calldata keys,
uint256[] calldata values
) external hasAccess(this, AclFlag.REPLACE_ADAPTER) {
require(adapterId != bytes32(0), "adapterId must not be empty");
address currentAdapterAddr = adapters[adapterId];
if (currentAdapterAddr != address(0x0)) {
delete inverseAdapters[currentAdapterAddr];
delete adapters[adapterId];
emit AdapterRemoved(adapterId);
}
for (uint256 i = 0; i < keys.length; i++) {
bytes32 key = keys[i];
uint256 value = values[i];
mainConfiguration[key] = value;
emit ConfigurationUpdated(key, value);
}
if (adapterAddress != address(0x0)) {
require(
inverseAdapters[adapterAddress].id == bytes32(0),
"adapterAddress already in use"
);
adapters[adapterId] = adapterAddress;
inverseAdapters[adapterAddress].id = adapterId;
inverseAdapters[adapterAddress].acl = acl;
emit AdapterAdded(adapterId, adapterAddress, acl);
}
}
/**
* @notice Looks up if there is an adapter of a given address
* @return Whether or not the address is an adapter
* @param adapterAddress The address to look up
*/
function isAdapter(address adapterAddress) public view returns (bool) {
return inverseAdapters[adapterAddress].id != bytes32(0);
}
/**
* @notice Checks if an adapter has a given ACL flag
* @return Whether or not the given adapter has the given flag set
* @param adapterAddress The address to look up
* @param flag The ACL flag to check against the given address
*/
function hasAdapterAccess(
address adapterAddress,
AclFlag flag
) external view returns (bool) {
return
DaoHelper.getFlag(inverseAdapters[adapterAddress].acl, uint8(flag));
}
/**
* @return The address of a given adapter ID
* @param adapterId The ID to look up
*/
function getAdapterAddress(
bytes32 adapterId
) external view returns (address) {
require(adapters[adapterId] != address(0), "adapter not found");
return adapters[adapterId];
}
/**
* EXTENSIONS
*/
/**
* @notice Adds a new extension to the registry
* @param extensionId The unique identifier of the new extension
* @param extension The address of the extension
*/
// slither-disable-next-line reentrancy-events
function addExtension(
bytes32 extensionId,
IExtension extension
) external hasAccess(this, AclFlag.ADD_EXTENSION) {
require(extensionId != bytes32(0), "extension id must not be empty");
require(
extensions[extensionId] == address(0x0),
"extensionId already in use"
);
require(
!inverseExtensions[address(extension)].deleted,
"extension can not be re-added"
);
extensions[extensionId] = address(extension);
inverseExtensions[address(extension)].id = extensionId;
emit ExtensionAdded(extensionId, address(extension));
}
// v1.0.6 signature
function addExtension(bytes32, IExtension, address) external {
revert("not implemented");
}
/**
* @notice Removes an adapter from the registry
* @param extensionId The unique identifier of the extension
*/
function removeExtension(
bytes32 extensionId
) external hasAccess(this, AclFlag.REMOVE_EXTENSION) {
require(extensionId != bytes32(0), "extensionId must not be empty");
address extensionAddress = extensions[extensionId];
require(extensionAddress != address(0x0), "extensionId not registered");
ExtensionEntry storage extEntry = inverseExtensions[extensionAddress];
extEntry.deleted = true;
//slither-disable-next-line mapping-deletion
delete extensions[extensionId];
emit ExtensionRemoved(extensionId);
}
/**
* @notice Looks up if there is an extension of a given address
* @return Whether or not the address is an extension
* @param extensionAddr The address to look up
*/
function isExtension(address extensionAddr) public view returns (bool) {
return inverseExtensions[extensionAddr].id != bytes32(0);
}
/**
* @notice It sets the ACL flags to an Adapter to make it possible to access specific functions of an Extension.
*/
function setAclToExtensionForAdapter(
address extensionAddress,
address adapterAddress,
uint256 acl
) external hasAccess(this, AclFlag.ADD_EXTENSION) {
require(isAdapter(adapterAddress), "not an adapter");
require(isExtension(extensionAddress), "not an extension");
inverseExtensions[extensionAddress].acl[adapterAddress] = acl;
}
/**
* @notice Checks if an adapter has a given ACL flag
* @return Whether or not the given adapter has the given flag set
* @param adapterAddress The address to look up
* @param flag The ACL flag to check against the given address
*/
function hasAdapterAccessToExtension(
address adapterAddress,
address extensionAddress,
uint8 flag
) external view returns (bool) {
return
isAdapter(adapterAddress) &&
DaoHelper.getFlag(
inverseExtensions[extensionAddress].acl[adapterAddress],
uint8(flag)
);
}
/**
* @return The address of a given extension Id
* @param extensionId The ID to look up
*/
function getExtensionAddress(
bytes32 extensionId
) external view returns (address) {
require(extensions[extensionId] != address(0), "extension not found");
return extensions[extensionId];
}
/**
* PROPOSALS
*/
/**
* @notice Submit proposals to the DAO registry
*/
function submitProposal(
bytes32 proposalId
) external hasAccess(this, AclFlag.SUBMIT_PROPOSAL) {
require(proposalId != bytes32(0), "invalid proposalId");
require(
!getProposalFlag(proposalId, ProposalFlag.EXISTS),
"proposalId must be unique"
);
proposals[proposalId] = Proposal(msg.sender, 1); // 1 means that only the first flag is being set i.e. EXISTS
emit SubmittedProposal(proposalId, 1);
}
/**
* @notice Sponsor proposals that were submitted to the DAO registry
* @dev adds SPONSORED to the proposal flag
* @param proposalId The ID of the proposal to sponsor
* @param sponsoringMember The member who is sponsoring the proposal
*/
function sponsorProposal(
bytes32 proposalId,
address sponsoringMember,
address votingAdapterAddr
) external onlyMember2(this, sponsoringMember) {
// also checks if the flag was already set
Proposal storage proposal = _setProposalFlag(
proposalId,
ProposalFlag.SPONSORED
);
uint256 flags = proposal.flags;
require(
proposal.adapterAddress == msg.sender,
"only the adapter that submitted the proposal can sponsor it"
);
require(
!DaoHelper.getFlag(flags, uint8(ProposalFlag.PROCESSED)),
"proposal already processed"
);
votingAdapter[proposalId] = votingAdapterAddr;
emit SponsoredProposal(proposalId, flags, votingAdapterAddr);
}
/**
* @notice Mark a proposal as processed in the DAO registry
* @param proposalId The ID of the proposal that is being processed
*/
function processProposal(bytes32 proposalId) external {
Proposal storage proposal = _setProposalFlag(
proposalId,
ProposalFlag.PROCESSED
);
require(proposal.adapterAddress == msg.sender, "err::adapter mismatch");
uint256 flags = proposal.flags;
emit ProcessedProposal(proposalId, flags);
}
/**
* @notice Sets a flag of a proposal
* @dev Reverts if the proposal is already processed
* @param proposalId The ID of the proposal to be changed
* @param flag The flag that will be set on the proposal
*/
function _setProposalFlag(
bytes32 proposalId,
ProposalFlag flag
) internal returns (Proposal storage) {
Proposal storage proposal = proposals[proposalId];
uint256 flags = proposal.flags;
require(
DaoHelper.getFlag(flags, uint8(ProposalFlag.EXISTS)),
"proposal does not exist for this dao"
);
require(
proposal.adapterAddress == msg.sender,
"invalid adapter try to set flag"
);
require(!DaoHelper.getFlag(flags, uint8(flag)), "flag already set");
flags = DaoHelper.setFlag(flags, uint8(flag), true);
proposals[proposalId].flags = flags;
return proposals[proposalId];
}
/**
* @return Whether or not a flag is set for a given proposal
* @param proposalId The proposal to check against flag
* @param flag The flag to check in the proposal
*/
function getProposalFlag(
bytes32 proposalId,
ProposalFlag flag
) public view returns (bool) {
return DaoHelper.getFlag(proposals[proposalId].flags, uint8(flag));
}
/**
* MEMBERS
*/
/**
* @notice Sets true for the JAILED flag.
* @param memberAddress The address of the member to update the flag.
*/
function jailMember(
address memberAddress
) external hasAccess(this, AclFlag.JAIL_MEMBER) {
require(memberAddress != address(0x0), "invalid member address");
Member storage member = members[memberAddress];
require(
DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS)),
"member does not exist"
);
member.flags = DaoHelper.setFlag(
member.flags,
uint8(MemberFlag.JAILED),
true
);
}
/**
* @notice Sets false for the JAILED flag.
* @param memberAddress The address of the member to update the flag.
*/
function unjailMember(
address memberAddress
) external hasAccess(this, AclFlag.JAIL_MEMBER) {
require(memberAddress != address(0x0), "invalid member address");
Member storage member = members[memberAddress];
require(
DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS)),
"member does not exist"
);
member.flags = DaoHelper.setFlag(
member.flags,
uint8(MemberFlag.JAILED),
false
);
}
/**
* @notice Checks if a given member address is not jailed.
* @param memberAddress The address of the member to check the flag.
*/
function notJailed(address memberAddress) external view returns (bool) {
return
!DaoHelper.getFlag(
members[memberAddress].flags,
uint8(MemberFlag.JAILED)
);
}
/**
* @notice Registers a member address in the DAO if it is not registered or invalid.
* @notice A potential new member is a member that holds no shares, and its registration still needs to be voted on.
*/
function potentialNewMember(
address memberAddress
) public hasAccess(this, AclFlag.NEW_MEMBER) {
require(memberAddress != address(0x0), "invalid member address");
Member storage member = members[memberAddress];
if (!DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS))) {
require(
memberAddressesByDelegatedKey[memberAddress] == address(0x0),
"member address already taken as delegated key"
);
member.flags = DaoHelper.setFlag(
member.flags,
uint8(MemberFlag.EXISTS),
true
);
memberAddressesByDelegatedKey[memberAddress] = memberAddress;
_members.push(memberAddress);
}
address bankAddress = extensions[DaoHelper.BANK];
if (bankAddress != address(0x0)) {
BankExtension bank = BankExtension(bankAddress);
if (bank.balanceOf(memberAddress, DaoHelper.MEMBER_COUNT) == 0) {
bank.addToBalance(
this,
memberAddress,
DaoHelper.MEMBER_COUNT,
1
);
}
}
}
/**
* @return Whether or not a given address is a member of the DAO.
* @dev it will resolve by delegate key, not member address.
* @param addr The address to look up
*/
function isMember(address addr) external view returns (bool) {
address memberAddress = memberAddressesByDelegatedKey[addr];
return getMemberFlag(memberAddress, MemberFlag.EXISTS);
}
/**
* @return Whether or not a flag is set for a given member
* @param memberAddress The member to check against flag
* @param flag The flag to check in the member
*/
function getMemberFlag(
address memberAddress,
MemberFlag flag
) public view returns (bool) {
return DaoHelper.getFlag(members[memberAddress].flags, uint8(flag));
}
/**
* @notice Returns the number of members in the registry.
*/
function getNbMembers() external view returns (uint256) {
return _members.length;
}
/**
* @notice Returns the member address for the given index.
*/
function getMemberAddress(uint256 index) external view returns (address) {
return _members[index];
}
/**
* DELEGATE
*/
/**
* @notice Updates the delegate key of a member
* @param memberAddr The member doing the delegation
* @param newDelegateKey The member who is being delegated to
*/
function updateDelegateKey(
address memberAddr,
address newDelegateKey
) external hasAccess(this, AclFlag.UPDATE_DELEGATE_KEY) {
require(newDelegateKey != address(0x0), "newDelegateKey cannot be 0");
// skip checks if member is setting the delegate key to their member address
if (newDelegateKey != memberAddr) {
require(
// newDelegate must not be delegated to
memberAddressesByDelegatedKey[newDelegateKey] == address(0x0),
"cannot overwrite existing delegated keys"
);
} else {
require(
memberAddressesByDelegatedKey[memberAddr] == address(0x0),
"address already taken as delegated key"
);
}
Member storage member = members[memberAddr];
require(
DaoHelper.getFlag(member.flags, uint8(MemberFlag.EXISTS)),
"member does not exist"
);
// Reset the delegation of the previous delegate
memberAddressesByDelegatedKey[
getCurrentDelegateKey(memberAddr)
] = address(0x0);
memberAddressesByDelegatedKey[newDelegateKey] = memberAddr;
_createNewDelegateCheckpoint(memberAddr, newDelegateKey);
emit UpdateDelegateKey(memberAddr, newDelegateKey);
}
/**
* @param checkAddr The address to check for a delegate
* @return the delegated address or the checked address if it is not a delegate
*/
function getAddressIfDelegated(
address checkAddr
) external view returns (address) {
address delegatedKey = memberAddressesByDelegatedKey[checkAddr];
return delegatedKey == address(0x0) ? checkAddr : delegatedKey;
}
/**
* @param memberAddr The member whose delegate will be returned
* @return the delegate key at the current time for a member
*/
function getCurrentDelegateKey(
address memberAddr
) public view returns (address) {
uint32 nCheckpoints = _numCheckpoints[memberAddr];
return
nCheckpoints > 0
? _checkpoints[memberAddr][nCheckpoints - 1].delegateKey
: memberAddr;
}
/**
* @param memberAddr The member address to look up
* @return The delegate key address for memberAddr at the second last checkpoint number
*/
function getPreviousDelegateKey(
address memberAddr
) external view returns (address) {
uint32 nCheckpoints = _numCheckpoints[memberAddr];
return
nCheckpoints > 1
? _checkpoints[memberAddr][nCheckpoints - 2].delegateKey
: memberAddr;
}
/**
* @notice Determine the prior number of votes for an account as of a block number
* @dev Block number must be a finalized block or else this function will revert to prevent misinformation.
* @param memberAddr The address of the account to check
* @param blockNumber The block number to get the vote balance at
* @return The delegate key of the member
*/
function getPriorDelegateKey(
address memberAddr,
uint256 blockNumber
) external view returns (address) {
require(blockNumber < block.number, "getPriorDelegateKey: NYD");
uint32 nCheckpoints = _numCheckpoints[memberAddr];
if (nCheckpoints == 0) {
return memberAddr;
}
// First check most recent balance
if (
_checkpoints[memberAddr][nCheckpoints - 1].fromBlock <= blockNumber
) {
return _checkpoints[memberAddr][nCheckpoints - 1].delegateKey;
}
// Next check implicit zero balance
if (_checkpoints[memberAddr][0].fromBlock > blockNumber) {
return memberAddr;
}
uint32 lower = 0;
uint32 upper = nCheckpoints - 1;
while (upper > lower) {
uint32 center = upper - (upper - lower) / 2; // ceil, avoiding overflow
DelegateCheckpoint memory cp = _checkpoints[memberAddr][center];
if (cp.fromBlock == blockNumber) {
return cp.delegateKey;
} else if (cp.fromBlock < blockNumber) {
lower = center;
} else {
upper = center - 1;
}
}
return _checkpoints[memberAddr][lower].delegateKey;
}
/**
* @notice Creates a new delegate checkpoint of a certain member
* @param member The member whose delegate checkpoints will be added to
* @param newDelegateKey The delegate key that will be written into the new checkpoint
*/
function _createNewDelegateCheckpoint(
address member,
address newDelegateKey
) internal {
uint32 nCheckpoints = _numCheckpoints[member];
// The only condition that we should allow the deletegaKey upgrade
// is when the block.number exactly matches the fromBlock value.
// Anything different from that should generate a new checkpoint.
if (
//slither-disable-next-line incorrect-equality
nCheckpoints > 0 &&
_checkpoints[member][nCheckpoints - 1].fromBlock == block.number
) {
_checkpoints[member][nCheckpoints - 1].delegateKey = newDelegateKey;
} else {
_checkpoints[member][nCheckpoints] = DelegateCheckpoint(
uint96(block.number),
newDelegateKey
);
_numCheckpoints[member] = nCheckpoints + 1;
}
}
}