以太坊DAO之時間鎖定Multisig

Decentralized Autonomous Organization,簡稱DAO,以太坊中重要的概念。通常翻譯爲去中心化的自治組織。php

有時候,時間也能夠用做一種很好的安全機制。如下代碼基於DAO區塊鏈大會,但有不一樣的變化。不是每一個操做須要X個成員批准,而是任何交易均可以由單個成員發起,但它們在執行以前都須要最少的延遲,這取決於交易的支持。提案的批准越多,就越早執行。會員能夠對交易進行投票,這意味着它將取消其餘一個已批准的簽名。java

時間鎖定Multisig

這意味着若是你沒有緊急程度,則執行任何交易可能只須要一個或兩個簽名。可是,若是單個密鑰被泄露,其餘密鑰能夠將該交易延遲數月或數年,甚至能夠阻止其執行。node

這個怎麼運做

全部密鑰都已批准的交易能夠在十分鐘後執行(此金額是可配置的),而且每5%未投票的成員每次須要的時間加倍(若是他們主動投票,則爲四倍)反對)。若是它是一個簡單的ether交易,只要支持投票將其置於所需的時間內,就會執行交易,但更復雜的交易將要求使用正確的字節碼手動執行交易。這些是默認值,但在建立合約時能夠設置不一樣的值:python

批准交易的成員數量:近似時間延遲android

  • 100%批准:10分鐘(最低默認值)
  • 90%批准:40分鐘
  • 80%:2小時40分鐘
  • 50%:大約一週
  • 40%:1個月
  • 30%:4個月
  • 20%:超過一年
  • 10%或更少:5年或從不 一旦最短的時間過去,任何人均可以執行交易(參見「國會」以得到更完整的步行)。這是故意的,由於它容許某人安排交易或僱用其餘人來執行交易。

代碼:程序員

pragma solidity >=0.4.22 <0.6.0;

contract owned {
    address public owner;

    constructor() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function transferOwnership(address newOwner) onlyOwner public {
        owner = newOwner;
    }
}

contract tokenRecipient {
    event receivedEther(address sender, uint amount);
    event receivedTokens(address _from, uint256 _value, address _token, bytes _extraData);

    function receiveApproval(address _from, uint256 _value, address _token, bytes memory _extraData) public {
        Token t = Token(_token);
        require(t.transferFrom(_from, address(this), _value));
        emit receivedTokens(_from, _value, _token, _extraData);
    }

    function () payable external {
        emit receivedEther(msg.sender, msg.value);
    }
}

interface Token {
    function transferFrom(address _from, address _to, uint256 _value) external returns (bool success);
}

