编写提议删除条目的函数。
投票删除和黑名单
三个条目删除功能:
- 1.删除条目:通过投票确认后,目标条目将被删除。投票时间:48小时。
- 2.紧急删除条目[仅限所有者]:只能由所有者触发。通过投票确认后,目标条目将被删除。投票时间:24小时。
- 3.紧急删除图像[仅限所有者]:仅适用于图像条目。只能由所有者触发。通过投票确认后,目标条目将被删除。投票时间:4小时。
单个地址条目的五个删除导致黑名单。
首先,删除功能:
代码语言:javascript复制modifier memberOnly() {
require(whitelist[msg.sender]);
require(!blacklist[msg.sender]);
_;
}
function proposeDeletion(bytes32 _hash, string _description) memberOnly public {
require(submissionExists(_hash), "Submission must exist to be deletable");
uint256 proposalId = proposals.length ;
Proposal storage p = proposals[proposalId];
p.description = _description;
p.executed = false;
p.creationDate = now;
p.submitter = msg.sender;
p.typeFlag = 1;
p.target = _hash;
p.deadline = now 2 days;
emit ProposalAdded(proposalId, 1, _hash, _description, msg.sender);
proposalCount = proposalId 1;
}
function proposeDeletionUrgent(bytes32 _hash, string _description) onlyOwner public {
require(submissionExists(_hash), "Submission must exist to be deletable");
uint256 proposalId = proposals.length ;
Proposal storage p = proposals[proposalId];
p.description = _description;
p.executed = false;
p.creationDate = now;
p.submitter = msg.sender;
p.typeFlag = 1;
p.target = _hash;
p.deadline = now 12 hours;
emit ProposalAdded(proposalId, 1, _hash, _description, msg.sender);
proposalCount = proposalId 1;
}
function proposeDeletionUrgentImage(bytes32 _hash, string _description) onlyOwner public {
require(submissions[_hash].image == true, "Submission must be existing image");
uint256 proposalId = proposals.length ;
Proposal storage p = proposals[proposalId];
p.description = _description;
p.executed = false;
p.creationDate = now;
p.submitter = msg.sender;
p.typeFlag = 1;
p.target = _hash;
p.deadline = now 4 hours;
emit ProposalAdded(proposalId, 1, _hash, _description, msg.sender);
proposalCount = proposalId 1;
}
一旦提出,建议书就会被添加到提案列表中,并记录条目哈希所针对的条目。保存说明并添加一些默认值,并根据提案类型计算截止日期。该提案添加了事件,并且提案总数增加了。
接下来看看如何执行提案。为了可执行,提案必须有足够的票数,并且必须超过其截止日期。执行功能将接受要执行的提议的ID。没有简单的方法可以让EVM立即执行所有待处理的提案。可能有太多人要等待执行,并且他们会对DAO中的数据进行大的更改,这可能会超过以太坊块的气体限制,从而导致交易失败。构建一个可以由具有明确规则的任何人调用的手动执行功能要容易得多,因此社区可以关注需要执行的提议。
代码语言:javascript复制function executeProposal(uint256 _id) public {
Proposal storage p = proposals[_id];
require(now >= p.deadline && !p.executed);
if (p.typeFlag == 1 && p.currentResult > 0) {
assert(deleteSubmission(p.target));
}
uint256 len = p.votes.length;
for (uint i = 0; i
通过其ID获取提案,检查它是否符合未执行的要求和截止日期过期,然后如果提案的类型是删除提案且投票结果是肯定的,我们使用已经写入的删除功能,最后发出了我们添加的新事件(将其添加到合约的顶部)。assert
调用与require
语句具有相同的用途:断言通常在“断言”结果为真时使用。要求用于先决条件。在功能上它们是相同的,assert
语句的差异在它们失败时无法接受消息参数。该功能通过为该一个提案中的所有投票解锁代币而结束。
使用相同的方法添加其他类型的提案,但首先,更新deleteSubmission
函数以禁止在其帐户上有五个或更多删除的用户:这意味着他们一直在提交社区投票反对的内容。更新deleteSubmission
函数:
function deleteSubmission(bytes32 hash) internal returns (bool) {
require(submissionExists(hash), "Submission must exist to be deletable.");
Submission storage sub = submissions[hash];
sub.exists = false;
deletions[submissions[hash].submitter] = 1;
if (deletions[submissions[hash].submitter] >= 5) {
blacklistAddress(submissions[hash].submitter);
}
emit SubmissionDeleted(
sub.index,
sub.content,
sub.image,
sub.submitter
);
nonDeletedSubmissions -= 1;
return true;
}
自动将五个删除列入黑名单。但是,如果不给黑名单地址提供赎回的机会,那是不公平的。需要定义黑名单功能本身。做这两件事并将不合理的费用设置为例如0.05以太。
代码语言:javascript复制function blacklistAddress(address _offender) internal {
require(blacklist[_offender] == false, "Can't blacklist a blacklisted user :/");
blacklist[_offender] == true;
token.increaseLockedAmount(_offender, token.getUnlockedAmount(_offender));
emit Blacklisted(_offender, true);
}
function unblacklistMe() payable public {
unblacklistAddress(msg.sender);
}
function unblacklistAddress(address _offender) payable public {
require(msg.value >= 0.05 ether, "Unblacklisting fee");
require(blacklist[_offender] == true, "Can't unblacklist a non-blacklisted user :/");
require(notVoting(_offender), "Offender must not be involved in a vote.");
withdrawableByOwner = withdrawableByOwner.add(msg.value);
blacklist[_offender] = false;
token.decreaseLockedAmount(_offender, token.balanceOf(_offender));
emit Blacklisted(_offender, false);
}
function notVoting(address _voter) internal view returns (bool) {
for (uint256 i = 0; i
请注意,列入黑名单的帐户的令牌会被锁定,直到他们发送不合格的费用为止。