-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.py
44 lines (37 loc) · 1.35 KB
/
Block.py
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
import time
import copy
class Block():
def __init__(self, transactions, lastHash, forger, blockCount):
self.blockCount = blockCount
self.transactions = transactions
self.lastHash = lastHash
self.timestamp = time.time()
self.forger = forger
self.signature = ''
# create the first block (genesis)
@staticmethod
def genesis():
genesisBlock = Block([], 'genesisHash', 'genesis', 0)
genesisBlock.timestamp = 0
return genesisBlock
# convert into json object
def toJson(self):
data = {}
data['blockCount'] = self.blockCount
data['lastHash'] = self.lastHash
data['signature'] = self.signature
data['forger'] = self.forger
data['timestamp'] = self.timestamp
jsonTransactions = []
for transaction in self.transactions:
jsonTransactions.append(transaction.toJson())
data['transactions'] = jsonTransactions
return data
# remove the signature information to create a payload for signature verification
def payload(self):
jsonRepresentation = copy.deepcopy(self.toJson())
jsonRepresentation['signature'] = ''
return jsonRepresentation
# assigns a signature to the block after it has been verified
def sign(self, signature):
self.signature = signature