-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* step request * making the refactoring working again * lint * using erc-1271 instead of ecrecover for offchain voting * new dependency * fix subgraph * fix subgraph * fix batch voting test * make step retreval flags merkle root specific * reduce require msg and add events * add check so you can only request a step during the grace period * set flag
- Loading branch information
Showing
12 changed files
with
441 additions
and
227 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
pragma solidity ^0.8.0; | ||
pragma experimental ABIEncoderV2; | ||
|
||
// SPDX-License-Identifier: MIT | ||
|
||
import "../../core/DaoRegistry.sol"; | ||
import "../../extensions/bank/Bank.sol"; | ||
import "../../core/DaoConstants.sol"; | ||
import "../../guards/MemberGuard.sol"; | ||
import "../../guards/AdapterGuard.sol"; | ||
import "../../utils/Signatures.sol"; | ||
import "../interfaces/IVoting.sol"; | ||
import "./Voting.sol"; | ||
import "./KickBadReporterAdapter.sol"; | ||
import "./SnapshotProposalContract.sol"; | ||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol"; | ||
import "@openzeppelin/contracts/utils/cryptography/SignatureChecker.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 OffchainVotingHashContract is DaoConstants { | ||
string public constant VOTE_RESULT_NODE_TYPE = | ||
"Message(uint64 timestamp,uint88 nbYes,uint88 nbNo,uint32 index,uint32 choice,bytes32 proposalId)"; | ||
string public constant VOTE_RESULT_ROOT_TYPE = "Message(bytes32 root)"; | ||
bytes32 public constant VOTE_RESULT_NODE_TYPEHASH = | ||
keccak256(abi.encodePacked(VOTE_RESULT_NODE_TYPE)); | ||
bytes32 public constant VOTE_RESULT_ROOT_TYPEHASH = | ||
keccak256(abi.encodePacked(VOTE_RESULT_ROOT_TYPE)); | ||
|
||
bytes32 constant VotingPeriod = keccak256("offchainvoting.votingPeriod"); | ||
bytes32 constant GracePeriod = keccak256("offchainvoting.gracePeriod"); | ||
bytes32 constant FallbackThreshold = | ||
keccak256("offchainvoting.fallbackThreshold"); | ||
|
||
mapping(address => mapping(bytes32 => mapping(uint256 => uint256))) flags; | ||
|
||
SnapshotProposalContract public snapshotContract; | ||
|
||
struct VoteStepParams { | ||
uint256 previousYes; | ||
uint256 previousNo; | ||
bytes32 proposalId; | ||
} | ||
|
||
struct VoteResultNode { | ||
uint32 choice; | ||
uint64 index; | ||
uint64 timestamp; | ||
uint88 nbNo; | ||
uint88 nbYes; | ||
bytes sig; | ||
bytes32 proposalId; | ||
bytes32[] proof; | ||
} | ||
|
||
constructor(SnapshotProposalContract _spc) { | ||
require(address(_spc) != address(0x0), "snapshot proposal"); | ||
snapshotContract = _spc; | ||
} | ||
|
||
function hashResultRoot( | ||
DaoRegistry dao, | ||
address actionId, | ||
bytes32 resultRoot | ||
) public view returns (bytes32) { | ||
return | ||
keccak256( | ||
abi.encodePacked( | ||
"\x19\x01", | ||
snapshotContract.DOMAIN_SEPARATOR(dao, actionId), | ||
keccak256(abi.encode(VOTE_RESULT_ROOT_TYPEHASH, resultRoot)) | ||
) | ||
); | ||
} | ||
|
||
function hashVotingResultNode(VoteResultNode memory node) | ||
public | ||
pure | ||
returns (bytes32) | ||
{ | ||
return | ||
keccak256( | ||
abi.encode( | ||
VOTE_RESULT_NODE_TYPEHASH, | ||
node.timestamp, | ||
node.nbYes, | ||
node.nbNo, | ||
node.index, | ||
node.choice, | ||
node.proposalId | ||
) | ||
); | ||
} | ||
|
||
function nodeHash( | ||
DaoRegistry dao, | ||
address actionId, | ||
VoteResultNode memory node | ||
) public view returns (bytes32) { | ||
return | ||
keccak256( | ||
abi.encodePacked( | ||
"\x19\x01", | ||
snapshotContract.DOMAIN_SEPARATOR(dao, actionId), | ||
hashVotingResultNode(node) | ||
) | ||
); | ||
} | ||
|
||
function hasVoted( | ||
DaoRegistry dao, | ||
address actionId, | ||
address voter, | ||
uint64 timestamp, | ||
bytes32 proposalId, | ||
uint32 choiceIdx, | ||
bytes memory sig | ||
) public view returns (bool) { | ||
bytes32 voteHash = | ||
snapshotContract.hashVote( | ||
dao, | ||
actionId, | ||
SnapshotProposalContract.VoteMessage( | ||
timestamp, | ||
SnapshotProposalContract.VotePayload(choiceIdx, proposalId) | ||
) | ||
); | ||
|
||
return SignatureChecker.isValidSignatureNow(voter, voteHash, sig); | ||
} | ||
|
||
function stringToUint(string memory s) | ||
public | ||
pure | ||
returns (bool success, uint256 result) | ||
{ | ||
bytes memory b = bytes(s); | ||
result = 0; | ||
success = false; | ||
for (uint256 i = 0; i < b.length; i++) { | ||
if (uint8(b[i]) >= 48 && uint8(b[i]) <= 57) { | ||
result = result * 10 + (uint8(b[i]) - 48); | ||
success = true; | ||
} else { | ||
result = 0; | ||
success = false; | ||
break; | ||
} | ||
} | ||
return (success, result); | ||
} | ||
|
||
function checkStep( | ||
DaoRegistry dao, | ||
address actionId, | ||
OffchainVotingHashContract.VoteResultNode memory node, | ||
uint256 snapshot, | ||
VoteStepParams memory params | ||
) public view returns (bool) { | ||
address account = dao.getMemberAddress(node.index); | ||
address voter = dao.getPriorDelegateKey(account, snapshot); | ||
BankExtension bank = BankExtension(dao.getExtensionAddress(BANK)); | ||
uint256 weight = bank.getPriorAmount(account, UNITS, snapshot); | ||
|
||
if (node.choice == 0) { | ||
if (params.previousYes != node.nbYes) { | ||
return true; | ||
} else if (params.previousNo != node.nbNo) { | ||
return true; | ||
} | ||
} | ||
|
||
if ( | ||
hasVoted( | ||
dao, | ||
actionId, | ||
voter, | ||
node.timestamp, | ||
node.proposalId, | ||
1, | ||
node.sig | ||
) | ||
) { | ||
if (params.previousYes + weight != node.nbYes) { | ||
return true; | ||
} else if (params.previousNo != node.nbNo) { | ||
return true; | ||
} | ||
} | ||
if ( | ||
hasVoted( | ||
dao, | ||
actionId, | ||
voter, | ||
node.timestamp, | ||
node.proposalId, | ||
2, | ||
node.sig | ||
) | ||
) { | ||
if (params.previousYes != node.nbYes) { | ||
return true; | ||
} else if (params.previousNo + weight != node.nbNo) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.