Skip to content

Commit 1cb2ecd

Browse files
committed
tx_ordering works for a testcase
1 parent 28dd988 commit 1cb2ecd

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

ordering.py

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from statistics import mode
1+
import statistics
22

33

44
class Tx:
@@ -15,20 +15,28 @@ def __repr__(self):
1515

1616

1717
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)
2324

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)
2829

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)
3240

3341
def tx_ordering(self, tx_id_ordered_by_timestamp, node_list):
3442
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):
3745
tx_list = [first_tx]
3846
while node_list:
3947
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)
4149
for connection in connections:
4250
tx, _ = connection
4351
if tx not in tx_list:
4452
tx_list.append(tx)
45-
node_list.pop(node_list.index(first_tx))
53+
node_list.remove(tx)
4654
tx_to_connect = tx
4755
added = True
4856
break
4957
if not added:
5058
tx_list.append(node_list[0])
5159
tx = node_list.pop(0)
5260
tx_to_connect = tx
53-
54-
return first_tx.content
61+
return tx_list
5562

5663
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)
5966

6067
def get_upcoming_connection_to(self, from_tx, ordered_tx_lists):
6168
# 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):
7279
tx_to_count_dict[next_elem] = 1
7380
else:
7481
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)
7683

7784

7885
class AequitasOrdering:

0 commit comments

Comments
 (0)