-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathid_bag.py
More file actions
61 lines (46 loc) · 1.66 KB
/
id_bag.py
File metadata and controls
61 lines (46 loc) · 1.66 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
class IdBag:
"""Manages which transaction id should be issued next. Holds a queue of tx ids that need to be reissued.
"""
def __init__(self, simulation):
"""
Arguments:
simulation {Simulation} -- Simulation that the bag will be used in (referenced for next_id when bag is empty).
"""
self.simulation = simulation
self.bag = [] # Id queue.
def getNextId(self):
"""
Returns:
int -- Next tx id that should be used. From the queue of reissued ids, or a new one if the queue is empty.
"""
if self.bag:
tx_id, miner = self.bag.pop(0)
miner.removeSheep(tx_id)
return tx_id
else:
return_id = self.simulation.next_id
self.simulation.next_id += 1
return return_id
def addId(self, tx_id, miner):
"""Adds an id to the bag's queue to be reissued.
Arguments:
tx_id {int} -- Id to be reiussed.
miner {Miner} -- Miner who is shepherding that id.
"""
self.bag.append((tx_id, miner)) # Stores id and pointer to miner.
def clear(self):
"""Emtpy the bag of ids.
"""
self.bag = []
singletons = {}
def getSingleBag(simulation):
"""
Arguments:
simulation {Simulation} -- Simulation that the bag will be used in.
Returns:
IdBag -- A singleton IdBag object.
"""
global singletons
if simulation.thread_id not in singletons:
singletons[simulation.thread_id] = IdBag(simulation)
return singletons[simulation.thread_id]