-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.py
More file actions
36 lines (25 loc) · 881 Bytes
/
blockchain.py
File metadata and controls
36 lines (25 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import hashlib
import time
import json
"""
DESCRIPTION:
This is a prototype of a blockchain based voting system.
"""
def create_hash(index, previous_hash, timestamp, vote_for):
return hashlib.sha256(f"{index}{previous_hash}{timestamp}{vote_for}".encode()).hexdigest()
def create_genesis_block():
return {
"index": 0,
"previous_hash": "0",
"timestamp": time.time(),
"vote_for": "Genesis Vote",
"hash": create_hash(0, "0", time.time(), "Genesis Vote")
}
def create_new_block(previous_block, vote_for):
return {
"index": previous_block['index'] + 1,
"previous_hash": previous_block['hash'],
"timestamp": time.time(),
"vote_for": vote_for,
"hash": create_hash(previous_block['index'] + 1, previous_block['hash'], time.time(), vote_for)
}