-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.py
More file actions
39 lines (25 loc) · 1.12 KB
/
Block.py
File metadata and controls
39 lines (25 loc) · 1.12 KB
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
37
38
39
import json
from hashlib import sha256
class Block:
# OBJECTIVE: To create components needed to make a block
def __init__(self, index, data, timestamp, previous_block_hash, nonce=0):
# Block's position and plain data
self.index = index
self.data = data
# Block'stime of creation
self.timestamp = timestamp
# Save hashes
self.previous_block_hash = previous_block_hash # <= Copied from previous block
self.current_block_hash = None # <= Yet to be computed
# Create pointers for network
self.previous_block = None
self.next_block = None
self.nonce = nonce
def compute_hash(self):
# OBJECTIVE: Compute hash value of block's contents
# NOTE: Function will return the same hash value, if given the same input. It doesn't matter how many times!
# Create a dictionary in JSON format and sort keys
json_string = json.dumps(self.__dict__, sort_keys=True)
# print(json_string)
# Return hash value of block's content
return sha256(json_string.encode()).hexdigest()