-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.py
39 lines (34 loc) · 1.15 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
import time
import copy
class Block:
def __init__(self, transactions, lastHash, forger, blockCount):
self.transactions = transactions
self.lastHash = lastHash
self.forger = forger
self.blockCount = blockCount
self.timestamp = time.time()
self.signature = ''
# the first block
@staticmethod
def genesis():
genesisBlock = Block([], 'genesisHash', 'genesis', 0)
genesisBlock.timestamp = 0
return genesisBlock
def toJson(self):
data = {}
data['lastHash'] = self.lastHash
data['forger'] = self.forger
data['blockCount'] = self.blockCount
data['timestamp'] = self.timestamp
data['signature'] = self.signature
jsonTransactions = []
for transaction in self.transactions:
jsonTransactions.append(transaction.toJson())
data['transactions'] = jsonTransactions
return data
def payload(self):
jsonRepresentation = copy.deepcopy(self.toJson())
jsonRepresentation['signature'] = ''
return jsonRepresentation
def sign(self, signature):
self.signature = signature