-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.py
70 lines (48 loc) · 1.96 KB
/
node.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
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
from block import Block
class Node(object):
def __init__(self, node_id, initial_coins, is_fast):
self.id = node_id
self.coins = initial_coins
self.is_fast = is_fast
self.receivedStamps = []
# List of this node's neighbours
self.peers = []
# Dict of all blocks this node has seen
# (can be thought of as a tree using prev_block attribute of a block)
self.blocks = {
# Each node begins with a genesis block
0: Block(block_id=0, created_at=0.0, creator_id=self.id,
prev_block_id=-1, chain_len=0)
}
# Dict of transactions this node has seen
self.transactions = {}
def __repr__(self):
r = (self.id, self.coins, ("fast" if self.is_fast else "slow"))
return "<Node %d:, coins=%d, %s>" % r
def longest_chain(self):
"""
Return the blocks of the longest chain.
"""
chain = []
# Alternative way? https://stackoverflow.com/a/4233482/2043048
# by_time = sorted(me.blocks.values(), key=lambda b: b.created_at)
# longest_blk = sorted(by_time, key=lambda b: b.chain_len)
# Start with the genesis block
longest_bk = self.blocks[0]
# Find the block ending with the longest chain
for bk in self.blocks.values():
# Use block creation time to break ties in case of equal length
if ((len(bk) > len(longest_bk)) or ((len(bk) == len(longest_bk)) and
(bk.created_at < longest_bk.created_at))):
longest_bk = bk
# Now find all the blocks in this chain
bk = longest_bk
# Do-While Loop!
while True:
chain.append(bk)
# Chain ends at Genesis block which have id 0
if bk.id == 0:
break
# Move backwards
bk = self.blocks[bk.prev_block_id]
return chain