0

I have a method which updates 3 mappings in my contract. To save data I use some items properties and message sender address, which allows easily to get users data.

mapping(bytes32 => uint256) private uintStorage1;
mapping(bytes32 => uint256[]) private arrStorage2;

function buy(uint256 itemId, address newOwner) public returns(uint256) {
    uintStorage1[_myHashFunc('item1', newOwner)]++;
    arrStorage2[_myHashFunc('type1', newOwner)].push(itemId);
    arrStorage2[_myHashFunc('id1', newOwner)].push(itemId);
}

here is a hash function which I use for that:

function _myHashFunc(string str1, address addr) private pure returns(bytes32) {
    return keccak256(abi.encodePacked(str1, addr));
}

it works fine when I use Ganache, but when I deployed such a contract in the main network it stopped working.

I think that the main problem is my using an address value in that hash function, because my other one which doesn't use address value works fine.

here is that function without address value:

 function _myHashFunc2(string str1, uint256 int1) private pure returns(bytes32) {
    return keccak256(abi.encodePacked(str1, int1));
}
  • Can you add which version of solidity are you compiling with to the question ? Have you tried deploying to a testnet ( Ropsten ) ? – Micky Socaci Dec 30 '18 at 15:55
  • @MickySocaci solidity version is 0.4.24, I have compiled with 0.4.24+commit.e67f0147 – Costa Rassco Dec 30 '18 at 16:09
  • What does "it stopped working" mean? ie the transaction revert or do nothing instead, or do something different, or run out of gas. Do you have a txhash of some failure? Can you try against other testnet like rinkeby or ropsten? – Ismael Dec 31 '18 at 5:44
  • @Ismael it does nothing, just doesn't save data and that's it. In one function I use _myHashFunc and _myHashFunc2 several times, data is saved in _myHashFunc2, but doesn't in _myHashFunc – Costa Rassco Jan 8 at 10:11
  • @CostaRassco I tried your snippets and they work as expected. Are you testing agains a public blockchain? Do you have a transaction hash of the failure? Perhaps it is a compiler bug that is triggered by other code. Or some misconfiguration in on a private blockchain. – Ismael Jan 9 at 16:32

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.