Building a NEO-Python API Server on Windows WSL (Ubuntu)

Step-by-Step Tutorial

Joshua Seagrave
Coinmonks
Published in
9 min readAug 24, 2018

--

Introduction

Would you like to help support the NEO Smart Economy Network in a tangible way, but don’t have a background in programming? This tutorial will guide you step-by-step through setting up a NEO-Python API server on your Windows computer. If you would like information about how to set up a development environment on Windows, please see this tutorial.

NOTE: This article assumes the reader has a basic understanding of computer functionality, but requires no previous knowledge of Python or NEO-Python code to get the server up and running. This article also assumes the user is using a Windows 10 (up-to-date) operating environment. You will need a reliable Internet connection and at least 20 GB of hard drive space in order to proceed.

Acknowledgments

Special thanks to Alex Guba, whose tutorial for Installing and Running a NEO Blockchain Node using neo-python on Windows 10 inspired a significant portion of the installation sections of this tutorial. Additionally, thank you to all the contributing members of City of Zion, who have created and continue to maintain the NEO-Python repository, especially the documentation. Finally, thanks to Jonathan Hu, whose article on how to Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host helped make this tutorial possible.

Table of Contents

Use these links to navigate to any section you desire. You can return to this table of contents by clicking the Back to Table of Contents link at the end of each section.

The next section provides background information about what an API Server is and how hosting a NEO API Server supports the NEO Smart Economy Network. If you want to start building your NEO-Python API Server now, skip to Installing Windows Dependency Programs.

Background Information

What is an API Server?

First, an API — or Application Programming Interface — receives requests from users, or programs, and sends responses after performing the requested function on the relevant, accessible data. This response is presented as easily manipulable data, so it can be integrated into programs.

In the context of the NEO Smart Economy Network, an API Server hosts a full blockchain node, which means it maintains an up-to-date copy of the entire NEO blockchain. The NEO blockchain is the data set which the API analyzes when responding to requests.

For more information about APIs see here.

How Does Hosting a NEO API Server Help?

Hosting a NEO API Server helps the NEO Smart Economy Network in two ways:

  1. By operating a NEO API Server, you help ensure a fast and efficient interface between the NEO blockchain and the users, smart contracts, and dApps constantly interacting with it. The NEO Smart Economy services an ever-increasing number of users while hosting a growing number of projects, smart contracts, and dApps, which interact with the NEO blockchain constantly. As the number of requests increase, the response time from servers can slow if they become overloaded. Your NEO API Server will help reduce API response time.
  2. Each NEO API Server hosts a full blockchain node, which means your server could act as a seed node for other servers and nodes in the network. If your node is employed as a seed node, it is being used as a block source for other servers or nodes that do not have a current copy of the NEO blockchain.

Choosing to host a NEO API Server is a tangible way to support the NEO Smart Economy Network and, as Fabio C. Canesin said, “build the infrastructure of the future.”

For more information see Fabio C. Canesin’s reddit post here.

The next section provides instructions for installing the required dependency programs on your Windows computer. If you already have these programs installed, skip to Installing NEO-Python.

Back to Table of Contents

Installing Windows Dependency Programs

NOTE: NEO-Python now supports Python 3.7!

Install Python 3.7 or Python 3.6

Python 3.7.0 can be found here:
https://www.python.org/downloads/release/python-370/

Python 3.6.6 can be found here: https://www.python.org/downloads/release/python-366/

IMPORTANT: Make sure to check the box on the installer to “Add Python 3.7 (3.6) to PATH”.

Install Git

Git can be found here: https://git-scm.com/download/win

Install Visual Studio Code

NOTE: Although not required, it is highly recommended you install Visual Studio Code in case you need to perform any subsequent trouble-shooting or server adjustment.

Visual Studio Code can be found here:
https://code.visualstudio.com/docs/?dv=win

If you use Visual Studio Code, make sure you install the Python extension when prompted.

Install the Windows Subsystem for Linux (WSL) — Ubuntu

Enable the Windows Subsystem for Linux (WSL)

  • Open the Windows search bar (start menu) and search “Turn Windows features on or off”
  • Enable “Check Windows Subsystem for Linux” and Save (reboot if necessary)

Install Ubuntu

  • Open the Windows search bar (start menu) and search “Microsoft Store”
  • Open the Microsoft Store
  • Search “Ubuntu”
  • Install Ubuntu 18.04

The next section provides instructions for installing NEO-Python on your Windows computer. If you already have NEO-Python installed, skip to Bootstrapping the NEO Blockchain.

Back to Table of Contents

Installing NEO-Python

NOTE: For the most up-to-date instructions, see the NEO-python readme.

  • Open Ubuntu
  • Set up an account when prompted
  • The first time you launch Ubuntu, it is recommended you update and upgrade Ubuntu. Use the following command:
sudo apt-get update && sudo apt-get upgrade
  • Install the required dependencies for Python 3.7:
sudo apt-get install python3.7 python3.7-dev python3.7-venv python3-pip libleveldb-dev libssl-dev g++
  • Or, install the required dependencies for Python 3.6:
sudo apt-get install python3.6 python3.6-dev python3.6-venv python3-pip libleveldb-dev libssl-dev g++
  • Clone the NEO-Python repository from Github using Git:
