Как требуется работа в локальных тестовых сетях?

Я создал этот контракт для проведения голосования:

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;
}
}

Дело в том, что я контролирую, началось ли голосование с флагом "abierta". Поэтому, если я попробую "votar" и abierta = false, это вызовет исключение, но ничего не произойдет. Я пробовал это на ремиксе и отлично работает, но когда я пытаюсь использовать свои скрипты на Python, это не работает.

Вот сценарий:

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")

Что я должен делать?

0 ответов

Другие вопросы по тегам