© Karan Singh Garewal 2020
K. S. GarewalPractical Blockchains and Cryptocurrencieshttps://doi.org/10.1007/978-1-4842-5893-4_7

7. The Helium Cryptocurrency Project

Karan Singh Garewal1 
(1)
Toronto, ON, Canada
 

In this chapter, we shall begin the construction of the Helium cryptocurrency. Our implementation is going to include all of the features that you expect in a production-quality cryptocurrency. Helium is modeled after Bitcoin, which is the canonical standard. We will implement Helium in Python. Python is a simple yet powerful language whose syntax is lucid and exceptionally easy to understand. Python is an ideal language for clearly exposing the algorithms and techniques that constitute the backbone of a cryptocurrency. The downside of using Python is that it is an interpreted language and thus inherently slow. This limitation makes it unsuitable for a production environment.

The Python code implementation of Helium should provide you with a reliable blueprint to implement a cryptocurrency in some other high-performance, compiled language, such as C, C++, Go, or Rust, if you are so inclined.1

If you are primarily interested in the theory of blockchains and cryptocurrencies, just read the “Helium Configuration” section of this chapter.

Python Installation and the Virtual Environment

The very first order of business is the creation of a virtual environment for Helium. You are going to need a Python version greater than or equal to 3.8. Appendix 1 of this book contains the instructions for creating such a virtual environment.

You can create your virtual environment inside any directory (folder), but if you want to follow this book exactly, then I suggest creating the virtual environment in the /var/www/ directory.2 In addition, install Python version 3.8 in your virtual environment. Your Helium virtual environment will then be inside the following directory (folder):
/var/www/helium/

We will call the directory /var/www/helium/ the root of the virtual environment or equivalently the root of the Helium project, and on many occasions, I will simply refer to this directory as the Helium root.

Inside the root directory, create the following 12 sub-directories: block, chainstate, config, crypt, data, hnetwork, mining, simulated_network, testnet, transactions, unit_tests, and wallet. The block directory will hold our blockchain implementation. The crypt directory will have the cryptographic functions that Helium requires, and our unit tests will reside inside the unit_tests directory.

The data directory will hold Helium blocks as well as other data. I will discuss the other directories in subsequent chapters.

In order to access the Python modules in these directories, we must add the paths to these directories to the Python search path. Make sure that you have activated your virtual environment as follows. From the root directory, do
source virtual/bin/activate
From the root directory, traverse to the virtual/lib/python3.8/site-packages/ directory.3 Create a file called helium.pth inside this directory, and then add the following lines to this file:
/var/www/helium/block/
/var/www/helium/chainstate
/var/www/helium/config/
/var/www/helium/crypt/
/var/www/helium/data/
/var/www/helium/hnetwork/
/var/www/helium/mining/
/var/www/helium/simulated_network
/var/www/helium/testnet/
/var/www/helium/transactions/
/var/www/helium/unit_tests/
/var/www/helium/wallet
Save this file and then deactivate and activate your environment from the root directory4
$(virtual) deactivate
$ /bin/activate
You should be using Git to periodically commit your work to a repository. Traverse to the root directory, initialize Git, and do an initial commit:5
$(virtual) git init
$(virtual) git add .
$(virtual) git commit -m "initial Helium commit, virtual environment created"
You can view your commit history with
$(virtual) git log

git will enable you to roll back to a previous version in case you make an error. You may want to commit source code changes often.

Helium Configuration

Helium uses several vital configuration parameters that define the characteristics and behavior of this cryptocurrency. The following is a brief description of these parameters. Do not be concerned if you do not understand the meaning of some of these parameters yet. Their meaning and significance will become clearer in our subsequent exposition.

The Helium Version Number

VERSION_NUMBER = 1

This is the current version number of the Helium application.

The Maximum Number of Helium Coins

MAX_HELIUM_COINS = 21_000_000

This is the maximum number of Helium coins that can come into existence. We will call a helium coin a helium, and a number of helium coins will be called heliums.

A fixed number of heliums are initially created and distributed in the genesis block. The genesis block is a hard-coded block that bootstraps the block creation process. In Bitcoin, Satoshi Nakamoto created and distributed 50 bitcoins in the genesis block.

