├── tests
├── __init__.py
└── test_blockchain.py
├── requirements.txt
├── csharp
├── BlockChain
│ ├── Node.cs
│ ├── Transaction.cs
│ ├── packages.config
│ ├── Block.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── BlockChain.csproj
│ ├── WebServer.cs
│ └── BlockChain.cs
├── BlockChain.Console
│ ├── Program.cs
│ ├── App.config
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── BlockChain.Console.csproj
└── BlockChain.sln
├── .travis.yml
├── Pipfile
├── Dockerfile
├── LICENSE
├── .gitignore
├── README.md
├── .gitattributes
├── Pipfile.lock
└── blockchain.py
/tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | flask==0.12.2
2 | requests==2.18.4
3 |
--------------------------------------------------------------------------------
/csharp/BlockChain/Node.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BlockChainDemo
4 | {
5 | public class Node
6 | {
7 | public Uri Address { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: python
2 |
3 | python:
4 | - 3.6
5 | - nightly
6 |
7 | install:
8 | - pip install pipenv
9 | - pipenv install --dev
10 |
11 | script:
12 | - pipenv run python -m unittest
13 |
--------------------------------------------------------------------------------
/csharp/BlockChain/Transaction.cs:
--------------------------------------------------------------------------------
1 | namespace BlockChainDemo
2 | {
3 | public class Transaction
4 | {
5 | public int Amount { get; set; }
6 | public string Recipient { get; set; }
7 | public string Sender { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/csharp/BlockChain/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Pipfile:
--------------------------------------------------------------------------------
1 | [[source]]
2 | url = "https://pypi.python.org/simple"
3 | verify_ssl = true
4 | name = "pypi"
5 |
6 | [dev-packages]
7 |
8 | [requires]
9 | python_version = "3.6"
10 |
11 | [packages]
12 |
13 | flask = "==0.12.2"
14 | requests = "==2.18.4"
15 |
16 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM python:3.6-alpine
2 |
3 | WORKDIR /app
4 |
5 | # Install dependencies.
6 | ADD requirements.txt /app
7 | RUN cd /app && \
8 | pip install -r requirements.txt
9 |
10 | # Add actual source code.
11 | ADD blockchain.py /app
12 |
13 | EXPOSE 5000
14 |
15 | CMD ["python", "blockchain.py", "--port", "5000"]
16 |
--------------------------------------------------------------------------------
/csharp/BlockChain.Console/Program.cs:
--------------------------------------------------------------------------------
1 | namespace BlockChainDemo.Console
2 | {
3 | class Program
4 | {
5 | static void Main(string[] args)
6 | {
7 | var chain = new BlockChain();
8 | var server = new WebServer(chain);
9 | System.Console.Read();
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/csharp/BlockChain.Console/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/csharp/BlockChain/Block.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace BlockChainDemo
5 | {
6 | public class Block
7 | {
8 | public int Index { get; set; }
9 | public DateTime Timestamp { get; set; }
10 | public List Transactions { get; set; }
11 | public int Proof { get; set; }
12 | public string PreviousHash { get; set; }
13 |
14 | public override string ToString()
15 | {
16 | return $"{Index} [{Timestamp.ToString("yyyy-MM-dd HH:mm:ss")}] Proof: {Proof} | PrevHash: {PreviousHash} | Trx: {Transactions.Count}";
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Daniel van Flymen
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/csharp/BlockChain/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("BlockChain")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("BlockChain")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("e06fc4ce-77d0-4a64-94a6-32a08920e481")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/csharp/BlockChain.Console/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("BlockChain.Console")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("BlockChain.Console")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("d0c795a0-6f20-4a8e-be44-801678754da4")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | env/
12 | build/
13 | develop-eggs/
14 | dist/
15 | downloads/
16 | eggs/
17 | .eggs/
18 | lib/
19 | lib64/
20 | parts/
21 | sdist/
22 | var/
23 | wheels/
24 | *.egg-info/
25 | .installed.cfg
26 | *.egg
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 |
49 | # Translations
50 | *.mo
51 | *.pot
52 |
53 | # Django stuff:
54 | *.log
55 | local_settings.py
56 |
57 | # Flask stuff:
58 | instance/
59 | .webassets-cache
60 |
61 | # Scrapy stuff:
62 | .scrapy
63 |
64 | # Sphinx documentation
65 | docs/_build/
66 |
67 | # PyBuilder
68 | target/
69 |
70 | # Jupyter Notebook
71 | .ipynb_checkpoints
72 |
73 | # pyenv
74 | .python-version
75 |
76 | # celery beat schedule file
77 | celerybeat-schedule
78 |
79 | # SageMath parsed files
80 | *.sage.py
81 |
82 | # dotenv
83 | .env
84 |
85 | # virtualenv
86 | .venv
87 | venv/
88 | ENV/
89 |
90 | # Spyder project settings
91 | .spyderproject
92 | .spyproject
93 |
94 | # Rope project settings
95 | .ropeproject
96 |
97 | # mkdocs documentation
98 | /site
99 |
100 | # mypy
101 | .mypy_cache/
102 |
103 | # PyCharm
104 | .idea/
105 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Learn Blockchains by Building One
2 |
3 | [](https://travis-ci.org/dvf/blockchain)
4 |
5 | This is the source code for my post on [Building a Blockchain](https://medium.com/p/117428612f46).
6 |
7 | ## Installation
8 |
9 | 1. Make sure [Python 3.6+](https://www.python.org/downloads/) is installed.
10 | 2. Install [pipenv](https://github.com/kennethreitz/pipenv).
11 |
12 | ```
13 | $ pip install pipenv
14 | ```
15 |
16 | 3. Create a _virtual environment_ and specify the Python version to use.
17 |
18 | ```
19 | $ pipenv --python=python3.6
20 | ```
21 |
22 | 4. Install requirements.
23 |
24 | ```
25 | $ pipenv install
26 | ```
27 |
28 | 5. Run the server:
29 | * `$ pipenv run python blockchain.py`
30 | * `$ pipenv run python blockchain.py -p 5001`
31 | * `$ pipenv run python blockchain.py --port 5002`
32 |
33 | ## Docker
34 |
35 | Another option for running this blockchain program is to use Docker. Follow the instructions below to create a local Docker container:
36 |
37 | 1. Clone this repository
38 | 2. Build the docker container
39 |
40 | ```
41 | $ docker build -t blockchain .
42 | ```
43 |
44 | 3. Run the container
45 |
46 | ```
47 | $ docker run --rm -p 80:5000 blockchain
48 | ```
49 |
50 | 4. To add more instances, vary the public port number before the colon:
51 |
52 | ```
53 | $ docker run --rm -p 81:5000 blockchain
54 | $ docker run --rm -p 82:5000 blockchain
55 | $ docker run --rm -p 83:5000 blockchain
56 | ```
57 |
58 | ## Installation (C# Implementation)
59 |
60 | 1. Install a free copy of Visual Studio IDE (Community Edition):
61 | https://www.visualstudio.com/vs/
62 |
63 | 2. Once installed, open the solution file (BlockChain.sln) using the File > Open > Project/Solution menu options within Visual Studio.
64 |
65 | 3. From within the "Solution Explorer", right click the BlockChain.Console project and select the "Set As Startup Project" option.
66 |
67 | 4. Click the "Start" button, or hit F5 to run. The program executes in a console window, and is controlled via HTTP with the same commands as the Python version.
68 |
69 |
70 | ## Contributing
71 |
72 | Contributions are welcome! Please feel free to submit a Pull Request.
73 |
74 |
--------------------------------------------------------------------------------
/csharp/BlockChain.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27004.2008
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain", "BlockChain\BlockChain.csproj", "{E06FC4CE-77D0-4A64-94A6-32A08920E481}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain.Console", "BlockChain.Console\BlockChain.Console.csproj", "{D0C795A0-6F20-4A8E-BE44-801678754DA4}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {7FB3650B-CAFB-4A71-940B-0CA6F0377422}
30 | EndGlobalSection
31 | GlobalSection(TeamFoundationVersionControl) = preSolution
32 | SccNumberOfProjects = 3
33 | SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
34 | SccTeamFoundationServer = https://devfire.visualstudio.com/
35 | SccLocalPath0 = .
36 | SccProjectUniqueName1 = BlockChain\\BlockChain.csproj
37 | SccProjectName1 = BlockChain
38 | SccLocalPath1 = BlockChain
39 | SccProjectUniqueName2 = BlockChain.Console\\BlockChain.Console.csproj
40 | SccProjectName2 = BlockChain.Console
41 | SccLocalPath2 = BlockChain.Console
42 | EndGlobalSection
43 | EndGlobal
44 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/csharp/BlockChain.Console/BlockChain.Console.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {D0C795A0-6F20-4A8E-BE44-801678754DA4}
8 | Exe
9 | BlockChainDemo.Console
10 | BlockChainDemo.Console
11 | v4.5.1
12 | 512
13 | true
14 | SAK
15 | SAK
16 | SAK
17 | SAK
18 |
19 |
20 |
21 | AnyCPU
22 | true
23 | full
24 | false
25 | bin\Debug\
26 | DEBUG;TRACE
27 | prompt
28 | 4
29 |
30 |
31 | AnyCPU
32 | pdbonly
33 | true
34 | bin\Release\
35 | TRACE
36 | prompt
37 | 4
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | {e06fc4ce-77d0-4a64-94a6-32a08920e481}
59 | BlockChain
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/tests/test_blockchain.py:
--------------------------------------------------------------------------------
1 | import hashlib
2 | import json
3 | from unittest import TestCase
4 |
5 | from blockchain import Blockchain
6 |
7 |
8 | class BlockchainTestCase(TestCase):
9 |
10 | def setUp(self):
11 | self.blockchain = Blockchain()
12 |
13 | def create_block(self, proof=123, previous_hash='abc'):
14 | self.blockchain.new_block(proof, previous_hash)
15 |
16 | def create_transaction(self, sender='a', recipient='b', amount=1):
17 | self.blockchain.new_transaction(
18 | sender=sender,
19 | recipient=recipient,
20 | amount=amount
21 | )
22 |
23 |
24 | class TestRegisterNodes(BlockchainTestCase):
25 |
26 | def test_valid_nodes(self):
27 | blockchain = Blockchain()
28 |
29 | blockchain.register_node('http://192.168.0.1:5000')
30 |
31 | self.assertIn('192.168.0.1:5000', blockchain.nodes)
32 |
33 | def test_malformed_nodes(self):
34 | blockchain = Blockchain()
35 |
36 | blockchain.register_node('http//192.168.0.1:5000')
37 |
38 | self.assertNotIn('192.168.0.1:5000', blockchain.nodes)
39 |
40 | def test_idempotency(self):
41 | blockchain = Blockchain()
42 |
43 | blockchain.register_node('http://192.168.0.1:5000')
44 | blockchain.register_node('http://192.168.0.1:5000')
45 |
46 | assert len(blockchain.nodes) == 1
47 |
48 |
49 | class TestBlocksAndTransactions(BlockchainTestCase):
50 |
51 | def test_block_creation(self):
52 | self.create_block()
53 |
54 | latest_block = self.blockchain.last_block
55 |
56 | # The genesis block is create at initialization, so the length should be 2
57 | assert len(self.blockchain.chain) == 2
58 | assert latest_block['index'] == 2
59 | assert latest_block['timestamp'] is not None
60 | assert latest_block['proof'] == 123
61 | assert latest_block['previous_hash'] == 'abc'
62 |
63 | def test_create_transaction(self):
64 | self.create_transaction()
65 |
66 | transaction = self.blockchain.current_transactions[-1]
67 |
68 | assert transaction
69 | assert transaction['sender'] == 'a'
70 | assert transaction['recipient'] == 'b'
71 | assert transaction['amount'] == 1
72 |
73 | def test_block_resets_transactions(self):
74 | self.create_transaction()
75 |
76 | initial_length = len(self.blockchain.current_transactions)
77 |
78 | self.create_block()
79 |
80 | current_length = len(self.blockchain.current_transactions)
81 |
82 | assert initial_length == 1
83 | assert current_length == 0
84 |
85 | def test_return_last_block(self):
86 | self.create_block()
87 |
88 | created_block = self.blockchain.last_block
89 |
90 | assert len(self.blockchain.chain) == 2
91 | assert created_block is self.blockchain.chain[-1]
92 |
93 |
94 | class TestHashingAndProofs(BlockchainTestCase):
95 |
96 | def test_hash_is_correct(self):
97 | self.create_block()
98 |
99 | new_block = self.blockchain.last_block
100 | new_block_json = json.dumps(self.blockchain.last_block, sort_keys=True).encode()
101 | new_hash = hashlib.sha256(new_block_json).hexdigest()
102 |
103 | assert len(new_hash) == 64
104 | assert new_hash == self.blockchain.hash(new_block)
105 |
--------------------------------------------------------------------------------
/csharp/BlockChain/BlockChain.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {E06FC4CE-77D0-4A64-94A6-32A08920E481}
8 | Library
9 | Properties
10 | BlockChainDemo
11 | BlockChainDemo
12 | v4.5.1
13 | 512
14 | SAK
15 | SAK
16 | SAK
17 | SAK
18 |
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 | ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | ..\packages\TinyWebServer.dll.1.0.1\lib\net40\TinyWebServer.dll
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
73 |
--------------------------------------------------------------------------------
/csharp/BlockChain/WebServer.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using System.Configuration;
3 | using System.IO;
4 | using System.Net;
5 | using System.Net.Http;
6 |
7 | namespace BlockChainDemo
8 | {
9 | public class WebServer
10 | {
11 | public WebServer(BlockChain chain)
12 | {
13 | var settings = ConfigurationManager.AppSettings;
14 | string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost";
15 | string port = settings["port"]?.Length > 1 ? settings["port"] : "12345";
16 |
17 | var server = new TinyWebServer.WebServer(request =>
18 | {
19 | string path = request.Url.PathAndQuery.ToLower();
20 | string query = "";
21 | string json = "";
22 | if (path.Contains("?"))
23 | {
24 | string[] parts = path.Split('?');
25 | path = parts[0];
26 | query = parts[1];
27 | }
28 |
29 | switch (path)
30 | {
31 | //GET: http://localhost:12345/mine
32 | case "/mine":
33 | return chain.Mine();
34 |
35 | //POST: http://localhost:12345/transactions/new
36 | //{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" }
37 | case "/transactions/new":
38 | if (request.HttpMethod != HttpMethod.Post.Method)
39 | return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
40 |
41 | json = new StreamReader(request.InputStream).ReadToEnd();
42 | Transaction trx = JsonConvert.DeserializeObject(json);
43 | int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount);
44 | return $"Your transaction will be included in block {blockId}";
45 |
46 | //GET: http://localhost:12345/chain
47 | case "/chain":
48 | return chain.GetFullChain();
49 |
50 | //POST: http://localhost:12345/nodes/register
51 | //{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] }
52 | case "/nodes/register":
53 | if (request.HttpMethod != HttpMethod.Post.Method)
54 | return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}";
55 |
56 | json = new StreamReader(request.InputStream).ReadToEnd();
57 | var urlList = new { Urls = new string[0] };
58 | var obj = JsonConvert.DeserializeAnonymousType(json, urlList);
59 | return chain.RegisterNodes(obj.Urls);
60 |
61 | //GET: http://localhost:12345/nodes/resolve
62 | case "/nodes/resolve":
63 | return chain.Consensus();
64 | }
65 |
66 | return "";
67 | },
68 | $"http://{host}:{port}/mine/",
69 | $"http://{host}:{port}/transactions/new/",
70 | $"http://{host}:{port}/chain/",
71 | $"http://{host}:{port}/nodes/register/",
72 | $"http://{host}:{port}/nodes/resolve/"
73 | );
74 |
75 | server.Run();
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/Pipfile.lock:
--------------------------------------------------------------------------------
1 | {
2 | "_meta": {
3 | "hash": {
4 | "sha256": "45af71184b5b013f58a8601f7f87c9f0b882afe19f197ce45d6d08e46d615159"
5 | },
6 | "host-environment-markers": {
7 | "implementation_name": "cpython",
8 | "implementation_version": "3.6.2",
9 | "os_name": "posix",
10 | "platform_machine": "x86_64",
11 | "platform_python_implementation": "CPython",
12 | "platform_release": "4.10.0-35-generic",
13 | "platform_system": "Linux",
14 | "platform_version": "#39~16.04.1-Ubuntu SMP Wed Sep 13 09:02:42 UTC 2017",
15 | "python_full_version": "3.6.2",
16 | "python_version": "3.6",
17 | "sys_platform": "linux"
18 | },
19 | "pipfile-spec": 6,
20 | "requires": {
21 | "python_version": "3.6"
22 | },
23 | "sources": [
24 | {
25 | "name": "pypi",
26 | "url": "https://pypi.python.org/simple",
27 | "verify_ssl": true
28 | }
29 | ]
30 | },
31 | "default": {
32 | "certifi": {
33 | "hashes": [
34 | "sha256:54a07c09c586b0e4c619f02a5e94e36619da8e2b053e20f594348c0611803704",
35 | "sha256:40523d2efb60523e113b44602298f0960e900388cf3bb6043f645cf57ea9e3f5"
36 | ],
37 | "version": "==2017.7.27.1"
38 | },
39 | "chardet": {
40 | "hashes": [
41 | "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691",
42 | "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae"
43 | ],
44 | "version": "==3.0.4"
45 | },
46 | "click": {
47 | "hashes": [
48 | "sha256:29f99fc6125fbc931b758dc053b3114e55c77a6e4c6c3a2674a2dc986016381d",
49 | "sha256:f15516df478d5a56180fbf80e68f206010e6d160fc39fa508b65e035fd75130b"
50 | ],
51 | "version": "==6.7"
52 | },
53 | "flask": {
54 | "hashes": [
55 | "sha256:0749df235e3ff61ac108f69ac178c9770caeaccad2509cb762ce1f65570a8856",
56 | "sha256:49f44461237b69ecd901cc7ce66feea0319b9158743dd27a2899962ab214dac1"
57 | ],
58 | "version": "==0.12.2"
59 | },
60 | "idna": {
61 | "hashes": [
62 | "sha256:8c7309c718f94b3a625cb648ace320157ad16ff131ae0af362c9f21b80ef6ec4",
63 | "sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f"
64 | ],
65 | "version": "==2.6"
66 | },
67 | "itsdangerous": {
68 | "hashes": [
69 | "sha256:cbb3fcf8d3e33df861709ecaf89d9e6629cff0a217bc2848f1b41cd30d360519"
70 | ],
71 | "version": "==0.24"
72 | },
73 | "jinja2": {
74 | "hashes": [
75 | "sha256:2231bace0dfd8d2bf1e5d7e41239c06c9e0ded46e70cc1094a0aa64b0afeb054",
76 | "sha256:ddaa01a212cd6d641401cb01b605f4a4d9f37bfc93043d7f760ec70fb99ff9ff"
77 | ],
78 | "version": "==2.9.6"
79 | },
80 | "markupsafe": {
81 | "hashes": [
82 | "sha256:a6be69091dac236ea9c6bc7d012beab42010fa914c459791d627dad4910eb665"
83 | ],
84 | "version": "==1.0"
85 | },
86 | "requests": {
87 | "hashes": [
88 | "sha256:6a1b267aa90cac58ac3a765d067950e7dbbf75b1da07e895d1f594193a40a38b",
89 | "sha256:9c443e7324ba5b85070c4a818ade28bfabedf16ea10206da1132edaa6dda237e"
90 | ],
91 | "version": "==2.18.4"
92 | },
93 | "urllib3": {
94 | "hashes": [
95 | "sha256:06330f386d6e4b195fbfc736b297f58c5a892e4440e54d294d7004e3a9bbea1b",
96 | "sha256:cc44da8e1145637334317feebd728bd869a35285b93cbb4cca2577da7e62db4f"
97 | ],
98 | "version": "==1.22"
99 | },
100 | "werkzeug": {
101 | "hashes": [
102 | "sha256:e8549c143af3ce6559699a01e26fa4174f4c591dbee0a499f3cd4c3781cdec3d",
103 | "sha256:903a7b87b74635244548b30d30db4c8947fe64c5198f58899ddcd3a13c23bb26"
104 | ],
105 | "version": "==0.12.2"
106 | }
107 | },
108 | "develop": {}
109 | }
110 |
--------------------------------------------------------------------------------
/csharp/BlockChain/BlockChain.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Diagnostics;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Net;
8 | using System.Security.Cryptography;
9 | using System.Text;
10 |
11 | namespace BlockChainDemo
12 | {
13 | public class BlockChain
14 | {
15 | private List _currentTransactions = new List();
16 | private List _chain = new List();
17 | private List _nodes = new List();
18 | private Block _lastBlock => _chain.Last();
19 |
20 | public string NodeId { get; private set; }
21 |
22 | //ctor
23 | public BlockChain()
24 | {
25 | NodeId = Guid.NewGuid().ToString().Replace("-", "");
26 | CreateNewBlock(proof: 100, previousHash: "1"); //genesis block
27 | }
28 |
29 | //private functionality
30 | private void RegisterNode(string address)
31 | {
32 | _nodes.Add(new Node { Address = new Uri(address) });
33 | }
34 |
35 | private bool IsValidChain(List chain)
36 | {
37 | Block block = null;
38 | Block lastBlock = chain.First();
39 | int currentIndex = 1;
40 | while (currentIndex < chain.Count)
41 | {
42 | block = chain.ElementAt(currentIndex);
43 | Debug.WriteLine($"{lastBlock}");
44 | Debug.WriteLine($"{block}");
45 | Debug.WriteLine("----------------------------");
46 |
47 | //Check that the hash of the block is correct
48 | if (block.PreviousHash != GetHash(lastBlock))
49 | return false;
50 |
51 | //Check that the Proof of Work is correct
52 | if (!IsValidProof(lastBlock.Proof, block.Proof, lastBlock.PreviousHash))
53 | return false;
54 |
55 | lastBlock = block;
56 | currentIndex++;
57 | }
58 |
59 | return true;
60 | }
61 |
62 | private bool ResolveConflicts()
63 | {
64 | List newChain = null;
65 | int maxLength = _chain.Count;
66 |
67 | foreach (Node node in _nodes)
68 | {
69 | var url = new Uri(node.Address, "/chain");
70 | var request = (HttpWebRequest)WebRequest.Create(url);
71 | var response = (HttpWebResponse)request.GetResponse();
72 |
73 | if (response.StatusCode == HttpStatusCode.OK)
74 | {
75 | var model = new
76 | {
77 | chain = new List(),
78 | length = 0
79 | };
80 | string json = new StreamReader(response.GetResponseStream()).ReadToEnd();
81 | var data = JsonConvert.DeserializeAnonymousType(json, model);
82 |
83 | if (data.chain.Count > _chain.Count && IsValidChain(data.chain))
84 | {
85 | maxLength = data.chain.Count;
86 | newChain = data.chain;
87 | }
88 | }
89 | }
90 |
91 | if (newChain != null)
92 | {
93 | _chain = newChain;
94 | return true;
95 | }
96 |
97 | return false;
98 | }
99 |
100 | private Block CreateNewBlock(int proof, string previousHash = null)
101 | {
102 | var block = new Block
103 | {
104 | Index = _chain.Count,
105 | Timestamp = DateTime.UtcNow,
106 | Transactions = _currentTransactions.ToList(),
107 | Proof = proof,
108 | PreviousHash = previousHash ?? GetHash(_chain.Last())
109 | };
110 |
111 | _currentTransactions.Clear();
112 | _chain.Add(block);
113 | return block;
114 | }
115 |
116 | private int CreateProofOfWork(int lastProof, string previousHash)
117 | {
118 | int proof = 0;
119 | while (!IsValidProof(lastProof, proof, previousHash))
120 | proof++;
121 |
122 | return proof;
123 | }
124 |
125 | private bool IsValidProof(int lastProof, int proof, string previousHash)
126 | {
127 | string guess = $"{lastProof}{proof}{previousHash}";
128 | string result = GetSha256(guess);
129 | return result.StartsWith("0000");
130 | }
131 |
132 | private string GetHash(Block block)
133 | {
134 | string blockText = JsonConvert.SerializeObject(block);
135 | return GetSha256(blockText);
136 | }
137 |
138 | private string GetSha256(string data)
139 | {
140 | var sha256 = new SHA256Managed();
141 | var hashBuilder = new StringBuilder();
142 |
143 | byte[] bytes = Encoding.Unicode.GetBytes(data);
144 | byte[] hash = sha256.ComputeHash(bytes);
145 |
146 | foreach (byte x in hash)
147 | hashBuilder.Append($"{x:x2}");
148 |
149 | return hashBuilder.ToString();
150 | }
151 |
152 | //web server calls
153 | internal string Mine()
154 | {
155 | int proof = CreateProofOfWork(_lastBlock.Proof, _lastBlock.PreviousHash);
156 |
157 | CreateTransaction(sender: "0", recipient: NodeId, amount: 1);
158 | Block block = CreateNewBlock(proof /*, _lastBlock.PreviousHash*/);
159 |
160 | var response = new
161 | {
162 | Message = "New Block Forged",
163 | Index = block.Index,
164 | Transactions = block.Transactions.ToArray(),
165 | Proof = block.Proof,
166 | PreviousHash = block.PreviousHash
167 | };
168 |
169 | return JsonConvert.SerializeObject(response);
170 | }
171 |
172 | internal string GetFullChain()
173 | {
174 | var response = new
175 | {
176 | chain = _chain.ToArray(),
177 | length = _chain.Count
178 | };
179 |
180 | return JsonConvert.SerializeObject(response);
181 | }
182 |
183 | internal string RegisterNodes(string[] nodes)
184 | {
185 | var builder = new StringBuilder();
186 | foreach (string node in nodes)
187 | {
188 | string url = $"http://{node}";
189 | RegisterNode(url);
190 | builder.Append($"{url}, ");
191 | }
192 |
193 | builder.Insert(0, $"{nodes.Count()} new nodes have been added: ");
194 | string result = builder.ToString();
195 | return result.Substring(0, result.Length - 2);
196 | }
197 |
198 | internal string Consensus()
199 | {
200 | bool replaced = ResolveConflicts();
201 | string message = replaced ? "was replaced" : "is authoritive";
202 |
203 | var response = new
204 | {
205 | Message = $"Our chain {message}",
206 | Chain = _chain
207 | };
208 |
209 | return JsonConvert.SerializeObject(response);
210 | }
211 |
212 | internal int CreateTransaction(string sender, string recipient, int amount)
213 | {
214 | var transaction = new Transaction
215 | {
216 | Sender = sender,
217 | Recipient = recipient,
218 | Amount = amount
219 | };
220 |
221 | _currentTransactions.Add(transaction);
222 |
223 | return _lastBlock != null ? _lastBlock.Index + 1 : 0;
224 | }
225 | }
226 | }
227 |
--------------------------------------------------------------------------------
/blockchain.py:
--------------------------------------------------------------------------------
1 | import hashlib
2 | import json
3 | from time import time
4 | from urllib.parse import urlparse
5 | from uuid import uuid4
6 |
7 | import requests
8 | from flask import Flask, jsonify, request
9 |
10 |
11 | class Blockchain:
12 | def __init__(self):
13 | self.current_transactions = []
14 | self.chain = []
15 | self.nodes = set()
16 |
17 | # Create the genesis block
18 | self.new_block(previous_hash='1', proof=100)
19 |
20 | def register_node(self, address):
21 | """
22 | Add a new node to the list of nodes
23 |
24 | :param address: Address of node. Eg. 'http://192.168.0.5:5000'
25 | """
26 |
27 | parsed_url = urlparse(address)
28 | if parsed_url.netloc:
29 | self.nodes.add(parsed_url.netloc)
30 | elif parsed_url.path:
31 | # Accepts an URL without scheme like '192.168.0.5:5000'.
32 | self.nodes.add(parsed_url.path)
33 | else:
34 | raise ValueError('Invalid URL')
35 |
36 |
37 | def valid_chain(self, chain):
38 | """
39 | Determine if a given blockchain is valid
40 |
41 | :param chain: A blockchain
42 | :return: True if valid, False if not
43 | """
44 |
45 | last_block = chain[0]
46 | current_index = 1
47 |
48 | while current_index < len(chain):
49 | block = chain[current_index]
50 | print(f'{last_block}')
51 | print(f'{block}')
52 | print("\n-----------\n")
53 | # Check that the hash of the block is correct
54 | last_block_hash = self.hash(last_block)
55 | if block['previous_hash'] != last_block_hash:
56 | return False
57 |
58 | # Check that the Proof of Work is correct
59 | if not self.valid_proof(last_block['proof'], block['proof'], last_block_hash):
60 | return False
61 |
62 | last_block = block
63 | current_index += 1
64 |
65 | return True
66 |
67 | def resolve_conflicts(self):
68 | """
69 | This is our consensus algorithm, it resolves conflicts
70 | by replacing our chain with the longest one in the network.
71 |
72 | :return: True if our chain was replaced, False if not
73 | """
74 |
75 | neighbours = self.nodes
76 | new_chain = None
77 |
78 | # We're only looking for chains longer than ours
79 | max_length = len(self.chain)
80 |
81 | # Grab and verify the chains from all the nodes in our network
82 | for node in neighbours:
83 | response = requests.get(f'http://{node}/chain')
84 |
85 | if response.status_code == 200:
86 | length = response.json()['length']
87 | chain = response.json()['chain']
88 |
89 | # Check if the length is longer and the chain is valid
90 | if length > max_length and self.valid_chain(chain):
91 | max_length = length
92 | new_chain = chain
93 |
94 | # Replace our chain if we discovered a new, valid chain longer than ours
95 | if new_chain:
96 | self.chain = new_chain
97 | return True
98 |
99 | return False
100 |
101 | def new_block(self, proof, previous_hash):
102 | """
103 | Create a new Block in the Blockchain
104 |
105 | :param proof: The proof given by the Proof of Work algorithm
106 | :param previous_hash: Hash of previous Block
107 | :return: New Block
108 | """
109 |
110 | block = {
111 | 'index': len(self.chain) + 1,
112 | 'timestamp': time(),
113 | 'transactions': self.current_transactions,
114 | 'proof': proof,
115 | 'previous_hash': previous_hash or self.hash(self.chain[-1]),
116 | }
117 |
118 | # Reset the current list of transactions
119 | self.current_transactions = []
120 |
121 | self.chain.append(block)
122 | return block
123 |
124 | def new_transaction(self, sender, recipient, amount):
125 | """
126 | Creates a new transaction to go into the next mined Block
127 |
128 | :param sender: Address of the Sender
129 | :param recipient: Address of the Recipient
130 | :param amount: Amount
131 | :return: The index of the Block that will hold this transaction
132 | """
133 | self.current_transactions.append({
134 | 'sender': sender,
135 | 'recipient': recipient,
136 | 'amount': amount,
137 | })
138 |
139 | return self.last_block['index'] + 1
140 |
141 | @property
142 | def last_block(self):
143 | return self.chain[-1]
144 |
145 | @staticmethod
146 | def hash(block):
147 | """
148 | Creates a SHA-256 hash of a Block
149 |
150 | :param block: Block
151 | """
152 |
153 | # We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes
154 | block_string = json.dumps(block, sort_keys=True).encode()
155 | return hashlib.sha256(block_string).hexdigest()
156 |
157 | def proof_of_work(self, last_block):
158 | """
159 | Simple Proof of Work Algorithm:
160 |
161 | - Find a number p' such that hash(pp') contains leading 4 zeroes
162 | - Where p is the previous proof, and p' is the new proof
163 |
164 | :param last_block: last Block
165 | :return:
166 | """
167 |
168 | last_proof = last_block['proof']
169 | last_hash = self.hash(last_block)
170 |
171 | proof = 0
172 | while self.valid_proof(last_proof, proof, last_hash) is False:
173 | proof += 1
174 |
175 | return proof
176 |
177 | @staticmethod
178 | def valid_proof(last_proof, proof, last_hash):
179 | """
180 | Validates the Proof
181 |
182 | :param last_proof: Previous Proof
183 | :param proof: Current Proof
184 | :param last_hash: The hash of the Previous Block
185 | :return: True if correct, False if not.
186 |
187 | """
188 |
189 | guess = f'{last_proof}{proof}{last_hash}'.encode()
190 | guess_hash = hashlib.sha256(guess).hexdigest()
191 | return guess_hash[:4] == "0000"
192 |
193 |
194 | # Instantiate the Node
195 | app = Flask(__name__)
196 |
197 | # Generate a globally unique address for this node
198 | node_identifier = str(uuid4()).replace('-', '')
199 |
200 | # Instantiate the Blockchain
201 | blockchain = Blockchain()
202 |
203 |
204 | @app.route('/mine', methods=['GET'])
205 | def mine():
206 | # We run the proof of work algorithm to get the next proof...
207 | last_block = blockchain.last_block
208 | proof = blockchain.proof_of_work(last_block)
209 |
210 | # We must receive a reward for finding the proof.
211 | # The sender is "0" to signify that this node has mined a new coin.
212 | blockchain.new_transaction(
213 | sender="0",
214 | recipient=node_identifier,
215 | amount=1,
216 | )
217 |
218 | # Forge the new Block by adding it to the chain
219 | previous_hash = blockchain.hash(last_block)
220 | block = blockchain.new_block(proof, previous_hash)
221 |
222 | response = {
223 | 'message': "New Block Forged",
224 | 'index': block['index'],
225 | 'transactions': block['transactions'],
226 | 'proof': block['proof'],
227 | 'previous_hash': block['previous_hash'],
228 | }
229 | return jsonify(response), 200
230 |
231 |
232 | @app.route('/transactions/new', methods=['POST'])
233 | def new_transaction():
234 | values = request.get_json()
235 |
236 | # Check that the required fields are in the POST'ed data
237 | required = ['sender', 'recipient', 'amount']
238 | if not all(k in values for k in required):
239 | return 'Missing values', 400
240 |
241 | # Create a new Transaction
242 | index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])
243 |
244 | response = {'message': f'Transaction will be added to Block {index}'}
245 | return jsonify(response), 201
246 |
247 |
248 | @app.route('/chain', methods=['GET'])
249 | def full_chain():
250 | response = {
251 | 'chain': blockchain.chain,
252 | 'length': len(blockchain.chain),
253 | }
254 | return jsonify(response), 200
255 |
256 |
257 | @app.route('/nodes/register', methods=['POST'])
258 | def register_nodes():
259 | values = request.get_json()
260 |
261 | nodes = values.get('nodes')
262 | if nodes is None:
263 | return "Error: Please supply a valid list of nodes", 400
264 |
265 | for node in nodes:
266 | blockchain.register_node(node)
267 |
268 | response = {
269 | 'message': 'New nodes have been added',
270 | 'total_nodes': list(blockchain.nodes),
271 | }
272 | return jsonify(response), 201
273 |
274 |
275 | @app.route('/nodes/resolve', methods=['GET'])
276 | def consensus():
277 | replaced = blockchain.resolve_conflicts()
278 |
279 | if replaced:
280 | response = {
281 | 'message': 'Our chain was replaced',
282 | 'new_chain': blockchain.chain
283 | }
284 | else:
285 | response = {
286 | 'message': 'Our chain is authoritative',
287 | 'chain': blockchain.chain
288 | }
289 |
290 | return jsonify(response), 200
291 |
292 |
293 | if __name__ == '__main__':
294 | from argparse import ArgumentParser
295 |
296 | parser = ArgumentParser()
297 | parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
298 | args = parser.parse_args()
299 | port = args.port
300 |
301 | app.run(host='0.0.0.0', port=port)
302 |
--------------------------------------------------------------------------------