contract TimeLockMultisig is owned, tokenRecipient {

    Proposal[] public proposals;
    uint public numProposals;
    mapping (address => uint) public memberId;
    Member[] public members;
    uint minimumTime = 10;

    event ProposalAdded(uint proposalID, address recipient, uint amount, string description);
    event Voted(uint proposalID, bool position, address voter, string justification);
    event ProposalExecuted(uint proposalID, int result, uint deadline);
    event MembershipChanged(address member, bool isMember);

    struct Proposal {
        address recipient;
        uint amount;
        string description;
        bool executed;
        int currentResult;
        bytes32 proposalHash;
        uint creationDate;
        Vote[] votes;
        mapping (address => bool) voted;
    }

    struct Member {
        address member;
        string name;
        uint memberSince;
    }

    struct Vote {
        bool inSupport;
        address voter;
        string justification;
    }

    // Modifier that allows only shareholders to vote and create new proposals
    modifier onlyMembers {
        require(memberId[msg.sender] != 0);
        _;
    }

    /**
     * Constructor
     *
     * First time setup
     */
    constructor(
        address founder, 
        address[] memory initialMembers, 
        uint minimumAmountOfMinutes
    ) payable public {
        if (founder != address(0)) owner = founder;
        if (minimumAmountOfMinutes !=0) minimumTime = minimumAmountOfMinutes;
        // It’s necessary to add an empty first member
        addMember(address(0), '');
        // and let's add the founder, to save a step later
        addMember(owner, 'founder');
        changeMembers(initialMembers, true);
    }

    /**
     * Add member
     *
     * @param targetMember address to add as a member
     * @param memberName label to give this member address
     */
    function addMember(address targetMember, string memory memberName) onlyOwner public
    {
        uint id;
        if (memberId[targetMember] == 0) {
            memberId[targetMember] = members.length;
            id = members.length++;
        } else {
            id = memberId[targetMember];
        }

        members[id] = Member({member: targetMember, memberSince: now, name: memberName});
        emit MembershipChanged(targetMember, true);
    }

    /**
     * Remove member
     *
     * @param targetMember the member to remove
     */
    function removeMember(address targetMember) onlyOwner public {
        require(memberId[targetMember] != 0);

        for (uint i = memberId[targetMember]; i<members.length-1; i++){
            members[i] = members[i+1];
            memberId[members[i].member] = i;
        }
        memberId[targetMember] = 0;
        delete members[members.length-1];
        members.length--;
    }

    /**
     * Edit existing members
     *
     * @param newMembers array of addresses to update
     * @param canVote new voting value that all the values should be set to
     */
    function changeMembers(address[] memory newMembers, bool canVote) public {
        for (uint i = 0; i < newMembers.length; i++) {
            if (canVote)
                addMember(newMembers[i], '');
            else
                removeMember(newMembers[i]);
        }
    }

    /**
     * Add Proposal
     *
     * Propose to send `weiAmount / 1e18` ether to `beneficiary` for `jobDescription`. `transactionBytecode ? Contains : Does not contain` code.
     *
     * @param beneficiary who to send the ether to
     * @param weiAmount amount of ether to send, in wei
     * @param jobDescription Description of job
     * @param transactionBytecode bytecode of transaction
     */
    function newProposal(
        address beneficiary,
        uint weiAmount,
        string memory jobDescription,
        bytes memory transactionBytecode
    )
        onlyMembers public
        returns (uint proposalID)
    {
        proposalID = proposals.length++;
        Proposal storage p = proposals[proposalID];
        p.recipient = beneficiary;
        p.amount = weiAmount;
        p.description = jobDescription;
        p.proposalHash = keccak256(abi.encodePacked(beneficiary, weiAmount, transactionBytecode));
        p.executed = false;
        p.creationDate = now;
        emit ProposalAdded(proposalID, beneficiary, weiAmount, jobDescription);
        numProposals = proposalID+1;
        vote(proposalID, true, '');

        return proposalID;
    }

    /**
     * Add proposal in Ether
     *
     * Propose to send `etherAmount` ether to `beneficiary` for `jobDescription`. `transactionBytecode ? Contains : Does not contain` code.
     * This is a convenience function to use if the amount to be given is in round number of ether units.
     *
     * @param beneficiary who to send the ether to
     * @param etherAmount amount of ether to send
     * @param jobDescription Description of job
     * @param transactionBytecode bytecode of transaction
     */
    function newProposalInEther(
        address beneficiary,
        uint etherAmount,
        string memory jobDescription,
        bytes memory transactionBytecode
    )
        onlyMembers public
        returns (uint proposalID)
    {
        return newProposal(beneficiary, etherAmount * 1 ether, jobDescription, transactionBytecode);
    }

    /**
     * Check if a proposal code matches
     *
     * @param proposalNumber ID number of the proposal to query
     * @param beneficiary who to send the ether to
     * @param weiAmount amount of ether to send
     * @param transactionBytecode bytecode of transaction
     */
    function checkProposalCode(
        uint proposalNumber,
        address beneficiary,
        uint weiAmount,
        bytes memory transactionBytecode
    )
        view public
        returns (bool codeChecksOut)
    {
        Proposal storage p = proposals[proposalNumber];
        return p.proposalHash == keccak256(abi.encodePacked(beneficiary, weiAmount, transactionBytecode));
    }

    /**
     * Log a vote for a proposal
     *
     * Vote `supportsProposal? in support of : against` proposal #`proposalNumber`
     *
     * @param proposalNumber number of proposal
     * @param supportsProposal either in favor or against it
     * @param justificationText optional justification text
     */
    function vote(
        uint proposalNumber,
        bool supportsProposal,
        string memory justificationText
    )
        onlyMembers public
    {
        Proposal storage p = proposals[proposalNumber]; // Get the proposal
        require(p.voted[msg.sender] != true);           // If has already voted, cancel
        p.voted[msg.sender] = true;                     // Set this voter as having voted
        if (supportsProposal) {                         // If they support the proposal
            p.currentResult++;                          // Increase score
        } else {                                        // If they don't
            p.currentResult--;                          // Decrease the score
        }

        // Create a log of this event
        emit Voted(proposalNumber,  supportsProposal, msg.sender, justificationText);

        // If you can execute it now, do it
        if ( now > proposalDeadline(proposalNumber)
            && p.currentResult > 0
            && p.proposalHash == keccak256(abi.encodePacked(p.recipient, p.amount, ''))
            && supportsProposal) {
            executeProposal(proposalNumber, '');
        }
    }

    function proposalDeadline(uint proposalNumber) public view returns(uint deadline) {
        Proposal storage p = proposals[proposalNumber];
        uint factor = calculateFactor(uint(p.currentResult), (members.length - 1));
        return p.creationDate + uint(factor * minimumTime *  1 minutes);
    }

    function calculateFactor(uint a, uint b) public pure returns (uint factor) {
        return 2**(20 - (20 * a)/b);
    }

    /**
     * Finish vote
     *
     * Count the votes proposal #`proposalNumber` and execute it if approved
     *
     * @param proposalNumber proposal number
     * @param transactionBytecode optional: if the transaction contained a bytecode, you need to send it
     */
    function executeProposal(uint proposalNumber, bytes memory transactionBytecode) public {
        Proposal storage p = proposals[proposalNumber];

        require(now >= proposalDeadline(proposalNumber)                                         // If it is past the voting deadline
            && p.currentResult > 0                                                              // and a minimum quorum has been reached
            && !p.executed                                                                      // and it is not currently being executed
            && checkProposalCode(proposalNumber, p.recipient, p.amount, transactionBytecode));  // and the supplied code matches the proposal...


        p.executed = true;
        (bool success, ) = p.recipient.call.value(p.amount)(transactionBytecode);
        require(success);

        // Fire Events
        emit ProposalExecuted(proposalNumber, p.currentResult, proposalDeadline(proposalNumber));
    }
}

