【发布时间】:2018-01-16 04:14:29
【问题描述】:
让我们从我的solidity代码开始:
pragma solidity ^0.4.18;
contract Voting {
address mainAddress;
bytes32[] candidateNames;
mapping(bytes32 => uint) candidateVotes;
mapping(bytes32 => bytes32) candidatesDetails;
address[] voters;
function Voting() public {
mainAddress = msg.sender;
}
modifier isMainAddress {
if (msg.sender == mainAddress) {
_;
}
}
function getAllCandidates() public view returns (bytes32[]) {
return candidateNames;
}
function setCandidate(bytes32 newCandidate) isMainAddress public {
candidateNames.push(newCandidate);
}
function setVote(bytes32 candidate) public {
require(validVoters());
candidateVotes[candidate] = candidateVotes[candidate] + 1;
voters.push(msg.sender);
}
function getVote(bytes32 candidate) public view returns (uint) {
return candidateVotes[candidate];
}
function setDescrption(bytes32 candidateName, bytes32 candidatesDesc) isMainAddress public {
candidatesDetails[candidateName] = candidatesDesc;
}
function getDescription(bytes32 candidateName) public view returns (bytes32){
return candidatesDetails[candidateName];
}
function getCurrentAddress() public view returns (address) {
return msg.sender;
}
function validVoters() public view returns (bool) {
for(uint i = 0; i < voters.length ; i++){
if (voters[i] == msg.sender) {
return false;
}
}
return true;
}
}
函数:Voting()、getAllCandidates()、setCandidate()、getVote()、setDescription()、getDescription()、getCurrentAddress()在多次调用时工作正常。所以,我想我们现在可以忽略它们。
函数setVote() 在第一次执行时运行良好即。当一个人投票一次时。当同一个人第二次尝试投票时,就会出现问题。它给出了以下错误:
这可能是初学者的错误,但我已经尝试修复此问题 2 天,现在我真的需要帮助。
还有,
我使用 Remix - 基于浏览器的 IDE 来运行/检查我的 Solidity 代码。
我将 Ganache 用于测试帐户。
谢谢。
【问题讨论】:
标签: solidity smartcontracts remix