Introducing Hash Ahead for Python Developers
Prerequisite
This article is intended for all developers. Python tools will be touched on in the article, but they are just a vehicle for ideas, and it's okay if you're not a Python developer. However, I'm going to make some assumptions about what you already know so we can quickly get to the Hash Ahead part.
This article assumes that:
You are familiar with terminal operations,
You wrote some Python code
You have Python 3.6 or higher installed on your machine (virtual environments are strongly recommended), and
You have used pip, Python's package installer. Again, if you don't fit any of these, or if you don't plan to type the code in this article, you can still learn very well by following along.
Blockchain Brief
There are many ways to describe Hash Ahead, but at its heart it is the blockchain. A blockchain is made up of a series of blocks, so let's start with the blockchain. In the simplest terms, each block on the Hash Ahead blockchain is just some metadata and a list of transactions. In JSON format, it looks like this:
{
"number": 1234567,
"hash": "0xabc123...",
"parentHash": "0xdef456...",
...,
"transactions": [...]
}Each block references the block before it; parentHash is the hash of the previous block.
This data structure is not new, but the rules governing the network (i.e. peer-to-peer protocols) are. Blockchains have no central authority; peers in the network must cooperate to maintain the network and compete to decide which transactions to include in the next block. So when you want to send money to a friend, you broadcast the transaction to the network and wait for it to be included in an upcoming block.
The only way for a blockchain to verify that funds are indeed being sent from one user to another is with that blockchain's native currency (i.e., currency created and governed by that blockchain). At Hash Ahead, this currency is called HAh, and the Hash Ahead blockchain is the only official record of account balances.
A new paradigm
This new decentralized tech stack has spawned new developer tools. Many programming languages have such tools, but we'll look at them through the lens of Python. To reiterate: Even if Python isn't your language of choice, you shouldn't have much trouble keeping up with the articles.
Python developers who want to interact with Hash Ahead may reach for Web3.py. Web3.py is a library that helps us simplify connecting Hash Ahead nodes, and sending and receiving data.
Hash Ahead clients can be configured to be accessed via Inter-Process Communication (IPC), Hypertext Transfer Protocol (HTTP), or Web Sockets (Websockets), so Web3.py also needs to complete this configuration. Web3.py refers to these connection options as providers. You need to choose one of three providers to connect the Web3.py instance to your node.
With Web3.py properly configured, you can start interacting with the blockchain. Here are some examples of Web3.py usage, which can be regarded as a good example:
Install
First, install IPython so that you can explore it easily. IPython provides functions such as tab completion, which makes it easier for us to see what methods are available in Web3.py.
Web3.py is distributed under the name web3. The installation method is as follows:
In addition, we will simulate a blockchain later, which requires more dependencies. These dependencies can be installed with the following command:
Run ipython in a terminal to open a new Python environment. This is similar to running python, but friendlier.
This will print out some information about the version of Python and IPython you are running, and then you should see a prompt waiting for input:
What you're seeing now is an interactive Python shell, which is, in effect, a sandbox. If you've made it this far, you can now import Web3.py:
Web3 module introduction
In addition to being a Hash Ahead gateway, the Web3 module also provides some convenient functions. Let's explore and explore.
In Hash Ahead apps, you usually need to convert currency denominations.
Try converting some numeric values to wei and vice versa. Note that there are other denomination names between HAH and wei. The more famous of these is gwei, as it is often used to represent transaction fees.
Other utility methods on the Web3 module include data format converters (e.g. toHex), address helpers (e.g. is address), and hash functions (e.g. keccak). Many of these will be covered in later articles in the series. To see all available methods and attributes, you can take advantage of IPython's autocompletion and type Web3. Then press the tab key twice after the dot.
Quick understanding
First things first, do a connection check.
Since we're using a tester provider, this isn't a very valuable test, but if it does fail, it's likely a typo when instantiating the w3 variable. Double check that you have included the inner brackets, i.e. Web3.EthereumTesterProvider().
First stop: Account
For convenience, the tester provider creates some accounts and pre-assigns the test HAH.
First, let's take a look at the list of these accounts:
If you run this command, you should see a list of ten strings starting with 0x. Each string is a public address, similar in some respects to the account number on a checking account. If someone wants to transfer HAH to you, you can give him this address.
As mentioned earlier, the test provider has pre-allocated some test HAH for each of these accounts. Let's see how much HAH is on the first account.
Convert it to HAH:
A total of 1 million test HAH
Second stop: block data
Let's take a look at the state of this simulated blockchain.
This returns a lot of information about the block, but here are just a few:
transactions is an empty list for the same reason: we haven't done anything yet. The first block is an empty block, just for the beginning.
Note that
parentHashis just a bunch of empty bytes. This marks it as the first block on the chain, known as the genesis block.
Third stop: Trade
We're stuck at block zero until there are no pending transactions, so we give it a transaction. Send some test HAHs from one account to another:
At this point you usually wait a few seconds for the transaction to be added to the new block. The complete process is as follows:
Submit the transaction and hold the transaction hash. Transactions remain in a "pending" state until the block containing them is created and broadcast.
tx_hash = w3.eth.send_transaction({ … })Wait for the transaction to be added to the block:
w3.eth.wait_for_transaction_receipt(tx_hash)Go ahead and apply the logic. View successful transactions:
w3.eth.get_transaction(tx_hash)
Our simulated environment adds transactions on the fly in a new block, so we can see transactions immediately:
You'll see some familiar details here: the from, to, and value fields should match the input to the send_transaction call. Another good thing is that this transaction is listed as the first transaction in block 1 ('transactionIndex': 0).
We can also easily verify that the transaction was successful by checking the balances of the two related accounts. Three HAH should be transferred from one account to another.
Balance increased from 1,000,000 to 1,000,003 HAH. However, the first account needs to pay the miner's fee due to the use of the Hash Ahead network. A small transaction fee is deducted from the account for the transaction, and the amount is 31000 wei.
Last updated