Skip to content

Commit f03d28f

Browse files
authored
Merge pull request dvf#9 from massa142/master
Add type hints
2 parents 961e2aa + a26be06 commit f03d28f

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

blockchain.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import hashlib
22
import json
33
from time import time
4+
from typing import Any, Dict, List, Optional
45
from urllib.parse import urlparse
56
from uuid import uuid4
67

78
import requests
89
from flask import Flask, jsonify, request
910

1011

11-
class Blockchain(object):
12+
class Blockchain:
1213
def __init__(self):
1314
self.current_transactions = []
1415
self.chain = []
1516
self.nodes = set()
1617

1718
# Create the genesis block
18-
self.new_block(previous_hash=1, proof=100)
19+
self.new_block(previous_hash='1', proof=100)
1920

20-
def register_node(self, address):
21+
def register_node(self, address: str) -> None:
2122
"""
2223
Add a new node to the list of nodes
2324
24-
:param address: <str> Address of node. Eg. 'http://192.168.0.5:5000'
25-
:return: None
25+
:param address: Address of node. Eg. 'http://192.168.0.5:5000'
2626
"""
2727

2828
parsed_url = urlparse(address)
2929
self.nodes.add(parsed_url.netloc)
3030

31-
def valid_chain(self, chain):
31+
def valid_chain(self, chain: List[Dict[str, Any]]) -> bool:
3232
"""
3333
Determine if a given blockchain is valid
3434
35-
:param chain: <list> A blockchain
36-
:return: <bool> True if valid, False if not
35+
:param chain: A blockchain
36+
:return: True if valid, False if not
3737
"""
3838

3939
last_block = chain[0]
@@ -57,12 +57,12 @@ def valid_chain(self, chain):
5757

5858
return True
5959

60-
def resolve_conflicts(self):
60+
def resolve_conflicts(self) -> bool:
6161
"""
6262
This is our consensus algorithm, it resolves conflicts
6363
by replacing our chain with the longest one in the network.
6464
65-
:return: <bool> True if our chain was replaced, False if not
65+
:return: True if our chain was replaced, False if not
6666
"""
6767

6868
neighbours = self.nodes
@@ -91,13 +91,13 @@ def resolve_conflicts(self):
9191

9292
return False
9393

94-
def new_block(self, proof, previous_hash=None):
94+
def new_block(self, proof: int, previous_hash: Optional[str]) -> Dict[str, Any]:
9595
"""
9696
Create a new Block in the Blockchain
9797
98-
:param proof: <int> The proof given by the Proof of Work algorithm
99-
:param previous_hash: (Optional) <str> Hash of previous Block
100-
:return: <dict> New Block
98+
:param proof: The proof given by the Proof of Work algorithm
99+
:param previous_hash: Hash of previous Block
100+
:return: New Block
101101
"""
102102

103103
block = {
@@ -114,14 +114,14 @@ def new_block(self, proof, previous_hash=None):
114114
self.chain.append(block)
115115
return block
116116

117-
def new_transaction(self, sender, recipient, amount):
117+
def new_transaction(self, sender: str, recipient: str, amount: int) -> int:
118118
"""
119119
Creates a new transaction to go into the next mined Block
120120
121-
:param sender: <str> Address of the Sender
122-
:param recipient: <str> Address of the Recipient
123-
:param amount: <int> Amount
124-
:return: <int> The index of the Block that will hold this transaction
121+
:param sender: Address of the Sender
122+
:param recipient: Address of the Recipient
123+
:param amount: Amount
124+
:return: The index of the Block that will hold this transaction
125125
"""
126126
self.current_transactions.append({
127127
'sender': sender,
@@ -132,30 +132,26 @@ def new_transaction(self, sender, recipient, amount):
132132
return self.last_block['index'] + 1
133133

134134
@property
135-
def last_block(self):
135+
def last_block(self) -> Dict[str: Any]:
136136
return self.chain[-1]
137137

138138
@staticmethod
139-
def hash(block):
139+
def hash(block: Dict[str, Any]) -> str:
140140
"""
141141
Creates a SHA-256 hash of a Block
142142
143-
:param block: <dict> Block
144-
:return: <str>
143+
:param block: Block
145144
"""
146145

147146
# We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes
148147
block_string = json.dumps(block, sort_keys=True).encode()
149148
return hashlib.sha256(block_string).hexdigest()
150149

151-
def proof_of_work(self, last_proof):
150+
def proof_of_work(self, last_proof: int) -> int:
152151
"""
153152
Simple Proof of Work Algorithm:
154153
- Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p'
155154
- p is the previous proof, and p' is the new proof
156-
157-
:param last_proof: <int>
158-
:return: <int>
159155
"""
160156

161157
proof = 0
@@ -165,13 +161,13 @@ def proof_of_work(self, last_proof):
165161
return proof
166162

167163
@staticmethod
168-
def valid_proof(last_proof, proof):
164+
def valid_proof(last_proof: int, proof: int) -> bool:
169165
"""
170166
Validates the Proof
171167
172-
:param last_proof: <int> Previous Proof
173-
:param proof: <int> Current Proof
174-
:return: <bool> True if correct, False if not.
168+
:param last_proof: Previous Proof
169+
:param proof: Current Proof
170+
:return: True if correct, False if not.
175171
"""
176172

177173
guess = f'{last_proof}{proof}'.encode()

0 commit comments

Comments
 (0)