Skip to content

Commit

Permalink
Tested the packaged Blockchain app.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevavuori Petteri committed May 15, 2018
1 parent 3b7b8df commit 1d3ffe1
Showing 1 changed file with 133 additions and 4 deletions.
137 changes: 133 additions & 4 deletions notebooks/II. Building a General Purpose Cryptocurrency.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"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>"
"<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><li><span><a href=\"#2.-Building-the-Cryptocurrency\" data-toc-modified-id=\"2.-Building-the-Cryptocurrency-2\">2. Building the Cryptocurrency</a></span></li></ul></div>"
]
},
{
Expand Down Expand Up @@ -125,15 +125,144 @@
"Whenever there are competing claims for the next block added to the chain, the one mined later usually gets orphaned. Or to be more precise, the block resulting in longer chains wins and the smaller chain gets orphaned. The transactions in the oprhaned block are not however lost, but returned the mempool. A rule of thumb is to wait for six confirmed blocks to be sure that the transaction within a mined block has been accepted and solidified into the chain."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Building the Cryptocurrency\n",
"\n",
"Next we will create an implementation of out Cryptocurrency named GeneralCoin. We will use the implementation of the blockchain present in the first notebook called \"I. Building a General Purpose Blockchain\". The implementation has been moved to an importable package for saving space from unnecessary repetition of code. We will implement necessary modifications step-by-step to the imported General Purpose Blockchain to transform it to a General Purpose Decentralized Cryptocurrency."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We first import the Blockchain and try running the App to see that it functions as intended."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" * Running on http://localhost:5000/ (Press CTRL+C to quit)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"BlockchainApp\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [15/May/2018 09:43:09] \"POST /blocks HTTP/1.1\" 200 -\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"block\": {\n",
" \"index\": 1,\n",
" \"previous_hash\": \"16672a224c72860895bff19dc3b09c1ef0514e15a0f8e0d6b8082375781ade10\",\n",
" \"proof\": 533,\n",
" \"timestamp\": \"2018-05-15 09:43:09.656716\"\n",
" },\n",
" \"message\": \"Congratulations, you just mined a Block!\"\n",
"}\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [15/May/2018 09:43:10] \"GET /blocks HTTP/1.1\" 200 -\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"blockchain\": [\n",
" {\n",
" \"index\": 0,\n",
" \"previous_hash\": \"0\",\n",
" \"proof\": 1,\n",
" \"timestamp\": \"2018-05-15 09:42:09.984854\"\n",
" },\n",
" {\n",
" \"index\": 1,\n",
" \"previous_hash\": \"16672a224c72860895bff19dc3b09c1ef0514e15a0f8e0d6b8082375781ade10\",\n",
" \"proof\": 533,\n",
" \"timestamp\": \"2018-05-15 09:43:09.656716\"\n",
" }\n",
" ],\n",
" \"length\": 2\n",
"}\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [15/May/2018 09:43:11] \"GET /blocks/validate HTTP/1.1\" 200 -\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"message\": \"Chain is valid.\",\n",
" \"valid\": true\n",
"}\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [15/May/2018 09:43:12] \"GET /shutdown HTTP/1.1\" 200 -\n"
]
}
],
"source": [
"import json\n",
"from blockchain.apps import BlockchainApp\n",
"\n",
"with BlockchainApp() as blockchain_app:\n",
"\n",
" print(blockchain_app.__class__.__name__)\n",
"\n",
" response = blockchain_app.mine_block()\n",
" print(json.dumps(response.json(), indent=2))\n",
"\n",
" response = blockchain_app.get_blockchain()\n",
" print(json.dumps(response.json(), indent=2))\n",
" \n",
" response = blockchain_app.validate_blockchain()\n",
" print(json.dumps(response.json(), indent=2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
Expand All @@ -149,7 +278,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
"version": "3.6.3"
},
"toc": {
"nav_menu": {},
Expand Down

0 comments on commit 1d3ffe1

Please sign in to comment.