-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_graph2.py
208 lines (170 loc) · 5.79 KB
/
transaction_graph2.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# used tranaction ids to get the CORRECT total ordering
import heapq, sys
import numpy as np
import db_tools
class HeapSet:
def __init__(self, min_heap = True):
self.heap = []
self.elements = set([])
self.min_heap = min_heap
def add(self,tx):
if tx not in self.elements:
self.elements.add(tx)
if self.min_heap:
priority = tx.id
else:
priority = -tx.id
heapq.heappush(self.heap, (priority, tx))
def pop(self):
priority, tx = heapq.heappop(self.heap)
self.elements.remove(tx)
if self.min_heap:
return priority, tx
else:
return -priority, tx
def __len__(self):
return len(self.heap)
cutoff = 50
class TransactionGraph:
def __init__(self):
self.nodes = set([])
self.edges = {}
def clear(self):
db_tools.instances = {}
def addSourceAddress(self, address_string, index = 0):
address = db_tools.Address(address_string)
self.backwardInTime(address)
self.forwardInTime(address)
def backwardInTime(self, address):
contaminated_outputs = HeapSet(min_heap = False)
self.funding_inputs = address.inputs
self.funding_ids = np.array([input.id for input in self.funding_inputs])
total_contamination = 0
for i,input in enumerate(self.funding_inputs):
total_contamination += input.value # fix so you do not double count coins.
input.rank = i
input.is_source = 2
contaminated_outputs.add(input)
while len(contaminated_outputs) > 0 and len(self.nodes) < 50:
output_id, output = contaminated_outputs.pop()
if output in self.funding_inputs:
if output.contamination is None:
output.contamination = np.zeros(len(self.funding_inputs))
assert output.contamination[self.funding_inputs.index(output)] == 0
additional_contamination = output.value - output.contamination.sum()
output.contamination[self.funding_inputs.index(output)] = additional_contamination
assert np.abs(output.contamination.sum() - output.value) < .1, (output.contamination.sum(), output.value)
if output.contamination.sum() < total_contamination / cutoff:
continue
else:
self.addNode(output)
for i,input in enumerate(output.transaction.inputs):
weight = input.value / output.transaction.inputValue
if input.contamination is None:
input.contamination = weight * output.contamination
else:
input.contamination += weight * output.contamination
assert input.contamination.sum() <= input.value + .1
input.rank = 10*output.rank + i
contaminated_outputs.add(input)
self.addEdge(input, output, weight * output.contamination.sum())
def forwardInTime(self, address):
contaminated_inputs = HeapSet(min_heap = True)
total_contamination = 0
for input in self.funding_inputs:
total_contamination += input.value
input.can_be_collapsed = False
contaminated_inputs.add(input)
while len(contaminated_inputs) > 0 and len(self.nodes) < 100:
input_id, input = contaminated_inputs.pop()
if input.contamination.sum() < total_contamination / cutoff:
continue
elif input.spend_transaction == 'Unspent':
self.addNode(input)
continue
else:
self.addNode(input)
outputs = input.spend_transaction.outputs
input_value = input.spend_transaction.inputValue
for i, output in enumerate(outputs):
weight = output.value / input_value
if output.contamination is None:
output.contamination = weight * input.contamination
else:
mask = input_id > self.funding_ids
output.contamination[mask] += weight * input.contamination[mask]
assert output.contamination.sum() <= output.value + .1
output.rank = 10*input.rank + i
if len(outputs) == 1:
output.can_be_collapsed = False
output.parent = [input]
else:
output.can_be_collapsed = False
if input.can_be_collapsed:
self.addEdge(input.parent[0],output,weight * input.contamination.sum())
output.parent = [input.parent[0]]
self.removeNode(input)
self.removeEdge(input.parent[0],input)
else:
self.addEdge(input, output, weight * input.contamination.sum())
contaminated_inputs.add(output)
def addNode(self,node):
self.nodes.add(node)
def removeNode(self,node):
try:
self.nodes.remove(node)
except KeyError:
pass
def addEdge(self,source,sink,weight):
self.edges[(source,sink)] = weight
def removeEdge(self,source,sink):
print 'hi!!!'
try:
del self.edges[(source,sink)]
except KeyError:
pass
def toDict(self):
nodes = []
self.nodes = list(self.nodes)
for output in self.nodes:
node = {}
node['name'] = output.transaction
node['color'] = colorFunction(output.contamination.sum() / output.value)
node['btc_value'] = output.value / 1e8
node['address'] = output.address
node['rank'] = output.rank
node['is_source'] = output.is_source
node['contamination'] = output.contamination.sum() / 1e8
node['time'] = output.height
nodes.append(node)
links = []
for source, target in self.edges:
link = {}
if source not in self.nodes or target not in self.nodes:
continue
link['source'] = self.nodes.index(source)
link['target'] = self.nodes.index(target)
link['value'] = self.edges[(source, target)] / 1e8
link['time'] = target.height
links.append(link)
return {'nodes': nodes, 'links': links}
def colorFunction(x):
return '#' + colorScale(x) + colorScale(0) + colorScale(0)
def colorScale(x):
x = int(255 * np.sqrt(x))
result = chr(x).encode('hex')
return result
if __name__ == '__main__':
test_address = '18c11RMANxg7aq6eqJ4MPe6dqvcwZtAAwk'
g = TransactionGraph()
g.addSourceAddress(test_address)
print g.toDict()
db_tools.instances = {}
#pizza_address = '17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'
#g = TransactionGraph()
#g.addSourceAddress(pizza_address)
#db_tools.instances = {}
#mtgox_address = '1eHhgW6vquBYhwMPhQ668HPjxTtpvZGPC'
#g = TransactionGraph()
#g.addSourceAddress(mtgox_address)
#g.connectEdges()