Lesson 2

投票人注册和投票

在本章中,我们将继续开发我们的去中心化投票系统,实现投票人注册和投票功能。我们将首先对Voter合约进行扩展,然后深入讲解Solidity映射和数组。

扩展Voter合约

我们将添加一个提案系统和一个供已注册投票人投票的选项。为此,我们需要一个数组来存储提案和一个新的函数用于投票。

Voter合约中,添加以下代码:

Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Voter {
    struct Person {
        bool voted;  
        uint vote;   
    }

    struct Proposal {
        string name;   
        uint voteCount; 
    }

    Proposal[] public proposals;
    mapping(address => Person) public voters;

    function registerVoter() public {
        voters[msg.sender].voted = false;
    }

    function addProposal(string memory _name) public {
        proposals.push(Proposal(_name, 0));
    }

    function vote(uint _proposal) public { 
     require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check 

     Person storage sender = voters[msg.sender]; 
     require(!sender.voted, "Already voted."); 
     sender.voted = true; 
     sender.vote = _proposal; 

     proposals[_proposal].voteCount += 1; 
    }
}

代码解析

为便于理解,我们将对新代码进行拆分讲解:

  1. Proposal结构:我们添加了一个新结构Proposal,包含提案的名称和它所获得的票数。

  2. Proposals数组:proposals数组包含投票系统中的所有提案。

  3. AddProposal函数:addProposal函数允许我们向proposals数组添加一个新提案。初始投票数设置为零。

  4. Vote函数:vote函数是使已注册投票人进行投票的函数。它以提案的索引为参数,并将提案的票数增加一个。此函数还会检查投票人是否已经投票并更新投票人的状态。

编译和测试

截至目前,我们已向合约添加了诸多功能,接下来便可以编译和测试它了。在Remix IDE中,单击左侧边栏的Solidity编译器图标,然后单击“Compile”按钮。

要测试合约,请进入“Deploy & Run Transactions”选项卡(Solidity编译器图标下方的图表)并单击“Deploy”按钮。部署完成后,您可以从此选项卡调用其函数来运行它。您还可以通过注册投票人、添加提案和投票,查看其运行方式。

在下一章中,我们将统计票数并宣布投票结果。在此之前,您可以随意探索和试用该合约。Solidity提供了很多功能,可以使您的合约更加安全、强大。祝编码愉快!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent Gate Learn.
Catalog
Lesson 2

投票人注册和投票

在本章中,我们将继续开发我们的去中心化投票系统,实现投票人注册和投票功能。我们将首先对Voter合约进行扩展,然后深入讲解Solidity映射和数组。

扩展Voter合约

我们将添加一个提案系统和一个供已注册投票人投票的选项。为此,我们需要一个数组来存储提案和一个新的函数用于投票。

Voter合约中,添加以下代码:

Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract Voter {
    struct Person {
        bool voted;  
        uint vote;   
    }

    struct Proposal {
        string name;   
        uint voteCount; 
    }

    Proposal[] public proposals;
    mapping(address => Person) public voters;

    function registerVoter() public {
        voters[msg.sender].voted = false;
    }

    function addProposal(string memory _name) public {
        proposals.push(Proposal(_name, 0));
    }

    function vote(uint _proposal) public { 
     require(_proposal < proposals.length, "Invalid proposal index."); // This is the added check 

     Person storage sender = voters[msg.sender]; 
     require(!sender.voted, "Already voted."); 
     sender.voted = true; 
     sender.vote = _proposal; 

     proposals[_proposal].voteCount += 1; 
    }
}

代码解析

为便于理解,我们将对新代码进行拆分讲解:

  1. Proposal结构:我们添加了一个新结构Proposal,包含提案的名称和它所获得的票数。

  2. Proposals数组:proposals数组包含投票系统中的所有提案。

  3. AddProposal函数:addProposal函数允许我们向proposals数组添加一个新提案。初始投票数设置为零。

  4. Vote函数:vote函数是使已注册投票人进行投票的函数。它以提案的索引为参数,并将提案的票数增加一个。此函数还会检查投票人是否已经投票并更新投票人的状态。

编译和测试

截至目前,我们已向合约添加了诸多功能,接下来便可以编译和测试它了。在Remix IDE中,单击左侧边栏的Solidity编译器图标,然后单击“Compile”按钮。

要测试合约,请进入“Deploy & Run Transactions”选项卡(Solidity编译器图标下方的图表)并单击“Deploy”按钮。部署完成后,您可以从此选项卡调用其函数来运行它。您还可以通过注册投票人、添加提案和投票,查看其运行方式。

在下一章中,我们将统计票数并宣布投票结果。在此之前,您可以随意探索和试用该合约。Solidity提供了很多功能,可以使您的合约更加安全、强大。祝编码愉快!

Disclaimer
* Crypto investment involves significant risks. Please proceed with caution. The course is not intended as investment advice.
* The course is created by the author who has joined Gate Learn. Any opinion shared by the author does not represent Gate Learn.
It seems that you are attempting to access our services from a Restricted Location where Gate.io is unable to provide services. We apologize for any inconvenience this may cause. Currently, the Restricted Locations include but not limited to: the United States of America, Canada, Cambodia, Thailand, Cuba, Iran, North Korea and so on. For more information regarding the Restricted Locations, please refer to the User Agreement. Should you have any other questions, please contact our Customer Support Team.