Subsequently, new heliums are created as blocks are added to the Helium blockchain. Note that the maximum number of heliums that can be created is identical to the maximum number of bitcoins that can be produced in the Bitcoin ecosystem.

The Smallest Helium Currency Unit

HELIUM_CENT = 1/100_000_000

HELIUM_CENT is the smallest subdivision of a helium that is possible. Observe that a hundred million Helium cents is equal to a helium. This definition corresponds to the definition of a satoshi in Bitcoin.

Helium Block Size

MAX_BLOCK_SIZE = 1_000_000

MAX_BLOCK_SIZE is the maximum size of a Helium block in bytes. The maximum size of a block determines the maximum number of Helium transactions that can be included in a block6 and a fortiori the average number of transactions in a block.

The Maximum Transaction Inputs

A Helium transaction collects unspent values from previous transactions and then transfers these values to one or more recipients of value. The Max-Inputs parameter limits the maximum number of inputs in a transaction. This limitation promotes efficient transaction processing and helps avert denial-of-service attacks:
MAX_INPUTS = 10

The Maximum Transaction Outputs

A Helium transaction transfers values to one or more recipients of value. The Max_Outputs parameter limits the maximum number of recipients in a transaction:
MAX_OUTPUTS = 10

The Locktime Interval

MAX_LOCKTIME = 60*1440*7

An entity creating a transaction can specify that this transaction is not to be processed until a certain amount of time has elapsed. This will prevent miners from including this transaction in a block to be mined until this time interval has elapsed. Elapsed time is in seconds.

MAX_LOCKTIME specifies the maximum amount of time in seconds that a transaction can be locked from the time of its creation. We do not want to indefinitely lock transactions or lock transactions for very long periods of time. This can give rise to denial-of-service attacks as transaction caches maintained by miners become clogged with future transactions.

The preceding configuration parameter sets the maximum period of time that a transaction can be locked to seven days.

The Coinbase Interval

COINBASE_INTERVAL = 100

This positive integer specifies the number of new blocks that must be mined after a previous reference block, before a coinbase transaction in the reference block can be spent. When a miner mines a block, he is rewarded with the issuance of a fixed amount of new cryptocurrency. This new currency is presented to the miner in a coinbase transaction in the block. The coinbase interval parameter precludes a miner from spending a coinbase transaction until a certain number of new blocks have been added to the blockchain.

In Helium, a new block is mined every ten minutes approximately, so Helium coinbase transactions are locked for approximately 1000 minutes.

Nonce

NONCE = 0

This is a seed value. NONCE is the starting value that will be used in mining proof-of-work computations. We will discuss this use of nonces subsequently.

The Difficulty Number

DIFFICULTY_BITS   = 20
DIFFICULTY_NUMBER =  1/ (10 ** (256 - DIFFICULTY_BITS))

The DIFFICULTY_BITS parameter lets us adjust the DIFFICULTY_NUMBER. In Helium as well as Bitcoin, mining a block involves solving a difficult mathematical problem. The DIFFICULTY_NUMBER determines how difficult this problem is and hence the average amount of time it will take to solve this problem.

A block is said to be mined when this difficult problem is solved by a Helium mining node. The node that solves this problem is entitled to append the mined block to the blockchain and is entitled to a reward of freshly mined heliums as well as the transaction fees attached to all of the transactions in the mined block.

Retarget Interval

RETARGET_INTERVAL = 1000

Helium attempts to mine new blocks every ten minutes. After 1000 new blocks have been mined, Helium uses an algorithm to adjust the DIFFICULTY_NUMBER so that blocks are mined every ten minutes on average.

The Bitcoin algorithm also attempts to mine a new block every ten minutes on average. Litecoin, which is quite similar to Bitcoin, attempts to mine new blocks every two minutes on average.

The Mining Reward

MINING_REWARD = 5_000_000_000

The MINING_REWARD is the amount of new heliums in HELIUM_CENTS that are created and awarded to a mining node that successfully mines a block. The initial mining reward in Helium is five billion HELIUM_CENTS or five Helium coins (five heliums). This reward corresponds to the initial mining reward in Bitcoin.

