📜  什么是去中心化投票应用程序(DApps)?

📅  最后修改于: 2022-05-13 01:55:32.765000             🧑  作者: Mango

什么是去中心化投票应用程序(DApps)?

项目名称是基于 Solidity 语言的去中心化投票应用程序(DApps) 。这个项目展示了 Solidity 的许多功能。它实现了一个投票合约。当然,电子投票的主要问题是如何防止分配重复投票。

一些重要的概念是:

1. 合约:合约就像 Solidity 中的一个类,由(其功能)和数据(其状态)组成,位于以太坊区块链上的特定地址。在每个合约中,我们可以定义状态变量、方法和事件等。智能合约完全按照程序运行,没有任何停机、审查、欺诈和第三方干扰的可能性。

2.结构:结构是不同类型数据类型的集合,如C和C++,如下例所示:

struct Voter{
          bool authorized;
          bool voted;
      }

3.映射:映射就像哈希表一样,它根据键存储值。它们不能用作公开可见的合约函数的参数或返回参数。你不能迭代映射,即你不能枚举它们的键。可以在它们之上实现数据结构并对其进行迭代。

例子:

mapping(address=>Voter) info; 

4.修饰符:修饰符用于轻松更改函数的行为。他们可以在执行功能之前自动检查条件。

modifier ownerOn() {
        require(msg.sender==owner);
        _;
    }
function temaAF(address _address) public {
        require(!info[_address].voted, "already voted person"); //If already not vote
        require(info[_address].authorized, "You Have No Right for Vote");
        info[_address].voted = true;
        teamA++;
        totalVotes++;
      }

解释:

require(!info[_address].voted, "already voted person"); 

首先,我们需要验证该人是否已投票。如果 Person 被投票,则停止继续执行代码,否则继续执行其余代码。

Solidity 中的实现

Solidity
// Solidity program to demonstrate
// DApps
 
pragma solidity 0.5.11;
 
// Smart Contract for the Voting application
contract VotingForTopper {
 
      // Refer to the owner
      address  owner;
 
      // Declaring the public variable 'purpose'
      // to demonstrate the purpose of voting
      string public purpose;
      
      // Defining a structure with boolean 
      // variables authorized and voted
      struct Voter{
          bool authorized; 
          bool voted;
      }
 
      // Declaring the unsigned integer
      // variables totalVotes, and for the
      //3 teams- A,B, and C
      uint  totalVotes;
      uint teamA;
      uint  teamB;
      uint teamC;
       
      // Creating a mapping for the total Votes
      mapping(address=>Voter) info;
 
      // Defining a constructor indicating
      // the purpose of voting
      constructor(
      string memory _name) public{
        purpose = _name;  
        owner = msg.sender;
      }
    
      // Defining a modifier to
      // verify the ownership
      modifier ownerOn() {
        require(msg.sender==owner);
        _;
    }
       
      // Defining a function to verify
      // the person is voted or not
      function authorize(
      address _person) ownerOn public {
        info[_person].authorized= true;
         
    }
    
      // Defining a function to check and
      // skip the code if the person is already
      // voted else allow to vote and
      // calculate totalvotes for team A  
      function temaAF(address _address) public {
        require(
        !info[_address].voted,
        "already voted person");
        require(
        info[_address].authorized,
        "You Have No Right for Vote");
        info[_address].voted = true;
        teamA++;
        totalVotes++;
      }
 
      // Defining a function to check
      // and skip the code if the person
      // is already voted else allow to vote
      // and calculate totalvotes for team B 
      function temaBF(address _address) public {
       require(
       !info[_address].voted,
       "already voted person");
        require(
        info[_address].authorized,
        "You Have No Right for Vote");
        teamB++;
        info[_address].voted = true;
        totalVotes++;
      }
 
      // Defining a function to check
      // and skip the code if the person
      // is already voted else allow to vote
      // and calculate totalvotes for team C  
      function temaCF(address _address) public returns(
      string memory){
        require(
        !info[_address].voted,
        "already voted person");
        require(
        info[_address].authorized,
        "You Have No Right for Vote");
        info[_address].voted = true;
        teamC++;
        totalVotes++;
        return("Thanks for Voting");
      }
 
      function totalVotesF() public view returns(uint){
          return totalVotes;
      }
 
      // Defining a function to announce
      // the result of voting and
      // the name of the winning team
      function resultOfVoting() public view returns(
      string memory){
          if(teamA>teamB){
              if(teamA>teamC){
                  return"A is Winning";
              }
              else if(teamC>teamA){
                  return "C is Winning"; } }
          else if(teamB>teamC) {
              return "B is Winning";
          }
          else if(
          teamA==teamB && teamA==teamC || teamB==teamC ){
              return "No One is Winning";
          }
      }
    }