git clone https://github.com/CityOfZion/neo-python.git
  • Change directory into the neo-python folder, which was just created:
cd neo-python

NOTE: When accessing your neo-python folder in the future (if you shutdown and restart Ubuntu), use:

cd /mnt/c/<your_folder_path>/neo-python
  • Create a virtual environment using Python 3.7 and activate:
python3.7 -m venv venv
source venv/bin/activate
  • Or, create a virtual environment using Python 3.6 and activate:
python3.6 -m venv venv
source venv/bin/activate
  • Install the package in an editable form:
pip install wheel -e .

Make sure you remember the “.” in the above command.

The next section provides instructions for Bootstrapping the NEO Blockhain on your Windows computer. If your mainnet NEO-Python Blockchain is already up-to-date, skip to Configuring Your Local Network.

Back to Table of Contents

Bootstrapping the NEO Blockchain

If you use NEO-Python for the first time, you need to synchronize the NEO Blockchain, which would normally take a long time. Included in NEO-Python is the script np-bootstrap to automatically download a chain directory for you. You will be running your NEO-Python API server on the mainnet, so use the following command to bootstrap the mainnet blockchain:
NOTE: This process could take awhile since the downloaded file is in excess of 3 GBs.

np-bootstrap -m

After bootstrapping the ‘Main’ blockchain, you should also bootstrap the notification database. Use the following command to bootstrap the mainnet notification database:

np-bootstrap -m -n

NOTE: Once bootstrapped, the easiest way to find your “Chains” folder is by opening your C: Drive and searching “Chains”.

The next section provides instructions for configuring your local network. If your local network is already correctly configured, skip to Running Your NEO-Python API Server.

Back to Table of Contents

Configuring Your Local Network

Open Applicable Ports on Your Router

The NEO API Server requires specific ports open on your router to service the network. You will need to consult your router documentation to find specific instructions for opening these ports. This tutorial assumes you will use port 10332 for your RPC API server, port 80 for your REST API server, and port 10333 for your NEO Blockchain seed nodes. So, you will need to open these ports: 80, 10332–10333.

For a more in-depth port description, see the Port Description section here.

For more information about the NEO-Python Seedlist, see here.

Open Applicable Ports on Your Windows Firewall

Since you are running NEO-Python on Windows WSL (Ubuntu), you need to open these same ports on your Windows Firewall.

  • Open the Windows search bar (start menu) and search “Windows Defender Firewall with Advanced Security”
  • Click on “Inbound Rules”
  • Click “New Rule”
  • Select “Port”
  • Insert your specific ports (e.g. 80, 10332–10333)
  • Select “Allow the Connection”
  • Make sure all boxes are checked
  • Add a name and select “Finish”

If you need to update your rule later, you can always access and edit it in “Inbound Rules” under your specified rule name.

Back to Table of Contents

Running Your NEO-Python API Server

If you have skipped to this portion, make sure you have accessed your neo-python folder:

cd /mnt/c/<your_folder_path>/neo-python

In order to maintain the NEO Blockchain, Ubuntu will need administrative authority to write the new blocks to your ‘Chains’ folder. Give Ubuntu this ability by “rooting” your instance of Ubuntu with the following command:

sudo -s

Then enter your Ubuntu user password. Next, activate your virtual environment:

source venv/bin/activate

Use the following command to start your NEO-Python API Server:

np-api-server --mainnet --port-rpc 10332 --port-rest 80

Now you are actively helping support the NEO Smart Economy Network!

If you would like more information about the capabilities of your NEO-Python API Server, see the API server (JSON and/or REST) section here.

Back to Table of Contents

Additional Resources

Viewing Your API Server

HappyNodes Home Page

HappyNodes is an in-house NEO tool for viewing the NEO Network. After a regular update, your NEO-Python API Server will become observable on HappyNodes. Just click “Table View” from the menu bar and type in your IP address in the Address column. If you don’t know your IP Address, you can see it here: https://whatsmyip.com/.

In the future, you can more easily access your API Server by going to https://happynodes.f27.ventures/<your ID number>. Your ID number is the first column in Table View.

Maximizing Your HappyNodes Score

HappyNodes takes the average of four metrics to determine your score: Blockheight Lag, Validated Peers, Stability, and Latency. Of these four metrics, you can most easily control Stability and Validated Peers.

Stability

Increasing your Stability is easy. Just make sure you keep your NEO-Python API Server running. You can see that Stability looks at the last 100 pings of your server.

Validated Peers

Increasing your percentage of Validated Peers is also simple. In order to have a higher percentage of Validated Peers, you need to connect to more Peer Nodes. In order to do this, you can increase the number of MAXPEERS allowed for your server. The number of default MAXPEERS is 5. To increase the number of MAXPEERS, specify the number of MAXPEERS at startup. For example:

np-api-server --mainnet --port-rpc 10332 --port-rest 80 --maxpeers 50

Now, your NEO-Python API Server will search for up to 50 peer nodes to connect to, and there is a high probability that several of those peer nodes will be Validated Peers.

Questions

If you have any questions, please direct them to #python or #support on the NEO Discord, or post a comment below.

Back to Table of Contents

Get Best Software Deals Directly In Your Inbox

--

--