import pandas as pd, sys, json, hashlib, threading
sys.path.insert(0,'/tmp/run'); from rpc import rpc
d=pd.read_pickle('clean.pkl')
hooks=sorted(set(d.hook.dropna())-{'0x'+'0'*40})
facs=sorted(set(d[d.hook_n==1].fac.dropna())-{''})
todo=[('h',h) for h in hooks]+[('f',f) for f in facs]
out={}; lock=threading.Lock()
def w():
    while True:
        with lock:
            if not todo: return
            k,a=todo.pop()
        c=rpc('eth_getCode',[a,'latest'])
        c=c if isinstance(c,str) else ''
        # proxy follow: EIP-1167
        impl=None
        if c.startswith('0x363d3d373d3d3d363d73'): impl='0x'+c[22:62]
        with lock: out[a]=(len(c)//2-1, hashlib.md5(c.encode()).hexdigest()[:8], impl)
ts=[threading.Thread(target=w) for _ in range(3)]; [t.start() for t in ts]; [t.join() for t in ts]
json.dump(out,open('codes.json','w')); print(len(out))
