azure - 为 Azure Blockchain Workbench 部署智能合约时出错

标签 azure blockchain solidity smartcontracts azure-blockchain-workbench

我一直在 Azure Blockchain Workbench 上构建投票应用程序。 我在上传智能合约文件时不断收到错误,提示构造函数和函数不存在。不过配置文件通过了检查。请帮忙解决错误。

这来自智能合约文件:

pragma solidity >= 0.4.0 <0.6.0;

contract Voting{
    enum StateType{
      Creation,
      Voting,
      BallotFinish
    }
    StateType public State;
    mapping(bytes32 => uint256) public votesCount;
    address public admin;
    bytes32[] public candidatesList;
    bytes32[] public voterList;
    constructor(bytes32[] memory candidateNames,bytes32[] memory voterNames) public{
        State = StateType.Creation;
        admin = msg.sender;
        candidatesList = candidateNames;
        voterList = voterNames;
    }
    function totalVotesFor(bytes32 candidate) public view returns (uint256)
    {
        require(validCandidate(candidate));
        return votesCount[candidate];
    }
    function voteForCandidate(bytes32 voter,bytes32 candidate) public {
        if(admin == msg.sender)
        {
            revert();
        }
        if(State == StateType.Voting)
        {
            require(validVoter(voter));
            require(validCandidate(candidate));
            votesCount[candidate] += 1;
        }
        else
        {
            revert();
        }
    }
    function validVoter(bytes32 voter) public view returns (bool) {
        for(uint i=0;i<voterList.length;i++)
        {
            if(voterList[i]==voter)
                return true;
        }
        return false;
    }
    function validCandidate(bytes32 candidate) public view returns (bool) {
        for(uint i=0 ; i < candidatesList.length ; i++)
        {
            if(candidatesList[i]==candidate)
                return true;
        }
        return false;
    }
}

这是配置文件:

{
    "ApplicationName": "Voting",
    "DisplayName": "Voting",
    "Description": "Test Voting App",
    "ApplicationRoles": [
      {
        "Name": "Admin",
        "Description": "Person who generates Candidate and Voter Lists"
      },
      {
        "Name": "Voter",
        "Description": "A person who votes"
      }
    ],
    "Workflows": [
      {
        "Name": "Voting",
        "DisplayName": "Vote Here",
        "Description": "A simple way to vote.",
        "Initiators": [ "Admin" ],
        "StartState": "Creation",
        "Properties": [
          {
            "Name": "State",
            "DisplayName": "State",
            "Description": "Holds the state of the contract.",
            "Type": {
              "Name": "state"
            }
          }
        ],
        "Constructor": {
          "Parameters": [
            {
              "Name": "candidateNames",
              "Description": "List of names of candidates",
              "DisplayName": "ListOfCandidates",
              "Type":
              {
                  "Name": "array",
                  "ElementType": {
                      "Name": "string"
                  }
              }
            },
            {
              "Name": "voterNames",
              "Description": "List of names of voters",
              "DisplayName": "ListOfVoters",
              "Type":{
                "Name": "array",
                "ElementType": {
                  "Name": "string"
                }
              }
            }
          ]
        },
        "Functions": [
          {
            "Name": "totalVotesFor",
            "DisplayName": "Get Votes for a Candidate",
            "Description": "...",
            "Parameters": [
              {
                "Name": "candidate",
                "Description": "...",
                "DisplayName": "Name of Candidate",
                "Type": {
                  "Name": "string"
                }
              }
            ]
          },
          {
            "Name": "voteForCandidate",
            "DisplayName": "Vote Function",
            "Description": "...",
            "Parameters": [
              {
                "Name": "voter",
                "Description": "...",
                "DisplayName": "Name of Voter",
                "Type": {
                  "Name": "string"
                }
              },
              {
                "Name": "candidate",
                "Description": "...",
                "DisplayName": "Name of Candidate",
                "Type": {
                  "Name": "string"
                }
              }
            ]
          }
        ],
        "States": [
          {
            "Name": "Creation",
            "DisplayName": "Ballot Creation",
            "Description": "...",
            "PercentComplete": 20,
            "Style": "Success",
            "Transitions": [
              {
                "AllowedRoles": ["Voter"],
                "AllowedInstanceRoles": [],
                "Description": "...",
                "Function": "voteForCandidate",
                "NextStates": [ "Voting" ],
                "DisplayName": "Give Vote"
              },
              {
                "AllowedRoles": ["Voter"],
                "AllowedInstanceRoles": [],
                "Description": "...",
                "Function": "totalVotesFor",
                "NextStates": [ "Voting" ],
                "DisplayName": "Get No of Votes"
              }
            ]
          },
          {
            "Name": "Voting",
            "DisplayName": "Voting Stage",
            "Description": "...",
            "PercentComplete": 20,
            "Style": "Success",
            "Transitions": [
              {
                "AllowedRoles": ["Voter"],
                "AllowedInstanceRoles": [],
                "Description": "...",
                "Function": "totalVotesFor",
                "NextStates": [ "Voting" ],
                "DisplayName": "Vote for a Candidate"
              }
            ]
          },
          {
            "Name": "BallotFinish",
            "DisplayName": "Voting Finished",
            "Description": "...",
            "PercentComplete": 100,
            "Style": "Success",
            "Transitions": []
          }
        ]
      }
    ]
  }

Screenshot of the error message

最佳答案

您的 sol 文件需要具有 json 文件中指定的合约构造函数。

我认为你需要将 byte32 更改为字符串并使用 StringUtil 进行字符串比较

关于azure - 为 Azure Blockchain Workbench 部署智能合约时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57354628/

相关文章:

metadata - 警告 : No visibility specified. 默认为 "public"

ethereum - 有没有办法根据以太坊 GitHub 提交者的提交合并频率对他们进行排序?

javascript - 如何使用松露测试代码来读取公共(public)变量?

mongodb - 配置从 Azure 应用服务对 MongoDB 集群的网络访问

azure - 如何将数据工厂与私有(private) GitHub 存储库集成

c# - ADAL 身份验证出错

ubuntu - gasLimit 随时间减少

Azure 事件中心多个收件人

javascript - 在 Chrome 中关闭登录弹出窗口时,Metamask 不返回 "code 4001 User rejected the request.",但在 Edge 中有效

blockchain - 为什么我在宠物店买不到 100 以太币?