4
contract SimpleStorage {
    mapping(bytes32 => string) storedData;

    function set(bytes32 key, string value) {
        storedData[key] = value;
    }

    function get(bytes32 key) constant returns (string retVal) {
        return storedData[key];
    } 
}

On the geth console:

  1. mySource = 'contract SimpleStorage { mapping(bytes32 => string) storedData; function set(bytes32 key, string value) { storedData[key] = value;} function get(bytes32 key) constant returns (string retVal) { return storedData[key];}}'

  2. myCompiled = web3.eth.compile.solidity(mySource)

  3. myContract = web3.eth.contract(myCompiled.SimpleStorage.info.abiDefinition);

  4. SimpleStorage = myContract.new({from:web3.eth.accounts[0], data: myCompiled.SimpleStorage.code, gas: 10000000})

  5. miner.start()

  6. SimpleStorage.address (I get "0x03b6676ec81168749af1a92783e67bf3b065add3")

  7. SimpleStorage.set.sendTransaction("hash", "value", {from: eth.accounts[0]})

  8. miner.start()

  9. miner.stop()

  10. eth.getStorageAt("0x03b6676ec81168749af1a92783e67bf3b065add3")

I get:

0x0000000000000000000000000000000000000000000000000000000000000000

Why has the contract storage not changed?

  • Why don't you just call get function ? – Laxmana May 22 at 12:38
1

The contract storage changed, you should just try the following command: eth.getStorageAt("0x03b6676ec81168749af1a92783e67bf3b065add3",web3.sha3(0x68617368000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,{"encoding":"hex"}))

The first argument is the address of your contract, while the second one is the position in storage where your value is stored. In this case the value is calculated as sha3(key+pos) where + is concatenation, key is 0x6861736800000000000000000000000000000000000000000000000000000000 (this is the result of web3.fromAscii("hash") and padded to 256 bits), while pos is 0000000000000000000000000000000000000000000000000000000000000000.

You will get 0x76616c756500000000000000000000000000000000000000000000000000000a, which is "value" but in hex format.

More info about storage layout can be found here.

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.