/docs/guides/eth/quickstart.rst

https://github.com/pipermerriam/py-evm · ReStructuredText · 89 lines · 57 code · 32 blank · 0 comment · 0 complexity · 3d1f91d7b681b7944a2f4f7676cb7f51 MD5 · raw file

  1. Quickstart
  2. ====================
  3. .. note::
  4. This quickstart is aspirational. The code examples may not work
  5. yet.
  6. Installation
  7. ------------
  8. .. code:: sh
  9. pip install py-evm
  10. Sync and interact with the Ropsten chain
  11. ----------------------------------------
  12. Currently we only provide a light client that will sync only block headers,
  13. although it can fetch block bodies on demand. The easiest way to try it is by
  14. running the lightchain_shell, which will run the LightPeerChain in the background
  15. and let you use the python interpreter to interact with it:
  16. .. code:: sh
  17. $ python -i -m eth.lightchain_shell -db /tmp/testnet.db
  18. That will immediately give you a python shell, with a chain variable that you
  19. can use even before it has finished syncing:
  20. .. code:: python
  21. >>> chain.get_canonical_head()
  22. <BlockHeader #2200794 e3f9c6bb>
  23. Some :class:`~p2p.lightchain.LightPeerChain` methods (e.g. those that need data
  24. from block bodies) are coroutines that need to be executed by asyncio's event
  25. loop, so for those we provide a helper that will schedule their execution and
  26. wait for the result:
  27. .. code:: python
  28. >>> wait_for_result(chain.get_canonical_block_by_number(42))
  29. <FrontierBlock(#Block #42)>
  30. Accessing an existing chain database
  31. ------------------------------------
  32. The :class:`~eth.chains.chain.Chain` object manages the series of fork rules
  33. contained in every blockchain. It requires that you define the VM ranges.
  34. Some pre-built chains are available for your convenience.
  35. To access the Mainnet chain you can use:
  36. .. code:: python
  37. from eth import MainnetChain
  38. from eth.chains.mainnet import MAINNET_GENESIS_HEADER
  39. from eth.db.backends.level import LevelDB
  40. from eth.db.chain import ChainDB
  41. # Read the previously saved chain database
  42. chaindb = ChainDB(LevelDB('/tmp/mainnet.db'))
  43. # Load the saved database into a mainnet chain object
  44. chain = MainnetChain(chaindb)
  45. Then you can read data about the chain that you already downloaded.
  46. For example:
  47. .. code:: python
  48. highest_block_num = chain.get_canonical_head().block_number
  49. block1 = chain.get_canonical_block_by_number(1)
  50. assert block1.number == 1
  51. blockhash = block1.hash()
  52. vm = chain.get_vm()
  53. blockgas = vm.get_cumulative_gas_used(block1)
  54. The methods available on the block are variable. They depend on what fork you're on.
  55. The mainnet follows "Frontier" rules at the beginning, then Homestead, and so on.
  56. To see block features for Frontier, see the API for
  57. :class:`~eth.vm.forks.frontier.blocks.FrontierBlock`.