у меня есть функция, которая получает число, autentic оценивает, находится ли оно уже в массиве, если это так, оно должно возвращать false, если нет, возвращать true. Тогда onlyOneVote добавит число в массив, если оно истинно, а не ложно. Проблема в том, что функция никогда ничего не добавляет в массив.
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;
}
Я думаю, можно с уверенностью сказать, что ваш код можно сократить до этого:
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);
}
Назовите это с помощью web3 следующим образом:
contractInstance.vote.sendTransaction(id, {from: _from});
где _from
находится адрес вашей учетной записи (возможно, web3.eth.accounts[0]).
Этот код сделает это, он протестирован и вот вам адрес для взаимодействия с ним на 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;
}
}
Несколько вещей, чтобы прокомментировать:
Здесь у вас есть информация: http://solidity.readthedocs.io/en/v0.4.21/types.html#mappings
Этот код:people[counter] = ida; //Add the ida to the array counter = counter + 1;
Можно заменить, чтобы быть точнее и дешевле по расходу газа:people.push(ida)//Puts on the last array's position the item ida;
Надеюсь, поможет!!
бхалгаликс
бордаликс
бхалгаликс
бордаликс
бхалгаликс
бордаликс