tengo una función que recibe un número, autentic evalúa si ya está en una matriz, si lo está, debería devolver falso, si no lo está, devolver verdadero. Entonces onlyOneVote agregará el número a la matriz si es verdadero y no si es falso. El problema es que la función nunca agrega nada a la matriz.
uint32[100] public people;
uint8 public counter;
function onlyOneVote(uint32 ida) public returns(bool) {
bool a = autentic(ida);
if (a == true) {
people[counter] = ida;
counter = counter + 1;
}
return a;
}
function autentic(uint32 idb) public returns(bool) {
bool b;
for(uint i = 0; i< people.length; i++) {
if (people[i] == idb) {
b = false;
break;
} else {
b = true;
}
}
return b;
}
Creo que es seguro decir que su código se puede reducir a esto:
uint32[100] public people;
uint8 public counter;
mapping (uint32 => bool) public alreadyVoted;
function vote(uint32 id) public returns(bool) {
if (alreadyVoted[id]) return(false);
alreadyVoted[id] = true;
people[counter] = id;
counter = counter + 1;
return(true);
}
Llámalo con web3 así:
contractInstance.vote.sendTransaction(id, {from: _from});
dónde _from
está la dirección de su cuenta (web3.eth.accounts[0] tal vez).
Este código lo hará, está probado y aquí tienes la dirección para interactuar con él en Kovan: 0xc807caebda01eaffd7998ab7fd9fcc4a2cf5730c
pragma solidity ^0.4.18;
contract Test {
uint32[100] public people;
uint256 public counter; // Is uint256 because the on the array[100], 100 is a uint256 variable.
mapping(uint32 => bool) public voteVerifier; //mapping saves you so much gas at checking if it's in the array
function onlyOneVote(uint32 ida) public returns(bool) {
require(!voteVerifier[ida]); //If false, enter to add the uint32 to array.
people[counter] = ida; //Add the ida to the array
counter = counter + 1;
voteVerifier[ida] = true;//Set on the mapping that this uint32 is now on the array.
return true;
}
}
Algunas cosas para comentar:
Aquí tienes información: http://solidity.readthedocs.io/en/v0.4.21/types.html#mappings
Este código:people[counter] = ida; //Add the ida to the array counter = counter + 1;
Se pueden reemplazar para ser más precisos y más baratos en el gasto de gas:people.push(ida)//Puts on the last array's position the item ida;
¡¡Espero eso ayude!!
bhalgalix
Bordalix
bhalgalix
Bordalix
bhalgalix
Bordalix