Skip to content

Commit

Permalink
Theory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Petteri Nevavuori committed Apr 18, 2018
1 parent fffd894 commit b2ff6d0
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions blockchain/II. Building a General Purpose Cryptocurrency.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Created by Petteri Nevavuori <<[email protected]>>.*\n",
"\n",
"---\n",
"\n",
"# II. Building a General Purpose Cryptocurrency\n",
"\n",
"In this notebook we will go through building a general purpose Cryptocurrency. We will reuse parts of code from the previous notebook *Building a General Purpose Blockchain* and develop those ideas further. This notebook is a bit different from the first as there will be some related notes written down for easier grasping of the concept of cryptocurrency."
]
},
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#1.-Introduction-to-the-Concept-of-Cryptocurrency\" data-toc-modified-id=\"1.-Introduction-to-the-Concept-of-Cryptocurrency-1\">1. Introduction to the Concept of Cryptocurrency</a></span><ul class=\"toc-item\"><li><span><a href=\"#1.1-Background\" data-toc-modified-id=\"1.1-Background-1.1\">1.1 Background</a></span></li><li><span><a href=\"#1.2-The-Difficulty-of-Mining\" data-toc-modified-id=\"1.2-The-Difficulty-of-Mining-1.2\">1.2 The Difficulty of Mining</a></span><ul class=\"toc-item\"><li><span><a href=\"#1.2.1-Calculating-the-Hash\" data-toc-modified-id=\"1.2.1-Calculating-the-Hash-1.2.1\">1.2.1 Calculating the Hash</a></span></li></ul></li><li><span><a href=\"#1.3-Mining-Pools\" data-toc-modified-id=\"1.3-Mining-Pools-1.3\">1.3 Mining Pools</a></span></li><li><span><a href=\"#1.4-Mempools\" data-toc-modified-id=\"1.4-Mempools-1.4\">1.4 Mempools</a></span></li></ul></li></ul></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Introduction to the Concept of Cryptocurrency\n",
"\n",
"Cryptocurrencies constitute of three distinct layers: the technology, the protocol and the token layer. Cryptocurrencies are in themselves just de-centralized distributed ledgers with network-wide consensus mechanisms for the validation of chains. The distinctions are in the protocols and tokens issued to the users. \n",
"\n",
"- **Technology**: The already handled Blockchain is essentially the key technology serving as the foundation of the cryptocurrencies.\n",
"\n",
"\n",
"- **Protocol**: A [protocol](https://en.wikipedia.org/wiki/Communication_protocol) is a definition of the channels and rules of communication. While cryptocurrencies aer usually seen as only being a form of a volatile virtual currency, essentially every coin defines a set of rules and channels with and through which the users communicate, i.e. a protocol. In the context of distributed ledgers the protocol defines for example how the consensus of validity is achieved or how the authentication process operates.\n",
"\n",
" The distinct coins of the cryptocurrencies are intimately linked to the distinct protocols and enforce the user compliance towards a cryptocurrencies set of rules and channels of communication. Thus the protocol itself is a distinct platform on top of which a multitude of token-based systems can be built upon (e.g. ).\n",
"\n",
"\n",
"- **Token**: Token is the basis of smart contracts. The initial coin offerings (ICOs) are actually not usually about the *coins*, but the *tokens* for a token-based system *built upon a coin protocol*, such as [Ethereum](https://www.ethereum.org/), [Waves](https://wavesplatform.com/) or [Neo](https://neo.org/). [Bitcoin](https://bitcoin.org/en/) for one can't be used with tokens, as the protocol does not enforce the idea of them, much like [Ripple](https://ripple.com/). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.1 Background\n",
"\n",
"The use of Blockchain as the foundational technology thus allows users to perform transactions without intermediaries. Normally an intermediary used in the transaction process acts fundamentally as a source of mutual trust between any two users. In distributed consensus networks the source of trust has been transferred to the underlying technology.\n",
"\n",
"The starting point for cryptocurrencies can be timed to the release of a white paper by Satoshi Nakamoto called Bitcoin: A Peer-to-Peer Electronic Cash System (2008). In the introduction the stressing point is to have a trust system not based on institutional intermediaries. In the article the basic blueprint for a cryptocurrency is given as follows:\n",
"\n",
"> *What is needed is an electronic payment system based on cryptographic proof instead of trust, allowing any two willing parties to transact directly with each other without the need for a trusted third party. Transactions that are computationally impractical to reverse would protect sellers from fraud, and routine escrow mechanisms could easily be implemented to protect buyers. In this paper, we propose a solution to the double-spending problem using a peer-to-peer distributed timestamp server to generate computational proof of the chronological order of transactions. The system is secure as long as honest nodes collectively control more CPU power than any cooperating group of attacker nodes.*\n",
"\n",
"The last point has to do with the Byzantine Fault Tolerance, which can be read more about from a paper by Lamport et al. called The Byzantine Generals Problem (1982) or an [article](https://medium.com/loom-network/understanding-blockchain-fundamentals-part-1-byzantine-fault-tolerance-245f46fe8419) by G. Konstantopoulos. The basic idea is that every command by a general should be validated by the receiving officers between each other. While some officers might distort the original command, the majority is believed to relay the general's command unchanged. Every officer should therefore act on the command with a majority of validations behind it. This is the concept behind the notion of controlling more CPU power."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 The Difficulty of Mining\n",
"\n",
"The solving of the cryptographic puzzle is an artificial means of making the mining of new Blocks and thus receiving associated tokens as rewards harder. The key element in making the mining harder is the issuence of **a target**. This is usually a limit associated at least with the proof-of-work consensus protocol in which the hashes calculated as the proofs must have a number of leading zeros. As the calculation of **golden nonces** that produce hashes below the target level requires brute force and is statisitically viewable as a game of guessing a number correctly with uniform distribution, the nodes with highest computing capabilities usually produce block-solidifying hashes.\n",
"\n",
"Even though the issue was illustrated already, it is still in place to underline the effects of requiring a number of leading zeros in a 64-digit hexadecimal string. Let's first see the effects with the decimal system with maximal representation capability and the effect of requiring leading zeros:\n",
"\n",
" XXXX = 0000 to 9999 = 10000 values\n",
" 0XXX = 0000 to 0999 = 1000 values\n",
" 00XX = 0000 to 0099 = 100 values\n",
" \n",
"Increasing the required leading zeros incrementally lowers the number of possible values to a tenth at a time. The effect is however greater with 16-base hexadecimal numbers:\n",
"\n",
" XXXX = 0000 to FFFF = 16*16*16*16 = 65536 values\n",
" 0XXX = 0000 to 0FFF = 16*16*16 = 4096 values\n",
" 00XX = 0000 to 00FF = 16*16 = 256 values\n",
" \n",
"And to reiterate: to produce a valid hash below the target level, the miner must be able to find a number with which the hash of proof is below the target, i.e. the golden nonce."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.2.1 Calculating the Hash\n",
"\n",
"The nonce isn't however an infinite number. It is an 32-bit unsigned integer, which essentially means that there are \n",
"\n",
"$$ 2^{32} = 4294967296 = 4 * 10^9 $$\n",
" \n",
"possible values for the nonce. If we assume no collisions happen between the nonces, we thus are able to produce a same amount, $4*10^9$ hashes. With the current state of difficulty of 18 leading zeros, there is a\n",
"\n",
"$$ 16^{64-18}/16^{64}\\approx 2 * 10^{-22} \\to 0.0000000000000000000002\\text{%} $$\n",
"\n",
"chance of just randomly picking a hash within the target. This means that combining these values, the number of possible hashes produced with just varying the nonce and the probability of finding a hash within the target range is\n",
"\n",
"$$ 4*10^9 * 2 * 10^{-22} \\approx 10^{-12} \\to 0.000000000001\\text{%}. $$\n",
"\n",
"Single nonce range isn't thus enough to produce a valid hash. However there is a timestamp always related to the mined block, which changes by the second. This creates the loop-hole for searching mining hashes that are below the target threshold, as the hashes are calculated with two changing attributes. This however creates a problem related to hashing performance. Because the timestamp changes by the second, there is effectively a second at maximum to calculate hashes for the whole nonce range of $4 * 10^9$ values before the timestamp changes and the hashes with it. A modest miner is able to cover a single nonce range in about 40 seconds with 100 MH/s, which is where a mining pool is more effective than just a single miner.\n",
"\n",
"The timestamp and the nonce are not only aspects effecting the hash of a block. The contents, the transactions, contribute to that as well. The transactions are contained in what are called mempools and a mined block must contain transactions to be approved as being mined. The transactions contain fees attached to them, which are then transferred to a mined if they are able to succesfully mine a block. Thus the transactions with the highest fees tend to get picked and included in a block."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3 Mining Pools\n",
"\n",
"While industrial level cryptomines do exist, the more usual way of participating in the mining process is through a mining pool. In a mining pool the nonce try-outs are distributed in e.g. non-overlapping fashion so that each node in the pool of mining machines is responsible for a range of nonces. The rewards are then dispersed according to the contribution made by each node. \n",
"\n",
"The challenge of the mining pool is to be able to divide the task of calculating second-wise nonces with transaction data to produce target-beating hashes *without wasting capacity to idleness*. This is why the architecture of the mining pool network must be able to disperse multiple block configurations across the network. The block reconfigurations are achieved by changing the transactions included within a block. This is handled by the pool and connecting to it is just about receiving the allocated portion to crunch through."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.4 Mempools\n",
"\n",
"Mempools form the transaction pools for the distributed peer-to-peer (P2P) networks. There exists a mempool for each node in the network. Essentially a mempool is a staging area for the transactions where performed transactions go to wait for inclusion in a mined block. When a transaction is added to a node's mempool, it is then relayed to the whole network. A single block can contain about 2000 transactions and mempools hold an average of 9000 pending transactions. Whenever a block is mined, the transactions selected for the mined block are then removed initially from the mining node and then from the whole network through relaying the information about the changed mempool."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
},
"toc": {
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit b2ff6d0

Please sign in to comment.