Skip to content

Commit aee7872

Browse files
committed
Complete section: Start the Blockchain Application
1 parent 474aaa7 commit aee7872

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

block.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import time
2+
3+
from crypto_hash import crypto_hash
4+
5+
class Block:
6+
"""
7+
Block: a unit of storage.
8+
Store transactions in a blockchain that supports a cryptocurrency.
9+
"""
10+
def __init__(self, timestamp, last_hash, hash, data):
11+
self.timestamp = timestamp
12+
self.last_hash = last_hash
13+
self.hash = hash
14+
self.data = data
15+
16+
def add_block(self, data):
17+
self.chain.append(Block(data))
18+
19+
def __repr__(self):
20+
return (
21+
'Block('
22+
f'timestamp: {self.timestamp}, '
23+
f'last_hash: {self.last_hash}, '
24+
f'hash: {self.hash}, '
25+
f'data: {self.data})'
26+
)
27+
28+
@staticmethod
29+
def mine_block(last_block, data):
30+
"""
31+
Mine a block based on the given last_block and data.
32+
"""
33+
timestamp = time.time_ns()
34+
last_hash = last_block.hash
35+
hash = crypto_hash(timestamp, last_hash, data)
36+
37+
return Block(timestamp, last_hash, hash, data)
38+
39+
@staticmethod
40+
def genesis():
41+
"""
42+
Generate the genesis block.
43+
"""
44+
return Block(1, 'genesis_last_hash', 'genesis_hash', [])
45+
46+
47+
def main():
48+
genesis_block = Block.genesis()
49+
block = Block.mine_block(genesis_block, 'foo')
50+
print(f'block: {block}')
51+
52+
if __name__ == '__main__':
53+
main()

blockchain.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from block import Block
2+
3+
class Blockchain:
4+
"""
5+
Blockchain: a public ledger of transactions.
6+
Implemented as a list of blocks - data sets of transactions
7+
"""
8+
def __init__(self):
9+
self.chain = [Block.genesis()]
10+
11+
def add_block(self, data):
12+
self.chain.append(Block.mine_block(self.chain[-1], data))
13+
14+
def __repr__(self):
15+
return f'Blockchain: {self.chain}'
16+
17+
def main():
18+
blockchain = Blockchain()
19+
blockchain.add_block('one')
20+
blockchain.add_block('two')
21+
22+
print(blockchain)
23+
print(f'blockchain.py __name__: {__name__}')
24+
25+
if __name__ == '__main__':
26+
main()

crypto_hash.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import hashlib
2+
import json
3+
4+
def crypto_hash(*args):
5+
"""
6+
Return a sha-256 hash of the given arguments.
7+
"""
8+
stringified_args = sorted(map(lambda data: json.dumps(data), args))
9+
joined_data = ''.join(stringified_args)
10+
11+
return hashlib.sha256(joined_data.encode('utf-8')).hexdigest()
12+
13+
def main():
14+
print(f"crypto_hash('one', 2, [3]): {crypto_hash('one', 2, [3])}")
15+
print(f"crypto_hash(2, 'one', [3]): {crypto_hash(2, 'one', [3])}")
16+
17+
if __name__ == '__main__':
18+
main()

0 commit comments

Comments
 (0)