-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.py
More file actions
72 lines (63 loc) · 2.02 KB
/
block.py
File metadata and controls
72 lines (63 loc) · 2.02 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import base64
from time import time
import hashlib
import json
import logging
import pickle
from typing import List
from transaction import Transaction
logging.basicConfig(level=logging.INFO)
class Block:
def __init__(self, index=None, previous_hash=None, proof=None, timestamp=time(), transactions=None):
"""
Creates a new block to which transactions can be added.
:param index:
:param previous_hash:
:param proof:
:param timestamp:
:param transactions:
"""
logging.info("Creating a new block...")
self.index = index
self.previous_hash = previous_hash
self.proof = proof
self.timestamp = timestamp
self.transactions: List[Transaction] = transactions if transactions else []
self.next: Block = None
@property
def proof_bytes(self) -> bytes:
"""
:return: bytes of block data that are used for calculating the proof of work
"""
return json.dumps({
"index": self.index,
"timestamp": self.timestamp,
"transactions": base64.b64encode(pickle.dumps(self.transactions)).decode()
}).encode()
@property
def hash_bytes(self) -> bytes:
"""
:return: bytes of data that are used to compute the hash of a block
"""
return json.dumps({
"previous_hash": self.previous_hash,
"index": self.index,
"timestamp": self.timestamp,
"transactions": base64.b64encode(pickle.dumps(self.transactions)).decode(),
"proof": self.proof
}).encode()
@property
def hash(self) -> str:
"""
:return: sha256 value of the block
"""
return hashlib.sha256(self.hash_bytes).hexdigest()
if __name__ == '__main__':
logging.info("Exporting the genesis block...")
genesis_block = Block(
index=0,
previous_hash=0,
proof=0
)
with open("genesis_block", "wb") as f:
f.write(pickle.dumps(genesis_block))