部署和使用

像之前同樣在這些教程上部署該代碼。在部署參數上,將最小時間留空將默認爲30分鐘,若是你想要更快的鎖定時間,則放1分鐘。上傳後,執行「添加成員」功能以添加組的新成員,他們能夠是你認識的其餘人,也能夠是不一樣計算機上的賬戶或離線存儲。web

設置爲全部者owner的賬戶很是強大,由於它能夠隨意添加或刪除成員。所以,在添加主成員後,咱們建議你經過執行Transfer Membership功能將owner設置爲另外一個賬戶。若是你但願對全部成員的添加或刪除進行投票,則將其設置爲multisig自己,就像任何其餘交易同樣。另外一種方法是將其設置爲另外一個受信任的multisig錢包,若是你但願永久修復成員數,則能夠設置爲0x000。請記住,此合約上的資金僅與「全部者」賬戶同樣安全。mongodb

與上述任何DAO同樣,此合約能夠持有以太幣,任何基於以太坊的代幣並執行任何合約。爲此,請檢查如何在國會DAO上執行復雜的提案。編程

警告和改進

爲簡單起見,對提案的投票僅僅算得少一點支持。若是你願意,你能夠玩弄負面投票更重要的想法,但這意味着少數成員能夠對任何提議的交易擁有有效的否決權!安全

你怎麼能改善這個合約?

咱們去探索吧!

