From 3fa90ff8ba3fc913fe80421f637d5762e3dbe12c Mon Sep 17 00:00:00 2001 From: Daniel van Flymen Date: Sun, 12 Nov 2017 15:32:10 -0500 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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