We have summarized from the introduction of pyethapp for using Ethereum with Python to the execution of contract. In this article, I will introduce up to the point of contracting with Ethereum according to the following flow.
--Building a virtual environment for Python --Installing pyethapp and pyethereum --Install serpent --Compile and execute contract
In addition, the commands executed in this article have been confirmed to work on ubuntu (16LTS) and CentOS (6.9).
Ethereum I will briefly explain the related terms of Ethereum that appear in this article.
Ethereum is a Turing-complete contract processing and enforcement platform for blockchain ledgers, with completely independent specifications and implementations rather than Bitcoin duplication. It has a built-in currency called ether and you will be asked to pay when you execute the contract.
The Ethereum blockchain is recorded by the contract. contract is a low-level language, written in Turing complete code, similar to bytecode. A contract is a program that runs on the Ethereum system and allows you to save, pay, receive and save data. It can also act as a decentralized and autonomous software agent to perform computationally unlimited length processing.
-Bitcoin and blockchain technology that supports cryptocurrencies (NTT Publishing)
Build a Python virtual environment to launch pyethapp.
--Virtual environment construction
$ pip install virtualenv
$ pip install virtualenvwrapper
//Reboot the terminal and virtualenvwrapper.make sh
$ reset
// virtualenvwrapper.Check the location of sh
$ which virtualenvwrapper.sh
/usr/bin/virtualenvwrapper.sh
$ source /usr/bin/virtualenvwrapper.sh
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ mkvirtualenv pyethapp
$ cd $WORKON_HOME/pyethapp/bin
$ source activate
--End
(pyethapp)$ deactivate
pyethapp is a module that implements Ethereum based on Python. In git, only the installation procedure with ubuntu is written, but I was able to install it with CentOS.
Ubuntu/Debian
$ apt-get install build-essential automake pkg-config libtool libffi-dev libgmp-dev
(pyethapp)$ pip install pyethapp
CentOS
$ yum groupinstall "Development Tools"
(pyethapp)$ yum install kernel-devel kernel-headers
(pyethapp)$ yum install automake libtool libffi libffi-devel gmp
--There is no related package Error countermeasures (mainly CentOS) http://stackoverflow.com/questions/31508612/pip-install-unable-to-find-ffi-h-even-though-it-recognizes-libffi http://www.techblogistech.com/2012/03/installing-build-essentials-in-centos-make-gcc-gdb/ http://blog.goo.ne.jp/asakurah/e/7f9cdf8cbada8841c388ece8cd421432
pyethereum is the core library for processing blockchain, ethereum and mining. By installing this in pyethapp, you can use each library.
(pyethapp)$ cd pyethapp
(pyethapp)$ git clone https://github.com/ethereum/pyethereum/
(pyethapp)$ cd pyethereum
(pyethapp)$ python setup.py install
The data directory is located in "~ / .config / pyethapp / leveldb" by default.
(pyethapp)$ cd pyethapp
(pyethapp)$ pyethapp -d "~/.config/pyethapp/leveldb" account new
//Enter the private key password twice
Password to encrypt private key:
Repeat for confirmation:
...
Account creation successful
Address: 8d69118ca81b5878ad22d40b701ea9ae88190a60
Id: None
Specify testnet in --profile to connect in the test environment. IPython is started by executing it with the console as an argument.
(pyethapp)$ workon pyethapp
(pyethapp)$ pyethapp -d "~/.config/pyethapp/leveldb" --profile testnet run --console
You can use the eth object without importing the library.
$ eth.latest
<CachedBlock(#0 0cd786a2)>
$ eth.latest.get_balance(eth.coinbase)
0
The pyethereum library can be used on the console by importing it in the same way as IPython.
//Import utilities for Ethereum
$ import ethereum.utils as u
//Import test module
$ import ethereum.tester as t
//Address of test account 1
$ t.a0
'\x82\xa9x\xb3\xf5\x96*[\tW\xd9\xee\x9e\xefG.\xe5[B\xf1'
$ u.is_numeric(t.a0)
False
$ u.is_string(t.a0)
True
//Private key for test account 1(k0)Convert from to address
$ u.privtoaddr(t.k0)
'\x82\xa9x\xb3\xf5\x96*[\tW\xd9\xee\x9e\xefG.\xe5[B\xf1'
//Create a new blockchain state
$ s=t.state()
//Check all blockchains
$ s.blocks
[<Block(#0 565a32e6)>]
//Increase chains by mining
$ s.mine(n=1)
$ s.blocks
[<Block(#0 50ba5c12)>, <Block(#1 6c0f699b)>]
serpent is a high-level programming language for writing Ethereum contracts. It is a language specialized in contract programming, which is similar in writing method to Python, allows clean and easy coding, and combines many advantages of low-level languages with a simple programming style. The serpent compiler is written in C ++.
There are two installation methods, but it doesn't matter which one.
--Install from source code
$ git clone https://github.com/ethereum/serpent.git
$ cd serpent
$ git checkout develop
$ make && sudo make install
$ python setup.py install
--Install on pyethapp
(pyethapp)$ git clone https://github.com/ethereum/pyethereum.git
(pyethapp)$ cd pyethereum
(pyethapp)$ git checkout develop
(pyethapp)$ pip install -r requirements.txt
(pyethapp)$ python setup.py install
Create the following serpent file directly under the pyethapp project.
mul2.se
def double(x):
return (x*2)
Compile using serpent.
$ serpent compile mul2.se
//Bytecode returned
6100...
On the pyethapp console, after compilation, the contract can be run consuming gas.
$ d=s.abi_contract("mul2.se")
$ s.block.gas_used
98065
$ d.double(2)
4
$ s.block.gas_used
119618
Ethereum Technology that supports Bitcoin and blockchain cryptocurrencies (NTT Publishing) https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
virtualenvwrapper http://virtualenvwrapper.readthedocs.io/en/latest/
pyethapp https://github.com/ethereum/pyethapp https://github.com/ethereum/pyethapp/wiki/The_Console
--There is no related package Error countermeasures (mainly CentOS) http://stackoverflow.com/questions/31508612/pip-install-unable-to-find-ffi-h-even-though-it-recognizes-libffi http://www.techblogistech.com/2012/03/installing-build-essentials-in-centos-make-gcc-gdb/ http://blog.goo.ne.jp/asakurah/e/7f9cdf8cbada8841c388ece8cd421432
pyethereum https://github.com/ethereum/pyethereum https://github.com/ethereum/pyethereum/wiki/Using-pyethereum.tester
Serpent https://github.com/ethereum/wiki/wiki/Serpent http://mc2-umd.github.io/ethereumlab/docs/serpent_tutorial.pdf
Recommended Posts