传递字符串错误作为参数(实心)

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

我签订了一份精巧的合同进行投票,但是我a。遇到其中一项职能的麻烦(“结果”)。这是智能合约:

pragma solidity ^0.5.7;

contract Voting{
    struct Option{
        string name;
        uint256 votes;
    }

    Option[] private options;
    address[] private alreadyVote;

    address private owner = msg.sender;

    constructor() public {}

    function addOption(string memory _name) public{
        require(msg.sender==owner,"Permiso denegado");
        options.push(Option(_name,0));
    }

    function vote(string memory _name) public{
        require(find(msg.sender,alreadyVote)==-1,"Ya ha ejercido su derecho a voto");
        int candidateIndex = findOption(_name);
        require(candidateIndex!=-1,"No se ha encontrado el candidato deseado");

        options[uint(candidateIndex)].votes++;

        alreadyVote.push(msg.sender);
    }

    function findOption(string memory _name) private view returns (int){
        int result = -1;
        for(uint i = 0;i<options.length;i++){
            if(compareStrings(options[i].name,_name))
                result = int(i);
        }
        return result;
    }

    function compareStrings (string memory a, string memory b) private pure returns (bool) {
        return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))) );
    }

    function find(address ad, address[] memory v) private pure returns (int){
        int result = -1;
        for(uint i = 0;i<v.length;i++){
            if(v[i]==ad)
                result = int(i);
        }
        return result;
    }

    function concat(string memory _base, string memory _value) public pure returns (string memory) {
        bytes memory _baseBytes = bytes(_base);
        bytes memory _valueBytes = bytes(_value);

        string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
        bytes memory _newValue = bytes(_tmpValue);

        uint i;
        uint j;

        for(i=0; i<_baseBytes.length; i++) {
            _newValue[j++] = _baseBytes[i];
        }

        for(i=0; i<_valueBytes.length; i++) {
            _newValue[j++] = _valueBytes[i];
        }

        return string(_newValue);
    }

    function getResult(string memory _name) public view returns (string memory ,uint){
        require(msg.sender==owner,"Denied");
        int index = findOption(_name);
        string memory error = concat("Didn't found candidate ",_name);
        require(index!=-1,error);

        return (options[uint(index)].name,options[uint(index)].votes);
    }
}

事实是,当我调用功能结果时,第二个require语句弹出。 Tgos os我如何从Web3.py调用它:

tx = contract_instance.functions.getResult("Name").call({'from':acct.address})

我认为这应该可行,因为我已经使用相同的语句调用了函数表决,但是它完全可以正常工作。这就是我所说的:

tx = contract_instance.functions.vote(candidato).buildTransaction({'nonce':w3.eth.getTransactionCount(acc),'gas': 1728712,'gasPrice': w3.toWei('21', 'gwei')})
#Get tx receipt to get contract address
signed_tx = w3.eth.account.signTransaction(tx, key)

我在混音上对其进行了测试并正常工作,所以我真的不知道会发生什么。

python solidity smartcontracts web3
1个回答
0
投票

您是否在addOption之前调用了getResult函数?

正如您所说,您在第二个require语句上失败了,如果您传递的名称不在候选列表中,则会发生这种情况。

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