<s> import gevent <EOL> import socket <EOL> import time <EOL> import datetime <EOL> from binascii import hexlify <EOL> from cryptokit . rpc import CoinRPCException <EOL> from collections import deque <EOL> from cryptokit . util import pack <EOL> from cryptokit . bitcoin import data as bitcoin_data <EOL> from gevent import sleep <EOL> from gevent . event import Event <EOL> from . import NodeMonitorMixin , Jobmanager <EOL> from . . exceptions import RPCException <EOL> from . . lib import loop , REQUIRED <EOL> class MonitorAuxNetwork ( Jobmanager , NodeMonitorMixin ) : <EOL> gl_methods = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> one_min_stats = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> defaults = dict ( enabled = False , <EOL> work_interval = <NUM_LIT:1> , <EOL> signal = None , <EOL> rpc_ping_int = <NUM_LIT:2> , <EOL> algo = REQUIRED , <EOL> currency = REQUIRED , <EOL> coinservs = REQUIRED , <EOL> flush = False , <EOL> send = True ) <EOL> def __init__ ( self , config ) : <EOL> self . _configure ( config ) <EOL> NodeMonitorMixin . __init__ ( self ) <EOL> self . new_job = Event ( ) <EOL> self . last_signal = <NUM_LIT:0.0> <EOL> self . last_work = { '<STR_LIT>' : None } <EOL> self . block_stats = dict ( accepts = <NUM_LIT:0> , <EOL> rejects = <NUM_LIT:0> , <EOL> stale = <NUM_LIT:0> , <EOL> solves = <NUM_LIT:0> , <EOL> last_solve_height = None , <EOL> last_solve_time = None , <EOL> last_solve_worker = None ) <EOL> self . current_net = dict ( difficulty = None , height = None , last_block = <NUM_LIT:0.0> ) <EOL> self . recent_blocks = deque ( maxlen = <NUM_LIT:15> ) <EOL> def start ( self ) : <EOL> super ( MonitorAuxNetwork , self ) . start ( ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . logger . info ( "<STR_LIT>" <EOL> . format ( self . config [ '<STR_LIT>' ] ) ) <EOL> gevent . signal ( self . config [ '<STR_LIT>' ] , <EOL> self . _check_new_jobs , <EOL> signal = True , <EOL> _single_exec = True ) <EOL> def found_block ( self , address , worker , header , coinbase_raw , job , start ) : <EOL> aux_data = job . merged_data [ self . config [ '<STR_LIT>' ] ] <EOL> new_height = aux_data [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> self . block_stats [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> stale = new_height <= self . current_net [ '<STR_LIT>' ] <EOL> self . logger . info ( "<STR_LIT>" <EOL> . format ( self . config [ '<STR_LIT>' ] , new_height ) ) <EOL> aux_block = ( <EOL> pack . IntType ( <NUM_LIT> , '<STR_LIT>' ) . pack ( aux_data [ '<STR_LIT>' ] ) . encode ( '<STR_LIT>' ) , <EOL> bitcoin_data . aux_pow_type . pack ( dict ( <EOL> merkle_tx = dict ( <EOL> tx = bitcoin_data . tx_type . unpack ( coinbase_raw ) , <EOL> block_hash = bitcoin_data . hash256 ( header ) , <EOL> merkle_link = job . merkle_link , <EOL> ) , <EOL> merkle_link = bitcoin_data . calculate_merkle_link ( aux_data [ '<STR_LIT>' ] , <EOL> aux_data [ '<STR_LIT:index>' ] ) , <EOL> parent_block_header = bitcoin_data . block_header_type . unpack ( header ) , <EOL> ) ) . encode ( '<STR_LIT>' ) , <EOL> ) <EOL> retries = <NUM_LIT:0> <EOL> while retries < <NUM_LIT:5> : <EOL> retries += <NUM_LIT:1> <EOL> res = False <EOL> try : <EOL> res = self . call_rpc ( '<STR_LIT>' , * aux_block ) <EOL> except ( CoinRPCException , socket . error , ValueError ) as e : <EOL> self . logger . error ( "<STR_LIT>" <EOL> . format ( self . config [ '<STR_LIT>' ] ) , exc_info = True ) <EOL> self . logger . error ( getattr ( e , '<STR_LIT:error>' ) ) <EOL> if res is True : <EOL> self . block_stats [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> self . recent_blocks . append ( <EOL> dict ( height = new_height , timestamp = int ( time . time ( ) ) ) ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> if start : <EOL> submission_time = time . time ( ) - start <EOL> self . manager . log_event ( <EOL> "<STR_LIT>" <EOL> . format ( name = self . manager . config [ '<STR_LIT>' ] , <EOL> curr = self . config [ '<STR_LIT>' ] , <EOL> t = submission_time * <NUM_LIT:1000> ) ) <EOL> hsh = hexlify ( pack . IntType ( <NUM_LIT> , '<STR_LIT>' ) . pack ( aux_data [ '<STR_LIT>' ] ) ) <EOL> self . logger . info ( <EOL> "<STR_LIT>" <EOL> . format ( self . config [ '<STR_LIT>' ] , hsh , new_height , <EOL> submission_time ) ) <EOL> block = None <EOL> amount = <NUM_LIT:0> <EOL> try : <EOL> block = self . call_rpc ( '<STR_LIT>' , hsh ) <EOL> except Exception : <EOL> self . logger . info ( "<STR_LIT>" , exc_info = True ) <EOL> else : <EOL> try : <EOL> trans = self . call_rpc ( '<STR_LIT>' , block [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> amount = trans [ '<STR_LIT:value>' ] <EOL> except Exception : <EOL> self . logger . info ( "<STR_LIT>" , exc_info = True ) <EOL> self . block_stats [ '<STR_LIT>' ] = hsh <EOL> return dict ( address = address , <EOL> height = new_height , <EOL> total_subsidy = int ( amount * <NUM_LIT> ) , <EOL> fees = None , <EOL> hex_bits = "<STR_LIT>" % bitcoin_data . FloatingInteger . from_target_upper_bound ( aux_data [ '<STR_LIT:target>' ] ) . bits , <EOL> hex_hash = hsh , <EOL> currency = self . config [ '<STR_LIT>' ] , <EOL> merged = True , <EOL> algo = self . config [ '<STR_LIT>' ] , <EOL> worker = worker ) <EOL> break <EOL> else : <EOL> self . logger . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . config [ '<STR_LIT>' ] , res ) , <EOL> exc_info = True ) <EOL> sleep ( <NUM_LIT:1> ) <EOL> else : <EOL> if stale : <EOL> self . block_stats [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> else : <EOL> self . block_stats [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> self . block_stats [ '<STR_LIT>' ] = aux_data [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> self . block_stats [ '<STR_LIT>' ] = "<STR_LIT>" . format ( address , worker ) <EOL> self . block_stats [ '<STR_LIT>' ] = datetime . datetime . utcnow ( ) <EOL> @ loop ( interval = '<STR_LIT>' ) <EOL> def _check_new_jobs ( self , signal = False ) : <EOL> if signal : <EOL> self . last_signal = time . time ( ) <EOL> self . logger . info ( "<STR_LIT>" <EOL> . format ( self . config [ '<STR_LIT>' ] ) ) <EOL> try : <EOL> auxblock = self . call_rpc ( '<STR_LIT>' ) <EOL> except RPCException : <EOL> sleep ( <NUM_LIT:2> ) <EOL> return False <EOL> hash = int ( auxblock [ '<STR_LIT>' ] , <NUM_LIT:16> ) <EOL> if hash != self . last_work [ '<STR_LIT>' ] : <EOL> try : <EOL> height = self . call_rpc ( '<STR_LIT>' ) <EOL> except RPCException : <EOL> sleep ( <NUM_LIT:2> ) <EOL> return False <EOL> target_int = pack . IntType ( <NUM_LIT> ) . unpack ( auxblock [ '<STR_LIT:target>' ] . decode ( '<STR_LIT>' ) ) <EOL> self . last_work . update ( dict ( <EOL> hash = hash , <EOL> target = target_int , <EOL> type = self . config [ '<STR_LIT>' ] , <EOL> height = height , <EOL> found_block = self . found_block , <EOL> chainid = auxblock [ '<STR_LIT>' ] <EOL> ) ) <EOL> new_block = False <EOL> if self . current_net [ '<STR_LIT>' ] != height : <EOL> self . current_net [ '<STR_LIT>' ] = height <EOL> self . _incr ( "<STR_LIT>" ) <EOL> self . _incr ( "<STR_LIT>" ) <EOL> self . new_job . flush = self . config [ '<STR_LIT>' ] <EOL> new_block = True <EOL> else : <EOL> self . _incr ( "<STR_LIT>" ) <EOL> self . new_job . flush = False <EOL> self . new_job . set ( ) <EOL> self . new_job . clear ( ) <EOL> if new_block : <EOL> self . current_net [ '<STR_LIT>' ] = time . time ( ) <EOL> self . current_net [ '<STR_LIT>' ] = bitcoin_data . target_to_difficulty ( target_int ) <EOL> self . logger . info ( "<STR_LIT>" <EOL> . format ( self . current_net [ '<STR_LIT>' ] , height ) ) <EOL> return True <EOL> @ property <EOL> def status ( self ) : <EOL> ret = dict ( block_stats = self . block_stats , <EOL> currency = self . config [ '<STR_LIT>' ] , <EOL> last_work = self . last_work , <EOL> last_signal = self . last_signal , <EOL> live_coinservers = len ( self . _live_connections ) , <EOL> down_coinservers = len ( self . _down_connections ) , <EOL> coinservers = { } , <EOL> current_net = self . current_net ) <EOL> for connection in self . _live_connections : <EOL> st = connection . status ( ) <EOL> st [ '<STR_LIT:status>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT>' ] [ connection . name ] = st <EOL> for connection in self . _down_connections : <EOL> st = connection . status ( ) <EOL> st [ '<STR_LIT:status>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT>' ] [ connection . name ] = st <EOL> return ret </s>
