ethereum/pyethereum

Name: pyethereum

Owner: ethereum

Description: Next generation cryptocurrency network

Created: 2013-12-24 02:40:05.0

Updated: 2018-01-17 19:58:42.0

Pushed: 2018-01-13 21:59:02.0

Homepage: null

Size: 5504

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status

This is the Python core library of the Ethereum project.

For the python based command line client see: https://github.com/ethereum/pyethapp

Installation:
 apt-get install libssl-dev build-essential automake pkg-config libtool libffi-dev libgmp-dev libyaml-cpp-dev
clone https://github.com/ethereum/pyethereum/
yethereum
on setup.py install
Components
ethereum.pow.chain

Contains the Chain class, which can be used to manage a blockchain. Main methods are:

ethereum.state

Contains the State class, which is used to manage a state. Main methods are:

Pyethereum follows a maximally state-centric model; the ONLY information needed to process a transaction or a block is located within the state itself, allowing the actual state transition logic to be a very clean apply_transaction(state, tx) and apply_block(state, block).

There are also many methods that modify the state, eg. set_code, set_storage_data, but it is generally recommended to avoid using these, and instead modify the state ONLY through apply_transaction and apply_block.

ethereum.meta

This file contains two functions:

ethereum.messages

The main function that should be called from here is apply_transaction(state, tx).

ethereum.utils

Contains a bunch of utility functions, including:

Numerical and hex conversions Cryptography Addresses Miscellaneous
ethereum.block

Contains the Block and BlockHeader classes. Generally recommended to avoid creating blocks and block headers directly, instead using mk_head_candidate. The member variables are straightforward:

And in the header:

ethereum.transactions

Contains the Transaction class, with the following methods and values:

ethereum.tools.keys

Creates encrypted private key storages

ethereum.abi

Most compilers for HLLs (solidity, serpent, viper, etc) on top of Ethereum have the option to output an ABI declaration for a program. This is a json object that looks something like this:

name": "ecrecover(uint256,uint256,uint256,uint256)", "type": "function", "constant": false,
nputs": [{"name": "h", "type": "uint256"}, {"name": "v", "type": "uint256"}, {"name": "r", "type": "uint256"}, {"name": "s", "type": "uint256"}],
utputs": [{"name": "out", "type": "int256[]"}]},
name": "PubkeyTripleLogEvent(uint256,uint256,uint256)", "type": "event",
nputs": [{"name": "x", "type": "uint256", "indexed": false}, {"name": "y", "type": "uint256", "indexed": false}, {"name": "z", "type": "uint256", "indexed": false}]}]

You can initialize an abi.ContractTranslator object to encode and decode data for contracts as follows:

, false = True, False  
 abi.ContractTranslator(<json here>)  
ta = ct.encode('function_name', [arg1, arg2, arg3])  

You can also call ct.decode_event([topic1, topic2...], logdata) to decode a log.

RLP encoding and decoding

For any transaction or block, you can simply do:

rt rlp  
ata = rlp.encode(<tx or block>)  

To decode:

rt rlp  
 ethereum.transactions import Transaction  
decode(blob, Transaction)  

Or:

rt rlp  
 ethereum.blocks import Block  
decode(blob, Block)  
Consensus abstraction

The pyethereum codebase is designed to be maximally friendly for use across many different consensus algorithms. If you want to add a new consensus algo, you'll need to take the following steps:

Tester module

See https://github.com/ethereum/pyethereum/wiki/Using-pyethereum.tester

Tests

Run python3.6 -m pytest ethereum/tests/<filename> for any .py file in that directory. Currently all tests are passing except for a few Metropolis-specific state tests and block tests.

To make your own state tests, use the tester module as follows:

 ethereum.tools import tester as t  
rt json  
t.Chain()  
c.contract(<code>, language=<language>)  
= t.mk_state_test_prefill(c)  
o(<args>)  
 = t.mk_state_test_postfill(c, pre)  
('output.json', 'w').write(json.dumps(post, indent=4))  

To make a test filler file instead, do post = t.mk_state_test_postfill(c, pre, True).

License

See LICENSE


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.