From the introduction of pyethapp to the execution of contract

Purpose of this article

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.

What is Ethereum

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.

What is a 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.

reference

-Bitcoin and blockchain technology that supports cryptocurrencies (NTT Publishing)

Python virtual environment construction

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

reference

Install pyethapp

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

reference

--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

Installation of pyethereum

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

reference

Run pyethapp

The data directory is located in "~ / .config / pyethapp / leveldb" by default.

Account creation

(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

Run on console

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)>]

reference

installation of serpent

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 ++.

Installation

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

Run

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

reference

Reference summary

--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

Recommended Posts

From the introduction of pyethapp to the execution of contract
Introduction to Scapy ① (From installation to execution of Scapy)
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
[Ansible installation procedure] From installation to execution of playbook
Setting to output the log of cron execution
Change the decimal point of logging from, to.
The story of moving from Pipenv to Poetry
The wall of changing the Django service from Python 2.7 to Python 3
Used from the introduction of Node.js in WSL environment
I want to grep the execution result of strace
[Introduction to cx_Oracle] (Part 11) Basics of PL / SQL Execution
[Introduction to Python] Basic usage of the library matplotlib
[Introduction to cx_Oracle] Overview of cx_Oracle
Summary from the beginning to Chapter 1 of the introduction to design patterns learned in the Java language
An introduction to object orientation-let's change the internal state of an object
Introduction to Statistics The University of Tokyo Press Chapter 2 Exercises
How to calculate the amount of calculation learned from ABC134-D
[Introduction to matplotlib] Read the end time from COVID-19 data ♬
The story of copying data from S3 to Google's TeamDrive
After all, the story of returning from Linux to Windows
[Introduction to logarithmic graph] Predict the end time of each country from the logarithmic graph of infection number data ♬
Take the execution log of Celery
Existence from the viewpoint of Python
[Introduction] From installing kibana to starting
Python (from first time to execution)
From Ubuntu 20.04 introduction to environment construction
Supplement to the explanation of vscode
[Python] Try to graph from the image of Ring Fit [OCR]
[Introduction to Python] I compared the naming conventions of C # and Python.
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
Read all the contents of proc / [pid] ~ From setgroups to wchan ~
Read all the contents of proc / [pid] ~ From cwd to loginuid ~
Read all the contents of proc / [pid] ~ From map_files to numa_maps ~
Read all the contents of proc / [pid] ~ From oom_adj to sessionid ~
[Python3] Define a decorator to measure the execution time of a function
How to monitor the execution status of sqlldr with the pv command
[Introduction to pytorch-lightning] Autoencoder of MNIST and Cifar10 made from scratch ♬
[Introduction to SIR model] Consider the fitting result of Diamond Princess ♬
Read all the contents of proc / [pid] ~ from attr to cpuset ~
Chapter 1 Introduction to Python Cut out only the good points of deep learning made from scratch
The story of trying to reconnect the client
About the order of learning programming languages (from beginner to intermediate) Part 2
Script to change the description of fasta
How to check the version of Django
The story of adding MeCab to ubuntu 16.04
Learning notes from the beginning of Python 1
Wikipedia goes from the era of writing to the era of creation ~ Automatic generation from Twitter
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Build the execution environment of Jupyter Lab
From MuJoCo license acquisition, renewal to execution
Probably the most straightforward introduction to TensorFlow
From Attention of Zero Tsuku 2 to Transformer
How to operate Linux from the console
How to access the Datastore from the outside
Get the song name from the title of the video you tried to sing
[Introduction to Python] Thorough explanation of the character string type used in Python!
The story of pep8 changing to pycodestyle
[Introduction to Python] What is the method of repeating with the continue statement?
Learning notes from the beginning of Python 2
[Introduction to Data Scientists] Basics of Python ♬
Push notifications to your smartphone from the Linux command line or scripts. (Introduction of a tool called ntfy)