import json
import networkx as nx
import pandas as pd
from tqdm import trange
'''
def generateGraph(callTxs, A, B):
callTxs = callTxs.reset_index(drop = True)
D = nx.DiGraph()
D.add_node(A)
D.add_node(B)
list_nodes = []
for i in range(len(callTxs)):
sender = callTxs['sender'][i]
recipient = callTxs['recipient'][i]
D.add_edge(sender, recipient)
return D
def getABAB(txs, hash):
txs = txs.reset_index(drop=True)
list_index = []
list_senderAndRecipient = []
list_input_hex = []
list_finalOutPut = []
count = 0
for i in range(len(txs)):
txStart_tmp = [txs['sender'][i], txs['recipient'][i], txs['input_hex'][i][0:8], txs['index'][i]]
if txStart_tmp[0] == txStart_tmp[1]:
continue;
for j in range(i + 1, len(txs)):
txEnd_tmp = [txs['sender'][j], txs['recipient'][j], txs['input_hex'][j][0:8], txs['index'][j]]
if (
(txStart_tmp[0] == txEnd_tmp[0])
& (txStart_tmp[2] == txEnd_tmp[2])
& (str(txEnd_tmp[3]).startswith(str(txStart_tmp[3]))) & (j - i > 1)):
A = txStart_tmp[0]
X = txStart_tmp[1]
AtoX_index = txStart_tmp[3]
AtoY_index = txEnd_tmp[3]
AtoX_input_hex = txStart_tmp[2]
AtoY_input_hex = txEnd_tmp[2]
list_middle_txs = txs.iloc[i + 1:j]
D = generateGraph(list_middle_txs, A, X)
if nx.has_path(D, X, A):
list_senderAndRecipient.append(A)
list_senderAndRecipient.append(X)
list_index.append(AtoX_index)
list_index.append(AtoY_index)
list_input_hex.append(AtoX_input_hex)
list_input_hex.append(AtoY_input_hex)
shortest_path = nx.shortest_path(D, X, A)
list_finalOutPut.append(list_senderAndRecipient)
list_finalOutPut.append(list_index)
list_finalOutPut.append(list_input_hex)
list_finalOutPut.append(shortest_path)
list_finalOutPut.append(hash)
f = open('test.txt', 'a')
f.write(",")
f.write(json.dumps(list_finalOutPut))
f.close()
list_index = []
list_senderAndRecipient = []
list_finalOutPut = []
list_input_hex = []
if __name__ == '__main__':
file_path = r'blockchair_ethereum_calls_20210917.tsv'
datas = pd.read_csv(file_path, sep='\t', header=0, index_col=None)
wp = datas.drop_duplicates(['transaction_hash'])
list_senderAndRecipient = []
list_paths = []
list_blocknum = []
flag = 0
for i in trange(len(wp['transaction_hash'].values)):
hash = wp['transaction_hash'].values[i]
blockNum = wp['block_id'].values[i]
if blockNum not in list_blocknum:
list_blocknum.append(blockNum)
print(blockNum)
if len(hash) != 64:
continue
txs = datas[datas['transaction_hash'] == hash]
callTxs = txs[txs['type'] == "call"]
callTxs = callTxs.dropna(axis=0, subset=["transaction_hash", "sender", "recipient"])
callTxs = callTxs.fillna('ABCDEFGH')
if (len(callTxs) > 2):
getABAB(callTxs, hash)