-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_json.py
More file actions
114 lines (98 loc) · 3.35 KB
/
generate_json.py
File metadata and controls
114 lines (98 loc) · 3.35 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
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
import sys, heapq
import numpy as np
import pymysql as mdb
import json
from graph_to_json import graphToDict
NULL_HASH = 64*'0'
class PriorityDict():
def __init__(self):
self.heap = [] # for keys of self.elements
self.elements = {}
def add(self,element):
priority = element.blockNumber
key = (element.transactionHash,element.outputIndex)
if key not in self.elements:
heapq.heappush(self.heap,(priority,key))
self.elements[key] = element
def pop(self):
priority,key = heapq.heappop(self.heap)
return self.elements[key]
def __len__(self):
return len(self.heap)
def __getitem__(self,output):
return self.elements[(output.transactionHash,output.outputIndex)]
class Output:
def __init__(self,parameters,taint=0):
self.amount = 0.0
self.taint = 0.0
self.blockNumber = parameters[0]
self.outputIndex = parameters[1]
self.spendHash = parameters[2]
self.transactionHash = parameters[3]
self.value = parameters[4]
def __hash__(self):
return hash((self.outputIndex,self.transactionHash))
def __eq__(self,other):
return hash(self) == hash(other)
con = mdb.connect('localhost','root','','bitcoin')
cur = con.cursor()
def getTxInputValue(transaction_hash):
command = 'SELECT SUM(value) FROM outputs WHERE spendHash = "%s"'
command = command % (transaction_hash)
cur.execute(command)
return cur.fetchone()[0]
def getTxOutputValue(transaction_hash):
command = 'SELECT SUM(value) FROM outputs WHERE transactionHash = "%s"'
command = command % (transaction_hash)
cur.execute(command)
return cur.fetchone()[0]
def getSourceOutputs(source_address):
command = 'SELECT blockNumber, outputIndex, spendHash, transactionHash, value '
command += 'FROM outputs WHERE address = "%s"' %source_address
cur.execute(command)
sources = PriorityDict()
for entry in cur.fetchall():
output = Output(entry)
output.amount = output.value
output.taint = 1.0
sources.add(output)
return sources
def getLinkedOutputs(output):
command = 'SELECT blockNumber,outputIndex,spendHash,transactionHash,value '
command += 'FROM outputs WHERE transactionHash = "%s"'
command = command % output.spendHash
cur.execute(command)
return [Output(entry) for entry in cur.fetchall()]
#try:
address = sys.argv[1]
#except IndexError:
# address = '1eHhgW6vquBYhwMPhQ668HPjxTtpvZGPC'#'17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'
#address = '17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ'
tainteds = getSourceOutputs(address)
total_amount = 0
for tainted in tainteds.elements.values():
tainteds[tainted].amount = tainted.value
total_amount += tainted.value
unspent_outputs = []
graph = []
while len(tainteds) > 0:
output = tainteds.pop()
if output.amount < total_amount / 100 or output.amount / output.value < .1:
continue
elif output.spendHash == NULL_HASH:
unspent_outputs.append(output)
else:
entry = [output.transactionHash]
entry.append(output.spendHash)
entry.append(output.amount)
entry.append(output.amount/output.value)
graph.append(entry)
txOutputValue = float(getTxOutputValue(output.spendHash))
for linked_output in getLinkedOutputs(output):
tainteds.add(linked_output)
for linked_output in getLinkedOutputs(output):
weight = linked_output.value / txOutputValue
increment = weight * tainteds[output].amount
tainteds[linked_output].amount += increment
json_output = open('/Users/mason/Desktop/app/static/data/output.json','w')
json.dump(graphToDict(graph),json_output)