-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin_classes.py
More file actions
177 lines (148 loc) · 5.07 KB
/
bitcoin_classes.py
File metadata and controls
177 lines (148 loc) · 5.07 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
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
import pymysql as mdb
import numpy as np
con = mdb.connect('localhost','root','','bitcoin')
cur = con.cursor()
class Address(str):
def incomingTxs(self):
command = 'SELECT DISTINCT(transactionHash) '
command += 'FROM outputs '
command += 'WHERE address = "%s" ' % self
cur.execute(command)
results = cur.fetchall()
return [Transaction(row) for row in cur.fetchall()]
def outgoingTxs(self):
command = 'SELECT DISTINCT(spendHash) '
command += 'FROM outputs '
command += 'WHERE address = "%s" ' % self
cur.execute(command)
return [Transaction(row[0]) for row in cur.fetchall()]
def countInputs(self):
command = 'SELECT COUNT(*) '
command += 'FROM outputs '
command += 'WHERE address = "%s" ' % self
cur.execute(command)
return int(cur.fetchone()[0])
def inputs(self):
command = 'SELECT * '
command += 'FROM outputs '
command += 'WHERE address = "%s" ' % self
cur.execute(command)
return [Coins(row) for row in cur.fetchall()]
def outputs(self):
command = 'SELECT b.* '
command += 'FROM outputs a, outputs b '
command += 'WHERE a.address = "%s" ' % self
command += 'AND a.spendHash = b.transactionHash'
cur.execute(command)
return [Coins(row) for row in cur.fetchall()]
def value(self,block_number):
command = 'SELECT SUM(value) '
command += 'FROM outputs WHERE address = "%s" ' % self
command += 'AND blockNumber <= %i' % block_number
cur.execute(command)
try:
total_in = int(cur.fetchone()[0])
except TypeError:
total_in = 0
command = 'SELECT SUM(b.value) '
command += 'FROM outputs a, outputs b '
command += 'WHERE a.address = "%s" ' % self
command += 'AND a.spendHash = b.transactionHash '
command += 'AND b.blockNumber <= %i' %block_number
cur.execute(command)
try:
total_out = int(cur.fetchone()[0])
except TypeError:
total_out = 0
return total_in - total_out
class Transaction(str):
def inputs(self):
command = 'SELECT * FROM outputs '
command += 'WHERE spendHash = "%s" ' %self
cur.execute(command)
return [Coins(row) for row in cur.fetchall()]
def height(self):
command = 'SELECT blockNumber FROM outputs '
command += 'WHERE transactionHash = "%s" LIMIT 1' %self
cur.execute(command)
return cur.fetchone()[0]
def inputValue(self):
command = 'SELECT SUM(value) FROM outputs WHERE spendHash = "%s"'
command = command % self
cur.execute(command)
try:
return float(cur.fetchone()[0])
except TypeError:
return 0.0
def outputs(self):
command = 'SELECT * FROM outputs '
command += 'WHERE transactionHash = "%s" ' %self
cur.execute(command)
return [Coins(row) for row in cur.fetchall()]
def __hash__(self):
return hash('%s' %self)
NULL_HASH = 64*'0'
def multiton(cls):
instances = {}
def getInstance(row):
key = tuple(row[:2])
if key not in instances:
instances[key] = cls(row)
return instances[key]
return getInstance
@multiton # on transction and output index
class Coins:
def __init__(self, row):
self.transaction = Transaction(row[0])
self.output_index = row[1]
self.value = float(row[2])
self.height = int(row[3])
self.spend_transaction = Transaction(row[4])
self.address = Address(row[5])
self.sinks = set([])
self.is_source = False
self.contamination = 0
def nextOutputs(self):
command = 'SELECT * FROM outputs '
command += 'WHERE transactionHash = "%s" ' % self.spend_transaction
cur.execute(command)
return [Coins(row) for row in cur.fetchall()]
def previousInputs(self):
command = 'SELECT * FROM outputs '
command += 'WHERE spendHash = "%s" ' % self.transaction
cur.execute(command)
return [Coins(row) for row in cur.fetchall()]
def __str__(self):
string = 'height: %i ' %self.height
string += 'address: %s ' %self.address
string += 'value: %e ' %self.value
string += 'contamination: %e ' %self.contamination
#assert self.contamination[0] <= self.value
#string += 'taint: %e ' %(self.contamination.max() / self.value)
#string += 'backward_taint: %e ' %(self.backward_contamination[0] / self.value)
#string += 'taint: %.2f' % (self.contamination / self.value)
return string
def forward_taint(self, index):
contamination = self.forward_contamination[index]
return float(contamination) / self.value
def backward_taint(self, index):
contamination = self.backward_contamination[index]
return float(contamination) / self.value
def notSpent(self):
return self.spend_transaction == NULL_HASH
def addSink(self, coins, flow):
self.sinks.add((coins,) + tuple(flow))
if __name__ == '__main__':
pizza_address = Address('17SkEw2md5avVNyYgj6RiXuQKNwkXaxFyQ')
assert pizza_address.value(57042) == 0
assert pizza_address.value(57043) == int(1e12)
assert pizza_address.value(57044) == 0
assert len(pizza_address.getInputs()) == 1
assert len(pizza_address.getOutputs()) == 2
assert len(pizza_address.nextTxs(57041)) == 1
assert len(pizza_address.nextTxs(57042)) == 1
assert len(pizza_address.nextTxs(57043)) == 1
assert len(pizza_address.nextTxs(57044)) == 1
assert len(pizza_address.nextTxs(57045)) == 0
inout_address = Address('1PrwYMffhu1XJyVXs6Ba67NidGZVjbGHCq')
assert len(inout_address.nextTxs(64683)) == 1