你已經到了本教程的末尾,但這只是一次偉大冒險的開始。回顧一下,看看你取得了多少成就:你創造了一個活生生的,有說服力的機器人,你本身的加密貨幣,經過無信息的衆籌籌集資金,並用它來啓動你本身的我的民主組織。

接下來會發生什麼?

  • 你仍然控制的代幣能夠在分散的交易所出售,或者交易商品和服務,以資助第一份合約的進一步發展和發展組織。
  • 你的DAO能夠在名稱註冊商處擁有本身的名稱,而後更改它重定向的位置,以便在代幣持有者批准時自行更新。
  • 該組織不只能夠擁有醚類,還能夠擁有在以太坊上創造的任何其餘類型的硬幣,包括其價值與比特幣或美圓相關的資產。
  • 能夠對DAO進行編程,以容許具備多個交易的提案,其中一些預約在將來。它還能夠擁有其餘DAO的股份,這意味着它能夠對更大的組織投票或成爲DAO聯盟的一部分。
  • 代幣合約能夠從新編程爲持有以太或持有其餘代幣並將其分發給代幣持有者。這會將代幣的價值與其餘資產的價值聯繫起來,所以只需將資金轉移到代幣地址便可實現支付股息。

這一切都意味着你創造的這個小社會能夠成長,從第三方得到資金,支付常常性工資,擁有任何類型的加密資產,甚至使用衆籌爲其活動提供資金。全部這些都具備徹底透明,徹底的問責制和徹底免受任何人爲干擾。當網絡存在時,合約將徹底執行它們被建立的代碼,而沒有任何例外,永遠執行。

那麼你的合約是什麼?它會是一個國家,一個公司,一個非營利組織嗎?你的代碼會作什麼?

隨你,由你決定。

======================================================================

分享一些以太坊、EOS、比特幣等區塊鏈相關的交互式在線編程實戰教程:

  • java以太坊開發教程,主要是針對java和android程序員進行區塊鏈以太坊開發的web3j詳解。
  • python以太坊,主要是針對python工程師使用web3.py進行區塊鏈以太坊開發的詳解。
  • php以太坊,主要是介紹使用php進行智能合約開發交互,進行帳號建立、交易、轉帳、代幣開發以及過濾器和交易等內容。
  • 以太坊入門教程,主要介紹智能合約與dapp應用開發,適合入門。
  • 以太坊開發進階教程,主要是介紹使用node.js、mongodb、區塊鏈、ipfs實現去中心化電商DApp實戰,適合進階。
  • C#以太坊,主要講解如何使用C#開發基於.Net的以太坊應用,包括帳戶管理、狀態與交易、智能合約開發與交互、過濾器和交易等。
  • EOS教程,本課程幫助你快速入門EOS區塊鏈去中心化應用的開發,內容涵蓋EOS工具鏈、帳戶與錢包、發行代幣、智能合約開發與部署、使用代碼與智能合約交互等核心知識點,最後綜合運用各知識點完成一個便籤DApp的開發。
  • java比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈存儲、去中心化共識機制、密鑰與腳本、交易與UTXO等,同時也詳細講解如何在Java代碼中集成比特幣支持功能,例如建立地址、管理錢包、構造裸交易等,是Java工程師不可多得的比特幣開發學習課程。
  • php比特幣開發教程,本課程面向初學者,內容即涵蓋比特幣的核心概念,例如區塊鏈存儲、去中心化共識機制、密鑰與腳本、交易與UTXO等,同時也詳細講解如何在Php代碼中集成比特幣支持功能,例如建立地址、管理錢包、構造裸交易等,是Php工程師不可多得的比特幣開發學習課程。
  • tendermint區塊鏈開發詳解,本課程適合但願使用tendermint進行區塊鏈開發的工程師,課程內容即包括tendermint應用開發模型中的核心概念,例如ABCI接口、默克爾樹、多版本狀態庫等,也包括代幣發行等豐富的實操代碼,是go語言工程師快速入門區塊鏈開發的最佳選擇。

匯智網原創翻譯,轉載請標明出處。這裏是原文以太坊DAO之時間鎖定的Multisig

相關文章
相關標籤/搜索