请问我该如何解决这个问题?应该有人帮忙。 我是新来建立blackchian投票系统的基地
编译器错误:堆栈太深。尝试使用
--via-ir
(cli) 或等效的 viaIR: true
(标准 JSON)进行编译,同时启用优化器。否则,尝试删除局部变量。
--> 合同/2.sol:372:38:
|
372 | 372 if (voter.election_id == _election_id) {
|
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/utils/Counters.sol";
import "hardhat/console.sol";
contract VotingSystem {
using Counters for Counters.Counter;
Counters.Counter public _electionID;
Counters.Counter public _candidateID;
Counters.Counter public _voterID;
// for who creates election, they are the chairperson
address public election_owner;
struct Election {
uint256 election_id;
string election_title;
string election_description;
address election_owner_address; // the owner of this election
string name;
}
event ElectionCreated(
uint256 indexed election_id,
string election_title,
string election_description,
address election_owner, // the owner of this election
string name
);
struct Candidate {
uint256 candidate_id;
uint256 election_id; // which election candidate
string name;
address candidate_address;
uint256 candidate_vote_count; // votes obtained
}
event CandidateCreated(
uint256 indexed candidate_id,
uint256 election_id, // which election candidate
string name,
address candidate_address,
uint256 candidate_vote_count // votes obtained
);
struct Voter {
uint256 voter_id;
uint256 election_id; // which election voter
string name;
address voter_address;
bool voter_voted; // did you vote
uint256 voter_weight; // how many votes are available
uint256 voter_vote; // For tracking vote
}
event VoterCreated(
uint256 indexed voter_id,
uint256 election_id, // which election voter
string name,
address voter_address,
bool voter_voted, // did you vote
uint256 voter_weight, // how many votes are available
uint256 voter_vote // For tracking vote
);
address[] public electionADD;
address[] public candidateAddress;
address[] public voterAddress;
mapping(uint256 => Election) public elections;
mapping(address => Candidate) public candidates;
mapping(address => Voter) public voters;
constructor() {
election_owner = msg.sender;
}
//create a Election
function CreateElection(
string memory _election_title,
string memory _election_description,
string memory _name
) public {
require(
bytes(_election_title).length > 0,
"Election Title is required"
);
require(
bytes(_election_description).length > 0,
"Election Description is required"
);
require(bytes(_name).length > 0, "Election Owner Name is required");
_electionID.increment();
uint256 idNumber = _electionID.current();
Election storage election = elections[idNumber];
election.election_id = idNumber;
election.election_title = _election_title;
election.election_description = _election_description;
election.election_owner_address = msg.sender; // Set the owner to the sender of this transaction
election.name = _name;
// If the length of the array is less than idNumber, extend the array
if (electionADD.length < idNumber) {
electionADD.push(msg.sender);
} else {
electionADD[idNumber - 1] = msg.sender;
}
emit ElectionCreated(
idNumber,
_election_title,
_election_description,
msg.sender,
_name
);
}
//get information of Election
function getElections()
public
view
returns (
uint256[] memory,
string[] memory,
string[] memory,
address[] memory,
string[] memory
)
{
uint256[] memory idE = new uint256[](_electionID.current());
string[] memory titles = new string[](_electionID.current());
string[] memory descriptions = new string[](_electionID.current());
address[] memory owners = new address[](_electionID.current());
string[] memory names = new string[](_electionID.current());
for (uint256 i = 0; i < _electionID.current(); i++) {
Election storage election = elections[i + 1];
idE[i] = election.election_id;
titles[i] = election.election_title;
descriptions[i] = election.election_description;
owners[i] = election.election_owner_address;
names[i] = election.name;
}
return (idE, titles, descriptions, owners, names);
}
// Create a Candidate
function createCandidate(
uint256 _election_id,
string memory _name,
address _candidate_address
) public {
require(bytes(_name).length > 0, "Candidate name is required");
require(
_candidate_address != address(0),
"Candidate address is required"
);
require(
_election_id > 0 && _election_id <= _electionID.current(),
"Invalid election ID"
);
Election storage election = elections[_election_id];
require(
election.election_owner_address == msg.sender,
"Only the owner of the election can add a candidate"
);
require(election.election_id != 0, "Invalid election ID");
_candidateID.increment();
uint256 idNumber = _candidateID.current();
Candidate storage candidate = candidates[_candidate_address];
candidate.candidate_id = idNumber;
candidate.election_id = _election_id;
candidate.name = _name;
candidate.candidate_address = _candidate_address;
candidate.candidate_vote_count = 0;
emit CandidateCreated(
idNumber,
_election_id,
_name,
_candidate_address,
0 // initial vote count is 0
);
candidateAddress.push(_candidate_address);
}
// Get Candidates Information by Election ID
function getCandidatesByElection(uint256 _election_id)
public
view
returns (
uint256 candidateCount,
uint256[] memory candidateIds,
string[] memory names,
address[] memory addresses,
uint256[] memory voteCounts
)
{
require(
_election_id > 0 && _election_id <= _electionID.current(),
"Invalid election ID"
);
uint256 count = 0;
for (uint256 i = 0; i < candidateAddress.length; i++) {
Candidate storage candidate = candidates[candidateAddress[i]];
if (candidate.election_id == _election_id) {
count++;
}
}
uint256[] memory finalCandidateIds = new uint256[](count);
string[] memory finalNames = new string[](count);
address[] memory finalAddresses = new address[](count);
uint256[] memory finalVoteCounts = new uint256[](count);
count = 0;
for (uint256 i = 0; i < candidateAddress.length; i++) {
Candidate storage candidate = candidates[candidateAddress[i]];
if (candidate.election_id == _election_id) {
finalCandidateIds[count] = candidate.candidate_id;
finalNames[count] = candidate.name;
finalAddresses[count] = candidate.candidate_address;
finalVoteCounts[count] = candidate.candidate_vote_count;
count++;
}
}
return (
count,
finalCandidateIds,
finalNames,
finalAddresses,
finalVoteCounts
);
}
//addVoter
function addVoter(
uint256 _election_id,
string memory _name,
address _voter_address
) public {
// Check input validity
require(bytes(_name).length > 0, "Voter name is required");
require(_voter_address != address(0), "Voter address is required");
require(
_election_id > 0 && _election_id <= _electionID.current(),
"Invalid election ID"
);
Election storage election = elections[_election_id];
require(
election.election_owner_address == msg.sender,
"Only the owner of the election can add a voter"
);
require(election.election_id != 0, "Invalid election ID");
_voterID.increment();
uint256 idNumber = _voterID.current();
Voter storage voter = voters[_voter_address];
voter.voter_id = idNumber;
voter.election_id = _election_id;
voter.name = _name;
voter.voter_address = _voter_address;
voter.voter_voted = false; // initial voting status is false
voter.voter_weight = 1; // initial voting weight is 1
voter.voter_vote = 0; // initial vote is 0
emit VoterCreated(
idNumber,
_election_id,
_name,
_voter_address,
false,
1,
0
);
// Add the voter address to the voterAddress array
voterAddress.push(_voter_address);
}
// Voter cast vote for a Candidate
function vote(uint256 _election_id, address _candidate_address) public {
// Check input validity
require(
_candidate_address != address(0),
"Candidate address is required"
);
require(
_election_id > 0 && _election_id <= _electionID.current(),
"Invalid election ID"
);
Voter storage voter = voters[msg.sender];
require(voter.voter_id != 0, "Voter not found");
require(
voter.election_id == _election_id,
"Voter not part of this election"
);
require(voter.voter_voted == false, "Voter already voted");
Candidate storage candidate = candidates[_candidate_address];
require(candidate.candidate_id != 0, "Candidate not found");
require(
candidate.election_id == _election_id,
"Candidate not part of this election"
);
// Make the voter vote
voter.voter_voted = true;
voter.voter_vote = candidate.candidate_id;
// Update candidate's vote count
candidate.candidate_vote_count += voter.voter_weight;
// Set voter's weight to 0 after voting
voter.voter_weight = 0;
}
// Get Voter Information by Election ID
function getVoterByElection(uint256 _election_id)
public
view
returns (
uint256 voterCount,
uint256[] memory voterIds,
string[] memory names,
address[] memory addresses,
bool[] memory votedStatus,
uint256[] memory weights,
uint256[] memory votes
)
{
require(
_election_id > 0 && _election_id <= _electionID.current(),
"Invalid election ID"
);
uint256 count = 0;
for (uint256 i = 0; i < voterAddress.length; i++) {
Voter storage voter = voters[voterAddress[i]];
if (voter.election_id == _election_id) {
count++;
}
}
uint256[] memory finalVoterIds = new uint256[](count);
string[] memory finalNames = new string[](count);
address[] memory finalAddresses = new address[](count);
bool[] memory finalVotedStatus = new bool[](count);
uint256[] memory finalWeights = new uint256[](count);
uint256[] memory finalVotes = new uint256[](count);
count = 0;
for (uint256 i = 0; i < voterAddress.length; i++) {
Voter storage voter = voters[voterAddress[i]];
if (voter.election_id == _election_id) {
finalVoterIds[count] = voter.voter_id;
finalNames[count] = voter.name;
finalAddresses[count] = voter.voter_address;
finalVotedStatus[count] = voter.voter_voted;
finalWeights[count] = voter.voter_weight;
finalVotes[count] = voter.voter_vote;
count++;
}
}
return (
count,
finalVoterIds,
finalNames,
finalAddresses,
finalVotedStatus,
finalWeights,
finalVotes
);
}
}
我希望它能解决这个问题,我是这个坚固性的新手。
坚固性:{ 版本:“0.8.17”, 设置: { 优化器:{ 启用:真, 运行次数:100, }, 通过IR:真实, }, },
将 ViaIR: true 添加到 Hardhat.config.js