-
Notifications
You must be signed in to change notification settings - Fork 10
/
execute.py
194 lines (159 loc) · 6.58 KB
/
execute.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from transactions import *
from sequence import *
from util import *
import numpy as np
import matplotlib.pyplot as plt
from ordering import *
from aequitas import *
import copy
tx_mapping = {}
def get_percent_difference(current, previous):
if current == previous:
return 0.0
try:
diff = (abs(current - previous) / previous) * 100.0
if diff > 100.0:
return 100.0
return diff
except ZeroDivisionError:
return 100
def get_sequence_difference(txs, tag1, tag2):
differences = []
for tx in txs:
if len(tx.metrics) > 0 and tag1 in tx.metrics and tag2 in tx.metrics:
print(tx.metrics)
diff = get_percent_difference(tx.metrics[tag1], tx.metrics[tag2])
if diff != -1:
differences.append(diff)
return differences
def LimitedRandDoubles(lim):
return np.random.uniform(low=0,high=lim,size=(1, 1000000))
rand_timing_doubles = LimitedRandDoubles(30)
rand_network_doubles = LimitedRandDoubles(10)
last_timing_double = 0
last_network_double = 0
def get_timestep():
global last_timing_double, rand_timing_doubles
last_timing_double += 1
return rand_timing_doubles[0][last_timing_double]
def get_network_delay():
global last_network_double
last_network_double += 1
return rand_network_doubles[0][last_network_double]
def same_order(txs):
return txs
def process_example_uniswap_transactions(data_file, order_function):
# Very messy parser of transactions in plaintext into objects
transactions = []
num_tx = 200
curr_num = 0
nodes_seen = {}
for transaction in open(data_file).read().splitlines()[0:]:
transaction = transaction.split()
tx = None
if curr_num >= num_tx:
break
if '//' in transaction:
# comment
next_tx_id = transaction[2]
next_tx_block = transaction[4]
continue
elif 'swaps' in transaction:
tokens = sorted([[int(transaction[7]), int(transaction[6])], [int(transaction[10]), int(transaction[9])]])
fee = 0
if len(transaction) == 17:
fee = int(transaction[15])
tx = SwapTransaction(tokens[0][0], tokens[1][0], tokens[0][1], tokens[1][1], int(transaction[0]), fee, next_tx_id, next_tx_block)
elif 'liquidity;' in transaction:
print("first_token, second_token, first_amount, second_amount, sender, fee")
print(transaction)
for i in range(len(transaction)):
print(i, transaction[i])
tokens = sorted([[int(transaction[3]), int(transaction[2])], [int(transaction[6]), int(transaction[5])]])
fee = 0
#if len(transaction) == 17:
# fee = int(transaction[15])
if 'adds' in transaction:
tx = AddLiquidityTransaction(tokens[0][0], tokens[1][0], tokens[0][1], tokens[1][1], int(transaction[0]), fee, next_tx_id, next_tx_block)
elif 'removes' in transaction:
tx = RemoveLiquidityTransaction(tokens[0][0], tokens[1][0], tokens[0][1], tokens[1][1], int(transaction[0]), fee, next_tx_id, next_tx_block)
if tx is not None:
transactions.append(tx)
curr_num += 1
assert(len(transactions) >= num_tx)
# transactions = transactions[:100]
# simulate timing data
curr_time = 0.0
for tx in transactions:
tx.time_sent = curr_time
curr_time += get_timestep()
# simulate network data
for node in range(0, 5):
if not node in nodes_seen:
nodes_seen[node] = []
nodes_seen[node].append((tx, tx.time_sent + get_network_delay()))
for node in range(0, 5):
nodes_seen[node] = sorted(nodes_seen[node], key = lambda x : x[1])
print(nodes_seen[0])
transactions = order_function(transactions)
print("Transactions", transactions)
baseline_sequence = TransactionSequence(transactions)
baseline_sequence = baseline_sequence.get_output_with_tagged_metrics("baseline")
for node in nodes_seen:
node_order_sequence = TransactionSequence([x[0] for x in nodes_seen[node]])
node_order = node_order_sequence.get_output_with_tagged_metrics(node)
differences = {}
for node in nodes_seen:
differences[node] = get_sequence_difference(transactions, "baseline", node)
plt.hist(differences.values(), alpha=0.5, bins=20)
plt.yscale('log')
plt.show()
max_differences = {}
curr_max_diff = 0
for i in range(1):
# Leader maliciously shuffling
for node in nodes_seen:
seq = [x[0] for x in nodes_seen[node]]
random.shuffle(seq)
node_order_sequence = TransactionSequence(seq)
node_order = node_order_sequence.get_output_with_tagged_metrics(node)
differences = {}
for node in nodes_seen:
differences[node] = get_sequence_difference(transactions, "baseline", node)
max_diff = sum([sum(val > 95 for val in differences[node]) for node in nodes_seen])
if max_diff >= curr_max_diff:
max_differences = differences
curr_max_diff = max_diff
plt.hist(max_differences.values(), alpha=0.5, bins=20)
plt.yscale('log')
plt.show()
print(nodes_seen[0])
# set up input for causal order (same as aequitas)
for node in nodes_seen:
nodes_seen[node] = [Tx(x[0], x[1]) for x in nodes_seen[node]]
# TODO: Need to fix this part given new tx format
# causal_order = CausalOrdering()
# causal_order = causal_order.order(copy.deepcopy(nodes_seen))
# #print(causal_order)
# output = TransactionSequence(causal_order).get_output_with_tagged_metrics('causal')
# difference_causal = get_sequence_difference(transactions, "baseline", "causal")
# plt.hist(difference_causal, alpha=0.5, bins=20)
# plt.yscale('log')
# plt.show()
for tx in transactions:
tx_mapping[str(tx)] = tx
print(nodes_seen[0])
aequitas_order = aequitas(copy.deepcopy(nodes_seen), 1, 1)
final_order = []
for output_set in aequitas_order:
y = [tx_mapping[x] for x in output_set]
final_order += y
print(final_order)
output = TransactionSequence(final_order).get_output_with_tagged_metrics('aequitas')
difference_aequitas = get_sequence_difference(transactions, "baseline", "aequitas")
plt.hist(difference_aequitas, alpha=0.5, bins=20)
plt.yscale('log')
plt.title("Aequitas")
plt.show()
if __name__ == '__main__':
process_example_uniswap_transactions('data/0x05f04f112a286c4c551897fb19ed2300272656c8.csv', same_order)