Skip to content

Commit

Permalink
step request (#311)
Browse files Browse the repository at this point in the history
* 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
adridadou authored Jul 7, 2021
1 parent aac07f7 commit 53e2b0a
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 227 deletions.
380 changes: 164 additions & 216 deletions contracts/adapters/voting/OffchainVoting.sol

Large diffs are not rendered by default.

233 changes: 233 additions & 0 deletions contracts/adapters/voting/OffchainVotingHash.sol
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;
}
}
1 change: 1 addition & 0 deletions contracts/adapters/voting/SnapshotProposalContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ contract SnapshotProposalContract {
struct ProposalMessage {
uint256 timestamp;
bytes32 spaceHash;
address submitter;
ProposalPayload payload;
bytes sig;
}
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test": "test"
},
"dependencies": {
"@openzeppelin/contracts": "^4.0.0",
"@openzeppelin/contracts": "^4.1.0",
"@truffle/contract": "^4.3.13",
"@truffle/hdwallet-provider": "1.2.2",
"eth-sig-util": "^3.0.0",
Expand Down
5 changes: 3 additions & 2 deletions subgraph/mappings/helpers/vote-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ export function loadProposalAndSaveVoteResults(
proposal.startingTime = voteResults.value5;
proposal.gracePeriodStartingTime = voteResults.value6;
proposal.isChallenged = voteResults.value7;
proposal.stepRequested = voteResults.value8;
// @todo its a mapping, not generated in schema
// proposal.fallbackVotes = voteResults.value10;
proposal.forceFailed = voteResults.value8;
proposal.fallbackVotesCount = voteResults.value9;
proposal.forceFailed = voteResults.value9;
proposal.fallbackVotesCount = voteResults.value10;

proposal.votingState = voteState.toString();
proposal.votingResult = voteId;
Expand Down
1 change: 1 addition & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type Proposal @entity {
startingTime: BigInt
gracePeriodStartingTime: BigInt
isChallenged: Boolean
stepRequested: BigInt
# fallbackVotes: BigInt
forceFailed: Boolean
fallbackVotesCount: BigInt
Expand Down
1 change: 1 addition & 0 deletions test/adapters/batch-voting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe("Adapter - BatchVoting", () => {
timestamp: Math.floor(new Date().getTime() / 1000),
space,
payload: proposalPayload,
submitter: this.members[0].address,
};

//signer for myAccount (its private key)
Expand Down
9 changes: 8 additions & 1 deletion test/adapters/offchain.voting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const {
takeChainSnapshot,
revertChainSnapshot,
web3,
OffchainVotingHashContract,
} = require("../../utils/OZTestUtil.js");

const {
Expand Down Expand Up @@ -95,6 +96,7 @@ const onboardMember = async (dao, voting, onboarding, bank, index) => {
timestamp: Math.floor(new Date().getTime() / 1000),
space,
payload: proposalPayload,
submitter: members[0].address,
};

//signer for myAccount (its private key)
Expand Down Expand Up @@ -167,6 +169,7 @@ const onboardMember = async (dao, voting, onboarding, bank, index) => {
dao.address,
proposalId,
voteResultTree.getHexRoot(),
members[0].address,
lastResult,
rootSig
);
Expand Down Expand Up @@ -225,6 +228,7 @@ describe("Adapter - Offchain Voting", () => {
timestamp: Math.floor(new Date().getTime() / 1000),
space: "tribute",
payload: proposalPayload,
submitter: members[0].address,
sig: "0x00",
};

Expand Down Expand Up @@ -321,7 +325,10 @@ describe("Adapter - Offchain Voting", () => {
chainId
);

const solNodeDef = await offchainVoting.VOTE_RESULT_NODE_TYPE();
const ovHashAddr = await offchainVoting.ovHash();
const ovHash = await OffchainVotingHashContract.at(ovHashAddr);

const solNodeDef = await ovHash.VOTE_RESULT_NODE_TYPE();
const jsNodeMsg = TypedDataUtils.encodeType("Message", nodeDef.types);

expect(solNodeDef).equal(jsNodeMsg);
Expand Down
1 change: 1 addition & 0 deletions utils/ContractUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const contracts = {
VotingContract: "./adapters/VotingContract",
SnapshotProposalContract: "./adapters/voting/SnapshotProposalContract",
OffchainVotingContract: "./adapters/voting/OffchainVotingContract",
OffchainVotingHashContract: "./adapters/voting/OffchainVotingHashContract",
KickBadReporterAdapter: "./adapters/voting/KickBadReporterAdapter",
BatchVotingContract: "./adapters/voting/BatchVotingContract",

Expand Down
8 changes: 8 additions & 0 deletions utils/DeploymentUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,23 @@ const configureOffchainVoting = async ({
SnapshotProposalContract,
KickBadReporterAdapter,
OffchainVotingContract,
OffchainVotingHashContract,
deployFunction,
}) => {
let snapshotProposalContract = await deployFunction(
SnapshotProposalContract,
[chainId]
);

let offchainVotingHashContract = await deployFunction(
OffchainVotingHashContract,
[snapshotProposalContract.address]
);

let handleBadReporterAdapter = await deployFunction(KickBadReporterAdapter);
let offchainVoting = await deployFunction(OffchainVotingContract, [
votingAddress,
offchainVotingHashContract.address,
snapshotProposalContract.address,
handleBadReporterAdapter.address,
offchainAdmin,
Expand Down
Loading

0 comments on commit 53e2b0a

Please sign in to comment.