<s> import bitHopper . Database <EOL> import bitHopper . Database . Commands <EOL> import btcnet_info <EOL> import logging , time , traceback , gevent <EOL> import bitHopper . Configuration . Pools <EOL> from speed import Speed <EOL> def get_diff ( pool ) : <EOL> coin = btcnet_info . get_pool ( pool ) <EOL> if coin : <EOL> coin = coin . coin <EOL> else : <EOL> coin = '<STR_LIT>' <EOL> return float ( btcnet_info . get_difficulty ( coin ) ) <EOL> getworks = None <EOL> accepted = None <EOL> rejected = None <EOL> hashrate = Speed ( ) <EOL> def build_dict ( ) : <EOL> """<STR_LIT>""" <EOL> __patch ( ) <EOL> res = { } <EOL> for key in getworks : <EOL> server , username , password , diff = key <EOL> if key not in accepted : <EOL> accepted [ key ] = <NUM_LIT:0> <EOL> if key not in rejected : <EOL> rejected [ key ] = <NUM_LIT:0> <EOL> if server not in res : <EOL> res [ server ] = { } <EOL> name = "<STR_LIT::>" . join ( [ shorten ( username ) , password ] ) <EOL> if name not in res [ server ] : <EOL> res [ server ] [ name ] = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> res [ server ] [ name ] [ <NUM_LIT:0> ] += getworks [ key ] <EOL> res [ server ] [ name ] [ <NUM_LIT:1> ] += accepted [ key ] <EOL> res [ server ] [ name ] [ <NUM_LIT:2> ] += rejected [ key ] <EOL> res [ server ] [ name ] = map ( int , res [ server ] [ name ] ) <EOL> return res <EOL> def __patch ( ) : <EOL> global getworks , accepted , rejected <EOL> if getworks == None : <EOL> getworks , accepted , rejected = load_from_db ( ) <EOL> gevent . spawn ( looping_store ) <EOL> def shorten ( name ) : <EOL> """<STR_LIT>""" <EOL> if len ( name ) > <NUM_LIT:10> : <EOL> name = name [ : <NUM_LIT:10> ] + '<STR_LIT>' <EOL> return name <EOL> def looping_store ( ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> gevent . sleep ( <NUM_LIT:30> ) <EOL> try : <EOL> store_current ( ) <EOL> except : <EOL> logging . error ( traceback . format_exc ( ) ) <EOL> def store_current ( ) : <EOL> """<STR_LIT>""" <EOL> for key , getwork_c in getworks . items ( ) : <EOL> accepted_c = accepted . get ( key , <NUM_LIT:0> ) <EOL> rejected_c = rejected . get ( key , <NUM_LIT:0> ) <EOL> server = key [ <NUM_LIT:0> ] <EOL> username = key [ <NUM_LIT:1> ] <EOL> password = key [ <NUM_LIT:2> ] <EOL> difficulty = key [ <NUM_LIT:3> ] <EOL> timestamp = time . asctime ( time . gmtime ( ) ) <EOL> try : <EOL> if get_diff ( server ) != difficulty : <EOL> continue <EOL> except : <EOL> logging . error ( traceback . format_exc ( ) ) <EOL> continue <EOL> sql = "<STR_LIT>" % ( getwork_c , accepted_c , rejected_c , timestamp , server , username , password , difficulty ) <EOL> bitHopper . Database . execute ( sql ) <EOL> result = bitHopper . Database . execute ( '<STR_LIT>' % ( server , username , password , difficulty ) ) <EOL> result = list ( result ) <EOL> if len ( result ) == <NUM_LIT:0> : <EOL> sql = "<STR_LIT>" % ( server , username , password , difficulty , timestamp , getwork_c , accepted_c , rejected_c ) <EOL> bitHopper . Database . execute ( sql ) <EOL> def load_from_db ( ) : <EOL> """<STR_LIT>""" <EOL> columns = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> bitHopper . Database . Commands . Create_Table ( '<STR_LIT>' , columns ) <EOL> results = bitHopper . Database . execute ( '<STR_LIT>' ) <EOL> getworks = { } <EOL> accepted = { } <EOL> rejected = { } <EOL> for server , username , password , difficulty , getworks_v , accepted_v , rejected_v in results : <EOL> key = ( server , username , password , difficulty ) <EOL> getworks [ key ] = getworks_v <EOL> accepted [ key ] = accepted_v <EOL> rejected [ key ] = rejected_v <EOL> return getworks , accepted , rejected <EOL> def get_key ( server , username , password ) : <EOL> """<STR_LIT>""" <EOL> difficulty = get_diff ( server ) <EOL> key = ( server , username , password , difficulty ) <EOL> return key <EOL> def add_getwork ( server , username , password ) : <EOL> """<STR_LIT>""" <EOL> __patch ( ) <EOL> key = get_key ( server , username , password ) <EOL> if key not in getworks : <EOL> getworks [ key ] = <NUM_LIT:0> <EOL> getworks [ key ] += <NUM_LIT:1> <EOL> username = shorten ( username ) <EOL> logging . info ( '<STR_LIT>' % ( username , password , server ) ) <EOL> def add_accepted ( server , username , password ) : <EOL> """<STR_LIT>""" <EOL> __patch ( ) <EOL> key = get_key ( server , username , password ) <EOL> if key not in accepted : <EOL> accepted [ key ] = <NUM_LIT:0> <EOL> accepted [ key ] += <NUM_LIT:1> <EOL> username = shorten ( username ) <EOL> hashrate . add_shares ( <NUM_LIT:1> ) <EOL> logging . info ( '<STR_LIT>' % ( username , password , server ) ) <EOL> def add_rejected ( server , username , password ) : <EOL> """<STR_LIT>""" <EOL> __patch ( ) <EOL> key = get_key ( server , username , password ) <EOL> if key not in rejected : <EOL> rejected [ key ] = <NUM_LIT:0> <EOL> rejected [ key ] += <NUM_LIT:1> <EOL> username = shorten ( username ) <EOL> hashrate . add_shares ( <NUM_LIT:1> ) <EOL> logging . info ( '<STR_LIT>' % ( username , password , server ) ) <EOL> def get_hashrate ( ) : <EOL> return hashrate . get_rate ( ) </s>
