错误:返回错误:处理事务时虚拟机异常:无效操作码

问题描述 投票:0回答:1

这是调用投票合约的server.js文件

我不知道问题出在哪里以及如何解决,我尝试了太多解决方案但没有人帮忙



    let web3;
    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        web3 = new Web3(new Web3.providers.HttpProvider('HTTP://127.0.0.1:7545'));
    }
    
    var bytecode = fs.readFileSync('./voting_sol_Ballot.bin').toString()
    var abi = JSON.parse(fs.readFileSync('./voting_sol_Ballot.abi').toString())
    const listOfCandidates = ['George W. Bush', 'Barack Obama', 'Joe Biden']
    const votingContract = new web3.eth.Contract(abi);
    
    
(async function () {
    const accounts = await web3.eth.getAccounts();
    let candidates = new Map();
    for (let i = 0; i < listOfCandidates.length; i++) {
        candidates[web3.utils.asciiToHex(listOfCandidates[i])] = listOfCandidates[i]
    }
    
    votingContract.deploy({
        data: bytecode,
        arguments: [listOfCandidates.map(name => web3.utils.asciiToHex(name)),
            [accounts[1], accounts[2], accounts[3], accounts[4], accounts[5], accounts[6], accounts[7]],
            "1622371495", "1622371495"]
    }).send({
        from: accounts[0],
        gas: '0xF4240', gasPrice: '0x4A817C800'
    }).then((newContractInstance) => {
        
        votingContract.options.address = newContractInstance.options.address
        console.log("Contract address: " + newContractInstance.options.address)
    })
})();

这是.sol 文件

// SPDX-许可证-标识符:GPL-3.0

pragma 实验 ABIEncoderV2; 编译指示可靠性 >=0.7.0 <0.9.0;

合约选票{

struct Voter {
    uint token;
    bool voted;  // if true, that person already voted
    uint vote;   // index of the voted proposal
    address voterAddress;
}

struct Proposal {
    bytes32 name;   // short name (up to 32 bytes)
    uint voteCount; // number of accumulated votes
}

address private government;

mapping(address => Voter) public voters;

Proposal[] public proposals;

uint private winningVoteCount = 0;

uint private tmpWinnerCount = 0;

uint public totalVote;

uint public voterCount;

uint private voteFinishTime;

uint private statViewFinishTime;

modifier alreadyVoted() {
    require(!voters[msg.sender].voted, "Already voted.");
    _;
}

modifier multipleWinner() {
    _;
    require(winningVoteCount != 0, "No vote submitted.");
    require(tmpWinnerCount < 2, "Two or more candidate has been voted equally");
}

modifier onlyVoters() {
    require(msg.sender != government, "Government cannot check vote.");
    _;
}

modifier onlyValidVoters() {
    require(voters[msg.sender].voted == false && voters[msg.sender].token != 0, "Only valid voters can cast a vote!");
    _;
}

constructor(bytes32[] memory proposalNames, address[] memory voterArr, uint voteFinishTime_, uint  statViewFinishTime_) {
    government = msg.sender;

    for (uint i = 0; i < proposalNames.length; i++) {
        // 'Proposal({...})' creates a temporary
        // Proposal object and 'proposals.push(...)'
        // appends it to the end of 'proposals'.
        proposals.push(Proposal({
            name: proposalNames[i],
            voteCount: 0
        }));
    }
    totalVote = 0;
    voterCount = voterArr.length;
    
    voteFinishTime = voteFinishTime_;
    statViewFinishTime = statViewFinishTime_;
    
    for (uint i = 0; i < voterArr.length; i++) {
       voters[voterArr[i]].token = 1;
    }
}


function vote(uint proposal) public alreadyVoted() onlyValidVoters() {
    Voter storage sender = voters[msg.sender];
    
    require(sender.token != 0, "Has no right to vote");
    require(block.timestamp < voteFinishTime, "Election has been finished! You cannot vote.");
    
    sender.voted = true;
    sender.vote = proposal;

    proposals[proposal].voteCount += sender.token;
    totalVote += sender.token;
    sender.token -= 1;
}



function winningProposal() private multipleWinner()
        returns (uint winningProposal_)   {
    
    for (uint p = 0; p < proposals.length; p++) {
        if (proposals[p].voteCount > winningVoteCount) {
            winningVoteCount = proposals[p].voteCount;
            winningProposal_ = p;
            tmpWinnerCount = 1;
        }
        if (proposals[p].voteCount == winningVoteCount) {
            tmpWinnerCount += 1;
        }
    }
}
function winnerName() public 
        returns (bytes32 winnerName_)
{
    require(block.timestamp > voteFinishTime, "Election has not been finished yet.");
    winnerName_ = proposals[winningProposal()].name;
}

function checkMyVote() public view onlyVoters() returns (bytes32 proposalName) {
    require(voters[msg.sender].voted, "You have not voted yet!");
    uint vote_index = voters[msg.sender].vote;
    
    return proposals[vote_index].name;
    
}

function getElectionResult() external view returns (bytes32[] memory names, uint[] memory voteCounts) {
    require( block.timestamp > statViewFinishTime, "Election has not been finished! You cannot view the results.");

    bytes32[] memory namesArr = new bytes32[](proposals.length);
    uint[] memory voteCountsArr = new uint[](proposals.length);

    for (uint i = 0; i < proposals.length; i++) {
        Proposal storage proposal = proposals[i];
        namesArr[i] = proposal.name;
        voteCountsArr[i] = proposal.voteCount;
    }

    return (namesArr, voteCountsArr);
}

}

我正在连接到 ganache v2.7.1 它没有中继到公共网络

javascript blockchain solidity smartcontracts web3js
1个回答
0
投票

您正在使用

pragma solidity >=0.7.0 <0.9.0
,如果您的
truffle-config.js
文件包含
solc : "0.8.20"(and above)
,则会出现问题。 将您的 solc 版本更改为“0.8.19”并重试,因为我在遇到此问题时也做了同样的事情

truffle-config.js file

© www.soinside.com 2019 - 2024. All rights reserved.