Skip to content

Instantly share code, notes, and snippets.

@windse7en
Created February 1, 2018 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save windse7en/b5bb21b4e78fa50cd0871cac2d5ecca9 to your computer and use it in GitHub Desktop.
Save windse7en/b5bb21b4e78fa50cd0871cac2d5ecca9 to your computer and use it in GitHub Desktop.
简易测试代码,comment掉了用户验证部分
pragma solidity ^0.4.17;
contract BasicMultiOwnerVault {
address[] public authorizedUsers;
address public owner;
address public withdrawObserver;
// 破解要用的新的contract地址
address public additionalAuthorizedContract;
// 用来更新additionalAuthorizedContract的中间变量
address public proposedAAA;
uint public lastUpdated;
// vote管理更新additionalAuthorizedContract的投票
bool[] public votes;
address [] public observerHistory;
modifier onlyAuthorized() {
bool pass = false;
if(additionalAuthorizedContract == msg.sender) {
pass = true;
}
for (uint i = 0; i < authorizedUsers.length; i++) {
if(authorizedUsers [i] == msg.sender) {
pass = true;
break;
}
}
// require (pass);
_;
}
modifier onlyOnce() {
require(owner == 0x0);
_;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier recordAction() {
lastUpdated = now;
_;
}
// 只有一次开始设定owner
function initilizeVault() recordAction onlyOnce {
owner = msg.sender;
}
// 添加observer的contract到withdrawObserver上面,可以调用observer function
// 有一个history来记录,如果有的话就不再添加。
function setObserver(address ob) {
bool duplicate = false;
for (uint i = 0; i < observerHistory.length; i++) {
if (observerHistory[i] == ob) {
duplicate = true;
}
}
if (!duplicate) {
withdrawObserver = ob;
observerHistory.push(ob);
}
}
// 加钱的代码,给合约加钱
function addToReserve() payable recordAction external returns (uint) {
assert(msg.value > 0.01 ether);
return this.balance;
}
// 取钱代码,不清楚怎么控制的,给dst加入0.01,但是什么时候在balance里面减少,不知道。
function basicWithdraw(address dst) internal returns (bool) {
require(this.balance >= 0.001 ether);
bool res = dst.call.value(0.001 ether)();
return res;
}
// 检查所有的vote为true
function checkAllVote() private returns (bool) {
for(uint i = 0; i < votes.length; i++) {
if(!votes[i]) {
return false;
}
}
return true;
}
// 开始所有的vote清零
function clearVote() private {
for(uint i = 0; i < votes.length; i++) {
votes[i] = false;
}
}
// 授权的用户来添加vote,如果当前proposal地址不对就会重新清空vote,更新proposal地址
// 如果一致,就更新添加的授权contract地址为proposedAAA,然后清空vote
function addAuthorizedAccount(uint votePosition, address proposal) onlyAuthorized external {
require(votePosition < authorizedUsers.length);
// require(msg.sender == authorizedUsers[votePosition]);
if (proposal != proposedAAA) {
clearVote();
proposedAAA = proposal;
}
votes[votePosition] = true;
if (checkAllVote()) {
additionalAuthorizedContract = proposedAAA;
clearVote();
}
}
// 过了12小时以后,老董可以从blockchain上删除整个contrac
// link: http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html#self-destruct
function resolve() onlyOwner {
if(now >= lastUpdated + 12 hours) {
selfdestruct(owner);
}
}
}
contract TimeDelayedVault is BasicMultiOwnerVault {
uint public nextWithdrawTime;
uint public withdrawCoolDownTime;
// 最开始第一次初始化,可以多次调用, 但里面调用了initializeVault
// 可能只可以一次,考虑可不可以多次调用这个function,来更新别的组的时间
function TimeDelayedVault() recordAction {
nextWithdrawTime = now;
withdrawCoolDownTime = 2 minutes;
this.call(bytes4(sha3("initializeVault()")));
// Please note, the following code chunk is different for each group, all group members are added to authorizedUsers array
authorizedUsers.push(0xcbFbF771482084A8d1Ef31fAC08937Ab16040d37);
for(uint i=0; i<authorizedUsers.length; i++) {
votes.push(false);
}
}
// 取钱,次数有限,会调用外部的observe function,更新取钱时间
function withdrawFund(address dst) onlyAuthorized external returns (bool) {
require(now > nextWithdrawTime);
assert(withdrawObserver.call(bytes4(sha3("observe()"))));
bool res = basicWithdraw(dst);
nextWithdrawTime = nextWithdrawTime + withdrawCoolDownTime;
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment