4

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!

  • Your code is correct, and i test everything is ok. do you confirm mining is success? – BinGoBinBin Sep 6 '17 at 15:54
  • @BinGoBinBin I think yes. There is my log, when I start mining. Starting mining operation INFO [09-06|18:10:01] Commit new mining work number=1400 txs=6 uncles=0 elapsed=1.092ms INFO [09-06|18:10:01] Successfully sealed new block number=1400 hash=8cd59b…0bb28c INFO [09-06|18:10:01] 🔗 block reached canonical chain number=1395 hash=81696a…5fc7fa INFO [09-06|18:10:01] 🔨 mined potential block number=1400 hash=8cd59b…0bb28c – Miller Sep 6 '17 at 19:37
  • Can you add the exact way you're calling these functions through web3? Also, between miner.start() and calling these functions, you should see something like "Transaction added to pool". Can you confirm that you see something like that? – wtk219 Sep 6 '17 at 20:50
  • @wtk219 I update my question, and provide more detailed steps that I did. – Miller Sep 7 '17 at 5:46
  • Try giving storedData a default value before you try to change it, that will tell you whether your problem is the reading or the writing. – Edmund Edgar Sep 7 '17 at 7:40
0

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

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.