0

this is my contract :

pragma solidity >=0.4.21 <0.6.0;



contract CloudProject {        
    uint public nbCandidates = 0;
    uint public nbBureau = 0;
    uint public nPartiPouvoir = 0;
    uint public pvCount;

    struct Pv {
    string[] names;
    uint[] voteCount;
}

struct PvF {
    string[] names;
    uint[] voteCount;
    uint[] percentage;
}

struct Candidate {
    uint bureauCount;
    mapping(uint => Pv) pvs;
    PvF pv_final;
}

mapping(string => Candidate) candidates;    
mapping(uint => Pv) bureaux;
uint[] bureauIndices;
PvF our_result;
PvF elecam_result;
PvF vrai_result;    
mapping(uint=> string) candidateName;    
uint candidateNameCount = 0;
//variables de l'intermédiarité
Pv[] pv_set_resumed;
uint[] occurences;
mapping(uint => mapping(uint => Pv)) bureauMiddle;
string[] substrings;

function init(uint nb_Candidate, uint nb_Bureau, uint n_PartiPouvoir) public {
    nbCandidates = nb_Candidate;
    nbBureau = nb_Bureau;
    nPartiPouvoir = n_PartiPouvoir;                   
}


function addPv(string memory name, uint bureauId, string memory pv_names, uint[] memory pv_counts) public
{
    pvCount++;          
    uint tmp = bureauId - 1;
    string memory cand_name = name;

    if((keccak256(abi.encodePacked(cand_name)) != keccak256(abi.encodePacked("Elecam")) && 
    keccak256(abi.encodePacked(cand_name)) != keccak256(abi.encodePacked("PV_Vrai"))) && 
    candidates[cand_name].bureauCount == 0)        
    {           
        candidateName[candidateNameCount] = name;
        candidateNameCount++;                        
    }

    if((keccak256(abi.encodePacked(cand_name)) == keccak256(abi.encodePacked("Elecam")) || 
    keccak256(abi.encodePacked(cand_name)) == keccak256(abi.encodePacked("PV_Vrai"))) && 
    candidates[cand_name].bureauCount == 1)        
    {           
        candidateName[candidateNameCount] = name;
        candidateNameCount++;                        
    }

    candidates[cand_name].bureauCount++; 

    //extraction of pv_names from the string by spliting
    string[] memory pv_name_array = splitStr(pv_names, ".");        

    // remplissage du PV                    
    for(uint i = 0; i < pv_counts.length; i++) {
        if(candidates[cand_name].bureauCount == 0)
            candidates[cand_name].pvs[tmp].names.push(pv_name_array[i]);
        candidates[cand_name].pvs[tmp].voteCount.push(pv_counts[i]);           
    }        

    //remplissage de pv_final
    for(uint i1 = 0; i1 < pv_counts.length; i1++) {

        if(candidates[cand_name].bureauCount == 0)
        {
            candidates[cand_name].pv_final.names.push(pv_name_array[i1]);
            candidates[cand_name].pv_final.voteCount.push(pv_counts[i1]);
        }
        else                
            candidates[cand_name].pv_final.voteCount[i1] += pv_counts[i1];            
    }

    uint sum = 0;
    uint sum_percent = 0;
    uint sum_last = 0;
    //calcul des pourcentages
    for(uint i2 = 0; i2 < candidates[cand_name].bureauCount; i2++) {             
        sum += candidates[cand_name].pv_final.voteCount[i2];         
    }

    for(uint i3 = 0; i3 < candidates[cand_name].bureauCount; i3++) {             
        candidates[cand_name].pv_final.percentage[i3] = candidates[cand_name].pv_final.voteCount[i3]*100/sum;
        sum_percent += candidates[cand_name].pv_final.voteCount[i3]*100/sum;
    }

    for(uint i4 = 0; i4 < candidates[cand_name].bureauCount-1; i4++) {             
        sum_last += candidates[cand_name].pv_final.percentage[i4];         
    }

    uint id = candidates[cand_name].bureauCount-1;

    candidates[cand_name].pv_final.percentage[id] = sum_percent - sum_last;
}


function splitStr(string memory str, string memory delimiter) private returns (string[] memory){ 

    bytes memory b = bytes(str); //cast the string to bytes to iterate
    bytes memory delm = bytes(delimiter);         
    uint i = 0;
    while(i<b.length)
    {
        if(b[i] == delm[0]) { //check if a not space

            bytes memory s1 = new bytes(i);
            bytes memory s2 = new bytes(b.length-i-1);
            for(uint a = 0; a < i;a++)
            {
                s1[a] = b[a];
            }
            substrings.push(string(s1));
            for(uint a1 = i+1; a1 < b.length; a1++)
            {
                s2[a1] = b[a1];
            }
            i = 0;
            b = s2;

        }
        else if(i == b.length - 1)
        {
            substrings.push(string(b));
            i++;
        }
        else
            i++;
    }   

    return substrings;     
}

}

I'm using web3 py to make addPv transaction :

for s in pv_list:
    print(*s)
    j = i%9 +1
    print(j)
    txnHash= cloudProject.functions.addPv(str(s[0]), int(s[1]), str(s[2]), s[3]).transact({'from' : accounts[j]})
    print(txnHash)
    i = i+1

accounts is the web3.eth.accounts... I'm getting this error :

Traceback (most recent call last):
File "ocr.py", line 235, in <module>
txnHash= cloudProject.functions.addPv(str(s[0]), int(s[1]), str(s[2]), s[3]).transact({'from' : accounts[j]})
File "/usr/local/lib/python3.6/dist-packages/web3/contract.py", line 1151, in transact
**self.kwargs
File "/usr/local/lib/python3.6/dist-packages/web3/contract.py", line 
1454, in transact_with_contract_function
txn_hash = web3.eth.sendTransaction(transact_transaction)
File "/usr/local/lib/python3.6/dist-packages/web3/eth.py", line 263, in sendTransaction
get_buffered_gas_estimate(self.web3, transaction),
File "/usr/local/lib/python3.6/dist packages/web3/utils/transactions.py", line 84, in get_buffered_gas_estimate
gas_estimate = web3.eth.estimateGas(gas_estimate_transaction)
File "/usr/local/lib/python3.6/dist-packages/web3/eth.py", line 304, in estimateGas [transaction],
File "/usr/local/lib/python3.6/dist-packages/web3/manager.py", line 112, in request_blocking raise ValueError(response["error"])
ValueError: {'message': 'VM Exception while processing transaction invalid opcode', 'code': -32000, 'data': {'stack': 'RuntimeError: VM Exception while processing transaction: invalid opcode\n 

I don't think the error is about the gas limit (i've changed and the error is the same) Please help me check if there is an error in the code.

  • There is not a single occurrence of estimateGas in the code that you posted!!! – goodvibration Jan 4 at 14:41
  • estimateGas is in the error message. I'm going to post it – Kouam Josiane Jan 4 at 14:59
  • It is not in the error message either! – goodvibration Jan 4 at 14:59
  • Do not use pragma solidity >4.21 < 6.0. 4 and 5 are not totally compatible, let’s imagine what it will be 5.99.. 😉 – Rick Park Jan 4 at 15:07
  • i wanted to use solidity 5.0 but it seems to be solidity 0.4.24 – Kouam Josiane Jan 4 at 15:09

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.