Skip to content
This repository has been archived by the owner on Aug 8, 2018. It is now read-only.

The_Console

heikoheiko edited this page May 24, 2015 · 10 revisions

The ipython console allows to introspect and interact with the running client.

Invocation

  • Start the client (with logs turned off e.g. pyethapp -l:error run, if you get distracted by logs in the console)
  • Wait three seconds
  • hit CTRL-z

Now you should see:

Entering Console
Tip: use loglevel `-l:error` to avoid logs
>> help(eth)
Python 2.7.9 (default, Jan  1 2015, 17:29:49) 
...

At this point the client is still running and interacting with other peers.

You can leave the console by hitting CRTL-d. Note that currently the client will exit after leaving the console.

The eth object

An object called eth is in the globals of the ipython session you entered. Type help(eth) for more info.

Usage Examples

Inspect the chain

eth.latest and eth.pending refer to the latest (head of chain) and pending (not yet mined) blocks respectively.

In [12]: eth.latest
Out[12]: <CachedBlock(#432199 ea119fcd)>

wait a few seconds, and you should see, a new latest block

In [13]: eth.latest
Out[13]: <CachedBlock(#432201 4466aa6b)>
In [16]: eth.latest.header
Out[16]: <BlockHeader(#432209 d1153010)>

In [20]: eth.latest.header.difficulty
Out[20]: 12519046990

In [21]: eth.latest.header.check_pow()
Out[21]: True

In [22]: eth.latest.header.coinbase.encode('hex')
Out[22]: '4d5c04cc051e1781e4f8326b8eda46532b64d9b0'

In [29]: eth.latest.to_dict()
Out[29]: 
{'header': {'bloom': '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  'coinbase': '430b90576bf17f691dd1b8420853c9fd59e330f7',
  'difficulty': '12506812416',
  'extra_data': '0x',
  'gas_limit': '3141592',
  'gas_used': '0',
  'mixhash': '0x4aac9619472743a380c1c469816ff363abfe5a841a6eff1582b6dfd5b539cff5',
  'nonce': '0x3e38d9552269831a',
  'number': '432229',
  'prevhash': '0x48af63a56d74bab1f41554431c062d01aadcc990538ed357ad365b0c936785a0',
  'receipts_root': '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  'state_root': '05153dd481ac06f7d05739ada449525fa7ebf052225fdf6d1bee90de72d564aa',
  'timestamp': '1432459127',
  'tx_list_root': '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  'uncles_hash': '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'},
 'transactions': []}

Get Transactions:

In [36]: eth.latest.get_transactions()
Out[36]: [<Transaction(ba4a)>, <Transaction(d5bd)>]

In [46]: tx = eth.latest.get_transactions()[0]

In [47]: tx.value
Out[47]: 14444999

In [48]: tx.sender.encode('hex')
Out[48]: '00fc572550f3bdfe84f72e3eaa99d02a43f69733'

In [49]: tx.log_dict()
Out[49]: 
{'data': '',
 'gasprice': 10000000000000,
 'hash': '6cf99d59ee667dd56cdc34c5b4f5349fdc2377ea9b715c27d1a9d2ba17796636',
 'nonce': 5047,
 'r': 37949441106074605966811028217872197823963701877937951182924847227904959108149L,
 's': 7840037590794696459998581397264706147072541541675627162552725158699447211262L,
 'sender': '00fc572550f3bdfe84f72e3eaa99d02a43f69733',
 'startgas': 90000,
 'to': 'd63b635a458b99f7e900477e2d261d5d13e45d59',
 'v': 27,
 'value': 14444999}

Creating Transactions

In [53]: eth.transact?
Signature: eth.transact(to, value=0, data='', sender=None, startgas=25000, gasprice=10000000000000)
In [55]: tx = eth.transact('d63b635a458b99f7e900477e2d261d5d13e45d59', value=100)
In [56]: eth.find_transaction(tx)
Out[56]: {'block': <Block(#432282 7dd76ad5)>, 'index': 0, 'tx': <Transaction(3384)>}
In [59]: eth.find_transaction(tx)['block'].get_transactions()[0] == tx
Out[59]: True

Creating Contracts

In [60]: code ="""
   ....: contract NameReg  {
   ....:        event AddressRegistered(bytes32 indexed name, address indexed account);
   ....:        mapping (address => bytes32) toName;
   ....: 
   ....:        function register(bytes32 name) {
   ....:                toName[msg.sender] = name;
   ....:                AddressRegistered(name, msg.sender);
   ....:        }
   ....: 
   ....:        function resolve(address addr) constant returns (bytes32 name) {
   ....:                return toName[addr];
   ....:        }
   ....: }
   ....: """

In [61]: evm_code = solidity.compile(code)
In [62]: abi = solidity.mk_full_signature(code)
In [64]: eth.transact?
Signature: eth.transact(to, value=0, data='', sender=None, startgas=25000, gasprice=10000000000000)
In [67]: tx = eth.transact(to='', data=evm_code, startgas=500000)
In [68]: eth.find_transaction(tx)
Out[68]: {'block': <Block(#432447 cf97e384)>, 'index': 0, 'tx': <Transaction(1553)>}
In [69]: tx.creates.encode('hex')
Out[69]: '01ac2517022e28782fbbbe01e7d614cf0b21a89e'
In [70]: eth.new_contract?
Signature: eth.new_contract(abi, address, sender=None)
In [71]: namereg = eth.new_contract(abi, tx.creates)
In [72]: tx = namereg.register('alice')
In [73]: eth.find_transaction(tx)
Out[73]: {}
In [75]: eth.find_transaction(tx)
Out[75]: {'block': <Block(#432458 5e1a914b)>, 'index': 0, 'tx': <Transaction(2b2f)>}
In [76]: namereg.resolve(eth.coinbase)
Out[76]: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Note: The last output should actually start with 'alice'. FIXME!