From 93fcd7208fce8f5fabf2212b8878e33acb393588 Mon Sep 17 00:00:00 2001 From: Petteri Nevavuori Date: Wed, 11 Apr 2018 10:21:43 +0300 Subject: [PATCH] Separated validity check to its own API call. --- ...uilding a General Purpose Blockchain.ipynb | 187 +++++++++++++----- 1 file changed, 133 insertions(+), 54 deletions(-) diff --git a/blockchain/Building a General Purpose Blockchain.ipynb b/blockchain/Building a General Purpose Blockchain.ipynb index 80092e3..6b4fb72 100644 --- a/blockchain/Building a General Purpose Blockchain.ipynb +++ b/blockchain/Building a General Purpose Blockchain.ipynb @@ -196,17 +196,30 @@ " 'blockchain': blockchain.chain,\n", " 'length': len(blockchain.chain),\n", " }\n", + " \n", + " return (jsonify(response), 200)\n", "\n", - " if blockchain.is_chain_valid(blockchain.chain):\n", + " \n", "\n", - " response['message'] = 'Chain is valid.'\n", - " return (jsonify(response), 200)\n", + " \n", + "@app.route('/blocks/validate', methods=['GET'])\n", + "def validate():\n", + " \n", + " if blockchain.is_chain_valid(blockchain.chain):\n", "\n", - " else:\n", + " response = {\n", + " 'message':'Chain is valid.',\n", + " 'valid': True\n", + " }\n", + " return (jsonify(response), 200)\n", "\n", - " response['message'] = 'Chain is not valid!'\n", - " return (jsonify(response), 500)\n", + " else:\n", "\n", + " response = {\n", + " 'message':'Chain is not valid!',\n", + " 'valid': False\n", + " }\n", + " return (jsonify(response), 500)\n", "\n", "@app.route('/shutdown')\n", "def shutdown():\n", @@ -239,6 +252,9 @@ "\n", " def get_blockchain(self):\n", " return requests.get('{}/blocks'.format(self.host_url))\n", + " \n", + " def validate_blockchain(self):\n", + " return requests.get('{}/blocks/validate'.format(self.host_url))\n", "\n", " def __enter__(self):\n", " self.start()\n", @@ -262,6 +278,13 @@ "execution_count": 4, "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + " * Running on http://localhost:5000/ (Press CTRL+C to quit)\n" + ] + }, { "name": "stdout", "output_type": "stream", @@ -273,8 +296,7 @@ "name": "stderr", "output_type": "stream", "text": [ - " * Running on http://localhost:5000/ (Press CTRL+C to quit)\n", - "127.0.0.1 - - [11/Apr/2018 10:10:42] \"POST /blocks HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:20:58] \"POST /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -284,9 +306,9 @@ "{\n", " \"block\": {\n", " \"index\": 1,\n", - " \"previous_hash\": \"9ea61d2712075fc3d3f9cf68353339caf4782f80850f8476322d8b9890d6606e\",\n", + " \"previous_hash\": \"473e323c81594e47a585199fc035303e4bef838c4491dfccb845172f79a73650\",\n", " \"proof\": 533,\n", - " \"timestamp\": \"2018-04-11 10:10:42.723852\"\n", + " \"timestamp\": \"2018-04-11 10:20:58.750420\"\n", " },\n", " \"message\": \"Congratulations, you just mined a Block!\"\n", "}\n" @@ -296,7 +318,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:43] \"GET /blocks HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:20:59] \"GET /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -309,17 +331,16 @@ " \"index\": 0,\n", " \"previous_hash\": \"0\",\n", " \"proof\": 1,\n", - " \"timestamp\": \"2018-04-11 10:10:41.676864\"\n", + " \"timestamp\": \"2018-04-11 10:20:57.680891\"\n", " },\n", " {\n", " \"index\": 1,\n", - " \"previous_hash\": \"9ea61d2712075fc3d3f9cf68353339caf4782f80850f8476322d8b9890d6606e\",\n", + " \"previous_hash\": \"473e323c81594e47a585199fc035303e4bef838c4491dfccb845172f79a73650\",\n", " \"proof\": 533,\n", - " \"timestamp\": \"2018-04-11 10:10:42.723852\"\n", + " \"timestamp\": \"2018-04-11 10:20:58.750420\"\n", " }\n", " ],\n", - " \"length\": 2,\n", - " \"message\": \"Chain is valid.\"\n", + " \"length\": 2\n", "}\n" ] }, @@ -327,7 +348,24 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:44] \"GET /shutdown HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:00] \"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 - - [11/Apr/2018 10:21:01] \"GET /shutdown HTTP/1.1\" 200 -\n" ] } ], @@ -340,6 +378,9 @@ " 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))" ] }, @@ -369,7 +410,7 @@ "output_type": "stream", "text": [ " * Running on http://localhost:5000/ (Press CTRL+C to quit)\n", - "127.0.0.1 - - [11/Apr/2018 10:10:46] \"POST /blocks HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:03] \"POST /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -383,7 +424,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:47] \"POST /blocks HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:04] \"POST /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -397,7 +438,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:48] \"POST /blocks HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:05] \"POST /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -411,7 +452,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:49] \"POST /blocks HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:06] \"POST /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -425,7 +466,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:50] \"GET /blocks HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:07] \"GET /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -438,41 +479,40 @@ " \"index\": 0,\n", " \"previous_hash\": \"0\",\n", " \"proof\": 1,\n", - " \"timestamp\": \"2018-04-11 10:10:41.676864\"\n", + " \"timestamp\": \"2018-04-11 10:20:57.680891\"\n", " },\n", " {\n", " \"index\": 1,\n", - " \"previous_hash\": \"9ea61d2712075fc3d3f9cf68353339caf4782f80850f8476322d8b9890d6606e\",\n", + " \"previous_hash\": \"473e323c81594e47a585199fc035303e4bef838c4491dfccb845172f79a73650\",\n", " \"proof\": 533,\n", - " \"timestamp\": \"2018-04-11 10:10:42.723852\"\n", + " \"timestamp\": \"2018-04-11 10:20:58.750420\"\n", " },\n", " {\n", " \"index\": 2,\n", - " \"previous_hash\": \"d0ad2fc530b3da23b651b4698bd13ed7ce344a1d4eb7c8a15703f41405e403df\",\n", + " \"previous_hash\": \"2cb09916209977eec566e07a9847ada61b809d27ac727e8e8107b18dee39ed3c\",\n", " \"proof\": 45293,\n", - " \"timestamp\": \"2018-04-11 10:10:46.087295\"\n", + " \"timestamp\": \"2018-04-11 10:21:03.094625\"\n", " },\n", " {\n", " \"index\": 3,\n", - " \"previous_hash\": \"edac1c96c8bdfcc8a25fecb23fb351e28190e8d407cb774091b0838eacd2685a\",\n", + " \"previous_hash\": \"49d0e71246405b4f3235157af7e4c8cc8d898ebca4d23419b62e97524ca94d97\",\n", " \"proof\": 21391,\n", - " \"timestamp\": \"2018-04-11 10:10:47.198487\"\n", + " \"timestamp\": \"2018-04-11 10:21:04.235408\"\n", " },\n", " {\n", " \"index\": 4,\n", - " \"previous_hash\": \"4f7812e3d8af187f80b8fb71c995d1e5bec82a7e228280d5c7c3e796b7e76207\",\n", + " \"previous_hash\": \"0c0b1d3c6f729f96acaab99f551536c94bf0229bdb719e5921d9697171f9fed7\",\n", " \"proof\": 8018,\n", - " \"timestamp\": \"2018-04-11 10:10:48.225742\"\n", + " \"timestamp\": \"2018-04-11 10:21:05.329277\"\n", " },\n", " {\n", " \"index\": 5,\n", - " \"previous_hash\": \"0d3f5fb4c69a4f869fee173f096c2f6157ac8dcebbc31dc0468bf9c382480e9e\",\n", + " \"previous_hash\": \"574d4f3e6def0906456dfc884d5b988c44ec02d305b6df2d2bbac0a3e051b636\",\n", " \"proof\": 48191,\n", - " \"timestamp\": \"2018-04-11 10:10:49.359195\"\n", + " \"timestamp\": \"2018-04-11 10:21:06.516897\"\n", " }\n", " ],\n", - " \"length\": 6,\n", - " \"message\": \"Chain is valid.\"\n", + " \"length\": 6\n", "}\n" ] }, @@ -480,7 +520,24 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:51] \"GET /shutdown HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:08] \"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 - - [11/Apr/2018 10:21:09] \"GET /shutdown HTTP/1.1\" 200 -\n" ] } ], @@ -493,14 +550,17 @@ " print(\"Proof={}\".format(response.json()['block']['proof']))\n", "\n", " response = blockchain_app.get_blockchain()\n", - " print(\"Chain={}\".format(json.dumps(response.json(), indent=2)))" + " print(\"Chain={}\".format(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": {}, "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." + "If you compare the first two Blocks with the test run you can see that the hashes and proofs match. This is intended, as already stated, as the ``Blockchain`` lives centralized in the server for this showcase and the ``BlockchainApp`` only makes calls to that server." ] }, { @@ -535,7 +595,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:52] \"GET /blocks HTTP/1.1\" 500 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:10] \"GET /blocks HTTP/1.1\" 200 -\n" ] }, { @@ -548,41 +608,40 @@ " \"index\": 0,\n", " \"previous_hash\": \"0\",\n", " \"proof\": 1,\n", - " \"timestamp\": \"2018-04-11 10:10:41.676864\"\n", + " \"timestamp\": \"2018-04-11 10:20:57.680891\"\n", " },\n", " {\n", " \"index\": 1,\n", - " \"previous_hash\": \"9ea61d2712075fc3d3f9cf68353339caf4782f80850f8476322d8b9890d6606e\",\n", + " \"previous_hash\": \"473e323c81594e47a585199fc035303e4bef838c4491dfccb845172f79a73650\",\n", " \"proof\": 12345,\n", - " \"timestamp\": \"2018-04-11 10:10:42.723852\"\n", + " \"timestamp\": \"2018-04-11 10:20:58.750420\"\n", " },\n", " {\n", " \"index\": 2,\n", - " \"previous_hash\": \"d0ad2fc530b3da23b651b4698bd13ed7ce344a1d4eb7c8a15703f41405e403df\",\n", + " \"previous_hash\": \"2cb09916209977eec566e07a9847ada61b809d27ac727e8e8107b18dee39ed3c\",\n", " \"proof\": 45293,\n", - " \"timestamp\": \"2018-04-11 10:10:46.087295\"\n", + " \"timestamp\": \"2018-04-11 10:21:03.094625\"\n", " },\n", " {\n", " \"index\": 3,\n", - " \"previous_hash\": \"edac1c96c8bdfcc8a25fecb23fb351e28190e8d407cb774091b0838eacd2685a\",\n", + " \"previous_hash\": \"49d0e71246405b4f3235157af7e4c8cc8d898ebca4d23419b62e97524ca94d97\",\n", " \"proof\": 21391,\n", - " \"timestamp\": \"2018-04-11 10:10:47.198487\"\n", + " \"timestamp\": \"2018-04-11 10:21:04.235408\"\n", " },\n", " {\n", " \"index\": 4,\n", - " \"previous_hash\": \"4f7812e3d8af187f80b8fb71c995d1e5bec82a7e228280d5c7c3e796b7e76207\",\n", + " \"previous_hash\": \"0c0b1d3c6f729f96acaab99f551536c94bf0229bdb719e5921d9697171f9fed7\",\n", " \"proof\": 8018,\n", - " \"timestamp\": \"2018-04-11 10:10:48.225742\"\n", + " \"timestamp\": \"2018-04-11 10:21:05.329277\"\n", " },\n", " {\n", " \"index\": 5,\n", - " \"previous_hash\": \"0d3f5fb4c69a4f869fee173f096c2f6157ac8dcebbc31dc0468bf9c382480e9e\",\n", + " \"previous_hash\": \"574d4f3e6def0906456dfc884d5b988c44ec02d305b6df2d2bbac0a3e051b636\",\n", " \"proof\": 48191,\n", - " \"timestamp\": \"2018-04-11 10:10:49.359195\"\n", + " \"timestamp\": \"2018-04-11 10:21:06.516897\"\n", " }\n", " ],\n", - " \"length\": 6,\n", - " \"message\": \"Chain is not valid!\"\n", + " \"length\": 6\n", "}\n" ] }, @@ -590,7 +649,24 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [11/Apr/2018 10:10:53] \"GET /shutdown HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [11/Apr/2018 10:21:11] \"GET /blocks/validate HTTP/1.1\" 500 -\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"message\": \"Chain is not valid!\",\n", + " \"valid\": false\n", + "}\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "127.0.0.1 - - [11/Apr/2018 10:21:12] \"GET /shutdown HTTP/1.1\" 200 -\n" ] } ], @@ -602,6 +678,9 @@ "with BlockchainApp() as blockchain_app:\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))\n" ] }, @@ -622,7 +701,7 @@ "output_type": "stream", "text": [ "Hashes: \n", - "\tOriginal =\td0ad2fc530b3da23b651b4698bd13ed7ce344a1d4eb7c8a15703f41405e403df\n", + "\tOriginal =\t2cb09916209977eec566e07a9847ada61b809d27ac727e8e8107b18dee39ed3c\n", "\tForged =\tb3e1a78ae4b5cd947a6690f4a5ee9b3a71a13a2df00d77cbc4c904c9b2ac2deb\n" ] }