I have just added one other part to my contracts addUser method (setting the isPermitted field) and it has blown up the amount of gas it uses to the point I keep getting out of gas exceptions. The code of my contract that corresponds to this is

contract UserStorage{
  struct location{
      bytes8 latitude;
      bytes8 longitude;
      bool checked;

  struct userData{
      bool isPermitted;
      bytes ownData;
      mapping (address => bytes) friends;
      address[] friendsList;
      mapping (bytes10 => location) locations;

  mapping (address => userData) private users;

  function addUser(bytes ipfs, bool permitted){
      users[msg.sender].ownData = ipfs;
      users[msg.sender].isPermitted = permitted;

I am calling this using a web3j wrapper in android with

userStorage.addUser(new DynamicBytes(ipfsHash), new Byte(true)).get();

It is storing ipfs data to the users account. It's using on average 5 million gas for that one operation, reporting as bad instruction on etherscan. Any idea why it's so high and what I can do to lower the gas usage? Thanks.

  • Could you provide the transaction hash or a link to Etherscan from when you tried this? – Edmund Edgar Aug 10 '17 at 5:02
  • I'm not clear - are you getting out-of-gas error, or are you getting invalid opcode/bad instruction? You say both, but they are different. I'm guessing it is invalid opcode. This means something went wrong and your code threw an error somewhere. It this case it will use up all the gas you supplied (that's what currently happens for all exceptions). So don't focus on the gas, look for an error somewhere. Your code here looks ok. The line you added in addUser should increase gas use by about 20k only. – benjaminion Aug 10 '17 at 5:36
  • @EdmundEdgar This is the link to etherscan kovan.etherscan.io/tx/…, I assumed it was an out of gas exception but I think I assumed wrong, why would this throw an error then? – B.K Lewis Aug 10 '17 at 14:29
  • @benjaminion Have you any idea what the invalid opcode would be? It seems to compile fine with solc and doesn't have any errors, do you think it could be with how the web3j wrapper is accessing the contract, as I said before it was just updating the ipfs, without the permitted bool. – B.K Lewis Aug 10 '17 at 14:33
  • "do you think it could be with how the web3j wrapper is accessing the contract" -> yes, I do suspect this. I don't know web3j, though, so I can't really help on that. Your contract itself looks ok and I executed it in Remix just fine. – benjaminion Aug 10 '17 at 14:42

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.