2
votes

In Ethereum private network (geth) I do have very simple contract (in Solidity).

version 1:

contract T {
   string log;
   function getLastLog() constant returns (string lastLog) { return log; }

   function T() { log = "[call end]: T()\n"; }

   struct TData {
      uint amount;
   }

   mapping (address => uint) balance;
   mapping (address => TData) mystructmap;

   function setBalance(address _user, uint _balance) {
       log = "[call start]: setBalance()\n";

       balance[_user] = _balance;
       mystructmap[_user] = TData({amount: 42});

       log = "[call end]: setBalance()\n";
   }

   function getBalance(address _user) constant returns (uint _balance) {
       return balance[_user];
   }
   function get42(address _user) constant returns (uint _fourtytwo) {
       return mystructmap[_user].amount;
   }
}

I do deploy contract and then call it like this (from web3.js):

  1. contract.getLog()
  2. contract.setBalance(valid_address, 55)
  3. contract.getLog()
  4. contract.getBalance(address)
  5. contract.get42(address)

And I get as output result:

  1. [call end]: T()
  2. [call end]: setBalance()
  3. 55
  4. 42

Now I just add one new field to TData structure:

version 2:

contract T {
   string log;
   function getLastLog() constant returns (string lastLog) { return log; }

   function T() { log = "[call end]: T()\n"; }

   struct TData {
      uint somedata;
      uint amount;
   }

   mapping (address => uint) balance;
   mapping (address => TData) mystructmap;

   function setBalance(address _user, uint _balance) {
       log = "[call start]: setBalance()\n";

       balance[_user] = _balance;
       mystructmap[_user] = TData({somedata: 11, amount: 42});

       log = "[call end]: setBalance()\n";
   }

   function getBalance(address _user) external constant returns (uint _balance) {
      return balance[_user];
   }
   function get42(address _user) external constant returns (uint _fourtytwo) {
      return mystructmap[_user].amount;
   }
}

I do the same calls as above:

  1. contract.getLog()
  2. contract.setBalance(valid_address, 55)
  3. contract.getLog()
  4. contract.getBalance(address)
  5. contract.get42(address)

But now I get:

  1. [call end]: T()
  2. [call end]: T()
  3. 0
  4. 0

Seems like 'setBalance()' function is not executed (or exited somewhere) and state in storage is not changed.

Please help!

Thanks.

2

2 Answers

2
votes

I had the same problem earlier. I'm pretty sure that it has to do with the amount of gas that you are sending with your requests. Web3 will guess but this has failed me before. Try manually sending different amounts of gas along with your request.

Here's something I'm doing:

store
.changeProduct(d.id, d.name, d.price, d.description, d.quantity,d.enabled, {from: account, gas:1000000})
0
votes
  1. Hardcoding Gas for any operation is a bad idea. You will keep getting into this kind of errors if you do that. You should check for Gas before firing the method
  2. Use the estimate Gas API - https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethestimategas
  3. As and when you take your DApp public, you have to be cognizant of the fact that miners can adjust Gas prices in the geth console.

miner.setGasPrice(gasPrice);

Hope that helps!