1

I have created this contract to manage a votation:

pragma solidity ^0.5.7;

contract Votacion{
mapping(uint => address[]) private votosCandidatos;
uint[] listaCandidatos;
bool abierta;

address private creador=msg.sender;

constructor(uint[] memory candidatosIds) public{
    listaCandidatos = candidatosIds;
    abierta = false;
    for (uint i = 0; i < candidatosIds.length; i++)
    {
        votosCandidatos[candidatosIds[i]];
    }
}

function ind(address[] memory v, address e) public pure returns (bool){
    bool found = false;

    for(uint i = 0; i < v.length && !found; i++)
    {
        found = v[i] == e;
    }

    return found;
}

function votar(uint candidato) public{
    bool ya_votado = false;

    for (uint i = 0; i < listaCandidatos.length && !ya_votado; i++)
    {
        ya_votado = ind(votosCandidatos[listaCandidatos[i]],msg.sender);
    }

    require(!ya_votado,"Ya ha ejercido su derecho a voto");
    //assert(!ya_votado);
    require(abierta,"La votación no es accesible en este momento");
    //assert(abierta);
    votosCandidatos[candidato].push(msg.sender);
}

function abrir() public{
    require(msg.sender == creador,"Permiso denegado");
    abierta = true;
}

function cerrar() public{
    require(msg.sender == creador,"Permiso denegado");
    abierta = false;
}

function revisar_voto() public view returns (uint)
{
    require(!abierta,"La votación no ha terminado aun");
    bool ya_votado = false;
    uint res;

    for (uint i = 0; i < listaCandidatos.length && !ya_votado; i++)
    {
        ya_votado = ind(votosCandidatos[listaCandidatos[i]],msg.sender);
        res = i;
    }

    return res;
}

function resultados() public view returns (uint[] memory){
    require(!abierta,"La votación no ha terminado aun");

    uint[] memory resul = new uint[](listaCandidatos.length);

    for (uint i = 0; i < listaCandidatos.length; i++)
    {
        resul[i] = votosCandidatos[listaCandidatos[i]].length;
    }

    return resul;
}
}

The thing is that I control if the votation has started with the flag abierta. So if I try votar and abierta=false, it would throw an exception, but nothing happens. I have tried this on remix and works perfectly, but when I try to use on my python script it doesn't work.

Here is the script:

import json
from web3 import Web3, IPCProvider
import os
import sys

w3=None

if __name__ == "__main__":
    w3 = Web3(IPCProvider("./testNet/geth.ipc"))

    argumentos=sys.argv
    direccion_contrato=argumentos[1]
    votante=argumentos[2]
    voto=argumentos[3]

    truffleFile = json.load(open('./build/contracts/Votacion.json'))
    abi = truffleFile['abi']
    bytecode = truffleFile['bytecode']
    contrato= w3.eth.contract(bytecode=bytecode, abi=abi,address=direccion_contrato)

    fichero=''
    ficheros = os.listdir("./testNet/keystore")
    cuenta=votante.replace("0x",'').lower()
    for fs in ficheros:
        print(fs,cuenta)
        if cuenta in fs:
            fichero=fs

    with open("./testNet/keystore/"+fichero) as keyfile:
        encrypted_key = keyfile.read()
        private_key = w3.eth.account.decrypt(encrypted_key, 'space treat blame exhibit tissue book decide fury exhaust hazard library effort')

    tx=contrato.functions.votar(int(voto)).buildTransaction({'from': votante,'gas':'0xa551','nonce':w3.eth.getTransactionCount(votante) + 1})
    signed_tx = w3.eth.account.signTransaction(tx, private_key.hex())
    try:
        hash= w3.eth.sendRawTransaction(signed_tx.rawTransaction)
        print("1")
    except Exception as e:
        print("0")

What should I do?

5
  • Are transactions mined? Can you send other transactions? Your gas might be low, also try setting nonce to the transaction count (without the extra + 1). – Ismael Dec 14 '19 at 5:13
  • I have tried everything. And transactions are mined and I have changed the nonce and gas params but it doesn't work. – Javier Jiménez de la Jara Dec 14 '19 at 11:25
  • It is very hard to give you an answer, there's no obvious error and the error "nothing happens" is not very helpful. At first I'd try to discard some problems in your private network. Can you deploy easier contracts? Can you execute view functions? Then I'd try to discard some problem with python. Can you execute a contract directly without signTransaction and sendRawTransaction? Can you query the transaction receipt with the hash returned? What does the receipt looks like? Also your votar looks very inefficient with two nested loops perhaps the gas cost is really high. – Ismael Dec 14 '19 at 15:48
  • 1
    At the end it was a gas problem. As you said in the last answer I have done the code more efficient and have increase the amount of gas used. Thank you very much. – Javier Jiménez de la Jara Dec 15 '19 at 16:32
  • It would be nice if you add an answer with the more efficient function so the question can be marked as resolved. – Ismael Dec 15 '19 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.