0

I'm following Alan Buxton's tutorial, and am trying to go one step beyond and run it with a single command. but the contract's address and functions are still invisible, even after the contract is mined on my personal testnet, while in the script; however the object is fully populated after the script exits and I'm dropped into the console:

jcomeau@aspire:~$ geth --datadir ~/.ethtest0 --networkid 911657 --preload simple_test.geth console
INFO [04-01|12:33:46] Starting peer-to-peer node               instance=Geth/v1.6.7-stable-ab5646c5/linux-amd64/go1.8.1
INFO [04-01|12:33:46] Allocated cache and file handles         database=/home/jcomeau/.ethtest0/geth/chaindata cache=128 handles=1024
INFO [04-01|12:33:46] Initialised chain configuration          config="{ChainID: 1907 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> Engine: unknown}"
INFO [04-01|12:33:46] Disk storage enabled for ethash caches   dir=/home/jcomeau/.ethtest0/geth/ethash count=3
INFO [04-01|12:33:46] Disk storage enabled for ethash DAGs     dir=/home/jcomeau/.ethash               count=2
INFO [04-01|12:33:46] Initialising Ethereum protocol           versions="[63 62]" network=911657
INFO [04-01|12:33:46] Loaded most recent local header          number=413 hash=19ef00…5a6b1f td=54502927
INFO [04-01|12:33:46] Loaded most recent local full block      number=413 hash=19ef00…5a6b1f td=54502927
INFO [04-01|12:33:46] Loaded most recent local fast block      number=413 hash=19ef00…5a6b1f td=54502927
WARN [04-01|12:33:46] Blockchain not empty, fast sync disabled 
INFO [04-01|12:33:46] Starting P2P networking 
INFO [04-01|12:33:48] UDP listener up                          self=enode://199436ddf39880a2ee4a9ab3b0a7e8f51a2cf4d8f9a7863cee196e2498bd8be3d3c254d614b00530aaaf85f147c0c12acedb9239fcde2ba43ab1ae78e688d546@[::]:30303
INFO [04-01|12:33:48] RLPx listener up                         self=enode://199436ddf39880a2ee4a9ab3b0a7e8f51a2cf4d8f9a7863cee196e2498bd8be3d3c254d614b00530aaaf85f147c0c12acedb9239fcde2ba43ab1ae78e688d546@[::]:30303
INFO [04-01|12:33:48] IPC endpoint opened: /home/jcomeau/.ethtest0/geth.ipc 
Unlock account 0x51344909b248a7ec9469b13161c0afae4ed4f001
Passphrase: 
INFO [04-01|12:33:57] Submitted contract creation              fullhash=0x198fca704efd39eddf9bade902e0eed81b327fa0436627c14ea3c88884be3d62 contract=0x837fd4a24ca76786c0e722cd443fec5ebcb37313
simple.transactionHash:  0x198fca704efd39eddf9bade902e0eed81b327fa0436627c14ea3c88884be3d62
INFO [04-01|12:33:57] Updated mining threads                   threads=2
INFO [04-01|12:33:57] Transaction pool price threshold updated price=18000000000
INFO [04-01|12:33:57] Starting mining operation 
INFO [04-01|12:33:57] Commit new mining work                   number=414 txs=1 uncles=0 elapsed=2.217ms
INFO [04-01|12:38:52] Successfully sealed new block            number=414 hash=b81df0…0279c9
INFO [04-01|12:38:52] 🔨 mined potential block                  number=414 hash=b81df0…0279c9
INFO [04-01|12:38:52] Commit new mining work                   number=415 txs=0 uncles=0 elapsed=1.229ms
simple.address:  undefined
simple:  {"_eth":{"_requestManager":{"polls":{"0x51ec59128e39d6ca54d1d22b0861a337":{"data":{"method":"eth_getFilterChanges","params":["0x51ec59128e39d6ca54d1d22b0861a337"]},"id":"0x51ec59128e39d6ca54d1d22b0861a337"}},"provider":{},"timeout":{}},"accounts":["0x51344909b248a7ec9469b13161c0afae4ed4f001","0xc652252b98dc81d83757f4ba577033838cbc63aa"],"blockNumber":414,"coinbase":"0x51344909b248a7ec9469b13161c0afae4ed4f001","compile":{},"gasPrice":"18000000000","hashrate":0,"mining":false,"pendingTransactions":[],"protocolVersion":"0x3f","syncing":false},"abi":[{"constant":false,"inputs":[{"name":"_a","type":"uint256"},{"name":"_b","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_a","type":"uint256"},{"name":"_b","type":"uint256"}],"name":"arithmetics","outputs":[{"name":"o_sum","type":"uint256"},{"name":"o_product","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}],"transactionHash":"0x198fca704efd39eddf9bade902e0eed81b327fa0436627c14ea3c88884be3d62"}
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.7-stable-ab5646c5/linux-amd64/go1.8.1
coinbase: 0x51344909b248a7ec9469b13161c0afae4ed4f001
at block: 414 (Sun, 01 Apr 2018 12:33:57 PDT)
 datadir: /home/jcomeau/.ethtest0
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> simple
{
  abi: [{
      constant: false,
      inputs: [{...}, {...}],
      name: "multiply",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}, {...}],
      name: "arithmetics",
      outputs: [{...}, {...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }],
  address: "0x837fd4a24ca76786c0e722cd443fec5ebcb37313",
  transactionHash: "0x198fca704efd39eddf9bade902e0eed81b327fa0436627c14ea3c88884be3d62",
  allEvents: function(),
  arithmetics: function(),
  multiply: function()
}
> simple.arithmetics.call(5, 6)
[11, 30]

here is the test script:

loadScript("simple.abi")
loadScript("simple.bin")
console.log("simple.transactionHash: ", simple.transactionHash)
miner.start(2)
admin.sleepBlocks(1)
miner.stop()
console.log("simple.address: ", simple.address)
if (simple.address == undefined) console.log("simple: ", JSON.stringify(simple))
else console.log("simple.aritmethics.call(5, 6): ",
  simple.arithmetics.call(5, 6))

could someone please explain why the address and arithmetics attributes aren't available in the script itself, and what I can do about it? thanks.

2
  • 1
    I think your problem is that the contract is confirmed asynchronously, and you are trying to execute it before. You can try sleeping more blocks as a workaround.
    – Ismael
    Apr 2, 2018 at 5:28
  • I've slept 50 blocks, no help. I think it has something to with scope. I must be getting a copy of the object rather than a reference to the "live" object that gets the address on being mined. notice that as soon as I'm dropped into the shell the address and functions are there. Apr 2, 2018 at 18:38

1 Answer 1

1

I still haven't figured out what the problem is but have come up with an easy workaround. here is the new simple.geth test script:

console.log("simple.transactionHash: ", simple.transactionHash)
miner.start(2)
admin.sleepBlocks(1)
miner.stop()
while (simple.address == undefined) {
  receipt = eth.getTransactionReceipt(simple.transactionHash)
  if (receipt && receipt.contractAddress) {
    contract = eth.contract(simple.abi)
    simple = contract.at(receipt.contractAddress)
  } else {
    admin.sleep(1)
  }
}
console.log("simple.address: ", simple.address)
console.log("simple.aritmethics.call(5, 6): ", simple.arithmetics.call(5, 6))

called thusly:

jcomeau@aspire:~/rentacoder/jcomeau/ethereum$ make simple.test
geth --datadir ~/.ethtest0 --networkid 911657 \
 --preload simple.abi,simple.bin,simple.geth console
INFO [04-02|11:31:48] Starting peer-to-peer node               instance=Geth/v1.6.7-stable-ab5646c5/linux-amd64/go1.8.1
INFO [04-02|11:31:48] Allocated cache and file handles         database=/home/jcomeau/.ethtest0/geth/chaindata cache=128 handles=1024
INFO [04-02|11:31:48] Initialised chain configuration          config="{ChainID: 1907 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Metropolis: <nil> Engine: unknown}"
INFO [04-02|11:31:48] Disk storage enabled for ethash caches   dir=/home/jcomeau/.ethtest0/geth/ethash count=3
INFO [04-02|11:31:48] Disk storage enabled for ethash DAGs     dir=/home/jcomeau/.ethash               count=2
INFO [04-02|11:31:48] Initialising Ethereum protocol           versions="[63 62]" network=911657
INFO [04-02|11:31:48] Loaded most recent local header          number=730 hash=f5dd5f…3c79a0 td=96607269
INFO [04-02|11:31:48] Loaded most recent local full block      number=730 hash=f5dd5f…3c79a0 td=96607269
INFO [04-02|11:31:48] Loaded most recent local fast block      number=730 hash=f5dd5f…3c79a0 td=96607269
WARN [04-02|11:31:48] Blockchain not empty, fast sync disabled 
INFO [04-02|11:31:48] Starting P2P networking 
INFO [04-02|11:31:51] UDP listener up                          self=enode://199436ddf39880a2ee4a9ab3b0a7e8f51a2cf4d8f9a7863cee196e2498bd8be3d3c254d614b00530aaaf85f147c0c12acedb9239fcde2ba43ab1ae78e688d546@[::]:30303
INFO [04-02|11:31:51] IPC endpoint opened: /home/jcomeau/.ethtest0/geth.ipc 
INFO [04-02|11:31:51] RLPx listener up                         self=enode://199436ddf39880a2ee4a9ab3b0a7e8f51a2cf4d8f9a7863cee196e2498bd8be3d3c254d614b00530aaaf85f147c0c12acedb9239fcde2ba43ab1ae78e688d546@[::]:30303
Unlock account 0x51344909b248a7ec9469b13161c0afae4ed4f001
Passphrase: 
INFO [04-02|11:31:58] Submitted contract creation              fullhash=0x7fbd8ee477322afbff204365a5f718bdd28750d70dfd322e305ef4727adde48b contract=0xf9994d1de99d53b786c4d1dfb8850102c3d77806
simple.transactionHash:  0x7fbd8ee477322afbff204365a5f718bdd28750d70dfd322e305ef4727adde48b
INFO [04-02|11:31:58] Updated mining threads                   threads=2
INFO [04-02|11:31:58] Transaction pool price threshold updated price=18000000000
INFO [04-02|11:31:58] Starting mining operation 
INFO [04-02|11:31:58] Commit new mining work                   number=731 txs=1 uncles=0 elapsed=2.499ms
INFO [04-02|11:34:17] Successfully sealed new block            number=731 hash=df1ed6…e5e1dc
INFO [04-02|11:34:17] 🔨 mined potential block                  number=731 hash=df1ed6…e5e1dc
INFO [04-02|11:34:17] Commit new mining work                   number=732 txs=0 uncles=0 elapsed=939.945µs
simple.address:  0xf9994d1de99d53b786c4d1dfb8850102c3d77806
simple.aritmethics.call(5, 6):  11,30
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.7-stable-ab5646c5/linux-amd64/go1.8.1
coinbase: 0x51344909b248a7ec9469b13161c0afae4ed4f001
at block: 731 (Mon, 02 Apr 2018 11:31:58 PDT)
 datadir: /home/jcomeau/.ethtest0
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

> INFO [04-02|11:35:19] IPC endpoint closed: /home/jcomeau/.ethtest0/geth.ipc 
INFO [04-02|11:35:19] Blockchain manager stopped 
INFO [04-02|11:35:19] Stopping Ethereum protocol 
INFO [04-02|11:35:19] Ethereum protocol stopped 
INFO [04-02|11:35:19] Transaction pool stopped 
INFO [04-02|11:35:19] Database closed                          database=/home/jcomeau/.ethtest0/geth/chaindata

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.