In Helium as well as Bitcoin, the mining reward algorithm halves the mining reward periodically. The point in time when the reward is halved depends upon the number of blocks that have already been mined. Because of this periodic halving, the mining reward asymptotically approaches zero.

A cryptocurrency mining algorithm does not have to halve the reward periodically. There are cryptocurrencies where the number of coins that are created remains constant or varies according to some formula.

Bitcoin came into existence on January 3, 2009, when Satoshi Nakamoto created and mined the genesis block. The mining reward was 50 bitcoins for this block and every block that was mined thereafter. In 2012, this mining reward was halved to 25, and in 2016, the reward was again halved to 12.5 bitcoins. In 2020, the reward was halved to 6.25 bitcoins.

Since the only way to create new heliums or bitcoins is through the mining process (aside from an initial distribution in the genesis block), the mining algorithms in these two cryptocurrencies create an upper limit on the number of heliums or bitcoins that can be created. Thus, these algorithms create a scarcity of heliums and bitcoins over time, and this scarcity theoretically enhances the value of these cryptocurrencies.

Reward Interval

REWARD_INTERVAL = 210,000

The REWARD_INTERVAL determines the number of additional blocks that must be mined in order to halve the mining reward. For example, after two hundred and ten thousand new blocks have been mined, the mining reward will be halved. This number is identical to the interval used in Bitcoin.

Helium Configuration Module

We create a configuration module for Helium as follows. Create a file hconfig.py in the config directory and copy the following source code into this file:
"""
hconfig.py:  parameters that are used to configure Helium.
"""
conf = {
    # The Helium version no.
    'VERSION_NO': 1,
    # The maximum number of Helium coins that can be mined
    'MAX_HELIUM_COINS': 21_000_000,
    # The smallest Helium currency unit in terms of one Helium coin
    'HELIUM_CENT':  1/100_000_000,
    # The maximum size of a Helium block in bytes
    'MAX_BLOCK_SIZE': 1_000_000,
    # The maximum amount of time in seconds that a transaction can be locked
    'MAX_LOCKTIME': 30*1440*60,
    # The maximum number of Inputs in a Helium Transaction
    'MAX_INPUTS': 10,
    # The maximum number of Outputs in a Helium Transaction
    'MAX_OUTPUTS': 10,
    # The number of new blocks from a reference block that must be mined before
    # coinbase transaction  in the previous reference block can be spent
    'COINBASE_INTERVAL': 100,
    # The starting nonce value for the mining proof of work computations
    'NONCE': 0,
    #
    # Difficulty Number used in mining proof of work computations
    #
    'DIFFICULTY_BITS': 20,
    'DIFFICULTY_NUMBER':  1/ (10 ** (256 - 20)),
    #
    # Retargeting interval in blocks in order to adjust the DIFFICULTY_NUMBER
    #
    'RETARGET_INTERVAL': 1000,
    #
    #Mining Reward
    #
    'MINING_REWARD': 5_000_000_000,
    #
    # Mining reward halving interval in blocks
    #
    'REWARD_INTERVAL': 210_000
}

Conclusion

In this chapter, we have commenced the construction of the Helium cryptocurrency by specifying important configuration parameters in a configuration module. These parameters bear upon the transaction processing, mining, and currency creation characteristics of Helium. They are, of course, modifiable by the software developer. Our Helium configuration is modeled after Bitcoin.

In the next chapter, we will develop the Helium cryptographic module. This module is used to sustain all of the cryptographic operations that are performed by Helium. After this, we will develop the Helium blockchain.

References

Bitcoin, retrieved from https://github.com/bitcoin/bitcoin, last accessed on June 29, 2020

Bitcoin Developer Reference, bitcoin.org, retrieved from https://developer.bitcoin.org/reference/, last accessed on June 29, 2020

Bitcoin Improvement Proposals, https://github.com/bitcoin/bips, last accessed on June 29, 2020

The Litecoin Project, https://github.com/litecoin-project/litecoin/tree/master/src/leveldb, last accessed on March 28, 2020

Dhillon V., Metcalfe D., Hooper Max, 1st ed., Blockchain Enabled Applications, Apress, 2017

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.187.155