1
- from statistics import mode
1
+ import statistics
2
2
3
3
4
4
class Tx :
@@ -15,20 +15,28 @@ def __repr__(self):
15
15
16
16
17
17
class CausalOrdering :
18
- def order (self , nodes_vs_transaction_times ):
19
- assert (len (nodes_vs_transaction_times ) != 0 )
20
- tx_sorted_by_timestamp = self .sort_tx_by_timestamp (nodes_vs_transaction_times )
21
- tx_list = self .get_tx_list (nodes_vs_transaction_times )
22
- return self .tx_ordering (tx_sorted_by_timestamp , tx_list )
18
+ def order (self , nodes_vs_tx_received ):
19
+ assert (len (nodes_vs_tx_received ) != 0 )
20
+ self .sort_tx_by_timestamp (nodes_vs_tx_received )
21
+ self .extract_content (nodes_vs_tx_received )
22
+ tx_list = self .get_unique_tx_list (nodes_vs_tx_received )
23
+ return self .tx_ordering (nodes_vs_tx_received , tx_list )
23
24
24
- def sort_tx_by_timestamp (self , nodes_vs_tx_times ):
25
- for node in nodes_vs_tx_times :
26
- assert (len (nodes_vs_tx_times [node ]) != 0 )
27
- nodes_vs_tx_times [node ].sort (key = lambda tx : tx .timestamp )
25
+ def sort_tx_by_timestamp (self , nodes_vs_tx_received ):
26
+ for node in nodes_vs_tx_received :
27
+ assert (len (nodes_vs_tx_received [node ]) != 0 )
28
+ nodes_vs_tx_received [node ].sort (key = lambda tx : tx .timestamp )
28
29
29
- def get_tx_list (self , nodes_vs_tx_times ):
30
- for node in nodes_vs_tx_times :
31
- return [tx .content for tx in nodes_vs_tx_times [node ]]
30
+ def extract_content (self , nodes_vs_tx_received ):
31
+ for node in nodes_vs_tx_received :
32
+ nodes_vs_tx_received [node ] = [tx .content for tx in nodes_vs_tx_received [node ]] # rename nodes_vs_tx_times
33
+
34
+ def get_unique_tx_list (self , nodes_vs_tx_received ):
35
+ nodes_set = set ()
36
+ for node in nodes_vs_tx_received :
37
+ for tx in nodes_vs_tx_received [node ]:
38
+ nodes_set .add (tx )
39
+ return list (nodes_set )
32
40
33
41
def tx_ordering (self , tx_id_ordered_by_timestamp , node_list ):
34
42
first_tx = self .first_tx (tx_id_ordered_by_timestamp )
@@ -37,25 +45,24 @@ def tx_ordering(self, tx_id_ordered_by_timestamp, node_list):
37
45
tx_list = [first_tx ]
38
46
while node_list :
39
47
added = False
40
- connections = self .get_most_popular_connection_to (tx_to_connect , tx_id_ordered_by_timestamp )
48
+ connections = self .get_upcoming_connection_to (tx_to_connect , tx_id_ordered_by_timestamp )
41
49
for connection in connections :
42
50
tx , _ = connection
43
51
if tx not in tx_list :
44
52
tx_list .append (tx )
45
- node_list .pop ( node_list . index ( first_tx ) )
53
+ node_list .remove ( tx )
46
54
tx_to_connect = tx
47
55
added = True
48
56
break
49
57
if not added :
50
58
tx_list .append (node_list [0 ])
51
59
tx = node_list .pop (0 )
52
60
tx_to_connect = tx
53
-
54
- return first_tx .content
61
+ return tx_list
55
62
56
63
def first_tx (self , tx_id_ordered_by_timestamp ):
57
- first_tx_list = [tx_id_ordered_by_timestamp [tx ][0 ]. content for tx in tx_id_ordered_by_timestamp ]
58
- return mode (first_tx_list )
64
+ first_tx_list = [tx_id_ordered_by_timestamp [tx ][0 ] for tx in tx_id_ordered_by_timestamp ]
65
+ return statistics . mode (first_tx_list )
59
66
60
67
def get_upcoming_connection_to (self , from_tx , ordered_tx_lists ):
61
68
# what if all connections are to one at end and the one at the end doesn't have any other connections?
@@ -72,7 +79,7 @@ def get_upcoming_connection_to(self, from_tx, ordered_tx_lists):
72
79
tx_to_count_dict [next_elem ] = 1
73
80
else :
74
81
tx_to_count_dict [next_elem ] += 1
75
- return tx_count .items (). sort ( key = lambda i , j : j , reverse = True )
82
+ return sorted ( list ( tx_to_count_dict .items ()), key = lambda i : i [ 1 ] , reverse = True )
76
83
77
84
78
85
class AequitasOrdering :
0 commit comments