Skip to content

Commit

Permalink
Added invalidation checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Petteri Nevavuori committed Apr 11, 2018
1 parent 0b5bebc commit bdf2135
Showing 1 changed file with 173 additions and 27 deletions.
200 changes: 173 additions & 27 deletions blockchain/Building a General Purpose Blockchain.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.-Building-the-Blockhain\" data-toc-modified-id=\"1.-Building-the-Blockhain-1\">1. Building the Blockhain</a></span></li><li><span><a href=\"#2.-Creating-the-Web-App\" data-toc-modified-id=\"2.-Creating-the-Web-App-2\">2. Creating the Web App</a></span><ul class=\"toc-item\"><li><span><a href=\"#2.1-Testing-the-Blockchain-App\" data-toc-modified-id=\"2.1-Testing-the-Blockchain-App-2.1\">2.1 Testing the Blockchain App</a></span></li></ul></li><li><span><a href=\"#3.-Mining--Blocks\" data-toc-modified-id=\"3.-Mining--Blocks-3\">3. Mining Blocks</a></span></li></ul></div>"
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#1.-Building-the-Blockhain\" data-toc-modified-id=\"1.-Building-the-Blockhain-1\">1. Building the Blockhain</a></span></li><li><span><a href=\"#2.-Creating-the-Web-App\" data-toc-modified-id=\"2.-Creating-the-Web-App-2\">2. Creating the Web App</a></span><ul class=\"toc-item\"><li><span><a href=\"#2.1-Testing-the-Blockchain-App\" data-toc-modified-id=\"2.1-Testing-the-Blockchain-App-2.1\">2.1 Testing the Blockchain App</a></span></li></ul></li><li><span><a href=\"#3.-Mining--Blocks\" data-toc-modified-id=\"3.-Mining--Blocks-3\">3. Mining Blocks</a></span><ul class=\"toc-item\"><li><span><a href=\"#3.1-Testing-the-Chain-Validity\" data-toc-modified-id=\"3.1-Testing-the-Chain-Validity-3.1\">3.1 Testing the Chain Validity</a></span></li></ul></li></ul></div>"
]
},
{
Expand Down Expand Up @@ -131,6 +131,7 @@
" next_proof = chain[i]['proof']\n",
"\n",
" if self.hash_proof(previous_proof, next_proof)[:4] != '0000':\n",
" \n",
" return False\n",
"\n",
" return True"
Expand Down Expand Up @@ -279,7 +280,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:49:58] \"POST /blocks HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:41] \"POST /blocks HTTP/1.1\" 200 -\n"
]
},
{
Expand All @@ -289,9 +290,9 @@
"{\n",
" \"block\": {\n",
" \"index\": 1,\n",
" \"previous_hash\": \"5cc26226686b36400b40fe89530ff4b0174d4e3f73f609af0efc8bc85e6f08cf\",\n",
" \"previous_hash\": \"f198e29bb5e45e308d25579fc8f52a36fea66ba22ca61d18e654125c04116340\",\n",
" \"proof\": 533,\n",
" \"timestamp\": \"2018-04-11 09:49:58.119854\"\n",
" \"timestamp\": \"2018-04-11 10:00:41.379830\"\n",
" },\n",
" \"message\": \"Congratulations, you just mined a Block!\"\n",
"}\n"
Expand All @@ -301,7 +302,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:49:59] \"GET /blocks HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:42] \"GET /blocks HTTP/1.1\" 200 -\n"
]
},
{
Expand All @@ -314,13 +315,13 @@
" \"index\": 0,\n",
" \"previous_hash\": \"0\",\n",
" \"proof\": 1,\n",
" \"timestamp\": \"2018-04-11 09:49:57.088693\"\n",
" \"timestamp\": \"2018-04-11 10:00:40.317214\"\n",
" },\n",
" {\n",
" \"index\": 1,\n",
" \"previous_hash\": \"5cc26226686b36400b40fe89530ff4b0174d4e3f73f609af0efc8bc85e6f08cf\",\n",
" \"previous_hash\": \"f198e29bb5e45e308d25579fc8f52a36fea66ba22ca61d18e654125c04116340\",\n",
" \"proof\": 533,\n",
" \"timestamp\": \"2018-04-11 09:49:58.119854\"\n",
" \"timestamp\": \"2018-04-11 10:00:41.379830\"\n",
" }\n",
" ],\n",
" \"length\": 2,\n",
Expand All @@ -332,7 +333,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:50:00] \"GET /shutdown HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:43] \"GET /shutdown HTTP/1.1\" 200 -\n"
]
}
],
Expand Down Expand Up @@ -374,7 +375,7 @@
"output_type": "stream",
"text": [
" * Running on http://localhost:5000/ (Press CTRL+C to quit)\n",
"127.0.0.1 - - [11/Apr/2018 09:50:01] \"POST /blocks HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:44] \"POST /blocks HTTP/1.1\" 200 -\n"
]
},
{
Expand All @@ -388,7 +389,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:50:02] \"POST /blocks HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:45] \"POST /blocks HTTP/1.1\" 200 -\n"
]
},
{
Expand All @@ -402,7 +403,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:50:03] \"POST /blocks HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:46] \"POST /blocks HTTP/1.1\" 200 -\n"
]
},
{
Expand All @@ -416,7 +417,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:50:04] \"POST /blocks HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:48] \"POST /blocks HTTP/1.1\" 200 -\n"
]
},
{
Expand All @@ -430,7 +431,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:50:05] \"GET /blocks HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:49] \"GET /blocks HTTP/1.1\" 200 -\n"
]
},
{
Expand All @@ -443,37 +444,37 @@
" \"index\": 0,\n",
" \"previous_hash\": \"0\",\n",
" \"proof\": 1,\n",
" \"timestamp\": \"2018-04-11 09:49:57.088693\"\n",
" \"timestamp\": \"2018-04-11 10:00:40.317214\"\n",
" },\n",
" {\n",
" \"index\": 1,\n",
" \"previous_hash\": \"5cc26226686b36400b40fe89530ff4b0174d4e3f73f609af0efc8bc85e6f08cf\",\n",
" \"previous_hash\": \"f198e29bb5e45e308d25579fc8f52a36fea66ba22ca61d18e654125c04116340\",\n",
" \"proof\": 533,\n",
" \"timestamp\": \"2018-04-11 09:49:58.119854\"\n",
" \"timestamp\": \"2018-04-11 10:00:41.379830\"\n",
" },\n",
" {\n",
" \"index\": 2,\n",
" \"previous_hash\": \"0dff44d24efffd7139e45d3248f95ea5ca738a0b0fcbc910841eef62bdf6639b\",\n",
" \"previous_hash\": \"8e0369ebcfe420065d5b3394c2ece9b06185757005b305a2d3e7277055281860\",\n",
" \"proof\": 45293,\n",
" \"timestamp\": \"2018-04-11 09:50:01.290186\"\n",
" \"timestamp\": \"2018-04-11 10:00:44.678168\"\n",
" },\n",
" {\n",
" \"index\": 3,\n",
" \"previous_hash\": \"632dd0c50d9db200bdc44cada8b33a4af68b69eda3e88d8e1a8c74fb31a619b5\",\n",
" \"previous_hash\": \"df340007864a5fbd2a2465a9d2ac25f183416b5564a61fda2e87eb44d1ca18fb\",\n",
" \"proof\": 21391,\n",
" \"timestamp\": \"2018-04-11 09:50:02.379049\"\n",
" \"timestamp\": \"2018-04-11 10:00:45.831691\"\n",
" },\n",
" {\n",
" \"index\": 4,\n",
" \"previous_hash\": \"299404399564af95463860d39b49ffdaa4f2d183dc5d8f625dedff45e22464ff\",\n",
" \"previous_hash\": \"04f6299127b6fb0fb659e5b0ad54b3e2fa5d43739ae13e72a31da9ec04c7a18a\",\n",
" \"proof\": 8018,\n",
" \"timestamp\": \"2018-04-11 09:50:03.411990\"\n",
" \"timestamp\": \"2018-04-11 10:00:46.882162\"\n",
" },\n",
" {\n",
" \"index\": 5,\n",
" \"previous_hash\": \"1fdae0359c52d943575befbfb55e4c4a19ef5ef267bbbd14903b1e16651ffc76\",\n",
" \"previous_hash\": \"003a0b87af5ad8d94070f7632b12b90ff18c4271fd7e2eae395be7b02aeaa50b\",\n",
" \"proof\": 48191,\n",
" \"timestamp\": \"2018-04-11 09:50:04.590271\"\n",
" \"timestamp\": \"2018-04-11 10:00:48.076540\"\n",
" }\n",
" ],\n",
" \"length\": 6,\n",
Expand All @@ -485,7 +486,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 09:50:06] \"GET /shutdown HTTP/1.1\" 200 -\n"
"127.0.0.1 - - [11/Apr/2018 10:00:50] \"GET /shutdown HTTP/1.1\" 200 -\n"
]
}
],
Expand All @@ -505,8 +506,153 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If you compare the first two Blocks with the test run you can see that the hashes and proofs match. This intended, as already stated.\n",
"If you compare the first two Blocks with the test run you can see that the hashes and proofs match. This intended, as already stated."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.1 Testing the Chain Validity\n",
"\n",
"We also want to see if changing the proof mid-chain breaks the chain. For this we must access the initialized ``Blockchain`` directly as the miniserver doesn't have a method for altering the chain - which is a wise starting point!"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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": [
"Proofs: Original=21391, Forged=21391\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 10:00:51] \"GET /blocks HTTP/1.1\" 500 -\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"blockchain\": [\n",
" {\n",
" \"index\": 0,\n",
" \"previous_hash\": \"0\",\n",
" \"proof\": 1,\n",
" \"timestamp\": \"2018-04-11 10:00:40.317214\"\n",
" },\n",
" {\n",
" \"index\": 1,\n",
" \"previous_hash\": \"f198e29bb5e45e308d25579fc8f52a36fea66ba22ca61d18e654125c04116340\",\n",
" \"proof\": 12345,\n",
" \"timestamp\": \"2018-04-11 10:00:41.379830\"\n",
" },\n",
" {\n",
" \"index\": 2,\n",
" \"previous_hash\": \"8e0369ebcfe420065d5b3394c2ece9b06185757005b305a2d3e7277055281860\",\n",
" \"proof\": 45293,\n",
" \"timestamp\": \"2018-04-11 10:00:44.678168\"\n",
" },\n",
" {\n",
" \"index\": 3,\n",
" \"previous_hash\": \"df340007864a5fbd2a2465a9d2ac25f183416b5564a61fda2e87eb44d1ca18fb\",\n",
" \"proof\": 21391,\n",
" \"timestamp\": \"2018-04-11 10:00:45.831691\"\n",
" },\n",
" {\n",
" \"index\": 4,\n",
" \"previous_hash\": \"04f6299127b6fb0fb659e5b0ad54b3e2fa5d43739ae13e72a31da9ec04c7a18a\",\n",
" \"proof\": 8018,\n",
" \"timestamp\": \"2018-04-11 10:00:46.882162\"\n",
" },\n",
" {\n",
" \"index\": 5,\n",
" \"previous_hash\": \"003a0b87af5ad8d94070f7632b12b90ff18c4271fd7e2eae395be7b02aeaa50b\",\n",
" \"proof\": 48191,\n",
" \"timestamp\": \"2018-04-11 10:00:48.076540\"\n",
" }\n",
" ],\n",
" \"length\": 6,\n",
" \"message\": \"Chain is not valid!\"\n",
"}\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"127.0.0.1 - - [11/Apr/2018 10:00:52] \"GET /shutdown HTTP/1.1\" 200 -\n"
]
}
],
"source": [
"original = blockchain.chain[3]['proof']\n",
"blockchain.chain[1]['proof'] = 12345\n",
"print('Proofs: Original={}, Forged={}'.format(original,blockchain.chain[3]['proof']))\n",
"\n",
"with BlockchainApp() as blockchain_app:\n",
" \n",
" response = blockchain_app.get_blockchain()\n",
" print(json.dumps(response.json(), indent=2))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And so we indeed have invalidated the chain! Let's yet see what the hash should've been for Block in index ``2``, as it would be the one invalidated by the forgery."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hashes: \n",
"\tOriginal =\t8e0369ebcfe420065d5b3394c2ece9b06185757005b305a2d3e7277055281860\n",
"\tForged =\tb3e1a78ae4b5cd947a6690f4a5ee9b3a71a13a2df00d77cbc4c904c9b2ac2deb\n"
]
}
],
"source": [
"proof_1 = blockchain.chain[1]['proof']\n",
"proof_2 = blockchain.chain[2]['proof']\n",
"\n",
"forged_hash = blockchain.hash_proof(proof_1, proof_2)\n",
"\n",
"print(\"Hashes: \\n\\tOriginal =\\t{}\\n\\tForged =\\t{}\".format(blockchain.chain[2]['previous_hash'],forged_hash))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What a difference. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**This concludes the building and mining of a general purpose centralized Blockchain.**"
]
}
Expand Down

0 comments on commit bdf2135

Please sign in to comment.