[Ethereum] Call write function, which doesn’t change contract value

blockchainethergo-ethereumsolidityweb3js

I'm new in smart contracts and faced with one problem.
After deploying contract to my test local blockchain network, i try to set new value via setter, after that I call get method of contract and it return old value.

This is my contract,

pragma solidity ^0.4.10;

contract Storage {
    uint256 storedData;

    function set(uint256 data) {
        storedData = data;
    }

    function get() constant returns (uint256) {
        return storedData;
    }
}

What steps I did:

  1. Deploy contract via web3j in java code, after deploying I see address of my contract,so it's deployed
  2. After than in geth console create variable abi:
    var abi = [{"constant":false,"inputs":[{"name":"data","type":"uint256"}],"name":"set","outputs":[],"payable":false,"statemutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"statemutability":"view","type":"function"}];
  3. load contract: var storage = eth.contract(abi)
  4. load contract by adress: var contractInstance = storage.at("0x45564fe8d90e542f3f896dd43b374210bc109299");
  5. try calling method get(): var result = contractInstance.get();
    it's output 0
  6. then try set new value: var result = contractInstance.set(566);
    After that I see something like this:
    INFO [09-07|08:42:07] Submitted transaction fullhash=0x7110b3fdfc8005187ed367beb8275a6f4b926f4852f726784219a5a3aa9c8d9e recipient=0x45564fe8d90e542f3f896dd43b374210bc109299
    "0x7110b3fdfc8005187ed367beb8275a6f4b926f4852f726784219a5a3aa9c8d9e"
  7. Next I start mining: miner.start(), at the beginning of mining a see log like this: INFO [09-07|08:44:15] Commit new mining work number=38 txs=1 uncles=0 elapsed=1.044ms
    INFO [09-07|08:44:15] Successfully sealed new block number=38 hash=e62548…39ad89
    INFO [09-07|08:44:15] 🔗 block reached canonical chain number=33 hash=9a6ae1…9cb82d
    INFO [09-07|08:44:15] 🔨 mined potential block number=38 hash=e62548…39ad89
    INFO [09-07|08:44:15] Commit new mining work number=39 txs=0 uncles=0 elapsed=182.5µs
    INFO [09-07|08:44:15] Successfully sealed new block number=39 hash=87b682…f52e39
    INFO [09-07|08:44:15] 🔗 block reached canonical chain number=34 hash=cc9a2d…36838a
    INFO [09-07|08:44:15] 🔨 mined potential block number=39 hash=87b682…f52e39
    INFO [09-07|08:44:15] Mining too far in the future wait=2s
  8. Then I stop mining: miner.stop()
    9.Then call contractInstance.get(): and see the same value 0

I'm confused(
Thanks everybody for help!

Best Answer

Use below Commands:

storage.get.call() 0 storage.set.sendTransaction(42, {from: eth.accounts[0], gas: 1000000}) "0x7a54ab329fcbf551432eb78c4b2a1ff48fc8b9f9aa23d94fa86330e5c1d711f3" storage.get.call() 42

Reference Link: https://medium.com/@gus_tavo_guim/deploying-a-smart-contract-the-hard-way-8aae778d4f2a