0

I am trying to push a bytes32 to an array, but it stays empty.

This is (part of) my code:

contract Voting {
    bytes32[] public encryptedVotes;
    bytes32[] public candidateList;
    address[] public pollingStationList;
    address public owner;
    function Voting(bytes32[] _candidateList, address[] _pollingStationList) public {
        candidateList = _candidateList;
        pollingStationList = _pollingStationList;
        owner = msg.sender;
    }

    function vote(bytes32 encryptedVote) public {
        encryptedVotes.push(encryptedVote);
    }

    function getEncryptedVotes() view public returns (bytes32[]) {
        return encryptedVotes;
    }
}

Don't mind the "encrypted" part of the code, it's part of more logic.

The problem lies in the "vote" function. When I call it it with web3, "contractInstance.vote.call("a value")", it returns [] but doesn't change the state. Calling getEncryptedVotes returns [].

Is there some logic surrounding bytes32 I am failing to see? Thanks in advance!

0

Your issue is with how you're calling it. Using ".call" of a contract function wont create a transaction, it'll do a read only call of the function. This is used for constant/view functions. So when you call contractInstance.vote.call("a value"), it'll run the function off-chain and not update the state. You should be able to just call constractInstance.vote("a value"), which will actually update the state of the contract.

  • Thank you very much! It's always these little things... Do you think I should delete this question because the question has little correlation to the answer? – Pizzapim Jan 23 '18 at 18:29
  • No problem. I've definitely had this issue a couple of times. Don't worry about deleting it, people will likely run into the same issue not realizing the actual cause. – flygoing Jan 23 '18 at 18:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.