From a26be060802c49beaec58caaa9e22c1c2de25e96 Mon Sep 17 00:00:00 2001 From: massa142 Date: Thu, 5 Oct 2017 18:20:56 +0900 Subject: [PATCH 01/15] Add type hints --- blockchain.py | 58 ++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/blockchain.py b/blockchain.py index 53c7b78..f55b269 100644 --- a/blockchain.py +++ b/blockchain.py @@ -1,6 +1,7 @@ import hashlib import json from time import time +from typing import Any, Dict, List, Optional from urllib.parse import urlparse from uuid import uuid4 @@ -8,32 +9,31 @@ import requests from flask import Flask, jsonify, request -class Blockchain(object): +class Blockchain: def __init__(self): self.current_transactions = [] self.chain = [] self.nodes = set() # Create the genesis block - self.new_block(previous_hash=1, proof=100) + self.new_block(previous_hash='1', proof=100) - def register_node(self, address): + def register_node(self, address: str) -> None: """ Add a new node to the list of nodes - :param address: Address of node. Eg. 'http://192.168.0.5:5000' - :return: None + :param address: Address of node. Eg. 'http://192.168.0.5:5000' """ parsed_url = urlparse(address) self.nodes.add(parsed_url.netloc) - def valid_chain(self, chain): + def valid_chain(self, chain: List[Dict[str, Any]]) -> bool: """ Determine if a given blockchain is valid - :param chain: A blockchain - :return: True if valid, False if not + :param chain: A blockchain + :return: True if valid, False if not """ last_block = chain[0] @@ -57,12 +57,12 @@ class Blockchain(object): return True - def resolve_conflicts(self): + def resolve_conflicts(self) -> bool: """ This is our consensus algorithm, it resolves conflicts by replacing our chain with the longest one in the network. - :return: True if our chain was replaced, False if not + :return: True if our chain was replaced, False if not """ neighbours = self.nodes @@ -91,13 +91,13 @@ class Blockchain(object): return False - def new_block(self, proof, previous_hash=None): + def new_block(self, proof: int, previous_hash: Optional[str]) -> Dict[str, Any]: """ Create a new Block in the Blockchain - :param proof: The proof given by the Proof of Work algorithm - :param previous_hash: (Optional) Hash of previous Block - :return: New Block + :param proof: The proof given by the Proof of Work algorithm + :param previous_hash: Hash of previous Block + :return: New Block """ block = { @@ -114,14 +114,14 @@ class Blockchain(object): self.chain.append(block) return block - def new_transaction(self, sender, recipient, amount): + def new_transaction(self, sender: str, recipient: str, amount: int) -> int: """ Creates a new transaction to go into the next mined Block - :param sender: Address of the Sender - :param recipient: Address of the Recipient - :param amount: Amount - :return: The index of the Block that will hold this transaction + :param sender: Address of the Sender + :param recipient: Address of the Recipient + :param amount: Amount + :return: The index of the Block that will hold this transaction """ self.current_transactions.append({ 'sender': sender, @@ -132,30 +132,26 @@ class Blockchain(object): return self.last_block['index'] + 1 @property - def last_block(self): + def last_block(self) -> Dict[str: Any]: return self.chain[-1] @staticmethod - def hash(block): + def hash(block: Dict[str, Any]) -> str: """ Creates a SHA-256 hash of a Block - :param block: Block - :return: + :param block: Block """ # We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes block_string = json.dumps(block, sort_keys=True).encode() return hashlib.sha256(block_string).hexdigest() - def proof_of_work(self, last_proof): + def proof_of_work(self, last_proof: int) -> int: """ Simple Proof of Work Algorithm: - Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p' - p is the previous proof, and p' is the new proof - - :param last_proof: - :return: """ proof = 0 @@ -165,13 +161,13 @@ class Blockchain(object): return proof @staticmethod - def valid_proof(last_proof, proof): + def valid_proof(last_proof: int, proof: int) -> bool: """ Validates the Proof - :param last_proof: Previous Proof - :param proof: Current Proof - :return: True if correct, False if not. + :param last_proof: Previous Proof + :param proof: Current Proof + :return: True if correct, False if not. """ guess = f'{last_proof}{proof}'.encode() From f44f9027530054cded077e0d2e911ec021c79fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richer=20Bal=C3=A1zs?= Date: Mon, 16 Oct 2017 20:56:31 +0200 Subject: [PATCH 02/15] Fix of type errors Traceback (most recent call last): File "blockchain.py", line 12, in class Blockchain: File "blockchain.py", line 135, in Blockchain def last_block(self) -> Dict[str: Any]: File "/usr/local/lib/python3.6/typing.py", line 682, in inner return func(*args, **kwds) File "/usr/local/lib/python3.6/typing.py", line 1106, in __getitem__ params = tuple(_type_check(p, msg) for p in params) File "/usr/local/lib/python3.6/typing.py", line 1106, in params = tuple(_type_check(p, msg) for p in params) File "/usr/local/lib/python3.6/typing.py", line 374, in _type_check raise TypeError(msg + " Got %.100r." % (arg,)) TypeError: Parameters to generic types must be types. Got slice(, typing.Any, None). --- blockchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain.py b/blockchain.py index f55b269..b1ca4e6 100644 --- a/blockchain.py +++ b/blockchain.py @@ -132,7 +132,7 @@ class Blockchain: return self.last_block['index'] + 1 @property - def last_block(self) -> Dict[str: Any]: + def last_block(self) -> Dict[str, Any]: return self.chain[-1] @staticmethod From b1a8a346626a2d22cb03a2f70dfbba5fa2f412c4 Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Mon, 30 Oct 2017 00:51:55 -0400 Subject: [PATCH 03/15] New block needs the previous hash --- blockchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain.py b/blockchain.py index f55b269..43a8dbe 100644 --- a/blockchain.py +++ b/blockchain.py @@ -201,7 +201,7 @@ def mine(): ) # Forge the new Block by adding it to the chain - block = blockchain.new_block(proof) + block = blockchain.new_block(proof, last_block['previous_hash']) response = { 'message': "New Block Forged", From 3fa90ff8ba3fc913fe80421f637d5762e3dbe12c Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 15:32:10 -0500 Subject: [PATCH 04/15] Remove Type Annotations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have some reasons for this, but could be swayed to keep them: - For folks who are learning, it tends to get in the way—there are way too many questions about this. - It doesn’t bring much value if there are reasonable tests. - Not many folks are using `mypy` before run time. --- blockchain.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/blockchain.py b/blockchain.py index cb1cb8a..a6c2f65 100644 --- a/blockchain.py +++ b/blockchain.py @@ -18,7 +18,7 @@ class Blockchain: # Create the genesis block self.new_block(previous_hash='1', proof=100) - def register_node(self, address: str) -> None: + def register_node(self, address): """ Add a new node to the list of nodes @@ -28,7 +28,7 @@ class Blockchain: parsed_url = urlparse(address) self.nodes.add(parsed_url.netloc) - def valid_chain(self, chain: List[Dict[str, Any]]) -> bool: + def valid_chain(self, chain): """ Determine if a given blockchain is valid @@ -57,7 +57,7 @@ class Blockchain: return True - def resolve_conflicts(self) -> bool: + def resolve_conflicts(self): """ This is our consensus algorithm, it resolves conflicts by replacing our chain with the longest one in the network. @@ -91,7 +91,7 @@ class Blockchain: return False - def new_block(self, proof: int, previous_hash: Optional[str]) -> Dict[str, Any]: + def new_block(self, proof, previous_hash): """ Create a new Block in the Blockchain @@ -114,7 +114,7 @@ class Blockchain: self.chain.append(block) return block - def new_transaction(self, sender: str, recipient: str, amount: int) -> int: + def new_transaction(self, sender, recipient, amount): """ Creates a new transaction to go into the next mined Block @@ -132,11 +132,11 @@ class Blockchain: return self.last_block['index'] + 1 @property - def last_block(self) -> Dict[str, Any]: + def last_block(self): return self.chain[-1] @staticmethod - def hash(block: Dict[str, Any]) -> str: + def hash(block): """ Creates a SHA-256 hash of a Block @@ -147,7 +147,7 @@ class Blockchain: block_string = json.dumps(block, sort_keys=True).encode() return hashlib.sha256(block_string).hexdigest() - def proof_of_work(self, last_proof: int) -> int: + def proof_of_work(self, last_proof): """ Simple Proof of Work Algorithm: - Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p' @@ -161,7 +161,7 @@ class Blockchain: return proof @staticmethod - def valid_proof(last_proof: int, proof: int) -> bool: + def valid_proof(last_proof, proof): """ Validates the Proof From 36572e07deb143a1dcf6445bf9e9b5eb65fc37c9 Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 16:05:13 -0500 Subject: [PATCH 05/15] Preliminary Tests Intend to add more tests --- tests/__init__.py | 0 tests/test_blockchain.py | 104 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_blockchain.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_blockchain.py b/tests/test_blockchain.py new file mode 100644 index 0000000..f2d85e1 --- /dev/null +++ b/tests/test_blockchain.py @@ -0,0 +1,104 @@ +import hashlib +import json +from unittest import TestCase + +from blockchain import Blockchain + + +class BlockchainTestCase(TestCase): + + def setUp(self): + self.blockchain = Blockchain() + + def create_block(self, proof=123, previous_hash='abc'): + self.blockchain.new_block(proof, previous_hash) + + def create_transaction(self, sender='a', recipient='b', amount=1): + self.blockchain.new_transaction( + sender=sender, + recipient=recipient, + amount=amount + ) + + +class TestRegisterNodes(BlockchainTestCase): + + def test_valid_nodes(self): + blockchain = Blockchain() + + blockchain.register_node('http://192.168.0.1:5000') + + self.assertIn('192.168.0.1:5000', blockchain.nodes) + + def test_malformed_nodes(self): + blockchain = Blockchain() + + blockchain.register_node('http//192.168.0.1:5000') + + self.assertNotIn('192.168.0.1:5000', blockchain.nodes) + + def test_idempotency(self): + blockchain = Blockchain() + + blockchain.register_node('http://192.168.0.1:5000') + blockchain.register_node('http://192.168.0.1:5000') + + assert len(blockchain.nodes) == 1 + + +class TestBlocksAndTransactions(BlockchainTestCase): + + def test_block_creation(self): + self.create_block() + + latest_block = self.blockchain.last_block + + # The genesis block is create at initialization, so the length should be 2 + assert len(self.blockchain.chain) == 2 + assert latest_block['index'] == 2 + assert latest_block['timestamp'] is not None + assert latest_block['proof'] == 123 + assert latest_block['previous_hash'] == 'abc' + + def test_create_transaction(self): + self.create_transaction() + + transaction = self.blockchain.current_transactions[-1] + + assert transaction + assert transaction['sender'] == 'a' + assert transaction['recipient'] == 'b' + assert transaction['amount'] == 1 + + def test_block_resets_transactions(self): + self.create_transaction() + + initial_length = len(self.blockchain.current_transactions) + + self.create_block() + + current_length = len(self.blockchain.current_transactions) + + assert initial_length == 1 + assert current_length == 0 + + def test_return_last_block(self): + self.create_block() + + created_block = self.blockchain.last_block + + assert len(self.blockchain.chain) == 2 + assert created_block is self.blockchain.chain[-1] + + +class TestHashingAndProofs(BlockchainTestCase): + + def test_hash_is_correct(self): + self.create_block() + + new_block = self.blockchain.last_block + new_block_json = json.dumps(self.blockchain.last_block, sort_keys=True).encode() + new_hash = hashlib.sha256(new_block_json).hexdigest() + + assert len(new_hash) == 64 + assert new_hash == self.blockchain.hash(new_block) From d02ce03dde3c980e3277c0124ae89b0f20f7a0d0 Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 16:06:02 -0500 Subject: [PATCH 06/15] Clean imports --- blockchain.py | 1 - 1 file changed, 1 deletion(-) diff --git a/blockchain.py b/blockchain.py index a6c2f65..aba163b 100644 --- a/blockchain.py +++ b/blockchain.py @@ -1,7 +1,6 @@ import hashlib import json from time import time -from typing import Any, Dict, List, Optional from urllib.parse import urlparse from uuid import uuid4 From 5d1b972b821db4a2bde4e871292e2678c9315a4e Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 16:20:22 -0500 Subject: [PATCH 07/15] Add Travis Integration --- .travis.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0eb0d5e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: python + +python: + - 3.6 + - nightly + +env: + - PIPENV_VENV_IN_PROJECT=1 + - PIPENV_IGNORE_VIRTUALENVS=1 + +install: + - pip install pipenv + - pipenv --three + - pipenv install -d --system + +script: + - pipenv run python -m unittest From 0a0b2a9b3d4a9db58d076d8d3c75078e46ee2e53 Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 16:26:00 -0500 Subject: [PATCH 08/15] Travis fix --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0eb0d5e..be655e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,6 @@ python: - 3.6 - nightly -env: - - PIPENV_VENV_IN_PROJECT=1 - - PIPENV_IGNORE_VIRTUALENVS=1 - install: - pip install pipenv - pipenv --three From 00ca8267dfd762d5e2814754942687d9e7265314 Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 16:30:14 -0500 Subject: [PATCH 09/15] And again... --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index be655e9..54cc6c5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,8 +6,7 @@ python: install: - pip install pipenv - - pipenv --three - - pipenv install -d --system + - pipenv install -dev script: - pipenv run python -m unittest From c765a738061bc76d86da213885e7c2e9d52da786 Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 16:31:50 -0500 Subject: [PATCH 10/15] May finally be losing my mind --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 54cc6c5..1991e5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ python: install: - pip install pipenv - - pipenv install -dev + - pipenv install --dev script: - pipenv run python -m unittest From ecc5883f3b1f01033a066aceb123603d4be8116d Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 16:35:26 -0500 Subject: [PATCH 11/15] Add Travis Icon --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0f46b5b..e77626b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Learn Blockchains by Building One +[![Build Status](https://travis-ci.org/dvf/blockchain.svg?branch=master)](https://travis-ci.org/dvf/blockchain) + This is the source code for my post on [Building a Blockchain](https://medium.com/p/117428612f46). ## Installation From a864c6aef4018d8646fd5d8e1af64cf3b534871b Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sat, 18 Nov 2017 12:49:29 -0500 Subject: [PATCH 12/15] Fix major bug --- blockchain.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blockchain.py b/blockchain.py index aba163b..4e92d07 100644 --- a/blockchain.py +++ b/blockchain.py @@ -200,7 +200,8 @@ def mine(): ) # Forge the new Block by adding it to the chain - block = blockchain.new_block(proof, last_block['previous_hash']) + previous_hash = blockchain.hash(last_block) + block = blockchain.new_block(proof, previous_hash) response = { 'message': "New Block Forged", From 81aede25ec9fb6551fca23ae8651229027260a68 Mon Sep 17 00:00:00 2001 From: davetoland Date: Sat, 18 Nov 2017 20:45:28 +0000 Subject: [PATCH 13/15] C# Implementation Ported from blockchain.py --- .gitattributes | 63 +++++ csharp/BlockChain.Console/App.config | 6 + .../BlockChain.Console.csproj | 63 +++++ csharp/BlockChain.Console/Program.cs | 12 + .../Properties/AssemblyInfo.cs | 36 +++ csharp/BlockChain.sln | 43 ++++ csharp/BlockChain/Block.cs | 19 ++ csharp/BlockChain/BlockChain.cs | 220 ++++++++++++++++++ csharp/BlockChain/BlockChain.csproj | 72 ++++++ csharp/BlockChain/Node.cs | 9 + csharp/BlockChain/Properties/AssemblyInfo.cs | 36 +++ csharp/BlockChain/Transaction.cs | 9 + csharp/BlockChain/WebServer.cs | 72 ++++++ csharp/BlockChain/packages.config | 5 + 14 files changed, 665 insertions(+) create mode 100644 .gitattributes create mode 100644 csharp/BlockChain.Console/App.config create mode 100644 csharp/BlockChain.Console/BlockChain.Console.csproj create mode 100644 csharp/BlockChain.Console/Program.cs create mode 100644 csharp/BlockChain.Console/Properties/AssemblyInfo.cs create mode 100644 csharp/BlockChain.sln create mode 100644 csharp/BlockChain/Block.cs create mode 100644 csharp/BlockChain/BlockChain.cs create mode 100644 csharp/BlockChain/BlockChain.csproj create mode 100644 csharp/BlockChain/Node.cs create mode 100644 csharp/BlockChain/Properties/AssemblyInfo.cs create mode 100644 csharp/BlockChain/Transaction.cs create mode 100644 csharp/BlockChain/WebServer.cs create mode 100644 csharp/BlockChain/packages.config diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/csharp/BlockChain.Console/App.config b/csharp/BlockChain.Console/App.config new file mode 100644 index 0000000..d0feca6 --- /dev/null +++ b/csharp/BlockChain.Console/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/csharp/BlockChain.Console/BlockChain.Console.csproj b/csharp/BlockChain.Console/BlockChain.Console.csproj new file mode 100644 index 0000000..5bc5670 --- /dev/null +++ b/csharp/BlockChain.Console/BlockChain.Console.csproj @@ -0,0 +1,63 @@ + + + + + Debug + AnyCPU + {D0C795A0-6F20-4A8E-BE44-801678754DA4} + Exe + BlockChainDemo.Console + BlockChainDemo.Console + v4.5.1 + 512 + true + SAK + SAK + SAK + SAK + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + {e06fc4ce-77d0-4a64-94a6-32a08920e481} + BlockChain + + + + \ No newline at end of file diff --git a/csharp/BlockChain.Console/Program.cs b/csharp/BlockChain.Console/Program.cs new file mode 100644 index 0000000..2573f93 --- /dev/null +++ b/csharp/BlockChain.Console/Program.cs @@ -0,0 +1,12 @@ +namespace BlockChainDemo.Console +{ + class Program + { + static void Main(string[] args) + { + var chain = new BlockChain(); + var server = new WebServer(chain); + System.Console.Read(); + } + } +} diff --git a/csharp/BlockChain.Console/Properties/AssemblyInfo.cs b/csharp/BlockChain.Console/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2b25de2 --- /dev/null +++ b/csharp/BlockChain.Console/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BlockChain.Console")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BlockChain.Console")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d0c795a0-6f20-4a8e-be44-801678754da4")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/csharp/BlockChain.sln b/csharp/BlockChain.sln new file mode 100644 index 0000000..0175024 --- /dev/null +++ b/csharp/BlockChain.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27004.2008 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain", "BlockChain\BlockChain.csproj", "{E06FC4CE-77D0-4A64-94A6-32A08920E481}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlockChain.Console", "BlockChain.Console\BlockChain.Console.csproj", "{D0C795A0-6F20-4A8E-BE44-801678754DA4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E06FC4CE-77D0-4A64-94A6-32A08920E481}.Release|Any CPU.Build.0 = Release|Any CPU + {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D0C795A0-6F20-4A8E-BE44-801678754DA4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7FB3650B-CAFB-4A71-940B-0CA6F0377422} + EndGlobalSection + GlobalSection(TeamFoundationVersionControl) = preSolution + SccNumberOfProjects = 3 + SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C} + SccTeamFoundationServer = https://devfire.visualstudio.com/ + SccLocalPath0 = . + SccProjectUniqueName1 = BlockChain\\BlockChain.csproj + SccProjectName1 = BlockChain + SccLocalPath1 = BlockChain + SccProjectUniqueName2 = BlockChain.Console\\BlockChain.Console.csproj + SccProjectName2 = BlockChain.Console + SccLocalPath2 = BlockChain.Console + EndGlobalSection +EndGlobal diff --git a/csharp/BlockChain/Block.cs b/csharp/BlockChain/Block.cs new file mode 100644 index 0000000..9ca9d80 --- /dev/null +++ b/csharp/BlockChain/Block.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; + +namespace BlockChainDemo +{ + public class Block + { + public int Index { get; set; } + public DateTime Timestamp { get; set; } + public List Transactions { get; set; } + public int Proof { get; set; } + public string PreviousHash { get; set; } + + public override string ToString() + { + return $"{Index} [{Timestamp.ToString("yyyy-MM-dd HH:mm:ss")}] Proof: {Proof} | PrevHash: {PreviousHash} | Trx: {Transactions.Count}"; + } + } +} \ No newline at end of file diff --git a/csharp/BlockChain/BlockChain.cs b/csharp/BlockChain/BlockChain.cs new file mode 100644 index 0000000..52a8f71 --- /dev/null +++ b/csharp/BlockChain/BlockChain.cs @@ -0,0 +1,220 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Cryptography; +using System.Text; + +namespace BlockChainDemo +{ + public class BlockChain + { + private List _currentTransactions = new List(); + private List _chain = new List(); + private List _nodes = new List(); + private Block _lastBlock => _chain.Last(); + + public string NodeId { get; private set; } + + //ctor + public BlockChain() + { + NodeId = Guid.NewGuid().ToString().Replace("-", ""); + CreateNewBlock(proof: 100, previousHash: "1"); //genesis block + } + + //private functionality + private void RegisterNode(string address) + { + _nodes.Add(new Node { Address = new Uri(address) }); + } + + private bool IsValidChain(List chain) + { + Block block = null; + Block lastBlock = chain.First(); + int currentIndex = 1; + while (currentIndex < chain.Count) + { + block = chain.ElementAt(currentIndex); + Debug.WriteLine($"{lastBlock}"); + Debug.WriteLine($"{block}"); + Debug.WriteLine("----------------------------"); + + //Check that the hash of the block is correct + if (block.PreviousHash != GetHash(lastBlock)) + return false; + + //Check that the Proof of Work is correct + if (!IsValidProof(lastBlock.Proof, block.Proof, lastBlock.PreviousHash)) + return false; + + lastBlock = block; + currentIndex++; + } + + return true; + } + + private bool ResolveConflicts() + { + List newChain = null; + int maxLength = _chain.Count; + + foreach (Node node in _nodes) + { + var request = (HttpWebRequest)WebRequest.Create(node.Address); + var response = (HttpWebResponse)request.GetResponse(); + + if (response.StatusCode == HttpStatusCode.OK) + { + string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); + List chain = JsonConvert.DeserializeObject>(json); + + if (chain.Count > _chain.Count && IsValidChain(chain)) + { + maxLength = chain.Count; + newChain = chain; + } + } + } + + if (newChain != null) + { + _chain = newChain; + return true; + } + + return false; + } + + private Block CreateNewBlock(int proof, string previousHash = null) + { + var block = new Block + { + Index = _chain.Count, + Timestamp = DateTime.UtcNow, + Transactions = _currentTransactions.ToList(), + Proof = proof, + PreviousHash = previousHash ?? GetHash(_chain.Last()) + }; + + _currentTransactions.Clear(); + _chain.Add(block); + return block; + } + + private int CreateProofOfWork(int lastProof, string previousHash) + { + int proof = 0; + while (!IsValidProof(lastProof, proof, previousHash)) + proof++; + + return proof; + } + + private bool IsValidProof(int lastProof, int proof, string previousHash) + { + string guess = $"{lastProof}{proof}{previousHash}"; + string result = GetSha256(guess); + return result.StartsWith("0000"); + } + + private string GetHash(Block block) + { + string blockText = JsonConvert.SerializeObject(block); + return GetSha256(blockText); + } + + private string GetSha256(string data) + { + var sha256 = new SHA256Managed(); + var hashBuilder = new StringBuilder(); + + byte[] bytes = Encoding.Unicode.GetBytes(data); + byte[] hash = sha256.ComputeHash(bytes); + + foreach (byte x in hash) + hashBuilder.Append($"{x:x2}"); + + return hashBuilder.ToString(); + } + + //web server calls + internal string Mine() + { + int proof = CreateProofOfWork(_lastBlock.Proof, _lastBlock.PreviousHash); + + CreateTransaction(sender: "0", recipient: NodeId, amount: 1); + Block block = CreateNewBlock(proof /*, _lastBlock.PreviousHash*/); + + var response = new + { + Message = "New Block Forged", + Index = block.Index, + Transactions = block.Transactions.ToArray(), + Proof = block.Proof, + PreviousHash = block.PreviousHash + }; + + return JsonConvert.SerializeObject(response); + } + + internal string GetFullChain() + { + var response = new + { + chain = _chain.ToArray(), + length = _chain.Count + }; + + return JsonConvert.SerializeObject(response); + } + + internal string RegisterNodes(string[] nodes) + { + var builder = new StringBuilder(); + foreach (string node in nodes) + { + string url = $"https://{node}"; + RegisterNode(url); + builder.Append($"{url}, "); + } + + builder.Insert(0, $"{nodes.Count()} new nodes have been added: "); + string result = builder.ToString(); + return result.Substring(0, result.Length - 2); + } + + internal string Consensus() + { + bool replaced = ResolveConflicts(); + string message = replaced ? "was replaced" : "is authoritive"; + + var response = new + { + Message = $"Our chain {message}", + Chain = _chain + }; + + return JsonConvert.SerializeObject(response); + } + + internal int CreateTransaction(string sender, string recipient, int amount) + { + var transaction = new Transaction + { + Sender = sender, + Recipient = recipient, + Amount = amount + }; + + _currentTransactions.Add(transaction); + + return _lastBlock != null ? _lastBlock.Index + 1 : 0; + } + } +} diff --git a/csharp/BlockChain/BlockChain.csproj b/csharp/BlockChain/BlockChain.csproj new file mode 100644 index 0000000..a4a176a --- /dev/null +++ b/csharp/BlockChain/BlockChain.csproj @@ -0,0 +1,72 @@ + + + + + Debug + AnyCPU + {E06FC4CE-77D0-4A64-94A6-32A08920E481} + Library + Properties + BlockChainDemo + BlockChainDemo + v4.5.1 + 512 + SAK + SAK + SAK + SAK + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + ..\packages\TinyWebServer.dll.1.0.1\lib\net40\TinyWebServer.dll + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/csharp/BlockChain/Node.cs b/csharp/BlockChain/Node.cs new file mode 100644 index 0000000..5ba425f --- /dev/null +++ b/csharp/BlockChain/Node.cs @@ -0,0 +1,9 @@ +using System; + +namespace BlockChainDemo +{ + public class Node + { + public Uri Address { get; set; } + } +} \ No newline at end of file diff --git a/csharp/BlockChain/Properties/AssemblyInfo.cs b/csharp/BlockChain/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..87b9d4e --- /dev/null +++ b/csharp/BlockChain/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BlockChain")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BlockChain")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e06fc4ce-77d0-4a64-94a6-32a08920e481")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/csharp/BlockChain/Transaction.cs b/csharp/BlockChain/Transaction.cs new file mode 100644 index 0000000..1cbaeb7 --- /dev/null +++ b/csharp/BlockChain/Transaction.cs @@ -0,0 +1,9 @@ +namespace BlockChainDemo +{ + public class Transaction + { + public int Amount { get; set; } + public string Recipient { get; set; } + public string Sender { get; set; } + } +} \ No newline at end of file diff --git a/csharp/BlockChain/WebServer.cs b/csharp/BlockChain/WebServer.cs new file mode 100644 index 0000000..c9930c1 --- /dev/null +++ b/csharp/BlockChain/WebServer.cs @@ -0,0 +1,72 @@ +using Newtonsoft.Json; +using System.IO; +using System.Net; +using System.Net.Http; + +namespace BlockChainDemo +{ + public class WebServer + { + public WebServer(BlockChain chain) + { + var server = new TinyWebServer.WebServer(request => + { + string path = request.Url.PathAndQuery.ToLower(); + string query = ""; + string json = ""; + if (path.Contains("?")) + { + string[] parts = path.Split('?'); + path = parts[0]; + query = parts[1]; + } + + switch (path) + { + //GET: http://localhost:12345/mine + case "/mine": + return chain.Mine(); + + //POST: http://localhost:12345/transactions/new + //{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" } + case "/transactions/new": + if (request.HttpMethod != HttpMethod.Post.Method) + return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"; + + json = new StreamReader(request.InputStream).ReadToEnd(); + Transaction trx = JsonConvert.DeserializeObject(json); + int blockId = chain.CreateTransaction(trx.Sender, trx.Recipient, trx.Amount); + return $"Your transaction will be included in block {blockId}"; + + //GET: http://localhost:12345/chain + case "/chain": + return chain.GetFullChain(); + + //POST: http://localhost:12345/nodes/register + case "/nodes/register": + if (request.HttpMethod != HttpMethod.Post.Method) + return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"; + + json = new StreamReader(request.InputStream).ReadToEnd(); + var urlList = new { Urls = new string[0] }; + var obj = JsonConvert.DeserializeAnonymousType(json, urlList); + return chain.RegisterNodes(obj.Urls); + + //GET: http://localhost:12345/nodes/register + case "/nodes/resolve": + return chain.Consensus(); + } + + return ""; + }, + "http://localhost:12345/mine/", + "http://localhost:12345/transactions/new/", + "http://localhost:12345/chain/", + "http://localhost:12345/nodes/register/", + "http://localhost:12345/nodes/resolve/" + ); + + server.Run(); + } + } +} diff --git a/csharp/BlockChain/packages.config b/csharp/BlockChain/packages.config new file mode 100644 index 0000000..4c8c064 --- /dev/null +++ b/csharp/BlockChain/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From 839fd24b14874b69cccd28304233da24a3bbf25c Mon Sep 17 00:00:00 2001 From: davetoland Date: Sat, 18 Nov 2017 20:50:44 +0000 Subject: [PATCH 14/15] Added missing comment --- csharp/BlockChain/WebServer.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp/BlockChain/WebServer.cs b/csharp/BlockChain/WebServer.cs index c9930c1..2e49dc7 100644 --- a/csharp/BlockChain/WebServer.cs +++ b/csharp/BlockChain/WebServer.cs @@ -43,6 +43,7 @@ namespace BlockChainDemo return chain.GetFullChain(); //POST: http://localhost:12345/nodes/register + //{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] } case "/nodes/register": if (request.HttpMethod != HttpMethod.Post.Method) return $"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}"; From db16e16ab0c82defb5f2209396362e936d593e7d Mon Sep 17 00:00:00 2001 From: davetoland Date: Sun, 19 Nov 2017 01:36:34 +0000 Subject: [PATCH 15/15] Bug fixes, plus config driven host and port --- csharp/BlockChain.Console/App.config | 4 ++++ csharp/BlockChain/BlockChain.cs | 22 ++++++++++++++-------- csharp/BlockChain/BlockChain.csproj | 1 + csharp/BlockChain/WebServer.cs | 17 +++++++++++------ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/csharp/BlockChain.Console/App.config b/csharp/BlockChain.Console/App.config index d0feca6..089f249 100644 --- a/csharp/BlockChain.Console/App.config +++ b/csharp/BlockChain.Console/App.config @@ -3,4 +3,8 @@ + + + + diff --git a/csharp/BlockChain/BlockChain.cs b/csharp/BlockChain/BlockChain.cs index 52a8f71..250d6f8 100644 --- a/csharp/BlockChain/BlockChain.cs +++ b/csharp/BlockChain/BlockChain.cs @@ -66,18 +66,24 @@ namespace BlockChainDemo foreach (Node node in _nodes) { - var request = (HttpWebRequest)WebRequest.Create(node.Address); + var url = new Uri(node.Address, "/chain"); + var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { - string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); - List chain = JsonConvert.DeserializeObject>(json); - - if (chain.Count > _chain.Count && IsValidChain(chain)) + var model = new { - maxLength = chain.Count; - newChain = chain; + chain = new List(), + length = 0 + }; + string json = new StreamReader(response.GetResponseStream()).ReadToEnd(); + var data = JsonConvert.DeserializeAnonymousType(json, model); + + if (data.chain.Count > _chain.Count && IsValidChain(data.chain)) + { + maxLength = data.chain.Count; + newChain = data.chain; } } } @@ -179,7 +185,7 @@ namespace BlockChainDemo var builder = new StringBuilder(); foreach (string node in nodes) { - string url = $"https://{node}"; + string url = $"http://{node}"; RegisterNode(url); builder.Append($"{url}, "); } diff --git a/csharp/BlockChain/BlockChain.csproj b/csharp/BlockChain/BlockChain.csproj index a4a176a..86681ac 100644 --- a/csharp/BlockChain/BlockChain.csproj +++ b/csharp/BlockChain/BlockChain.csproj @@ -39,6 +39,7 @@ ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + diff --git a/csharp/BlockChain/WebServer.cs b/csharp/BlockChain/WebServer.cs index 2e49dc7..cd7b86b 100644 --- a/csharp/BlockChain/WebServer.cs +++ b/csharp/BlockChain/WebServer.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using System.Configuration; using System.IO; using System.Net; using System.Net.Http; @@ -9,6 +10,10 @@ namespace BlockChainDemo { public WebServer(BlockChain chain) { + var settings = ConfigurationManager.AppSettings; + string host = settings["host"]?.Length > 1 ? settings["host"] : "localhost"; + string port = settings["port"]?.Length > 1 ? settings["port"] : "12345"; + var server = new TinyWebServer.WebServer(request => { string path = request.Url.PathAndQuery.ToLower(); @@ -53,18 +58,18 @@ namespace BlockChainDemo var obj = JsonConvert.DeserializeAnonymousType(json, urlList); return chain.RegisterNodes(obj.Urls); - //GET: http://localhost:12345/nodes/register + //GET: http://localhost:12345/nodes/resolve case "/nodes/resolve": return chain.Consensus(); } return ""; }, - "http://localhost:12345/mine/", - "http://localhost:12345/transactions/new/", - "http://localhost:12345/chain/", - "http://localhost:12345/nodes/register/", - "http://localhost:12345/nodes/resolve/" + $"http://{host}:{port}/mine/", + $"http://{host}:{port}/transactions/new/", + $"http://{host}:{port}/chain/", + $"http://{host}:{port}/nodes/register/", + $"http://{host}:{port}/nodes/resolve/" ); server.Run();