3
pragma solidity ^0.4.0;

contract DataStore {

   struct DataModel
   {
       uint id;
       bytes32 string1;
       bytes32 string2;
       bytes32 string3;
       uint actorId;
       bytes32 string4;
       bytes32 string5;
       bytes32 string6;
       bytes32 string7;
    }

    mapping(bytes32 => DataModel) dataMapping;
    DataModel datamodel;
    address owner;

    function DataStore()
    {
        owner = msg.sender;
    }

    function storeData(bytes32 hash, uint id, bytes32 string1, bytes32    string2, bytes32 string3, uint actorId, bytes32 string4, bytes32 string5, bytes32 string6, bytes32 string7) returns (bool response)
    {
        if(msg.sender != owner)
        {
            return false;
        }else
        {
            datamodel.id = id;
            datamodel.string1 = string1;
            datamodel.string2 = string2;
            datamodel.string3 = string3;
            datamodel.actorId = actorId;
            datamodel.string4 = string4;
            datamodel.string5 = string5;
            datamodel.string6 = string6;
            datamodel.string7 = string7;

            dataMapping[hash] = datamodel;

            return true;
        }
    }

    function queryData(bytes32 hash) returns (uint, bytes32, bytes32, bytes32, uint, bytes32, bytes32, bytes32, bytes32)
    {
        if(msg.sender != owner)
        {
            throw;
        }

        DataModel model = dataMapping[hash];
        return (model.id, model.string1, model.string2, model.string3, model.actorId, model.string4, model.string5, model.string6, model.string7);
    }
}
  1. I have deployed the contract and received the address.
  2. To store data, I write: DataStore.storeData.sendTransaction("hash", 1, "instrument", "ccy", "er", 64, "st", "end", "ts", "tz", {from: eth.accounts[0]}

  3. I receive:

    I1026 18:36:14.659905 eth/api.go:1185] Tx(0xcfd97f6c50c4ecca05e8df6939c17b2d9da01107064ccecf943024dabb8998b2) to: 0x21f3801411da8b93346bb6e2c216159d1e21ff53 "0xcfd97f6c50c4ecca05e8df6939c17b2d9da01107064ccecf943024dabb8998b2"

  4. I start mining using miner.start() and let some blocks be mined. Then I stop mining using miner.stop()

  5. Then I check storage using: eth.getStorageAt("0x21f3801411da8b93346bb6e2c216159d1e21ff53") and I get:

    "0x0000000000000000000000000000000000000000000000000000000000000000"

  6. If I try: DataStore.queryData("hash"), I get:

    invalid address

2

The following code works:

pragma solidity ^0.4.0;

contract DataStore{
    struct DataModel {
        uint id;
        bytes32 string1;
        bytes32 string2;
        bytes32 string3;
        uint actorId;
        bytes32 string4;
        bytes32 string5;
        bytes32 string6;
        bytes32 string7;
    }

    mapping(bytes32 => DataModel) dataMapping;
    address owner;

    function DataStore() {
        owner = msg.sender;
    }

    function storeData(bytes32 hash, uint id, bytes32 string1, bytes32 string2, 
      bytes32 string3, uint actorId, bytes32 string4, bytes32 string5, 
      bytes32 string6, bytes32 string7) returns (bool response) {
        if (msg.sender != owner) {
            return false;
        } else {
            DataModel memory datamodel;
            datamodel.id = id;
            datamodel.string1 = string1;
            datamodel.string2 = string2;
            datamodel.string3 = string3;
            datamodel.actorId = actorId;
            datamodel.string4 = string4;
            datamodel.string5 = string5;
            datamodel.string6 = string6;
            datamodel.string7 = string7;
            dataMapping[hash] = datamodel;
            return true;
        }
    }

    function queryData(bytes32 hash) constant returns (uint, bytes32, bytes32, 
      bytes32, uint, bytes32, bytes32, bytes32, bytes32) {
        if (msg.sender != owner) {
            throw;
        }
        DataModel memory model = dataMapping[hash];
        return (model.id, model.string1, model.string2, model.string3, 
          model.actorId, model.string4, model.string5, model.string6, 
          model.string7);
    }
}

And here is the Browser Solidity screen showing that the storeData(...) and queryData(...) methods work as expected:

enter image description here

The changes I made are:

  • In storeData(...) and queryData(...), I declared datamodel with the memory keyword as these are temporary variables. From What does the keyword “memory” do exactly?:

    “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.

  • In queryData(...), I've declared the method constant as it does not modify any data. From Solidity - Frequently Asked Questions:

    What is the difference between a function marked constant and one that is not?

    constant functions can perform some action and return a value, but cannot change state (this is not yet enforced by the compiler). In other words, a constant function cannot save or update any variables within the contract or wider blockchain. These functions are called using c.someFunction(...) from geth or any other web3.js environment.

    “non-constant” functions (those lacking the constant specifier) must be called with c.someMethod.sendTransaction({from:eth.accounts[x], gas: 1000000}); That is, because they can change state, they have to have a gas payment sent along to get the work done.

  • 1
    I'll try it in a while and accept it as the correct answer if it works :) Thanks! – Harsh Pokharna Oct 27 '16 at 13:51
1

I don't think there is much wrong with the contract code as such. Are you sending the right amount of gas while sending the transaction for creating the contract. Mappings generally consume quite a lot of gas in storage.

If you are sending low gas, your contract may not be able to store values. This could be the reason that the byte code is not getting stored at the address of the contract.

  • Thanks Praveen for the response. It worked after I added the 'constant' keyword to the queryData function – Harsh Pokharna Oct 29 '16 at 16:48

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.