-
Notifications
You must be signed in to change notification settings - Fork 0
/
litecoin.py
163 lines (100 loc) · 3.21 KB
/
litecoin.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
#!/usr/bin/env python
# coding: utf-8
# In[31]:
# necessary packages
import pandas as pd
from pandas.io.json import json_normalize
import gzip
import json
import multiprocessing as mp
import time
import copy
import pickle
import networkx
# from tqdm._tqdm_notebook import tqdm_notebook
# tqdm_notebook.pandas()
# ## Creating a DataFrame from the Dataset
# In[52]:
def convert_json(json_str):
# converts json string to df
return json_normalize(json.loads(json_str))
def convert_data(prefix, file_num):
for i in range(file_num):
suffix = str(i).zfill(12)
filename = prefix + suffix
with gzip.open(filename, "rt", encoding = "utf-8") as file:
with mp.Pool() as pool:
results = pool.map(convert_json, [line for line in file if line])
mode = 'a' if i else 'w'
pd.concat(results, sort=False).to_csv('data.csv', header = not i, mode = mode)
# In[ ]:
# convert gunzipped json files from google bigquery to a single csv
# convert_data('data/2019_01_04_', 4)
# In[23]:
def load_data(filename):
df = pd.read_csv(filename)
return df
df = load_data('data.csv')
# ## Creating Address Clusters
# In[ ]:
# %time lists = [set(df[df['tx_hash'] == tx]['inputs_addresses']) for tx in set(df.head(10000)['tx_hash'])]
def get_input_addrs(tx):
return set(df[df['tx_hash'] == tx]['inputs_addresses'])
def get_prelim_clusters(df):
start = time.time()
with mp.Pool() as pool:
prelim = pool.map(get_input_addrs, set(df['tx_hash']))
pool.close()
pool.join()
end = time.time()
print(f'Creating the prelim clusters took {round((end - start) / 60, 2)} min.')
return prelim
def construct_clusters(prelim):
def pairs(lst):
i = iter(lst)
first = prev = item = next(i)
for item in i:
yield prev, item
prev = item
yield item, first
graph = networkx.Graph()
for cluster in prelim:
for edge in pairs(cluster):
graph.add_edge(*edge)
clusters = list(networkx.connected_components(graph))
return clusters
prelim = get_prelim_clusters(df)
clusters = construct_clusters(prelim)
# In[43]:
print(len(prelim))
# In[45]:
print(len(clusters))
# In[13]:
def picklify(obj, filename):
# save obj in a pickle file for later
with open(filename, 'wb') as file:
pickle.dump(obj, file)
picklify(clusters, 'clusters.pickle')
def unpicklify(filename):
with open(filename, 'rb') as file:
return pickle.load(file)
# clusters = unpicklify('clusters.pickle')
# In[19]:
def clusters_are_consolidated(clusters):
for idx, cluster in enumerate(clusters):
for other in clusters[idx + 1:]:
inter = cluster.intersection(other)
if inter:
return False
return True
# def consolidate_clusters(clusters):
# consolidated = clusters_are_consolidated(clusters)
# while()
# In[15]:
set(df[df['block_hash'] == '499d9daf3a398f5cfd6ccc060616423a83c16725be71bfa13a1115fd7fc03d85']['tx_hash'])
# In[11]:
set([time[:10] for time in df['block_timestamp']])
# In[13]:
df.columns
# In[26]:
df[df['outputs_addresses']=='LLMRAtr3qBje2ySEa3CnZ55LA4TQMWnRY3']['is_coinbase']