Exemple #1
0
def main(args, net, datadir_path, merged_urls, worker_endpoint):
    try:
        print 'p2pool (version %s)' % (p2pool.__version__,)
        print
        
        @defer.inlineCallbacks
        def connect_p2p():
            # connect to bitcoind over bitcoin-p2p
            print '''Testing bitcoind P2P connection to '%s:%s'...''' % (args.bitcoind_address, args.bitcoind_p2p_port)
            factory = bitcoin_p2p.ClientFactory(net.PARENT)
            reactor.connectTCP(args.bitcoind_address, args.bitcoind_p2p_port, factory)
            yield factory.getProtocol() # waits until handshake is successful
            print '    ...success!'
            print
            defer.returnValue(factory)
        
        if args.testnet: # establish p2p connection first if testnet so bitcoind can work without connections
            factory = yield connect_p2p()
        
        # connect to bitcoind over JSON-RPC and do initial getmemorypool
        url = '%s://%s:%i/' % ('https' if args.bitcoind_rpc_ssl else 'http', args.bitcoind_address, args.bitcoind_rpc_port)
        print '''Testing bitcoind RPC connection to '%s' with username '%s'...''' % (url, args.bitcoind_rpc_username)
        bitcoind = jsonrpc.HTTPProxy(url, dict(Authorization='Basic ' + base64.b64encode(args.bitcoind_rpc_username + ':' + args.bitcoind_rpc_password)), timeout=30)
        yield helper.check(bitcoind, net)
        temp_work = yield helper.getwork(bitcoind)
        
        bitcoind_warning_var = variable.Variable(None)
        @defer.inlineCallbacks
        def poll_warnings():
            errors = (yield deferral.retry('Error while calling getmininginfo:')(bitcoind.rpc_getmininginfo)())['errors']
            bitcoind_warning_var.set(errors if errors != '' else None)
        yield poll_warnings()
        task.LoopingCall(poll_warnings).start(20*60)
        
        print '    ...success!'
        print '    Current block hash: %x' % (temp_work['previous_block'],)
        print '    Current block height: %i' % (temp_work['height'] - 1,)
        print
        
        if not args.testnet:
            factory = yield connect_p2p()
        
        print 'Determining payout address...'
        pubkey_path = os.path.join(datadir_path, 'cached_payout_pubkey')
        
        if os.path.exists(pubkey_path):
            with open(pubkey_path, 'rb') as f:
                pubkey = f.read().strip('\r\n')
            print '    Loaded cached pubkey, payout address: %s...' % (bitcoin_data.pubkey_to_address(pubkey.decode('hex'), net.PARENT),)
        else:
            pubkey = None

        if pubkey is not None:
                res = yield deferral.retry('Error validating cached pubkey:', 5)(lambda: bitcoind.rpc_validatepubkey(pubkey))()
                if not res['isvalid'] or not res['ismine']:
                    print '    Cached pubkey is either invalid or not controlled by local bitcoind!'
                    address = None

        if pubkey is None:
            print '    Getting payout pubkey from bitcoind...'
            pubkey = yield deferral.retry('Error getting payout pubkey from bitcoind:', 5)(lambda: bitcoind.rpc_getnewpubkey('p2pool'))()
            
            with open(pubkey_path, 'wb') as f:
                f.write(pubkey)

        my_pubkey = pubkey.decode('hex')

        address = bitcoin_data.pubkey_to_address(my_pubkey, net.PARENT)

        my_pubkey_hash = bitcoin_data.address_to_pubkey_hash(address, net.PARENT)
        print '    ...success! Payout address:', bitcoin_data.pubkey_hash_to_address(my_pubkey_hash, net.PARENT)
        print
        
        ss = p2pool_data.ShareStore(os.path.join(datadir_path, 'shares.'), net)
        shares = {}
        known_verified = set()
        print "Loading shares..."
        for i, (mode, contents) in enumerate(ss.get_shares()):
            if mode == 'share':
                contents.time_seen = 0
                shares[contents.hash] = contents
                if len(shares) % 1000 == 0 and shares:
                    print "    %i" % (len(shares),)
            elif mode == 'verified_hash':
                known_verified.add(contents)
            else:
                raise AssertionError()
        print "    ...done loading %i shares (%i verified)!" % (len(shares), len(known_verified))
        print
        
        
        print 'Initializing work...'
        
        node = p2pool_node.Node(factory, bitcoind, shares.values(), known_verified, net)
        yield node.start()
        
        for share_hash in shares:
            if share_hash not in node.tracker.items:
                ss.forget_share(share_hash)
        for share_hash in known_verified:
            if share_hash not in node.tracker.verified.items:
                ss.forget_verified_share(share_hash)
        del shares, known_verified
        node.tracker.removed.watch(lambda share: ss.forget_share(share.hash))
        node.tracker.verified.removed.watch(lambda share: ss.forget_verified_share(share.hash))
        
        def save_shares():
            for share in node.tracker.get_chain(node.best_share_var.value, min(node.tracker.get_height(node.best_share_var.value), 2*net.CHAIN_LENGTH)):
                ss.add_share(share)
                if share.hash in node.tracker.verified.items:
                    ss.add_verified_hash(share.hash)
        task.LoopingCall(save_shares).start(60)
        
        print '    ...success!'
        print
        
        
        print 'Joining p2pool network using port %i...' % (args.p2pool_port,)
        
        @defer.inlineCallbacks
        def parse(x):
            if ':' in x:
                ip, port = x.split(':')
                defer.returnValue(((yield reactor.resolve(ip)), int(port)))
            else:
                defer.returnValue(((yield reactor.resolve(x)), net.P2P_PORT))
        
        addrs = {}
        if os.path.exists(os.path.join(datadir_path, 'addrs')):
            try:
                with open(os.path.join(datadir_path, 'addrs'), 'rb') as f:
                    addrs.update(dict((tuple(k), v) for k, v in json.loads(f.read())))
            except:
                print >>sys.stderr, 'error parsing addrs'
        for addr_df in map(parse, net.BOOTSTRAP_ADDRS):
            try:
                addr = yield addr_df
                if addr not in addrs:
                    addrs[addr] = (0, time.time(), time.time())
            except:
                log.err()
        
        connect_addrs = set()
        for addr_df in map(parse, args.p2pool_nodes):
            try:
                connect_addrs.add((yield addr_df))
            except:
                log.err()
        
        node.p2p_node = p2pool_node.P2PNode(node,
            port=args.p2pool_port,
            max_incoming_conns=args.p2pool_conns,
            addr_store=addrs,
            connect_addrs=connect_addrs,
            desired_outgoing_conns=args.p2pool_outgoing_conns,
        )
        node.p2p_node.start()
        
        def save_addrs():
            with open(os.path.join(datadir_path, 'addrs'), 'wb') as f:
                f.write(json.dumps(node.p2p_node.addr_store.items()))
        task.LoopingCall(save_addrs).start(60)
        
        print '    ...success!'
        print
        
        if args.upnp:
            @defer.inlineCallbacks
            def upnp_thread():
                while True:
                    try:
                        is_lan, lan_ip = yield ipdiscover.get_local_ip()
                        if is_lan:
                            pm = yield portmapper.get_port_mapper()
                            yield pm._upnp.add_port_mapping(lan_ip, args.p2pool_port, args.p2pool_port, 'p2pool', 'TCP')
                    except defer.TimeoutError:
                        pass
                    except:
                        if p2pool.DEBUG:
                            log.err(None, 'UPnP error:')
                    yield deferral.sleep(random.expovariate(1/120))
            upnp_thread()
        
        # start listening for workers with a JSON-RPC server
        
        print 'Listening for workers on %r port %i...' % (worker_endpoint[0], worker_endpoint[1])
        
        wb = work.WorkerBridge(node, my_pubkey, args.donation_percentage, merged_urls, args.worker_fee)
        web_root = web.get_web_root(wb, datadir_path, bitcoind_warning_var)
        caching_wb = worker_interface.CachingWorkerBridge(wb)
        worker_interface.WorkerInterface(caching_wb).attach_to(web_root, get_handler=lambda request: request.redirect('/static/'))
        web_serverfactory = server.Site(web_root)
        
        
        serverfactory = switchprotocol.FirstByteSwitchFactory({'{': stratum.StratumServerFactory(caching_wb)}, web_serverfactory)
        deferral.retry('Error binding to worker port:', traceback=False)(reactor.listenTCP)(worker_endpoint[1], serverfactory, interface=worker_endpoint[0])
        
        with open(os.path.join(os.path.join(datadir_path, 'ready_flag')), 'wb') as f:
            pass
        
        print '    ...success!'
        print
        
        
        # done!
        print 'Started successfully!'
        print 'Go to http://127.0.0.1:%i/ to view graphs and statistics!' % (worker_endpoint[1],)
        if args.donation_percentage > 1.1:
            print '''Donating %.1f%% of work towards P2Pool's development. Thanks for the tip!''' % (args.donation_percentage,)
        elif args.donation_percentage < .9:
            print '''Donating %.1f%% of work towards P2Pool's development. Please donate to encourage further development of P2Pool!''' % (args.donation_percentage,)
        else:
            print '''Donating %.1f%% of work towards P2Pool's development. Thank you!''' % (args.donation_percentage,)
            print 'You can increase this amount with --give-author argument! (or decrease it, if you must)'
        print
        
        
        if hasattr(signal, 'SIGALRM'):
            signal.signal(signal.SIGALRM, lambda signum, frame: reactor.callFromThread(
                sys.stderr.write, 'Watchdog timer went off at:\n' + ''.join(traceback.format_stack())
            ))
            signal.siginterrupt(signal.SIGALRM, False)
            task.LoopingCall(signal.alarm, 30).start(1)
        
        if args.irc_announce:
            from twisted.words.protocols import irc
            class IRCClient(irc.IRCClient):
                nickname = 'p2pool%02i' % (random.randrange(100),)
                channel = net.ANNOUNCE_CHANNEL
                def lineReceived(self, line):
                    if p2pool.DEBUG:
                        print repr(line)
                    irc.IRCClient.lineReceived(self, line)
                def signedOn(self):
                    self.in_channel = False
                    irc.IRCClient.signedOn(self)
                    self.factory.resetDelay()
                    self.join(self.channel)
                    @defer.inlineCallbacks
                    def new_share(share):
                        if not self.in_channel:
                            return
                        if share.pow_hash <= share.header['bits'].target and abs(share.timestamp - time.time()) < 10*60:
                            yield deferral.sleep(random.expovariate(1/60))
                            message = '\x02%s BLOCK FOUND by %s! %s%064x' % (net.NAME.upper(), bitcoin_data.script2_to_address(share.new_script, net.PARENT), net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash)
                            if all('%x' % (share.header_hash,) not in old_message for old_message in self.recent_messages):
                                self.say(self.channel, message)
                                self._remember_message(message)
                    self.watch_id = node.tracker.verified.added.watch(new_share)
                    self.recent_messages = []
                def joined(self, channel):
                    self.in_channel = True
                def left(self, channel):
                    self.in_channel = False
                def _remember_message(self, message):
                    self.recent_messages.append(message)
                    while len(self.recent_messages) > 100:
                        self.recent_messages.pop(0)
                def privmsg(self, user, channel, message):
                    if channel == self.channel:
                        self._remember_message(message)
                def connectionLost(self, reason):
                    node.tracker.verified.added.unwatch(self.watch_id)
                    print 'IRC connection lost:', reason.getErrorMessage()
            class IRCClientFactory(protocol.ReconnectingClientFactory):
                protocol = IRCClient
            reactor.connectTCP("irc.freenode.net", 6667, IRCClientFactory())
        
        @defer.inlineCallbacks
        def status_thread():
            last_str = None
            last_time = 0
            while True:
                yield deferral.sleep(3)
                try:
                    height = node.tracker.get_height(node.best_share_var.value)
                    this_str = 'P2Pool: %i shares in chain (%i verified/%i total) Peers: %i (%i incoming)' % (
                        height,
                        len(node.tracker.verified.items),
                        len(node.tracker.items),
                        len(node.p2p_node.peers),
                        sum(1 for peer in node.p2p_node.peers.itervalues() if peer.incoming),
                    ) + (' FDs: %i R/%i W' % (len(reactor.getReaders()), len(reactor.getWriters())) if p2pool.DEBUG else '')
                    
                    datums, dt = wb.local_rate_monitor.get_datums_in_last()
                    my_att_s = sum(datum['work']/dt for datum in datums)
                    this_str += '\n Local: %sH/s in last %s Local dead on arrival: %s Expected time to share: %s' % (
                        math.format(int(my_att_s)),
                        math.format_dt(dt),
                        math.format_binomial_conf(sum(1 for datum in datums if datum['dead']), len(datums), 0.95),
                        math.format_dt(2**256 / node.tracker.items[node.best_share_var.value].max_target / my_att_s) if my_att_s and node.best_share_var.value else '???',
                    )
                    
                    if height > 2:
                        (stale_orphan_shares, stale_doa_shares), shares, _ = wb.get_stale_counts()
                        stale_prop = p2pool_data.get_average_stale_prop(node.tracker, node.best_share_var.value, min(60*60//net.SHARE_PERIOD, height))
                        real_att_s = p2pool_data.get_pool_attempts_per_second(node.tracker, node.best_share_var.value, min(height - 1, 60*60//net.SHARE_PERIOD)) / (1 - stale_prop)
                        
                        this_str += '\n Shares: %i (%i orphan, %i dead) Stale rate: %s Efficiency: %s Current payout: %.4f %s' % (
                            shares, stale_orphan_shares, stale_doa_shares,
                            math.format_binomial_conf(stale_orphan_shares + stale_doa_shares, shares, 0.95),
                            math.format_binomial_conf(stale_orphan_shares + stale_doa_shares, shares, 0.95, lambda x: (1 - x)/(1 - stale_prop)),
                            node.get_current_txouts().get(bitcoin_data.pubkey_to_script2(my_pubkey), 0) * 1e-6, net.PARENT.SYMBOL,
                        )
                        this_str += '\n Pool: %sH/s Stale rate: %.1f%% Expected time to block: %s' % (
                            math.format(int(real_att_s)),
                            100*stale_prop,
                            math.format_dt(2**256 / node.bitcoind_work.value['bits'].target / real_att_s),
                        )
                        
                        for warning in p2pool_data.get_warnings(node.tracker, node.best_share_var.value, net, bitcoind_warning_var.value, node.bitcoind_work.value):
                            print >>sys.stderr, '#'*40
                            print >>sys.stderr, '>>> Warning: ' + warning
                            print >>sys.stderr, '#'*40
                        
                        if gc.garbage:
                            print '%i pieces of uncollectable cyclic garbage! Types: %r' % (len(gc.garbage), map(type, gc.garbage))
                    
                    if this_str != last_str or time.time() > last_time + 15:
                        print this_str
                        last_str = this_str
                        last_time = time.time()
                except:
                    log.err()
        status_thread()
    except:
        reactor.stop()
        log.err(None, 'Fatal error:')
Exemple #2
0
def main(args, net, datadir_path, merged_urls, worker_endpoint):
    try:
        print 'p2pool (version %s)' % (p2pool.__version__,)
        print
        
        @defer.inlineCallbacks
        def connect_p2p():
            # connect to bitcoind over bitcoin-p2p
            print '''Testing bitcoind P2P connection to '%s:%s'...''' % (args.bitcoind_address, args.bitcoind_p2p_port)
            factory = bitcoin_p2p.ClientFactory(net.PARENT)
            reactor.connectTCP(args.bitcoind_address, args.bitcoind_p2p_port, factory)
            def long():
                print '''    ...taking a while. Common reasons for this include all of bitcoind's connection slots being used...'''
            long_dc = reactor.callLater(5, long)
            yield factory.getProtocol() # waits until handshake is successful
            if not long_dc.called: long_dc.cancel()
            print '    ...success!'
            print
            defer.returnValue(factory)
        
        if args.testnet: # establish p2p connection first if testnet so bitcoind can work without connections
            factory = yield connect_p2p()
        
        # connect to bitcoind over JSON-RPC and do initial getmemorypool
        url = '%s://%s:%i/' % ('https' if args.bitcoind_rpc_ssl else 'http', args.bitcoind_address, args.bitcoind_rpc_port)
        print '''Testing bitcoind RPC connection to '%s' with username '%s'...''' % (url, args.bitcoind_rpc_username)
        bitcoind = jsonrpc.HTTPProxy(url, dict(Authorization='Basic ' + base64.b64encode(args.bitcoind_rpc_username + ':' + args.bitcoind_rpc_password)), timeout=30)
        yield helper.check(bitcoind, net, args)
        temp_work = yield helper.getwork(bitcoind)
        
        bitcoind_getinfo_var = variable.Variable(None)
        @defer.inlineCallbacks
        def poll_warnings():
            bitcoind_getinfo_var.set((yield deferral.retry('Error while calling getinfo:')(bitcoind.rpc_getnetworkinfo)()))
        yield poll_warnings()
        deferral.RobustLoopingCall(poll_warnings).start(20*60)
        
        print '    ...success!'
        print '    Current block hash: %x' % (temp_work['previous_block'],)
        print '    Current block height: %i' % (temp_work['height'] - 1,)
        print
        
        if not args.testnet:
            factory = yield connect_p2p()
        
        print 'Determining payout address...'
        pubkeys = keypool()
        if args.pubkey_hash is None and args.address != 'dynamic':
            address_path = os.path.join(datadir_path, 'cached_payout_address')
            
            if os.path.exists(address_path):
                with open(address_path, 'rb') as f:
                    address = f.read().strip('\r\n')
                print '    Loaded cached address: %s...' % (address,)
            else:
                address = None
            
            if address is not None:
                try:
                    res = yield deferral.retry('Error validating cached address:', 5, 1)(lambda: bitcoind.rpc_getaddressinfo(address))()# rpc_validateaddress(address))()
                    if not res['ismine']:
                        print '    Cached address is invalid!'
                        address = None
                    else:
                    # checks address controlled by local bitcoind
                        res = yield deferral.retry('Error validating cached address:', 5)(lambda: bitcoind.rpc_getaddressinfo(address))()
                        if not res['ismine']:
                            print '    Cached address is not controlled by local bitcoind!'
                            address = None
                        # validateaddress DEPRECATION WARNING: Parts of this command have been deprecated and moved to getaddressinfo.
                        # Clients must transition to using getaddressinfo to access this information before upgrading to v0.18.
                        # The following deprecated fields have moved to getaddressinfo and will only be shown here with 
                        # -deprecatedrpc=validateaddress: ismine, iswatchonly, script, hex, pubkeys, sigsrequired, pubkey, addresses, embedded, iscompressed, account, timestamp, hdkeypath, kdmasterkeyid.
                except Exception:
                    print '    Cached address is invalid!'
                    address = None
                                
            if address is None:
                print "    Getting payout address from bitcoind labeled \'p2pool\'..."
                # you should assign label 'p2pool' to Your wallet mining address before
                try:
                    address = yield deferral.retry('Error getting payout address from bitcoind:', 5, 1)(lambda: bitcoind.rpc_getaccountaddress('p2pool'))()
                except Exception:
                    print u"\u001b[31m   Bitcoind has no \'p2pool\' labeled address! Please, specify default payout address! Exiting...\u001b[0m"
                    exit()

                print "    Overwriting cached address from bitcoind \'p2pool\' labeled address"
                with open(address_path, 'wb') as f:
                    f.write(address)

            my_address = address
            print(u'\u001b[32m    ...success! Payout address: %s\u001b[0m' % my_address)
            print()

            pubkeys.addkey({'address': my_address})
        elif args.address != 'dynamic':
            my_address = args.address
            print('    ...success! Payout address: %s' % my_address)
            print()

            pubkeys.addkey({'address': my_address})
        else:
            print '    Entering dynamic address mode.'

            if args.numaddresses < 2:
                print ' ERROR: Can not use fewer than 2 addresses in dynamic mode. Resetting to 2.'
                args.numaddresses = 2
            for i in range(args.numaddresses):
                address = yield deferral.retry('Error getting a dynamic address from bitcoind:', 5)(lambda: bitcoind.rpc_getnewaddress('p2pool'))()
                pubkeys.addkey({'address': address})

            pubkeys.updatestamp(time.time())

            my_address = pubkeys.keys[0]['address']

            for i in range(len(pubkeys.keys)):
                print('    ...payout %d: %s' % (i, pubkeys[i]['address']))
        
        print "Loading shares..."
        shares = {}
        known_verified = set()
        def share_cb(share):
            share.time_seen = 0 # XXX
            shares[share.hash] = share
            if len(shares) % 1000 == 0 and shares:
                print "    %i" % (len(shares),)
        ss = p2pool_data.ShareStore(os.path.join(datadir_path, 'shares.'), net, share_cb, known_verified.add)
        print "    ...done loading %i shares (%i verified)!" % (len(shares), len(known_verified))
        print
        
        
        print 'Initializing work...'
        
        global gnode
        gnode = node = p2pool_node.Node(factory, bitcoind, shares.values(), known_verified, net)
        yield node.start()
        
        for share_hash in shares:
            if share_hash not in node.tracker.items:
                ss.forget_share(share_hash)
        for share_hash in known_verified:
            if share_hash not in node.tracker.verified.items:
                ss.forget_verified_share(share_hash)
        node.tracker.removed.watch(lambda share: ss.forget_share(share.hash))
        node.tracker.verified.removed.watch(lambda share: ss.forget_verified_share(share.hash))
        
        def save_shares():
            for share in node.tracker.get_chain(node.best_share_var.value, min(node.tracker.get_height(node.best_share_var.value), 2*net.CHAIN_LENGTH)):
                ss.add_share(share)
                if share.hash in node.tracker.verified.items:
                    ss.add_verified_hash(share.hash)
        deferral.RobustLoopingCall(save_shares).start(60)

        if len(shares) > net.CHAIN_LENGTH:
            best_share = shares[node.best_share_var.value]
            previous_share = shares[best_share.share_data['previous_share_hash']]
            counts = p2pool_data.get_desired_version_counts(node.tracker, node.tracker.get_nth_parent_hash(previous_share.hash, net.CHAIN_LENGTH*9//10), net.CHAIN_LENGTH//10)
            p2pool_data.update_min_protocol_version(counts, best_share)
        
        print '    ...success!'
        print
        
        
        print 'Joining p2pool network using port %i...' % (args.p2pool_port,)
        
        @defer.inlineCallbacks
        def parse(host):
            port = net.P2P_PORT
            if ':' in host:
                host, port_str = host.split(':')
                port = int(port_str)
            defer.returnValue(((yield reactor.resolve(host)), port))
        
        addrs = {}
        if os.path.exists(os.path.join(datadir_path, 'addrs')):
            try:
                with open(os.path.join(datadir_path, 'addrs'), 'rb') as f:
                    addrs.update(dict((tuple(k), v) for k, v in json.loads(f.read())))
            except:
                print >>sys.stderr, 'error parsing addrs'
        for addr_df in map(parse, net.BOOTSTRAP_ADDRS):
            try:
                addr = yield addr_df
                if addr not in addrs:
                    addrs[addr] = (0, time.time(), time.time())
            except:
                log.err()
        
        connect_addrs = set()
        for addr_df in map(parse, args.p2pool_nodes):
            try:
                connect_addrs.add((yield addr_df))
            except:
                log.err()
        
        node.p2p_node = p2pool_node.P2PNode(node,
            port=args.p2pool_port,
            max_incoming_conns=args.p2pool_conns,
            addr_store=addrs,
            connect_addrs=connect_addrs,
            desired_outgoing_conns=args.p2pool_outgoing_conns,
            advertise_ip=args.advertise_ip,
            external_ip=args.p2pool_external_ip,
        )
        node.p2p_node.start()
        
        def save_addrs():
            with open(os.path.join(datadir_path, 'addrs'), 'wb') as f:
                f.write(json.dumps(node.p2p_node.addr_store.items()))
        deferral.RobustLoopingCall(save_addrs).start(60)
        
        print '    ...success!'
        print
        
        if args.upnp:
            @defer.inlineCallbacks
            def upnp_thread():
                while True:
                    try:
                        is_lan, lan_ip = yield ipdiscover.get_local_ip()
                        if is_lan:
                            pm = yield portmapper.get_port_mapper()
                            yield pm._upnp.add_port_mapping(lan_ip, args.p2pool_port, args.p2pool_port, 'p2pool', 'TCP')
                    except defer.TimeoutError:
                        pass
                    except:
                        if p2pool.DEBUG:
                            log.err(None, 'UPnP error:')
                    yield deferral.sleep(random.expovariate(1/120))
            upnp_thread()
        
        # start listening for workers with a JSON-RPC server
        
        print 'Listening for workers on %r port %i...' % (worker_endpoint[0], worker_endpoint[1])
        
        wb = work.WorkerBridge(node, my_address, args.donation_percentage,
                               merged_urls, args.worker_fee, args, pubkeys,
                               bitcoind, args.share_rate)
        web_root = web.get_web_root(wb, datadir_path, bitcoind_getinfo_var, static_dir=args.web_static)
        caching_wb = worker_interface.CachingWorkerBridge(wb)
        worker_interface.WorkerInterface(caching_wb).attach_to(web_root, get_handler=lambda request: request.redirect('/static/'))
        web_serverfactory = server.Site(web_root)
        
        serverfactory = switchprotocol.FirstByteSwitchFactory({'{': stratum.StratumServerFactory(caching_wb)}, web_serverfactory)
        deferral.retry('Error binding to worker port:', traceback=False)(reactor.listenTCP)(worker_endpoint[1], serverfactory, interface=worker_endpoint[0])
        
        with open(os.path.join(os.path.join(datadir_path, 'ready_flag')), 'wb') as f:
            pass
        
        print '    ...success!'
        print
        
        
        # done!
        print 'Started successfully!'
        print 'Go to http://127.0.0.1:%i/ to view graphs and statistics!' % (worker_endpoint[1],)
        if args.donation_percentage > 1.1:
            print '''Donating %.1f%% of work towards P2Pool's development. Thanks for the tip!''' % (args.donation_percentage,)
        elif args.donation_percentage < .9:
            print '''Donating %.1f%% of work towards P2Pool's development. Please donate to encourage further development of P2Pool!''' % (args.donation_percentage,)
        else:
            print '''Donating %.1f%% of work towards P2Pool's development. Thank you!''' % (args.donation_percentage,)
            print 'You can increase this amount with --give-author argument! (or decrease it, if you must)'
        print
        
        
        if hasattr(signal, 'SIGALRM'):
            signal.signal(signal.SIGALRM, lambda signum, frame: reactor.callFromThread(
                sys.stderr.write, 'Watchdog timer went off at:\n' + ''.join(traceback.format_stack())
            ))
            signal.siginterrupt(signal.SIGALRM, False)
            deferral.RobustLoopingCall(signal.alarm, 30).start(1)
        
        if args.irc_announce:
            from twisted.words.protocols import irc
            class IRCClient(irc.IRCClient):
                nickname = 'p2pool%02i' % (random.randrange(100),)
                channel = net.ANNOUNCE_CHANNEL
                def lineReceived(self, line):
                    if p2pool.DEBUG:
                        print repr(line)
                    irc.IRCClient.lineReceived(self, line)
                def signedOn(self):
                    self.in_channel = False
                    irc.IRCClient.signedOn(self)
                    self.factory.resetDelay()
                    self.join(self.channel)
                    @defer.inlineCallbacks
                    def new_share(share):
                        if not self.in_channel:
                            return
                        if share.pow_hash <= share.header['bits'].target and abs(share.timestamp - time.time()) < 10*60:
                            yield deferral.sleep(random.expovariate(1/60))
                            message = '\x02%s BLOCK FOUND by %s! %s%064x' % (
                                    net.NAME.upper(),
                                    bitcoin_data.script2_to_address(
                                        share.new_script, net.ADDRESS_VERSION,
                                        -1, net.PARENT) if
                                            share.VERSION < 34 else
                                                share.address,
                                    net.PARENT.BLOCK_EXPLORER_URL_PREFIX,
                                    share.header_hash)
                            if all('%x' % (share.header_hash,) not in old_message for old_message in self.recent_messages):
                                self.say(self.channel, message)
                                self._remember_message(message)
                    self.watch_id = node.tracker.verified.added.watch(new_share)
                    self.recent_messages = []
                def joined(self, channel):
                    self.in_channel = True
                def left(self, channel):
                    self.in_channel = False
                def _remember_message(self, message):
                    self.recent_messages.append(message)
                    while len(self.recent_messages) > 100:
                        self.recent_messages.pop(0)
                def privmsg(self, user, channel, message):
                    if channel == self.channel:
                        self._remember_message(message)
                def connectionLost(self, reason):
                    node.tracker.verified.added.unwatch(self.watch_id)
                    print 'IRC connection lost:', reason.getErrorMessage()
            class IRCClientFactory(protocol.ReconnectingClientFactory):
                protocol = IRCClient
            reactor.connectTCP("irc.freenode.net", 6667, IRCClientFactory(), bindAddress=(worker_endpoint[0], 0))
        
        @defer.inlineCallbacks
        def status_thread():
            last_str = None
            last_time = 0
            while True:
                yield deferral.sleep(30)
                try:
                    height = node.tracker.get_height(node.best_share_var.value)
                    this_str = 'P2Pool: %i shares in chain (%i verified/%i total) Peers: %i (%i incoming)' % (
                        height,
                        len(node.tracker.verified.items),
                        len(node.tracker.items),
                        len(node.p2p_node.peers),
                        sum(1 for peer in node.p2p_node.peers.itervalues() if peer.incoming),
                    ) + (' FDs: %i R/%i W' % (len(reactor.getReaders()), len(reactor.getWriters())) if p2pool.DEBUG else '')
                    
                    datums, dt = wb.local_rate_monitor.get_datums_in_last()
                    my_att_s = sum(datum['work']/dt for datum in datums)
                    my_shares_per_s = sum(datum['work']/dt/bitcoin_data.target_to_average_attempts(datum['share_target']) for datum in datums)
                    this_str += '\n Local: %sH/s in last %s Local dead on arrival: %s Expected time to share: %s' % (
                        math.format(int(my_att_s)),
                        math.format_dt(dt),
                        math.format_binomial_conf(sum(1 for datum in datums if datum['dead']), len(datums), 0.95),
                        math.format_dt(1/my_shares_per_s) if my_shares_per_s else '???',
                    )
                    
                    if height > 2:
                        (stale_orphan_shares, stale_doa_shares), shares, _ = wb.get_stale_counts()
                        stale_prop = p2pool_data.get_average_stale_prop(node.tracker, node.best_share_var.value, min(60*60//net.SHARE_PERIOD, height))
                        real_att_s = p2pool_data.get_pool_attempts_per_second(node.tracker, node.best_share_var.value, min(height - 1, 60*60//net.SHARE_PERIOD)) / (1 - stale_prop)
                        
                        paystr = ''
                        paytot = 0.0
                        for i in range(len(pubkeys.keys)):
                            curtot = node.get_current_txouts().get(
                                    pubkeys.keys[i]['address'], 0)
                            paytot += curtot*1e-8
                            paystr += "(%.4f)" % (curtot*1e-8,)
                        paystr += "=%.4f" % (paytot,)
                        this_str += '\n Shares: %i (%i orphan, %i dead) Stale rate: %s Efficiency: %s Current payout: %s %s' % (
                            shares, stale_orphan_shares, stale_doa_shares,
                            math.format_binomial_conf(stale_orphan_shares + stale_doa_shares, shares, 0.95),
                            math.format_binomial_conf(stale_orphan_shares + stale_doa_shares, shares, 0.95, lambda x: (1 - x)/(1 - stale_prop)),
                            paystr, net.PARENT.SYMBOL,
                        )
                        this_str += '\n Pool: %sH/s Stale rate: %.1f%% Expected time to block: %s' % (
                            math.format(int(real_att_s)),
                            100*stale_prop,
                            math.format_dt(2**256 / node.bitcoind_work.value['bits'].target / real_att_s),
                        )
                        
                        for warning in p2pool_data.get_warnings(node.tracker, node.best_share_var.value, net, bitcoind_getinfo_var.value, node.bitcoind_work.value):
                            print >>sys.stderr, '#'*40
                            print >>sys.stderr, '>>> Warning: ' + warning
                            print >>sys.stderr, '#'*40
                        
                        if gc.garbage:
                            print '%i pieces of uncollectable cyclic garbage! Types: %r' % (len(gc.garbage), map(type, gc.garbage))
                    
                    if this_str != last_str or time.time() > last_time + 15:
                        print this_str
                        last_str = this_str
                        last_time = time.time()
                except:
                    log.err()
        status_thread()
    except:
        reactor.stop()
        log.err(None, 'Fatal error:')
Exemple #3
0
def main(args, net, datadir_path, worker_endpoint):
    try:
        print 'p2pool (version %s)' % (p2pool.__version__, )
        print

        @defer.inlineCallbacks
        def connect_p2p():  # TODO: fix unknown type no type for 'getminings'
            # connect to dcrd over decred-p2p
            print '''Testing dcrd P2P connection to '%s:%s'...''' % (
                args.dcrd_address, args.dcrd_p2p_port)
            factory = decred_p2p.ClientFactory(net.PARENT)
            reactor.connectTCP(args.dcrd_address, args.dcrd_p2p_port, factory)

            def long():
                print '''    ...taking a while. Common reasons for this include all of dcrd's connection slots being used...'''

            long_dc = reactor.callLater(5, long)
            yield factory.getProtocol()  # waits until handshake is successful
            if not long_dc.called: long_dc.cancel()
            print '    ...success!'
            print
            defer.returnValue(factory)

        if args.testnet:  # establish p2p connection first if testnet so dcrd can work without connections
            factory = yield connect_p2p()

        # connect to dcrd over JSON-RPC and do initial getmemorypool
        url = '%s://%s:%i/' % ('http' if args.dcrd_no_rpc_ssl else 'https',
                               args.dcrd_address, args.dcrd_rpc_port)
        print '''Testing dcrd RPC connection to '%s' with username '%s'...''' % (
            url, args.dcrd_rpc_username)
        dcrd = jsonrpc.HTTPProxy(
            url,
            dict(Authorization='Basic ' +
                 base64.b64encode(args.dcrd_rpc_username + ':' +
                                  args.dcrd_rpc_password)),
            timeout=30)
        yield helper.check(dcrd, net)

        temp_work = yield helper.getwork(dcrd)

        dcrd_getinfo_var = variable.Variable(None)

        @defer.inlineCallbacks
        def poll_warnings():
            dcrd_getinfo_var.set(
                (yield deferral.retry('Error while calling getinfo:')(
                    dcrd.rpc_getinfo)()))

        yield poll_warnings()
        deferral.RobustLoopingCall(poll_warnings).start(20 * 60)

        print '    ...success!'
        print '    Current block hash: {0:x}'.format(
            temp_work['previous_block'])
        print '    Current block height: {}'.format(temp_work['height'] - 1)
        print

        if not args.testnet:
            factory = yield connect_p2p()

        if args.pubkey_hash is None:
            #
            # Connect to locally running dcrwallet instance
            #
            # - TODO: find out if we use any other wallet api's apart from the 'getaccountaddress' below.
            #   If not we can:
            #    - make this self contained then remove the connection
            #      OR
            #    - remove all wallet connection logic and demand an address on the commandline
            #
            # wallet separate
            # 9111/19111
            walleturl = '%s://%s:%i/' % ('http' if args.dcrd_no_rpc_ssl else
                                         'https', args.dcrd_address,
                                         args.dcrd_rpc_wallet_port)
            print '''Testing dcrdwallet RPC connection to '%s' with username '%s'...''' % (
                walleturl, args.dcrd_rpc_username)
            dcrwallet = jsonrpc.HTTPProxy(
                walleturl,
                dict(Authorization='Basic ' +
                     base64.b64encode(args.dcrd_rpc_username + ':' +
                                      args.dcrd_rpc_password)),
                timeout=30)
            wallet_online = yield helper.checkwallet(dcrwallet, net)

        print 'Determining payout address...'
        pubkeys = keypool()
        if args.pubkey_hash is None:
            address_path = os.path.join(datadir_path, 'cached_payout_address')

            if os.path.exists(address_path):
                with open(address_path, 'rb') as f:
                    address = f.read().strip('\r\n')
                print '    Loaded cached address: %s...' % (address, )
            else:
                address = None

            if address is None and wallet_online:
                print '    Getting payout address from local dcrwallet...not a great idea...'
                address = yield deferral.retry('Error getting payout address from local dcrwallet:', 5) \
                                                (lambda: dcrwallet.rpc_getaccountaddress('default'))()
                with open(address_path, 'wb') as f:
                    f.write(address)

            if address is None:
                raise Exception('Failed to get an address from local wallet')

            my_pubkey_hash = decred_addr.address_to_pubkey_hash(
                address, net.PARENT)

            pubkeys.addkey(my_pubkey_hash)

            print '    ...success! Payout address:', address, 'pubkey hash', my_pubkey_hash
            print

        else:
            my_pubkey_hash = args.pubkey_hash
            print '    ...success! Payout address:', decred_addr.pubkey_hash_to_address(
                my_pubkey_hash, net.PARENT)
            print
            pubkeys.addkey(my_pubkey_hash)

        print "Loading shares..."
        shares = {}
        known_verified = set()

        def share_cb(share):
            share.time_seen = 0  # XXX
            shares[share.hash] = share
            if len(shares) % 1000 == 0 and shares:
                print "    %i" % (len(shares), )

        ss = p2pool_data.ShareStore(os.path.join(datadir_path, 'shares.'), net,
                                    share_cb, known_verified.add)
        print "    ...done loading %i shares (%i verified)!" % (
            len(shares), len(known_verified))
        print

        print 'Initializing work...'

        node = p2pool_node.Node(factory, dcrd, shares.values(), known_verified,
                                net)
        yield node.start()

        for share_hash in shares:
            if share_hash not in node.tracker.items:
                ss.forget_share(share_hash)
        for share_hash in known_verified:
            if share_hash not in node.tracker.verified.items:
                ss.forget_verified_share(share_hash)
        node.tracker.removed.watch(lambda share: ss.forget_share(share.hash))
        node.tracker.verified.removed.watch(
            lambda share: ss.forget_verified_share(share.hash))

        def save_shares():
            for share in node.tracker.get_chain(
                    node.best_share_var.value,
                    min(node.tracker.get_height(node.best_share_var.value),
                        2 * net.CHAIN_LENGTH)):
                ss.add_share(share)
                if share.hash in node.tracker.verified.items:
                    ss.add_verified_hash(share.hash)

        deferral.RobustLoopingCall(save_shares).start(60)

        print '    ...success!'
        print

        print 'Joining p2pool network using port %i...' % (args.p2pool_port, )

        @defer.inlineCallbacks
        def parse(host):
            port = net.P2P_PORT
            if ':' in host:
                host, port_str = host.split(':')
                port = int(port_str)
            defer.returnValue(((yield reactor.resolve(host)), port))

        addrs = {}
        if os.path.exists(os.path.join(datadir_path, 'addrs')):
            try:
                with open(os.path.join(datadir_path, 'addrs'), 'rb') as f:
                    addrs.update(
                        dict((tuple(k), v) for k, v in json.loads(f.read())))
            except:
                print >> sys.stderr, 'error parsing addrs'
        for addr_df in map(parse, net.BOOTSTRAP_ADDRS):
            try:
                addr = yield addr_df
                if addr not in addrs:
                    addrs[addr] = (0, time.time(), time.time())
            except:
                log.err()

        connect_addrs = set()
        for addr_df in map(parse, args.p2pool_nodes):
            try:
                connect_addrs.add((yield addr_df))
            except:
                log.err()

        node.p2p_node = p2pool_node.P2PNode(
            node,
            port=args.p2pool_port,
            max_incoming_conns=args.p2pool_conns,
            addr_store=addrs,
            connect_addrs=connect_addrs,
            desired_outgoing_conns=args.p2pool_outgoing_conns,
            advertise_ip=args.advertise_ip,
            external_ip=args.p2pool_external_ip,
        )
        node.p2p_node.start()

        def save_addrs():
            with open(os.path.join(datadir_path, 'addrs'), 'wb') as f:
                f.write(json.dumps(node.p2p_node.addr_store.items()))

        deferral.RobustLoopingCall(save_addrs).start(60)

        print '    ...success!'
        print

        if args.upnp:

            @defer.inlineCallbacks
            def upnp_thread():
                while True:
                    try:
                        is_lan, lan_ip = yield ipdiscover.get_local_ip()
                        if is_lan:
                            pm = yield portmapper.get_port_mapper()
                            yield pm._upnp.add_port_mapping(
                                lan_ip, args.p2pool_port, args.p2pool_port,
                                'p2pool', 'TCP')
                    except defer.TimeoutError:
                        pass
                    except:
                        if p2pool.DEBUG:
                            log.err(None, 'UPnP error:')
                    yield deferral.sleep(random.expovariate(1 / 120))

            upnp_thread()

        # start listening for workers with a JSON-RPC server

        print 'Listening for workers on %r port %i...' % (worker_endpoint[0],
                                                          worker_endpoint[1])

        wb = work.WorkerBridge(node, my_pubkey_hash, args.donation_percentage,
                               args.worker_fee, args, pubkeys, dcrd)
        web_root = web.get_web_root(wb, datadir_path, dcrd_getinfo_var)
        caching_wb = worker_interface.CachingWorkerBridge(wb)
        worker_interface.WorkerInterface(caching_wb).attach_to(
            web_root, get_handler=lambda request: request.redirect('/static/'))
        web_serverfactory = server.Site(web_root)

        serverfactory = switchprotocol.FirstByteSwitchFactory(
            {'{': stratum.StratumServerFactory(caching_wb)}, web_serverfactory)
        deferral.retry('Error binding to worker port:', traceback=False)(
            reactor.listenTCP)(worker_endpoint[1],
                               serverfactory,
                               interface=worker_endpoint[0])

        with open(os.path.join(os.path.join(datadir_path, 'ready_flag')),
                  'wb') as f:
            pass

        print '    ...success!'
        print

        # done!
        print 'Started successfully!'
        print 'Go to http://127.0.0.1:%i/ to view graphs and statistics!' % (
            worker_endpoint[1], )
        if args.donation_percentage > 1.1:
            print '''Donating %.1f%% of work towards P2Pool's development. Thanks for the tip!''' % (
                args.donation_percentage, )
        elif args.donation_percentage < .9:
            print '''Donating %.1f%% of work towards P2Pool's development. Please donate to encourage further development of P2Pool!''' % (
                args.donation_percentage, )
        else:
            print '''Donating %.1f%% of work towards P2Pool's development. Thank you!''' % (
                args.donation_percentage, )
            print 'You can increase this amount with --give-author argument! (or decrease it, if you must)'
        print

        if hasattr(signal, 'SIGALRM'):
            signal.signal(
                signal.SIGALRM, lambda signum, frame: reactor.callFromThread(
                    sys.stderr.write, 'Watchdog timer went off at:\n' + ''.
                    join(traceback.format_stack())))
            signal.siginterrupt(signal.SIGALRM, False)
            deferral.RobustLoopingCall(signal.alarm, 30).start(1)


#         if args.irc_announce:
#             from twisted.words.protocols import irc
#             class IRCClient(irc.IRCClient):
#                 nickname = 'p2pool%02i' % (random.randrange(100),)
#                 channel = net.ANNOUNCE_CHANNEL
#                 def lineReceived(self, line):
#                     if p2pool.DEBUG:
#                         print repr(line)
#                     irc.IRCClient.lineReceived(self, line)
#                 def signedOn(self):
#                     self.in_channel = False
#                     irc.IRCClient.signedOn(self)
#                     self.factory.resetDelay()
#                     self.join(self.channel)
#                     @defer.inlineCallbacks
#                     def new_share(share):
#                         if not self.in_channel:
#                             return
#                         if share.pow_hash <= share.header['bits'].target and abs(share.timestamp - time.time()) < 10*60:
#                             yield deferral.sleep(random.expovariate(1/60))
#                             message = '\x02%s BLOCK FOUND by %s! %s%064x' % (net.NAME.upper(), decred_addr.script2_to_address(share.new_script, net.PARENT), net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash)
#                             if all('%x' % (share.header_hash,) not in old_message for old_message in self.recent_messages):
#                                 self.say(self.channel, message)
#                                 self._remember_message(message)
#                     self.watch_id = node.tracker.verified.added.watch(new_share)
#                     self.recent_messages = []
#                 def joined(self, channel):
#                     self.in_channel = True
#                 def left(self, channel):
#                     self.in_channel = False
#                 def _remember_message(self, message):
#                     self.recent_messages.append(message)
#                     while len(self.recent_messages) > 100:
#                         self.recent_messages.pop(0)
#                 def privmsg(self, user, channel, message):
#                     if channel == self.channel:
#                         self._remember_message(message)
#                 def connectionLost(self, reason):
#                     node.tracker.verified.added.unwatch(self.watch_id)
#                     print 'IRC connection lost:', reason.getErrorMessage()
#             class IRCClientFactory(protocol.ReconnectingClientFactory):
#                 protocol = IRCClient
#             reactor.connectTCP("irc.freenode.net", 6667, IRCClientFactory(), bindAddress=(worker_endpoint[0], 0))

        @defer.inlineCallbacks
        def status_thread():
            last_str = None
            last_time = 0
            while True:
                yield deferral.sleep(3)
                try:
                    height = node.tracker.get_height(node.best_share_var.value)
                    this_str = 'P2Pool: %i shares in chain (%i verified/%i total) Peers: %i (%i incoming)' % (
                        height,
                        len(node.tracker.verified.items),
                        len(node.tracker.items),
                        len(node.p2p_node.peers),
                        sum(1 for peer in node.p2p_node.peers.itervalues()
                            if peer.incoming),
                    ) + (' FDs: %i R/%i W' %
                         (len(reactor.getReaders()), len(reactor.getWriters()))
                         if p2pool.DEBUG else '')

                    datums, dt = wb.local_rate_monitor.get_datums_in_last()
                    my_att_s = sum(datum['work'] / dt for datum in datums)
                    my_shares_per_s = sum(
                        datum['work'] / dt /
                        decred_data.target_to_average_attempts(
                            datum['share_target']) for datum in datums)
                    this_str += '\n Local: %sH/s in last %s Local dead on arrival: %s Expected time to share: %s' % (
                        math.format(int(my_att_s)),
                        math.format_dt(dt),
                        math.format_binomial_conf(
                            sum(1 for datum in datums if datum['dead']),
                            len(datums), 0.95),
                        math.format_dt(1 / my_shares_per_s)
                        if my_shares_per_s else '???',
                    )

                    if height > 2:
                        (stale_orphan_shares,
                         stale_doa_shares), shares, _ = wb.get_stale_counts()
                        stale_prop = p2pool_data.get_average_stale_prop(
                            node.tracker, node.best_share_var.value,
                            min(60 * 60 // net.SHARE_PERIOD, height))
                        real_att_s = p2pool_data.get_pool_attempts_per_second(
                            node.tracker, node.best_share_var.value,
                            min(height - 1, 60 * 60 //
                                net.SHARE_PERIOD)) / (1 - stale_prop)

                        paystr = ''
                        paytot = 0.0
                        for i in range(len(pubkeys.keys)):
                            curtot = node.get_current_txouts().get(
                                decred_addr.pubkey_hash_to_script2(
                                    pubkeys.keys[i]), 0)
                            paytot += curtot * 1e-8
                            paystr += "(%.4f)" % (curtot * 1e-8, )
                        paystr += "=%.4f" % (paytot, )
                        this_str += '\n Shares: %i (%i orphan, %i dead) Stale rate: %s Efficiency: %s Current payout: %s %s' % (
                            shares,
                            stale_orphan_shares,
                            stale_doa_shares,
                            math.format_binomial_conf(
                                stale_orphan_shares + stale_doa_shares, shares,
                                0.95),
                            math.format_binomial_conf(
                                stale_orphan_shares + stale_doa_shares, shares,
                                0.95, lambda x: (1 - x) / (1 - stale_prop)),
                            paystr,
                            net.PARENT.SYMBOL,
                        )
                        this_str += '\n Pool: %sH/s Stale rate: %.1f%% Expected time to block: %s' % (
                            math.format(int(real_att_s)),
                            100 * stale_prop,
                            math.format_dt(
                                2**256 / node.dcrd_work.value['bits'].target /
                                real_att_s),
                        )

                        for warning in p2pool_data.get_warnings(
                                node.tracker, node.best_share_var.value, net,
                                dcrd_getinfo_var.value, node.dcrd_work.value):
                            print >> sys.stderr, '#' * 40
                            print >> sys.stderr, '>>> Warning: ' + warning
                            print >> sys.stderr, '#' * 40

                        if gc.garbage:
                            print '%i pieces of uncollectable cyclic garbage! Types: %r' % (
                                len(gc.garbage), map(type, gc.garbage))

                    if this_str != last_str or time.time() > last_time + 15:
                        print this_str
                        last_str = this_str
                        last_time = time.time()
                except:
                    log.err()

        status_thread()
    except:
        reactor.stop()
        log.err(None, 'Fatal error:')
Exemple #4
0
    def __init__(self, node, my_pubkey_hash, donation_percentage, merged_urls,
                 worker_fee, args, pubkeys, dashd):
        worker_interface.WorkerBridge.__init__(self)
        self.recent_shares_ts_work = []

        self.node = node

        self.dashd = dashd
        self.pubkeys = pubkeys
        self.args = args
        self.my_pubkey_hash = my_pubkey_hash

        self.donation_percentage = args.donation_percentage
        self.worker_fee = args.worker_fee

        self.net = self.node.net.PARENT
        self.running = True
        self.pseudoshare_received = variable.Event()
        self.share_received = variable.Event()
        self.local_rate_monitor = math.RateMonitor(10 * 60)
        self.local_addr_rate_monitor = math.RateMonitor(10 * 60)

        self.removed_unstales_var = variable.Variable((0, 0, 0))
        self.removed_doa_unstales_var = variable.Variable(0)

        self.last_work_shares = variable.Variable({})

        self.my_share_hashes = set()
        self.my_doa_share_hashes = set()

        self.address_throttle = 0

        self.tracker_view = forest.TrackerView(
            self.node.tracker,
            forest.get_attributedelta_type(
                dict(
                    forest.AttributeDelta.attrs,
                    my_count=lambda share: 1
                    if share.hash in self.my_share_hashes else 0,
                    my_doa_count=lambda share: 1
                    if share.hash in self.my_doa_share_hashes else 0,
                    my_orphan_announce_count=lambda share: 1
                    if share.hash in self.my_share_hashes and share.share_data[
                        'stale_info'] == 'orphan' else 0,
                    my_dead_announce_count=lambda share: 1
                    if share.hash in self.my_share_hashes and share.share_data[
                        'stale_info'] == 'doa' else 0,
                )))

        @self.node.tracker.verified.removed.watch
        def _(share):
            if share.hash in self.my_share_hashes and self.node.tracker.is_child_of(
                    share.hash, self.node.best_share_var.value):
                assert share.share_data['stale_info'] in [
                    None, 'orphan', 'doa'
                ]  # we made these shares in this instance
                self.removed_unstales_var.set((
                    self.removed_unstales_var.value[0] + 1,
                    self.removed_unstales_var.value[1] +
                    (1 if share.share_data['stale_info'] == 'orphan' else 0),
                    self.removed_unstales_var.value[2] +
                    (1 if share.share_data['stale_info'] == 'doa' else 0),
                ))
            if share.hash in self.my_doa_share_hashes and self.node.tracker.is_child_of(
                    share.hash, self.node.best_share_var.value):
                self.removed_doa_unstales_var.set(
                    self.removed_doa_unstales_var.value + 1)

        # MERGED WORK

        self.merged_work = variable.Variable({})

        @defer.inlineCallbacks
        def set_merged_work(merged_url, merged_userpass):
            merged_proxy = jsonrpc.HTTPProxy(
                merged_url,
                dict(Authorization='Basic ' +
                     base64.b64encode(merged_userpass)))
            while self.running:
                auxblock = yield deferral.retry(
                    'Error while calling merged getauxblock on %s:' %
                    (merged_url, ), 30)(merged_proxy.rpc_getauxblock)()
                self.merged_work.set(
                    math.merge_dicts(
                        self.merged_work.value, {
                            auxblock['chainid']:
                            dict(
                                hash=int(auxblock['hash'], 16),
                                target='p2pool' if auxblock['target']
                                == 'p2pool' else pack.IntType(256).unpack(
                                    auxblock['target'].decode('hex')),
                                merged_proxy=merged_proxy,
                            )
                        }))
                yield deferral.sleep(1)

        for merged_url, merged_userpass in merged_urls:
            set_merged_work(merged_url, merged_userpass)

        @self.merged_work.changed.watch
        def _(new_merged_work):
            print 'Got new merged mining work!'

        # COMBINE WORK

        self.current_work = variable.Variable(None)

        def compute_work():
            t = self.node.dashd_work.value
            bb = self.node.best_block_header.value
            if bb is not None and bb['previous_block'] == t[
                    'previous_block'] and self.node.net.PARENT.POW_FUNC(
                        dash_data.block_header_type.pack(
                            bb)) <= t['bits'].target:
                print 'Skipping from block %x to block %x! NewHeight=%s' % (
                    bb['previous_block'],
                    self.node.net.PARENT.BLOCKHASH_FUNC(
                        dash_data.block_header_type.pack(bb)),
                    t['height'] + 1,
                )
                '''
                # New block template from Dash daemon only
                t = dict(
                    version=bb['version'],
                    previous_block=self.node.net.PARENT.BLOCKHASH_FUNC(dash_data.block_header_type.pack(bb)),
                    bits=bb['bits'], # not always true
                    coinbaseflags='',
                    height=t['height'] + 1,
                    time=bb['timestamp'] + 600, # better way?
                    transactions=[],
                    transaction_fees=[],
                    merkle_link=dash_data.calculate_merkle_link([None], 0),
                    subsidy=self.node.dashd_work.value['subsidy'],
                    last_update=self.node.dashd_work.value['last_update'],
                    payment_amount=self.node.dashd_work.value['payment_amount'],
                    packed_payments=self.node.dashd_work.value['packed_payments'],
                )
                '''

            self.current_work.set(t)

        self.node.dashd_work.changed.watch(lambda _: compute_work())
        self.node.best_block_header.changed.watch(lambda _: compute_work())
        compute_work()

        self.new_work_event = variable.Event()

        @self.current_work.transitioned.watch
        def _(before, after):
            # trigger LP if version/previous_block/bits changed or transactions changed from nothing
            if any(before[x] != after[x]
                   for x in ['version', 'previous_block', 'bits']) or (
                       not before['transactions'] and after['transactions']):
                self.new_work_event.happened()

        self.merged_work.changed.watch(
            lambda _: self.new_work_event.happened())
        self.node.best_share_var.changed.watch(
            lambda _: self.new_work_event.happened())
Exemple #5
0
    def __init__(self, node, my_pubkey_hash, donation_percentage, worker_fee,
                 args, pubkeys, dcrd):
        worker_interface.WorkerBridge.__init__(self)
        self.recent_shares_ts_work = []

        self.node = node

        self.dcrd = dcrd
        self.pubkeys = pubkeys
        self.args = args
        self.my_pubkey_hash = my_pubkey_hash

        self.donation_percentage = args.donation_percentage
        self.worker_fee = args.worker_fee

        self.net = self.node.net.PARENT
        self.running = True
        self.pseudoshare_received = variable.Event()
        self.share_received = variable.Event()
        self.local_rate_monitor = math.RateMonitor(10 * 60)
        self.local_addr_rate_monitor = math.RateMonitor(10 * 60)

        self.removed_unstales_var = variable.Variable((0, 0, 0))
        self.removed_doa_unstales_var = variable.Variable(0)

        self.last_work_shares = variable.Variable({})
        self.my_share_hashes = set()
        self.my_doa_share_hashes = set()

        self.address_throttle = 0

        self.tracker_view = forest.TrackerView(
            self.node.tracker,
            forest.get_attributedelta_type(
                dict(
                    forest.AttributeDelta.attrs,
                    my_count=lambda share: 1
                    if share.hash in self.my_share_hashes else 0,
                    my_doa_count=lambda share: 1
                    if share.hash in self.my_doa_share_hashes else 0,
                    my_orphan_announce_count=lambda share: 1
                    if share.hash in self.my_share_hashes and share.share_data[
                        'stale_info'] == 'orphan' else 0,
                    my_dead_announce_count=lambda share: 1
                    if share.hash in self.my_share_hashes and share.share_data[
                        'stale_info'] == 'doa' else 0,
                )))

        @self.node.tracker.verified.removed.watch
        def _(share):
            if share.hash in self.my_share_hashes and self.node.tracker.is_child_of(
                    share.hash, self.node.best_share_var.value):
                assert share.share_data['stale_info'] in [
                    None, 'orphan', 'doa'
                ]  # we made these shares in this instance
                self.removed_unstales_var.set((
                    self.removed_unstales_var.value[0] + 1,
                    self.removed_unstales_var.value[1] +
                    (1 if share.share_data['stale_info'] == 'orphan' else 0),
                    self.removed_unstales_var.value[2] +
                    (1 if share.share_data['stale_info'] == 'doa' else 0),
                ))
            if share.hash in self.my_doa_share_hashes and self.node.tracker.is_child_of(
                    share.hash, self.node.best_share_var.value):
                self.removed_doa_unstales_var.set(
                    self.removed_doa_unstales_var.value + 1)

        # COMBINE WORK

        self.current_work = variable.Variable(None)

        def compute_work():
            t = self.node.dcrd_work.value
            bb = self.node.best_block_header.value
            if bb is not None and bb['previous_block'] == t[
                    'previous_block'] and self.node.net.PARENT.POW_FUNC(
                        decred_data.block_header_type.pack(
                            bb)) <= t['bits'].target:
                print 'Skipping from block %x to block %x!' % (
                    bb['previous_block'],
                    decred_data.hash256(
                        decred_data.block_header_type.pack(bb)))
                t = dict(
                    version=bb['version'],
                    previous_block=decred_data.hash256(
                        decred_data.block_header_type.pack(bb)),
                    bits=bb['bits'],  # not always true
                    coinbaseflags='',
                    height=t['height'] + 1,
                    time=bb['timestamp'] + 300,  # better way?
                    transactions=[],
                    transaction_fees=[],
                    merkle_link=decred_data.calculate_merkle_link([None], 0),
                    subsidy=self.node.net.PARENT.SUBSIDY_FUNC(
                        self.node.dcrd_work.value['height']),
                    last_update=self.node.dcrd_work.value['last_update'],
                )

            self.current_work.set(t)

        self.node.dcrd_work.changed.watch(lambda _: compute_work())
        self.node.best_block_header.changed.watch(lambda _: compute_work())
        compute_work()

        self.new_work_event = variable.Event()

        @self.current_work.transitioned.watch
        def _(before, after):
            # trigger LP if version/previous_block/bits changed or transactions changed from nothing
            if any(before[x] != after[x]
                   for x in ['version', 'previous_block', 'bits']) or (
                       not before['transactions'] and after['transactions']):
                self.new_work_event.happened()

        self.node.best_share_var.changed.watch(
            lambda _: self.new_work_event.happened())
Exemple #6
0
def main(args, net, datadir_path):
    try:
        print 'p2pool (version %s)' % (p2pool.__version__, )
        print
        try:
            from . import draw
        except ImportError:
            draw = None
            print "Install Pygame and PIL to enable visualizations! Visualizations disabled."
            print

        # connect to bitcoind over JSON-RPC and do initial getmemorypool
        url = 'http://%s:%i/' % (args.bitcoind_address, args.bitcoind_rpc_port)
        print '''Testing bitcoind RPC connection to '%s' with username '%s'...''' % (
            url, args.bitcoind_rpc_username)
        bitcoind = jsonrpc.Proxy(
            url, (args.bitcoind_rpc_username, args.bitcoind_rpc_password))
        good = yield deferral.retry('Error while checking bitcoind identity:',
                                    1)(net.BITCOIN_RPC_CHECK)(bitcoind)
        if not good:
            print "    Check failed! Make sure that you're connected to the right bitcoind with --bitcoind-rpc-port!"
            return
        temp_work = yield getwork(bitcoind)
        print '    ...success!'
        print '    Current block hash: %x' % (
            temp_work['previous_block_hash'], )
        print

        # connect to bitcoind over bitcoin-p2p
        print '''Testing bitcoind P2P connection to '%s:%s'...''' % (
            args.bitcoind_address, args.bitcoind_p2p_port)
        factory = bitcoin_p2p.ClientFactory(net)
        reactor.connectTCP(args.bitcoind_address, args.bitcoind_p2p_port,
                           factory)
        yield factory.getProtocol()  # waits until handshake is successful
        print '    ...success!'
        print

        if args.pubkey_hash is None:
            print 'Getting payout address from bitcoind...'
            my_script = yield get_payout_script2(bitcoind, net)
        else:
            print 'Computing payout script from provided address....'
            my_script = bitcoin_data.pubkey_hash_to_script2(args.pubkey_hash)
        print '    ...success!'
        print '    Payout script:', bitcoin_data.script2_to_human(
            my_script, net)
        print

        ht = bitcoin_p2p.HeightTracker(bitcoind, factory)

        tracker = p2pool_data.OkayTracker(net)
        shared_share_hashes = set()
        ss = p2pool_data.ShareStore(os.path.join(datadir_path, 'shares.'), net)
        known_verified = set()
        print "Loading shares..."
        for i, (mode, contents) in enumerate(ss.get_shares()):
            if mode == 'share':
                if contents.hash in tracker.shares:
                    continue
                shared_share_hashes.add(contents.hash)
                contents.time_seen = 0
                tracker.add(contents)
                if len(tracker.shares) % 1000 == 0 and tracker.shares:
                    print "    %i" % (len(tracker.shares), )
            elif mode == 'verified_hash':
                known_verified.add(contents)
            else:
                raise AssertionError()
        print "    ...inserting %i verified shares..." % (
            len(known_verified), )
        for h in known_verified:
            if h not in tracker.shares:
                ss.forget_verified_share(h)
                continue
            tracker.verified.add(tracker.shares[h])
        print "    ...done loading %i shares!" % (len(tracker.shares), )
        print
        tracker.removed.watch(lambda share: ss.forget_share(share.hash))
        tracker.verified.removed.watch(
            lambda share: ss.forget_verified_share(share.hash))
        tracker.removed.watch(
            lambda share: shared_share_hashes.discard(share.hash))

        peer_heads = expiring_dict.ExpiringDict(
            300)  # hash -> peers that know of it

        pre_current_work = variable.Variable(None)
        pre_current_work2 = variable.Variable(None)
        pre_merged_work = variable.Variable(None)
        # information affecting work that should trigger a long-polling update
        current_work = variable.Variable(None)
        # information affecting work that should not trigger a long-polling update
        current_work2 = variable.Variable(None)

        work_updated = variable.Event()

        requested = expiring_dict.ExpiringDict(300)

        @defer.inlineCallbacks
        def set_real_work1():
            work = yield getwork(bitcoind)
            pre_current_work2.set(
                dict(
                    time=work['time'],
                    transactions=work['transactions'],
                    subsidy=work['subsidy'],
                    clock_offset=time.time() - work['time'],
                    last_update=time.time(),
                ))  # second set first because everything hooks on the first
            pre_current_work.set(
                dict(
                    version=work['version'],
                    previous_block=work['previous_block_hash'],
                    target=work['target'],
                ))

        def set_real_work2():
            best, desired = tracker.think(
                ht, pre_current_work.value['previous_block'],
                time.time() - pre_current_work2.value['clock_offset'])

            current_work2.set(pre_current_work2.value)
            t = dict(pre_current_work.value)
            t['best_share_hash'] = best
            t['aux_work'] = pre_merged_work.value
            current_work.set(t)

            t = time.time()
            for peer2, share_hash in desired:
                if share_hash not in tracker.tails:  # was received in the time tracker.think was running
                    continue
                last_request_time, count = requested.get(share_hash, (None, 0))
                if last_request_time is not None and last_request_time - 5 < t < last_request_time + 10 * 1.5**count:
                    continue
                potential_peers = set()
                for head in tracker.tails[share_hash]:
                    potential_peers.update(peer_heads.get(head, set()))
                potential_peers = [
                    peer for peer in potential_peers if peer.connected2
                ]
                if count == 0 and peer2 is not None and peer2.connected2:
                    peer = peer2
                else:
                    peer = random.choice(
                        potential_peers
                    ) if potential_peers and random.random() > .2 else peer2
                    if peer is None:
                        continue

                print 'Requesting parent share %s from %s' % (
                    p2pool_data.format_hash(share_hash), '%s:%i' % peer.addr)
                peer.send_getshares(
                    hashes=[share_hash],
                    parents=2000,
                    stops=list(
                        set(tracker.heads) | set(
                            tracker.get_nth_parent_hash(
                                head,
                                min(
                                    max(
                                        0,
                                        tracker.get_height_and_last(head)[0] -
                                        1), 10))
                            for head in tracker.heads))[:100],
                )
                requested[share_hash] = t, count + 1

        pre_current_work.changed.watch(lambda _: set_real_work2())

        print 'Initializing work...'
        yield set_real_work1()
        print '    ...success!'
        print

        pre_merged_work.changed.watch(lambda _: set_real_work2())
        ht.updated.watch(set_real_work2)

        @defer.inlineCallbacks
        def set_merged_work():
            if not args.merged_url:
                return
            merged = jsonrpc.Proxy(args.merged_url, (args.merged_userpass, ))
            while True:
                auxblock = yield deferral.retry(
                    'Error while calling merged getauxblock:',
                    1)(merged.rpc_getauxblock)()
                pre_merged_work.set(
                    dict(
                        hash=int(auxblock['hash'], 16),
                        target=bitcoin_data.HashType().unpack(
                            auxblock['target'].decode('hex')),
                        chain_id=auxblock['chainid'],
                    ))
                yield deferral.sleep(1)

        set_merged_work()

        start_time = time.time() - current_work2.value['clock_offset']

        # setup p2p logic and join p2pool network

        def p2p_shares(shares, peer=None):
            if len(shares) > 5:
                print 'Processing %i shares...' % (len(shares), )

            new_count = 0
            for share in shares:
                if share.hash in tracker.shares:
                    #print 'Got duplicate share, ignoring. Hash: %s' % (p2pool_data.format_hash(share.hash),)
                    continue

                new_count += 1

                #print 'Received share %s from %r' % (p2pool_data.format_hash(share.hash), share.peer.addr if share.peer is not None else None)

                tracker.add(share)

            if shares and peer is not None:
                peer_heads.setdefault(shares[0].hash, set()).add(peer)

            if new_count:
                set_real_work2()

            if len(shares) > 5:
                print '... done processing %i shares. New: %i Have: %i/~%i' % (
                    len(shares), new_count, len(
                        tracker.shares), 2 * net.CHAIN_LENGTH)

        @tracker.verified.added.watch
        def _(share):
            if share.pow_hash <= share.header['target']:
                if factory.conn.value is not None:
                    factory.conn.value.send_block(
                        block=share.as_block(tracker))
                else:
                    print 'No bitcoind connection! Erp!'
                print
                print 'GOT BLOCK! Passing to bitcoind! %s bitcoin: %x' % (
                    p2pool_data.format_hash(share.hash),
                    share.header_hash,
                )
                print

        def p2p_share_hashes(share_hashes, peer):
            t = time.time()
            get_hashes = []
            for share_hash in share_hashes:
                if share_hash in tracker.shares:
                    continue
                last_request_time, count = requested.get(share_hash, (None, 0))
                if last_request_time is not None and last_request_time - 5 < t < last_request_time + 10 * 1.5**count:
                    continue
                print 'Got share hash, requesting! Hash: %s' % (
                    p2pool_data.format_hash(share_hash), )
                get_hashes.append(share_hash)
                requested[share_hash] = t, count + 1

            if share_hashes and peer is not None:
                peer_heads.setdefault(share_hashes[0], set()).add(peer)
            if get_hashes:
                peer.send_getshares(hashes=get_hashes, parents=0, stops=[])

        def p2p_get_shares(share_hashes, parents, stops, peer):
            parents = min(parents, 1000 // len(share_hashes))
            stops = set(stops)
            shares = []
            for share_hash in share_hashes:
                for share in tracker.get_chain(
                        share_hash,
                        min(parents + 1, tracker.get_height(share_hash))):
                    if share.hash in stops:
                        break
                    shares.append(share)
            print 'Sending %i shares to %s:%i' % (len(shares), peer.addr[0],
                                                  peer.addr[1])
            peer.sendShares(shares)

        print 'Joining p2pool network using port %i...' % (args.p2pool_port, )

        def parse(x):
            if ':' in x:
                ip, port = x.split(':')
                return ip, int(port)
            else:
                return x, net.P2P_PORT

        nodes = set([
            ('72.14.191.28', net.P2P_PORT),
            ('62.204.197.159', net.P2P_PORT),
            ('142.58.248.28', net.P2P_PORT),
            ('94.23.34.145', net.P2P_PORT),
        ])
        for host in [
                'p2pool.forre.st',
                'dabuttonfactory.com',
        ] + (['liteco.in'] if net.NAME == 'litecoin' else []) + []:
            try:
                nodes.add(((yield reactor.resolve(host)), net.P2P_PORT))
            except:
                log.err(None, 'Error resolving bootstrap node IP:')

        addrs = {}
        try:
            addrs = dict(
                eval(x) for x in open(os.path.join(datadir_path, 'addrs.txt')))
        except:
            print "error reading addrs"

        def save_addrs():
            open(os.path.join(datadir_path, 'addrs.txt'),
                 'w').writelines(repr(x) + '\n' for x in addrs.iteritems())

        task.LoopingCall(save_addrs).start(60)

        p2p_node = p2p.Node(
            current_work=current_work,
            port=args.p2pool_port,
            net=net,
            addr_store=addrs,
            preferred_addrs=set(map(parse, args.p2pool_nodes)) | nodes,
        )
        p2p_node.handle_shares = p2p_shares
        p2p_node.handle_share_hashes = p2p_share_hashes
        p2p_node.handle_get_shares = p2p_get_shares

        p2p_node.start()

        # send share when the chain changes to their chain
        def work_changed(new_work):
            #print 'Work changed:', new_work
            shares = []
            for share in tracker.get_chain(
                    new_work['best_share_hash'],
                    tracker.get_height(new_work['best_share_hash'])):
                if share.hash in shared_share_hashes:
                    break
                shared_share_hashes.add(share.hash)
                shares.append(share)

            for peer in p2p_node.peers.itervalues():
                peer.sendShares(
                    [share for share in shares if share.peer is not peer])

        current_work.changed.watch(work_changed)

        def save_shares():
            for share in tracker.get_chain(
                    current_work.value['best_share_hash'],
                    min(
                        tracker.get_height(
                            current_work.value['best_share_hash']),
                        2 * net.CHAIN_LENGTH)):
                ss.add_share(share)
                if share.hash in tracker.verified.shares:
                    ss.add_verified_hash(share.hash)

        task.LoopingCall(save_shares).start(60)

        print '    ...success!'
        print

        @defer.inlineCallbacks
        def upnp_thread():
            while True:
                try:
                    is_lan, lan_ip = yield ipdiscover.get_local_ip()
                    if is_lan:
                        pm = yield portmapper.get_port_mapper()
                        yield pm._upnp.add_port_mapping(
                            lan_ip, args.p2pool_port, args.p2pool_port,
                            'p2pool',
                            'TCP')  # XXX try to forward external correct port?
                except defer.TimeoutError:
                    pass
                except:
                    if p2pool.DEBUG:
                        log.err(None, "UPnP error:")
                yield deferral.sleep(random.expovariate(1 / 120))

        if args.upnp:
            upnp_thread()

        # start listening for workers with a JSON-RPC server

        print 'Listening for workers on port %i...' % (args.worker_port, )

        # setup worker logic

        merkle_root_to_transactions = expiring_dict.ExpiringDict(300)
        run_identifier = struct.pack('<I', random.randrange(2**32))

        share_counter = skiplists.CountsSkipList(tracker, run_identifier)
        removed_unstales = set()

        def get_share_counts(doa=False):
            height, last = tracker.get_height_and_last(
                current_work.value['best_share_hash'])
            matching_in_chain = share_counter(
                current_work.value['best_share_hash'],
                height) | removed_unstales
            shares_in_chain = my_shares & matching_in_chain
            stale_shares = my_shares - matching_in_chain
            if doa:
                stale_doa_shares = stale_shares & doa_shares
                stale_not_doa_shares = stale_shares - stale_doa_shares
                return len(shares_in_chain) + len(stale_shares), len(
                    stale_doa_shares), len(stale_not_doa_shares)
            return len(shares_in_chain) + len(stale_shares), len(stale_shares)

        @tracker.verified.removed.watch
        def _(share):
            if share.hash in my_shares and tracker.is_child_of(
                    share.hash, current_work.value['best_share_hash']):
                removed_unstales.add(share.hash)

        def get_payout_script_from_username(user):
            if user is None:
                return None
            try:
                return bitcoin_data.pubkey_hash_to_script2(
                    bitcoin_data.address_to_pubkey_hash(user, net))
            except:  # XXX blah
                return None

        def compute(request):
            state = current_work.value
            user = worker_interface.get_username(request)

            payout_script = get_payout_script_from_username(user)
            if payout_script is None or random.uniform(0,
                                                       100) < args.worker_fee:
                payout_script = my_script

            if len(p2p_node.peers) == 0 and net.PERSIST:
                raise jsonrpc.Error(-12345,
                                    u'p2pool is not connected to any peers')
            if state['best_share_hash'] is None and net.PERSIST:
                raise jsonrpc.Error(-12345, u'p2pool is downloading shares')
            if time.time() > current_work2.value['last_update'] + 60:
                raise jsonrpc.Error(-12345, u'lost contact with bitcoind')

            previous_share = None if state[
                'best_share_hash'] is None else tracker.shares[
                    state['best_share_hash']]
            subsidy = current_work2.value['subsidy']
            share_info, generate_tx = p2pool_data.generate_transaction(
                tracker=tracker,
                share_data=dict(
                    previous_share_hash=state['best_share_hash'],
                    coinbase='' if state['aux_work'] is None else
                    '\xfa\xbemm' + bitcoin_data.HashType().pack(
                        state['aux_work']['hash'])[::-1] +
                    struct.pack('<ii', 1, 0),
                    nonce=run_identifier +
                    struct.pack('<Q', random.randrange(2**64)),
                    new_script=payout_script,
                    subsidy=subsidy,
                    donation=math.perfect_round(
                        65535 * args.donation_percentage / 100),
                    stale_frac=(lambda shares, stales: 255 if shares == 0 else
                                math.perfect_round(254 * stales / shares))(
                                    *get_share_counts()),
                ),
                block_target=state['target'],
                desired_timestamp=int(time.time() -
                                      current_work2.value['clock_offset']),
                net=net,
            )

            print 'New work for worker %s! Difficulty: %.06f Payout if block: %.6f %s Total block value: %.6f %s including %i transactions' % (
                user,
                bitcoin_data.target_to_difficulty(share_info['target']),
                (sum(t['value'] for t in generate_tx['tx_outs']
                     if t['script'] == payout_script) - subsidy // 200) * 1e-8,
                net.BITCOIN_SYMBOL,
                subsidy * 1e-8,
                net.BITCOIN_SYMBOL,
                len(current_work2.value['transactions']),
            )

            transactions = [generate_tx] + list(
                current_work2.value['transactions'])
            merkle_root = bitcoin_data.merkle_hash(transactions)
            merkle_root_to_transactions[
                merkle_root] = share_info, transactions, time.time()

            return bitcoin_getwork.BlockAttempt(
                state['version'], state['previous_block'], merkle_root,
                current_work2.value['time'], state['target'],
                share_info['target']), state['best_share_hash']

        my_shares = set()
        doa_shares = set()

        def got_response(header, request):
            try:
                user = worker_interface.get_username(request)
                # match up with transactions
                xxx = merkle_root_to_transactions.get(header['merkle_root'],
                                                      None)
                if xxx is None:
                    print '''Couldn't link returned work's merkle root with its transactions - should only happen if you recently restarted p2pool'''
                    return False
                share_info, transactions, getwork_time = xxx

                hash_ = bitcoin_data.block_header_type.hash256(header)

                pow_hash = net.BITCOIN_POW_FUNC(header)

                if pow_hash <= header['target'] or p2pool.DEBUG:
                    if factory.conn.value is not None:
                        factory.conn.value.send_block(
                            block=dict(header=header, txs=transactions))
                    else:
                        print 'No bitcoind connection! Erp!'
                    if pow_hash <= header['target']:
                        print
                        print 'GOT BLOCK! Passing to bitcoind! bitcoin: %x' % (
                            hash_, )
                        print

                if current_work.value[
                        'aux_work'] is not None and pow_hash <= current_work.value[
                            'aux_work']['target']:
                    try:
                        aux_pow = dict(
                            merkle_tx=dict(
                                tx=transactions[0],
                                block_hash=hash_,
                                merkle_branch=[
                                    x['hash'] for x in p2pool_data.
                                    calculate_merkle_branch(transactions, 0)
                                ],
                                index=0,
                            ),
                            merkle_branch=[],
                            index=0,
                            parent_block_header=header,
                        )

                        a, b = transactions[0]['tx_ins'][0]['script'][
                            -32 - 8:-8].encode(
                                'hex'), bitcoin_data.aux_pow_type.pack(
                                    aux_pow).encode('hex')
                        #print a, b
                        merged = jsonrpc.Proxy(args.merged_url,
                                               (args.merged_userpass, ))

                        def _(res):
                            print "MERGED RESULT:", res

                        merged.rpc_getauxblock(a, b).addBoth(_)
                    except:
                        log.err(None,
                                'Error while processing merged mining POW:')

                target = share_info['target']
                if pow_hash > target:
                    print 'Worker submitted share with hash > target:\nhash  : %x\ntarget: %x' % (
                        pow_hash, target)
                    return False
                share = p2pool_data.Share(net,
                                          header,
                                          share_info,
                                          other_txs=transactions[1:])
                my_shares.add(share.hash)
                if share.previous_hash != current_work.value['best_share_hash']:
                    doa_shares.add(share.hash)
                print 'GOT SHARE! %s %s prev %s age %.2fs' % (
                    user, p2pool_data.format_hash(share.hash),
                    p2pool_data.format_hash(share.previous_hash), time.time() -
                    getwork_time) + (' DEAD ON ARRIVAL' if share.previous_hash
                                     != current_work.value['best_share_hash']
                                     else '')
                good = share.previous_hash == current_work.value[
                    'best_share_hash']
                # maybe revert back to tracker being non-blocking so 'good' can be more accurate?
                p2p_shares([share])
                # eg. good = share.hash == current_work.value['best_share_hash'] here
                return good
            except:
                log.err(None, 'Error processing data received from worker:')
                return False

        web_root = worker_interface.WorkerInterface(compute, got_response,
                                                    current_work.changed)

        def get_rate():
            if current_work.value['best_share_hash'] is not None:
                height, last = tracker.get_height_and_last(
                    current_work.value['best_share_hash'])
                att_s = p2pool_data.get_pool_attempts_per_second(
                    tracker, current_work.value['best_share_hash'],
                    min(height - 1, 720))
                fracs = [
                    share.stale_frac for share in tracker.get_chain(
                        current_work.value['best_share_hash'], min(
                            120, height)) if share.stale_frac is not None
                ]
                return json.dumps(
                    int(att_s / (1. - (math.median(fracs) if fracs else 0))))
            return json.dumps(None)

        def get_users():
            height, last = tracker.get_height_and_last(
                current_work.value['best_share_hash'])
            weights, total_weight, donation_weight = tracker.get_cumulative_weights(
                current_work.value['best_share_hash'], min(height, 720),
                65535 * 2**256)
            res = {}
            for script in sorted(weights, key=lambda s: weights[s]):
                res[bitcoin_data.script2_to_human(
                    script, net)] = weights[script] / total_weight
            return json.dumps(res)

        class WebInterface(resource.Resource):
            def __init__(self, func, mime_type):
                self.func, self.mime_type = func, mime_type

            def render_GET(self, request):
                request.setHeader('Content-Type', self.mime_type)
                return self.func()

        web_root.putChild('rate', WebInterface(get_rate, 'application/json'))
        web_root.putChild('users', WebInterface(get_users, 'application/json'))
        web_root.putChild(
            'fee',
            WebInterface(lambda: json.dumps(args.worker_fee),
                         'application/json'))
        if draw is not None:
            web_root.putChild(
                'chain_img',
                WebInterface(
                    lambda: draw.get(tracker, current_work.value[
                        'best_share_hash']), 'image/png'))

        reactor.listenTCP(args.worker_port, server.Site(web_root))

        print '    ...success!'
        print

        # done!

        # do new getwork when a block is heard on the p2p interface

        def new_block(block_hash):
            work_updated.happened()

        factory.new_block.watch(new_block)

        print 'Started successfully!'
        print

        @defer.inlineCallbacks
        def work1_thread():
            while True:
                flag = work_updated.get_deferred()
                try:
                    yield set_real_work1()
                except:
                    log.err()
                yield defer.DeferredList(
                    [flag, deferral.sleep(random.uniform(1, 10))],
                    fireOnOneCallback=True)

        @defer.inlineCallbacks
        def work2_thread():
            while True:
                try:
                    set_real_work2()
                except:
                    log.err()
                yield deferral.sleep(random.expovariate(1 / 20))

        work1_thread()
        work2_thread()

        if hasattr(signal, 'SIGALRM'):

            def watchdog_handler(signum, frame):
                print 'Watchdog timer went off at:'
                traceback.print_stack()

            signal.signal(signal.SIGALRM, watchdog_handler)
            task.LoopingCall(signal.alarm, 30).start(1)

        @defer.inlineCallbacks
        def status_thread():
            last_str = None
            last_time = 0
            while True:
                yield deferral.sleep(3)
                try:
                    if time.time() > current_work2.value['last_update'] + 60:
                        print '''---> LOST CONTACT WITH BITCOIND for 60 seconds, check that it isn't frozen or dead <---'''
                    if current_work.value['best_share_hash'] is not None:
                        height, last = tracker.get_height_and_last(
                            current_work.value['best_share_hash'])
                        if height > 2:
                            att_s = p2pool_data.get_pool_attempts_per_second(
                                tracker, current_work.value['best_share_hash'],
                                min(height - 1, 720))
                            weights, total_weight, donation_weight = tracker.get_cumulative_weights(
                                current_work.value['best_share_hash'],
                                min(height, 720), 65535 * 2**256)
                            shares, stale_doa_shares, stale_not_doa_shares = get_share_counts(
                                True)
                            stale_shares = stale_doa_shares + stale_not_doa_shares
                            fracs = [
                                share.stale_frac
                                for share in tracker.get_chain(
                                    current_work.value['best_share_hash'],
                                    min(120, height))
                                if share.stale_frac is not None
                            ]
                            this_str = 'Pool: %sH/s in %i shares (%i/%i verified) Recent: %.02f%% >%sH/s Shares: %i (%i orphan, %i dead) Peers: %i' % (
                                math.format(
                                    int(att_s /
                                        (1. -
                                         (math.median(fracs) if fracs else 0)))
                                ),
                                height,
                                len(tracker.verified.shares),
                                len(tracker.shares),
                                weights.get(my_script, 0) / total_weight * 100,
                                math.format(
                                    int(
                                        weights.get(my_script, 0) * att_s //
                                        total_weight /
                                        (1. -
                                         (math.median(fracs) if fracs else 0)))
                                ),
                                shares,
                                stale_not_doa_shares,
                                stale_doa_shares,
                                len(p2p_node.peers),
                            ) + (' FDs: %i R/%i W' %
                                 (len(reactor.getReaders()),
                                  len(reactor.getWriters()))
                                 if p2pool.DEBUG else '')
                            if fracs:
                                med = math.median(fracs)
                                this_str += '\nPool stales: %i%%' % (
                                    int(100 * med + .5), )
                                conf = 0.95
                                if shares:
                                    this_str += u' Own: %i±%i%%' % tuple(
                                        int(100 * x + .5) for x in
                                        math.interval_to_center_radius(
                                            math.binomial_conf_interval(
                                                stale_shares, shares, conf)))
                                    if med < .99:
                                        this_str += u' Own efficiency: %i±%i%%' % tuple(
                                            int(100 * x + .5) for x in
                                            math.interval_to_center_radius(
                                                (1 - y) / (1 - med) for y in
                                                math.binomial_conf_interval(
                                                    stale_shares, shares,
                                                    conf)[::-1]))
                            if this_str != last_str or time.time(
                            ) > last_time + 15:
                                print this_str
                                last_str = this_str
                                last_time = time.time()
                except:
                    log.err()

        status_thread()
    except:
        log.err(None, 'Fatal error:')
Exemple #7
0
    def __init__(self, my_pubkey_hash, net, donation_percentage, bitcoind_work,
                 best_block_header, merged_urls, best_share_var, tracker,
                 my_share_hashes, my_doa_share_hashes, worker_fee, p2p_node,
                 submit_block, set_best_share, broadcast_share,
                 block_height_var):
        worker_interface.WorkerBridge.__init__(self)
        self.recent_shares_ts_work = []

        self.my_pubkey_hash = my_pubkey_hash
        self.net = net
        self.donation_percentage = donation_percentage
        self.bitcoind_work = bitcoind_work
        self.best_block_header = best_block_header
        self.best_share_var = best_share_var
        self.tracker = tracker
        self.my_share_hashes = my_share_hashes
        self.my_doa_share_hashes = my_doa_share_hashes
        self.worker_fee = worker_fee
        self.p2p_node = p2p_node
        self.submit_block = submit_block
        self.set_best_share = set_best_share
        self.broadcast_share = broadcast_share
        self.block_height_var = block_height_var

        self.pseudoshare_received = variable.Event()
        self.share_received = variable.Event()
        self.local_rate_monitor = math.RateMonitor(10 * 60)

        self.removed_unstales_var = variable.Variable((0, 0, 0))
        self.removed_doa_unstales_var = variable.Variable(0)

        @tracker.verified.removed.watch
        def _(share):
            if share.hash in self.my_share_hashes and tracker.is_child_of(
                    share.hash, self.best_share_var.value):
                assert share.share_data['stale_info'] in [
                    None, 'orphan', 'doa'
                ]  # we made these shares in this instance
                self.removed_unstales_var.set((
                    self.removed_unstales_var.value[0] + 1,
                    self.removed_unstales_var.value[1] +
                    (1 if share.share_data['stale_info'] == 'orphan' else 0),
                    self.removed_unstales_var.value[2] +
                    (1 if share.share_data['stale_info'] == 'doa' else 0),
                ))
            if share.hash in self.my_doa_share_hashes and self.tracker.is_child_of(
                    share.hash, self.best_share_var.value):
                self.removed_doa_unstales_var.set(
                    self.removed_doa_unstales_var.value + 1)

        # MERGED WORK

        self.merged_work = variable.Variable({})

        @defer.inlineCallbacks
        def set_merged_work(merged_url, merged_userpass):
            merged_proxy = jsonrpc.Proxy(
                merged_url,
                dict(Authorization='Basic ' +
                     base64.b64encode(merged_userpass)))
            while True:
                auxblock = yield deferral.retry(
                    'Error while calling merged getauxblock:',
                    30)(merged_proxy.rpc_getauxblock)()
                self.merged_work.set(
                    dict(
                        self.merged_work.value, **{
                            auxblock['chainid']:
                            dict(
                                hash=int(auxblock['hash'], 16),
                                target='p2pool' if auxblock['target']
                                == 'p2pool' else pack.IntType(256).unpack(
                                    auxblock['target'].decode('hex')),
                                merged_proxy=merged_proxy,
                            )
                        }))
                yield deferral.sleep(1)

        for merged_url, merged_userpass in merged_urls:
            set_merged_work(merged_url, merged_userpass)

        @self.merged_work.changed.watch
        def _(new_merged_work):
            print 'Got new merged mining work!'

        # COMBINE WORK

        self.current_work = variable.Variable(None)

        def compute_work():
            t = self.bitcoind_work.value
            bb = self.best_block_header.value
            if bb is not None and bb['previous_block'] == t[
                    'previous_block'] and net.PARENT.POW_FUNC(
                        bitcoin_data.block_header_type.pack(
                            bb)) <= t['bits'].target:
                print 'Skipping from block %x to block %x!' % (
                    bb['previous_block'],
                    bitcoin_data.hash256(
                        bitcoin_data.block_header_type.pack(bb)))
                t = dict(
                    version=bb['version'],
                    previous_block=bitcoin_data.hash256(
                        bitcoin_data.block_header_type.pack(bb)),
                    bits=bb['bits'],  # not always true
                    coinbaseflags='',
                    height=t['height'] + 1,
                    time=bb['timestamp'] + 600,  # better way?
                    transactions=[],
                    merkle_link=bitcoin_data.calculate_merkle_link([None], 0),
                    subsidy=net.PARENT.SUBSIDY_FUNC(
                        self.block_height_var.value),
                    last_update=self.bitcoind_work.value['last_update'],
                )

            self.current_work.set(t)

        self.bitcoind_work.changed.watch(lambda _: compute_work())
        self.best_block_header.changed.watch(lambda _: compute_work())
        compute_work()

        self.new_work_event = variable.Event()

        @self.current_work.transitioned.watch
        def _(before, after):
            # trigger LP if version/previous_block/bits changed or transactions changed from nothing
            if any(before[x] != after[x]
                   for x in ['version', 'previous_block', 'bits']) or (
                       not before['transactions'] and after['transactions']):
                self.new_work_event.happened()

        self.merged_work.changed.watch(
            lambda _: self.new_work_event.happened())
        self.best_share_var.changed.watch(
            lambda _: self.new_work_event.happened())
Exemple #8
0
def main(args, net, datadir_path, merged_urls, worker_endpoint):
    try:
        print 'p2pool (version %s)' % (p2pool.__version__, )
        print

        traffic_happened = variable.Event()

        @defer.inlineCallbacks
        def connect_p2p():
            # connect to bitcoind over bitcoin-p2p
            print '''Testing bitcoind P2P connection to '%s:%s'...''' % (
                args.bitcoind_address, args.bitcoind_p2p_port)
            factory = bitcoin_p2p.ClientFactory(net.PARENT)
            reactor.connectTCP(args.bitcoind_address, args.bitcoind_p2p_port,
                               factory)
            yield factory.getProtocol()  # waits until handshake is successful
            print '    ...success!'
            print
            defer.returnValue(factory)

        if args.testnet:  # establish p2p connection first if testnet so bitcoind can work without connections
            factory = yield connect_p2p()

        # connect to bitcoind over JSON-RPC and do initial getmemorypool
        url = '%s://%s:%i/' % ('https' if args.bitcoind_rpc_ssl else 'http',
                               args.bitcoind_address, args.bitcoind_rpc_port)
        print '''Testing bitcoind RPC connection to '%s' with username '%s'...''' % (
            url, args.bitcoind_rpc_username)
        bitcoind = jsonrpc.Proxy(
            url,
            dict(Authorization='Basic ' +
                 base64.b64encode(args.bitcoind_rpc_username + ':' +
                                  args.bitcoind_rpc_password)),
            timeout=30)

        @deferral.retry('Error while checking Bitcoin connection:', 1)
        @defer.inlineCallbacks
        def check():
            if not (yield net.PARENT.RPC_CHECK(bitcoind)):
                print >> sys.stderr, "    Check failed! Make sure that you're connected to the right bitcoind with --bitcoind-rpc-port!"
                raise deferral.RetrySilentlyException()
            if not net.VERSION_CHECK(
                (yield bitcoind.rpc_getinfo())['version']):
                print >> sys.stderr, '    Bitcoin version too old! Upgrade to 0.6.4 or newer!'
                raise deferral.RetrySilentlyException()

        yield check()
        temp_work = yield getwork(bitcoind)

        if not args.testnet:
            factory = yield connect_p2p()

        block_height_var = variable.Variable(None)

        @defer.inlineCallbacks
        def poll_height():
            block_height_var.set(
                (yield deferral.retry('Error while calling getblockcount:')(
                    bitcoind.rpc_getblockcount)()))

        yield poll_height()
        task.LoopingCall(poll_height).start(60 * 60)

        bitcoind_warning_var = variable.Variable(None)

        @defer.inlineCallbacks
        def poll_warnings():
            errors = (yield
                      deferral.retry('Error while calling getmininginfo:')(
                          bitcoind.rpc_getmininginfo)())['errors']
            bitcoind_warning_var.set(errors if errors != '' else None)

        yield poll_warnings()
        task.LoopingCall(poll_warnings).start(20 * 60)

        print '    ...success!'
        print '    Current block hash: %x' % (temp_work['previous_block'], )
        print '    Current block height: %i' % (block_height_var.value, )
        print

        print 'Determining payout address...'
        if args.pubkey_hash is None:
            address_path = os.path.join(datadir_path, 'cached_payout_address')

            if os.path.exists(address_path):
                with open(address_path, 'rb') as f:
                    address = f.read().strip('\r\n')
                print '    Loaded cached address: %s...' % (address, )
            else:
                address = None

            if address is not None:
                res = yield deferral.retry(
                    'Error validating cached address:',
                    5)(lambda: bitcoind.rpc_validateaddress(address))()
                if not res['isvalid'] or not res['ismine']:
                    print '    Cached address is either invalid or not controlled by local bitcoind!'
                    address = None

            if address is None:
                print '    Getting payout address from bitcoind...'
                address = yield deferral.retry(
                    'Error getting payout address from bitcoind:',
                    5)(lambda: bitcoind.rpc_getaccountaddress('p2pool'))()

            with open(address_path, 'wb') as f:
                f.write(address)

            my_pubkey_hash = bitcoin_data.address_to_pubkey_hash(
                address, net.PARENT)
        else:
            my_pubkey_hash = args.pubkey_hash
        print '    ...success! Payout address:', bitcoin_data.pubkey_hash_to_address(
            my_pubkey_hash, net.PARENT)
        print

        my_share_hashes = set()
        my_doa_share_hashes = set()

        tracker = p2pool_data.OkayTracker(net, my_share_hashes,
                                          my_doa_share_hashes)
        shared_share_hashes = set()
        ss = p2pool_data.ShareStore(os.path.join(datadir_path, 'shares.'), net)
        known_verified = set()
        print "Loading shares..."
        for i, (mode, contents) in enumerate(ss.get_shares()):
            if mode == 'share':
                if contents.hash in tracker.items:
                    continue
                shared_share_hashes.add(contents.hash)
                contents.time_seen = 0
                tracker.add(contents)
                if len(tracker.items) % 1000 == 0 and tracker.items:
                    print "    %i" % (len(tracker.items), )
            elif mode == 'verified_hash':
                known_verified.add(contents)
            else:
                raise AssertionError()
        print "    ...inserting %i verified shares..." % (
            len(known_verified), )
        for h in known_verified:
            if h not in tracker.items:
                ss.forget_verified_share(h)
                continue
            tracker.verified.add(tracker.items[h])
        print "    ...done loading %i shares!" % (len(tracker.items), )
        print
        tracker.removed.watch(lambda share: ss.forget_share(share.hash))
        tracker.verified.removed.watch(
            lambda share: ss.forget_verified_share(share.hash))
        tracker.removed.watch(
            lambda share: shared_share_hashes.discard(share.hash))

        print 'Initializing work...'

        # BITCOIND WORK

        bitcoind_work = variable.Variable((yield getwork(bitcoind)))

        @defer.inlineCallbacks
        def work_poller():
            while True:
                flag = factory.new_block.get_deferred()
                try:
                    bitcoind_work.set(
                        (yield
                         getwork(bitcoind,
                                 bitcoind_work.value['use_getblocktemplate'])))
                except:
                    log.err()
                yield defer.DeferredList([flag, deferral.sleep(15)],
                                         fireOnOneCallback=True)

        work_poller()

        # PEER WORK

        best_block_header = variable.Variable(None)

        def handle_header(new_header):
            # check that header matches current target
            if not (net.PARENT.POW_FUNC(
                    bitcoin_data.block_header_type.pack(new_header)) <=
                    bitcoind_work.value['bits'].target):
                return
            bitcoind_best_block = bitcoind_work.value['previous_block']
            if (best_block_header.value is None or
                (new_header['previous_block'] == bitcoind_best_block
                 and bitcoin_data.hash256(
                     bitcoin_data.block_header_type.pack(
                         best_block_header.value)) == bitcoind_best_block
                 )  # new is child of current and previous is current
                    or (bitcoin_data.hash256(
                        bitcoin_data.block_header_type.pack(new_header))
                        == bitcoind_best_block
                        and best_block_header.value['previous_block'] !=
                        bitcoind_best_block)
                ):  # new is current and previous is not a child of current
                best_block_header.set(new_header)

        @defer.inlineCallbacks
        def poll_header():
            handle_header((yield factory.conn.value.get_block_header(
                bitcoind_work.value['previous_block'])))

        bitcoind_work.changed.watch(lambda _: poll_header())
        yield deferral.retry('Error while requesting best block header:')(
            poll_header)()

        # BEST SHARE

        get_height_rel_highest = yield height_tracker.get_height_rel_highest_func(
            bitcoind, factory, lambda: bitcoind_work.value['previous_block'],
            net)

        best_share_var = variable.Variable(None)
        desired_var = variable.Variable(None)

        def set_best_share():
            best, desired = tracker.think(
                get_height_rel_highest, bitcoind_work.value['previous_block'],
                bitcoind_work.value['bits'])

            best_share_var.set(best)
            desired_var.set(desired)

        bitcoind_work.changed.watch(lambda _: set_best_share())
        set_best_share()

        print '    ...success!'
        print

        # setup p2p logic and join p2pool network

        class Node(p2p.Node):
            def handle_shares(self, shares, peer):
                if len(shares) > 5:
                    print 'Processing %i shares from %s...' % (
                        len(shares),
                        '%s:%i' % peer.addr if peer is not None else None)

                new_count = 0
                for share in shares:
                    if share.hash in tracker.items:
                        #print 'Got duplicate share, ignoring. Hash: %s' % (p2pool_data.format_hash(share.hash),)
                        continue

                    new_count += 1

                    #print 'Received share %s from %r' % (p2pool_data.format_hash(share.hash), share.peer.addr if share.peer is not None else None)

                    tracker.add(share)

                if new_count:
                    set_best_share()

                if len(shares) > 5:
                    print '... done processing %i shares. New: %i Have: %i/~%i' % (
                        len(shares), new_count, len(
                            tracker.items), 2 * net.CHAIN_LENGTH)

            @defer.inlineCallbacks
            def handle_share_hashes(self, hashes, peer):
                new_hashes = [x for x in hashes if x not in tracker.items]
                if not new_hashes:
                    return
                try:
                    shares = yield peer.get_shares(
                        hashes=new_hashes,
                        parents=0,
                        stops=[],
                    )
                except:
                    log.err(None, 'in handle_share_hashes:')
                else:
                    self.handle_shares(shares, peer)

            def handle_get_shares(self, hashes, parents, stops, peer):
                parents = min(parents, 1000 // len(hashes))
                stops = set(stops)
                shares = []
                for share_hash in hashes:
                    for share in tracker.get_chain(
                            share_hash,
                            min(parents + 1, tracker.get_height(share_hash))):
                        if share.hash in stops:
                            break
                        shares.append(share)
                print 'Sending %i shares to %s:%i' % (
                    len(shares), peer.addr[0], peer.addr[1])
                return shares

            def handle_bestblock(self, header, peer):
                if net.PARENT.POW_FUNC(
                        bitcoin_data.block_header_type.pack(
                            header)) > header['bits'].target:
                    raise p2p.PeerMisbehavingError(
                        'received block header fails PoW test')
                handle_header(header)

        @deferral.retry('Error submitting primary block: (will retry)', 10, 10)
        def submit_block_p2p(block):
            if factory.conn.value is None:
                print >> sys.stderr, 'No bitcoind connection when block submittal attempted! %s%064x' % (
                    net.PARENT.BLOCK_EXPLORER_URL_PREFIX,
                    bitcoin_data.hash256(
                        bitcoin_data.block_header_type.pack(block['header'])))
                raise deferral.RetrySilentlyException()
            factory.conn.value.send_block(block=block)

        @deferral.retry('Error submitting block: (will retry)', 10, 10)
        @defer.inlineCallbacks
        def submit_block_rpc(block, ignore_failure):
            if bitcoind_work.value['use_getblocktemplate']:
                result = yield bitcoind.rpc_submitblock(
                    bitcoin_data.block_type.pack(block).encode('hex'))
                success = result is None
            else:
                result = yield bitcoind.rpc_getmemorypool(
                    bitcoin_data.block_type.pack(block).encode('hex'))
                success = result
            success_expected = net.PARENT.POW_FUNC(
                bitcoin_data.block_header_type.pack(
                    block['header'])) <= block['header']['bits'].target
            if (not success and success_expected
                    and not ignore_failure) or (success
                                                and not success_expected):
                print >> sys.stderr, 'Block submittal result: %s (%r) Expected: %s' % (
                    success, result, success_expected)

        def submit_block(block, ignore_failure):
            submit_block_p2p(block)
            submit_block_rpc(block, ignore_failure)

        @tracker.verified.added.watch
        def _(share):
            if share.pow_hash <= share.header['bits'].target:
                submit_block(share.as_block(tracker), ignore_failure=True)
                print
                print 'GOT BLOCK FROM PEER! Passing to bitcoind! %s bitcoin: %s%064x' % (
                    p2pool_data.format_hash(share.hash),
                    net.PARENT.BLOCK_EXPLORER_URL_PREFIX, share.header_hash)
                print

                def spread():
                    if (get_height_rel_highest(share.header['previous_block'])
                            > -5 or bitcoind_work.value['previous_block'] in [
                                share.header['previous_block'],
                                share.header_hash
                            ]):
                        broadcast_share(share.hash)

                spread()
                reactor.callLater(
                    5, spread)  # so get_height_rel_highest can update

        print 'Joining p2pool network using port %i...' % (args.p2pool_port, )

        @defer.inlineCallbacks
        def parse(x):
            if ':' in x:
                ip, port = x.split(':')
                defer.returnValue(((yield reactor.resolve(ip)), int(port)))
            else:
                defer.returnValue(((yield reactor.resolve(x)), net.P2P_PORT))

        addrs = {}
        if os.path.exists(os.path.join(datadir_path, 'addrs')):
            try:
                with open(os.path.join(datadir_path, 'addrs'), 'rb') as f:
                    addrs.update(
                        dict((tuple(k), v) for k, v in json.loads(f.read())))
            except:
                print >> sys.stderr, 'error parsing addrs'
        for addr_df in map(parse, net.BOOTSTRAP_ADDRS):
            try:
                addr = yield addr_df
                if addr not in addrs:
                    addrs[addr] = (0, time.time(), time.time())
            except:
                log.err()

        connect_addrs = set()
        for addr_df in map(parse, args.p2pool_nodes):
            try:
                connect_addrs.add((yield addr_df))
            except:
                log.err()

        p2p_node = Node(
            best_share_hash_func=lambda: best_share_var.value,
            port=args.p2pool_port,
            net=net,
            addr_store=addrs,
            connect_addrs=connect_addrs,
            max_incoming_conns=args.p2pool_conns,
            traffic_happened=traffic_happened,
        )
        p2p_node.start()

        def save_addrs():
            with open(os.path.join(datadir_path, 'addrs'), 'wb') as f:
                f.write(json.dumps(p2p_node.addr_store.items()))

        task.LoopingCall(save_addrs).start(60)

        @best_block_header.changed.watch
        def _(header):
            for peer in p2p_node.peers.itervalues():
                peer.send_bestblock(header=header)

        @defer.inlineCallbacks
        def broadcast_share(share_hash):
            shares = []
            for share in tracker.get_chain(
                    share_hash, min(5, tracker.get_height(share_hash))):
                if share.hash in shared_share_hashes:
                    break
                shared_share_hashes.add(share.hash)
                shares.append(share)

            for peer in list(p2p_node.peers.itervalues()):
                yield peer.sendShares(
                    [share for share in shares if share.peer is not peer])

        # send share when the chain changes to their chain
        best_share_var.changed.watch(broadcast_share)

        def save_shares():
            for share in tracker.get_chain(
                    best_share_var.value,
                    min(tracker.get_height(best_share_var.value),
                        2 * net.CHAIN_LENGTH)):
                ss.add_share(share)
                if share.hash in tracker.verified.items:
                    ss.add_verified_hash(share.hash)

        task.LoopingCall(save_shares).start(60)

        @apply
        @defer.inlineCallbacks
        def download_shares():
            while True:
                desired = yield desired_var.get_when_satisfies(
                    lambda val: len(val) != 0)
                peer2, share_hash = random.choice(desired)

                if len(p2p_node.peers) == 0:
                    yield deferral.sleep(1)
                    continue
                peer = random.choice(p2p_node.peers.values())

                print 'Requesting parent share %s from %s' % (
                    p2pool_data.format_hash(share_hash), '%s:%i' % peer.addr)
                try:
                    shares = yield peer.get_shares(
                        hashes=[share_hash],
                        parents=500,
                        stops=[],
                    )
                except:
                    log.err(None, 'in download_shares:')
                    continue

                if not shares:
                    yield deferral.sleep(
                        1
                    )  # sleep so we don't keep rerequesting the same share nobody has
                    continue
                p2p_node.handle_shares(shares, peer)

        print '    ...success!'
        print

        if args.upnp:

            @defer.inlineCallbacks
            def upnp_thread():
                while True:
                    try:
                        is_lan, lan_ip = yield ipdiscover.get_local_ip()
                        if is_lan:
                            pm = yield portmapper.get_port_mapper()
                            yield pm._upnp.add_port_mapping(
                                lan_ip, args.p2pool_port, args.p2pool_port,
                                'p2pool', 'TCP')
                    except defer.TimeoutError:
                        pass
                    except:
                        if p2pool.DEBUG:
                            log.err(None, 'UPnP error:')
                    yield deferral.sleep(random.expovariate(1 / 120))

            upnp_thread()

        # start listening for workers with a JSON-RPC server

        print 'Listening for workers on %r port %i...' % (worker_endpoint[0],
                                                          worker_endpoint[1])

        get_current_txouts = lambda: p2pool_data.get_expected_payouts(
            tracker, best_share_var.value, bitcoind_work.value['bits'].target,
            bitcoind_work.value['subsidy'], net)

        wb = work.WorkerBridge(my_pubkey_hash, net, args.donation_percentage,
                               bitcoind_work, best_block_header, merged_urls,
                               best_share_var, tracker, my_share_hashes,
                               my_doa_share_hashes, args.worker_fee, p2p_node,
                               submit_block, set_best_share, broadcast_share,
                               block_height_var)
        web_root = web.get_web_root(
            tracker, bitcoind_work, get_current_txouts, datadir_path, net,
            wb.get_stale_counts, my_pubkey_hash, wb.local_rate_monitor,
            args.worker_fee, p2p_node, wb.my_share_hashes,
            wb.pseudoshare_received, wb.share_received, best_share_var,
            bitcoind_warning_var, traffic_happened)
        worker_interface.WorkerInterface(wb).attach_to(
            web_root, get_handler=lambda request: request.redirect('/static/'))

        deferral.retry('Error binding to worker port:', traceback=False)(
            reactor.listenTCP)(worker_endpoint[1],
                               server.Site(web_root),
                               interface=worker_endpoint[0])

        with open(os.path.join(os.path.join(datadir_path, 'ready_flag')),
                  'wb') as f:
            pass

        print '    ...success!'
        print

        # done!
        print 'Started successfully!'
        print 'Go to http://127.0.0.1:%i/ to view graphs and statistics!' % (
            worker_endpoint[1], )
        if args.donation_percentage > 0.51:
            print '''Donating %.1f%% of work towards P2Pool's development. Thanks for the tip!''' % (
                args.donation_percentage, )
        elif args.donation_percentage < 0.49:
            print '''Donating %.1f%% of work towards P2Pool's development. Please donate to encourage further development of P2Pool!''' % (
                args.donation_percentage, )
        else:
            print '''Donating %.1f%% of work towards P2Pool's development. Thank you!''' % (
                args.donation_percentage, )
            print 'You can increase this amount with --give-author argument! (or decrease it, if you must)'
        print

        if hasattr(signal, 'SIGALRM'):
            signal.signal(
                signal.SIGALRM, lambda signum, frame: reactor.callFromThread(
                    sys.stderr.write, 'Watchdog timer went off at:\n' + ''.
                    join(traceback.format_stack())))
            signal.siginterrupt(signal.SIGALRM, False)
            task.LoopingCall(signal.alarm, 30).start(1)

        if args.irc_announce:
            from twisted.words.protocols import irc

            class IRCClient(irc.IRCClient):
                nickname = 'p2pool%02i' % (random.randrange(100), )
                channel = net.ANNOUNCE_CHANNEL

                def lineReceived(self, line):
                    if p2pool.DEBUG:
                        print repr(line)
                    irc.IRCClient.lineReceived(self, line)

                def signedOn(self):
                    irc.IRCClient.signedOn(self)
                    self.factory.resetDelay()
                    self.join(self.channel)

                    @defer.inlineCallbacks
                    def new_share(share):
                        if share.pow_hash <= share.header[
                                'bits'].target and abs(share.timestamp -
                                                       time.time()) < 10 * 60:
                            yield deferral.sleep(random.expovariate(1 / 60))
                            message = '\x02%s BLOCK FOUND by %s! %s%064x' % (
                                net.NAME.upper(),
                                bitcoin_data.script2_to_address(
                                    share.new_script, net.PARENT),
                                net.PARENT.BLOCK_EXPLORER_URL_PREFIX,
                                share.header_hash)
                            if all('%x' %
                                   (share.header_hash, ) not in old_message
                                   for old_message in self.recent_messages):
                                self.say(self.channel, message)
                                self._remember_message(message)

                    self.watch_id = tracker.verified.added.watch(new_share)
                    self.recent_messages = []

                def _remember_message(self, message):
                    self.recent_messages.append(message)
                    while len(self.recent_messages) > 100:
                        self.recent_messages.pop(0)

                def privmsg(self, user, channel, message):
                    if channel == self.channel:
                        self._remember_message(message)

                def connectionLost(self, reason):
                    tracker.verified.added.unwatch(self.watch_id)
                    print 'IRC connection lost:', reason.getErrorMessage()

            class IRCClientFactory(protocol.ReconnectingClientFactory):
                protocol = IRCClient

            reactor.connectTCP("irc.freenode.net", 6667, IRCClientFactory())

        @defer.inlineCallbacks
        def status_thread():
            last_str = None
            last_time = 0
            while True:
                yield deferral.sleep(3)
                try:
                    height = tracker.get_height(best_share_var.value)
                    this_str = 'P2Pool: %i shares in chain (%i verified/%i total) Peers: %i (%i incoming)' % (
                        height,
                        len(tracker.verified.items),
                        len(tracker.items),
                        len(p2p_node.peers),
                        sum(1 for peer in p2p_node.peers.itervalues()
                            if peer.incoming),
                    ) + (' FDs: %i R/%i W' %
                         (len(reactor.getReaders()), len(reactor.getWriters()))
                         if p2pool.DEBUG else '')

                    datums, dt = wb.local_rate_monitor.get_datums_in_last()
                    my_att_s = sum(datum['work'] / dt for datum in datums)
                    this_str += '\n Local: %sH/s in last %s Local dead on arrival: %s Expected time to share: %s' % (
                        math.format(int(my_att_s)),
                        math.format_dt(dt),
                        math.format_binomial_conf(
                            sum(1 for datum in datums if datum['dead']),
                            len(datums), 0.95),
                        math.format_dt(2**256 / tracker.items[
                            best_share_var.value].max_target / my_att_s)
                        if my_att_s and best_share_var.value else '???',
                    )

                    if height > 2:
                        (stale_orphan_shares,
                         stale_doa_shares), shares, _ = wb.get_stale_counts()
                        stale_prop = p2pool_data.get_average_stale_prop(
                            tracker, best_share_var.value,
                            min(60 * 60 // net.SHARE_PERIOD, height))
                        real_att_s = p2pool_data.get_pool_attempts_per_second(
                            tracker, best_share_var.value,
                            min(height - 1, 60 * 60 //
                                net.SHARE_PERIOD)) / (1 - stale_prop)

                        this_str += '\n Shares: %i (%i orphan, %i dead) Stale rate: %s Efficiency: %s Current payout: %.4f %s' % (
                            shares,
                            stale_orphan_shares,
                            stale_doa_shares,
                            math.format_binomial_conf(
                                stale_orphan_shares + stale_doa_shares, shares,
                                0.95),
                            math.format_binomial_conf(
                                stale_orphan_shares + stale_doa_shares, shares,
                                0.95, lambda x: (1 - x) / (1 - stale_prop)),
                            get_current_txouts().get(
                                bitcoin_data.pubkey_hash_to_script2(
                                    my_pubkey_hash), 0) * 1e-8,
                            net.PARENT.SYMBOL,
                        )
                        this_str += '\n Pool: %sH/s Stale rate: %.1f%% Expected time to block: %s' % (
                            math.format(int(real_att_s)),
                            100 * stale_prop,
                            math.format_dt(
                                2**256 / bitcoind_work.value['bits'].target /
                                real_att_s),
                        )

                        for warning in p2pool_data.get_warnings(
                                tracker, best_share_var.value, net,
                                bitcoind_warning_var.value,
                                bitcoind_work.value):
                            print >> sys.stderr, '#' * 40
                            print >> sys.stderr, '>>> Warning: ' + warning
                            print >> sys.stderr, '#' * 40

                    if this_str != last_str or time.time() > last_time + 15:
                        print this_str
                        last_str = this_str
                        last_time = time.time()
                except:
                    log.err()

        status_thread()
    except:
        reactor.stop()
        log.err(None, 'Fatal error:')
Exemple #9
0
    def __init__(self, node, my_pubkey_hash, donation_percentage, merged_urls, worker_fee):
        worker_interface.WorkerBridge.__init__(self)
        self.recent_shares_ts_work = []
        
        self.node = node
        self.my_pubkey_hash = my_pubkey_hash
        self.donation_percentage = donation_percentage
        self.worker_fee = worker_fee
        
        self.net = self.node.net.PARENT
        self.running = True
        self.pseudoshare_received = variable.Event()
        self.share_received = variable.Event()
        self.local_rate_monitor = math.RateMonitor(10*60)
        self.local_addr_rate_monitor = math.RateMonitor(10*60)
        
        self.removed_unstales_var = variable.Variable((0, 0, 0))
        self.removed_doa_unstales_var = variable.Variable(0)

        self.last_work_shares = variable.Variable( {} )        
        
        self.my_share_hashes = set()
        self.my_doa_share_hashes = set()
        
        self.tracker_view = forest.TrackerView(self.node.tracker, forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs,
            my_count=lambda share: 1 if share.hash in self.my_share_hashes else 0,
            my_doa_count=lambda share: 1 if share.hash in self.my_doa_share_hashes else 0,
            my_orphan_announce_count=lambda share: 1 if share.hash in self.my_share_hashes and share.share_data['stale_info'] == 'orphan' else 0, # 고아
            my_dead_announce_count=lambda share: 1 if share.hash in self.my_share_hashes and share.share_data['stale_info'] == 'doa' else 0, # 죽음
        )))


        # Python Logger
        self.logger = logging.getLogger('pool_log')
        fomatter = logging.Formatter('[%(levelname)s|%(filename)s - %(lineno)s] : %(message)s')

        fileHandler = logging.FileHandler('./difficulty.log')
        streamHandler = logging.StreamHandler()

        fileHandler.setFormatter(fomatter)
        streamHandler.setFormatter(fomatter)

        self.logger.addHandler(fileHandler)
        self.logger.addHandler(streamHandler)

        @self.node.tracker.verified.removed.watch
        def _(share):
            if share.hash in self.my_share_hashes and self.node.tracker.is_child_of(share.hash, self.node.best_share_var.value):
                assert share.share_data['stale_info'] in [None, 'orphan', 'doa'] # we made these shares in this instance
                self.removed_unstales_var.set((
                    self.removed_unstales_var.value[0] + 1,
                    self.removed_unstales_var.value[1] + (1 if share.share_data['stale_info'] == 'orphan' else 0),
                    self.removed_unstales_var.value[2] + (1 if share.share_data['stale_info'] == 'doa' else 0),
                ))
            if share.hash in self.my_doa_share_hashes and self.node.tracker.is_child_of(share.hash, self.node.best_share_var.value):
                self.removed_doa_unstales_var.set(self.removed_doa_unstales_var.value + 1)
        
        # MERGED WORK
        
        self.merged_work = variable.Variable({})
        
        @defer.inlineCallbacks
        def set_merged_work(merged_url, merged_userpass):
            merged_proxy = jsonrpc.HTTPProxy(merged_url, dict(Authorization='Basic ' + base64.b64encode(merged_userpass)))
            while self.running:
                auxblock = yield deferral.retry('Error while calling merged getauxblock on %s:' % (merged_url,), 30)(merged_proxy.rpc_getauxblock)()
                self.merged_work.set(math.merge_dicts(self.merged_work.value, {auxblock['chainid']: dict(
                    hash=int(auxblock['hash'], 16),
                    target='p2pool' if auxblock['target'] == 'p2pool' else pack.IntType(256).unpack(auxblock['target'].decode('hex')),
                    merged_proxy=merged_proxy,
                )}))
                yield deferral.sleep(1)
        for merged_url, merged_userpass in merged_urls:
            set_merged_work(merged_url, merged_userpass)
        
        @self.merged_work.changed.watch
        def _(new_merged_work):
            print 'Got new merged mining work!'
        
        # COMBINE WORK
        
        self.current_work = variable.Variable(None)
        def compute_work():
            t = self.node.bitcoind_work.value # nTime?
            bb = self.node.best_block_header.value # Tx?
            if bb is not None and bb['previous_block'] == t['previous_block'] and bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(bb)) <= t['bits'].target:
                print 'Skipping from block %x to block %x!' % (bb['previous_block'],
                    #bitcoin_data.hash256(bitcoin_data.block_header_type.pack(bb)))
                    bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(bb)))
                t = dict(
                    version=bb['version'],
                    previous_block=bitcoin_data.scrypt(bitcoin_data.block_header_type.pack(bb)),
                    bits=self.node.pow_bits, # not always true
                    coinbaseflags='',
                    height=t['height'] + 1,
                    time=bb['time'] + 600, # better way?
                    transactions=[],
                    transaction_fees=[],
                    txn_timestamp=0,
                    merkle_link=bitcoin_data.calculate_merkle_link([None], 0),
                    subsidy=self.node.pow_subsidy,
                    last_update=self.node.bitcoind_work.value['last_update'],
                )
            
            self.current_work.set(t)
        self.node.bitcoind_work.changed.watch(lambda _: compute_work())
        self.node.best_block_header.changed.watch(lambda _: compute_work())
        compute_work()
        
        self.new_work_event = variable.Event()
        @self.current_work.transitioned.watch
        def _(before, after):
            # trigger LP if version/previous_block/bits changed or transactions changed from nothing
            if any(before[x] != after[x] for x in ['version', 'previous_block', 'bits']) or (not before['transactions'] and after['transactions']):
                self.new_work_event.happened()
        self.merged_work.changed.watch(lambda _: self.new_work_event.happened())
        self.node.best_share_var.changed.watch(lambda _: self.new_work_event.happened())
Exemple #10
0
    def __init__(self, node, my_pubkey_hash, donation_percentage, merged_urls,
                 worker_fee, min_difficulty, share_rate, share_rate_type, args,
                 pubkeys, bitcoind):
        worker_interface.WorkerBridge.__init__(self)
        self.recent_shares_ts_work = []

        self.node = node

        self.bitcoind = bitcoind
        self.pubkeys = pubkeys
        self.args = args
        self.my_pubkey_hash = my_pubkey_hash

        self.donation_percentage = donation_percentage
        self.worker_fee = worker_fee
        self.min_difficulty = min_difficulty
        self.share_rate = share_rate
        self.share_rate_type = share_rate_type

        self.net = self.node.net.PARENT
        self.running = True
        self.pseudoshare_received = variable.Event()
        self.share_received = variable.Event()
        self.local_rate_monitor = math.RateMonitor(10 * 60)
        self.local_addr_rate_monitor = math.RateMonitor(10 * 60)

        self.removed_unstales_var = variable.Variable((0, 0, 0))
        self.removed_doa_unstales_var = variable.Variable(0)

        self.last_work_shares = variable.Variable({})
        self.my_share_hashes = set()
        self.my_doa_share_hashes = set()

        self.address_throttle = 0

        self.tracker_view = forest.TrackerView(
            self.node.tracker,
            forest.get_attributedelta_type(
                dict(
                    forest.AttributeDelta.attrs,
                    my_count=lambda share: 1
                    if share.hash in self.my_share_hashes else 0,
                    my_doa_count=lambda share: 1
                    if share.hash in self.my_doa_share_hashes else 0,
                    my_orphan_announce_count=lambda share: 1
                    if share.hash in self.my_share_hashes and share.share_data[
                        'stale_info'] == 'orphan' else 0,
                    my_dead_announce_count=lambda share: 1
                    if share.hash in self.my_share_hashes and share.share_data[
                        'stale_info'] == 'doa' else 0,
                )))

        @self.node.tracker.verified.removed.watch
        def _(share):
            if share.hash in self.my_share_hashes and self.node.tracker.is_child_of(
                    share.hash, self.node.best_share_var.value):
                assert share.share_data['stale_info'] in [
                    None, 'orphan', 'doa'
                ]  # we made these shares in this instance
                self.removed_unstales_var.set((
                    self.removed_unstales_var.value[0] + 1,
                    self.removed_unstales_var.value[1] +
                    (1 if share.share_data['stale_info'] == 'orphan' else 0),
                    self.removed_unstales_var.value[2] +
                    (1 if share.share_data['stale_info'] == 'doa' else 0),
                ))
            if share.hash in self.my_doa_share_hashes and self.node.tracker.is_child_of(
                    share.hash, self.node.best_share_var.value):
                self.removed_doa_unstales_var.set(
                    self.removed_doa_unstales_var.value + 1)

        # MERGED WORK

        self.merged_work = variable.Variable({})

        @defer.inlineCallbacks
        def set_merged_work(merged_url, merged_userpass):
            merged_proxy = jsonrpc.HTTPProxy(
                merged_url,
                dict(Authorization='Basic ' +
                     base64.b64encode(merged_userpass)))
            while self.running:
                auxblock = yield deferral.retry(
                    'Error while calling merged getauxblock on %s:' %
                    (merged_url, ), 30)(merged_proxy.rpc_getauxblock)()
                target = auxblock[
                    'target'] if 'target' in auxblock else auxblock['_target']
                self.merged_work.set(
                    math.merge_dicts(
                        self.merged_work.value, {
                            auxblock['chainid']:
                            dict(
                                hash=int(auxblock['hash'], 16),
                                target='p2pool' if target == 'p2pool' else
                                pack.IntType(256).unpack(target.decode('hex')),
                                merged_proxy=merged_proxy,
                            )
                        }))
                yield deferral.sleep(1)

        for merged_url, merged_userpass in merged_urls:
            set_merged_work(merged_url, merged_userpass)

        @self.merged_work.changed.watch
        def _(new_merged_work):
            print 'Got new merged mining work!'

        # COMBINE WORK

        self.current_work = variable.Variable(None)

        def compute_work():
            t = self.node.bitcoind_work.value
            bb = self.node.best_block_header.value
            if bb is not None and bb['previous_block'] == t[
                    'previous_block'] and self.node.net.PARENT.POW_FUNC(
                        bitcoin_data.block_header_type.pack(
                            bb)) <= t['bits'].target:
                print 'Skipping from block %x to block %x!' % (
                    bb['previous_block'],
                    bitcoin_data.hash256(
                        bitcoin_data.block_header_type.pack(bb)))
                t = dict(
                    version=bb['version'],
                    previous_block=bitcoin_data.hash256(
                        bitcoin_data.block_header_type.pack(bb)),
                    bits=bb['bits'],  # not always true
                    coinbaseflags='',
                    height=t['height'] + 1,
                    time=max(int(time.time() + 0.5), bb['timestamp'] + 1),
                    transactions=[],
                    transaction_hashes=[],
                    transaction_fees=[],
                    merkle_link=bitcoin_data.calculate_merkle_link([None], 0),
                    subsidy=self.node.net.PARENT.SUBSIDY_FUNC(
                        self.node.bitcoind_work.value['height']),
                    last_update=t['last_update'],
                    skipping=self.current_work.value.get('skipping', 3) -
                    1 if self.current_work.value is not None else 2)

            self.current_work.set(t)

        self.node.bitcoind_work.changed.watch(lambda _: compute_work())
        self.node.best_block_header.changed.watch(lambda _: compute_work())
        compute_work()

        self.new_work_event = variable.Event()

        @self.current_work.transitioned.watch
        def _(before, after):
            # trigger LP if version/previous_block/bits changed or transactions changed from nothing
            if any(before[x] != after[x]
                   for x in ['version', 'previous_block', 'bits']) or (
                       not before['transactions'] and after['transactions']):
                self.new_work_event.happened()
            # refetch block template if best block changed
            if after.get('skipping', -2) >= 0:
                time.sleep(0.5)
                self.node.bitcoind_work.set((yield helper.getwork(
                    self.bitcoind,
                    self.node.bitcoind_work.value['use_getblocktemplate'])))
            elif after.get('skipping', -2) == -1:
                # revert if bitcoind doesn't have the new block
                h = yield self.bitcoind.rpc_getblockheader(
                    (yield self.bitcoind.rpc_getbestblockhash()))
                self.node.best_block_header.set(
                    dict(version=h['version'],
                         previous_block=int(h['previousblockhash'], 16),
                         merkle_root=int(h['merkleroot'], 16),
                         timestamp=h['time'],
                         bits=bitcoin_data.FloatingIntegerType().unpack(
                             h['bits'].decode('hex')[::-1]) if isinstance(
                                 h['bits'], (str, unicode)) else
                         bitcoin_data.FloatingInteger(h['bits']),
                         nonce=h['nonce']))

        self.merged_work.changed.watch(
            lambda _: self.new_work_event.happened())
        self.node.best_share_var.changed.watch(
            lambda _: self.new_work_event.happened())
Exemple #11
0
    def __init__(self, node, my_pubkey_hash, donation_percentage, merged_urls, worker_fee, args, pubkeys, bitcoind):
        worker_interface.WorkerBridge.__init__(self)
        self.recent_shares_ts_work = []
        
        self.node = node

        self.bitcoind = bitcoind
        self.pubkeys = pubkeys
        self.args = args
        self.my_pubkey_hash = my_pubkey_hash

        self.donation_percentage = args.donation_percentage
        self.worker_fee = args.worker_fee
        
        self.net = self.node.net.PARENT
        self.running = True
        self.pseudoshare_received = variable.Event()
        self.share_received = variable.Event()
        self.local_rate_monitor = math.RateMonitor(10*60)
        self.local_addr_rate_monitor = math.RateMonitor(10*60)
        
        self.removed_unstales_var = variable.Variable((0, 0, 0))
        self.removed_doa_unstales_var = variable.Variable(0)
        
        self.last_work_shares = variable.Variable( {} )
        self.my_share_hashes = set()
        self.my_doa_share_hashes = set()

        self.address_throttle = 0

        # DONATION_SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
        # print data.script2_to_address(DONATION_SCRIPT, node.net.PARENT)
        # print data.address_to_pubkey_hash("1E482inuE9GckE6kXoX5sBCTD7g4rgGgfN",node.net.PARENT)


        self.tracker_view = forest.TrackerView(self.node.tracker, forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs,
            my_count=lambda share: 1 if share.hash in self.my_share_hashes else 0,
            my_doa_count=lambda share: 1 if share.hash in self.my_doa_share_hashes else 0,
            my_orphan_announce_count=lambda share: 1 if share.hash in self.my_share_hashes and share.share_data['stale_info'] == 'orphan' else 0,
            my_dead_announce_count=lambda share: 1 if share.hash in self.my_share_hashes and share.share_data['stale_info'] == 'doa' else 0,
        )))
        
        @self.node.tracker.verified.removed.watch
        def _(share):
            if share.hash in self.my_share_hashes and self.node.tracker.is_child_of(share.hash, self.node.best_share_var.value):
                assert share.share_data['stale_info'] in [None, 'orphan', 'doa'] # we made these shares in this instance
                self.removed_unstales_var.set((
                    self.removed_unstales_var.value[0] + 1,
                    self.removed_unstales_var.value[1] + (1 if share.share_data['stale_info'] == 'orphan' else 0),
                    self.removed_unstales_var.value[2] + (1 if share.share_data['stale_info'] == 'doa' else 0),
                ))
            if share.hash in self.my_doa_share_hashes and self.node.tracker.is_child_of(share.hash, self.node.best_share_var.value):
                self.removed_doa_unstales_var.set(self.removed_doa_unstales_var.value + 1)
        
        # MERGED WORK
        
        self.merged_work = variable.Variable({})
        
        @defer.inlineCallbacks
        def set_merged_work(merged_url, merged_userpass):
            merged_proxy = jsonrpc.HTTPProxy(merged_url, dict(Authorization='Basic ' + base64.b64encode(merged_userpass)))
            while self.running:
                auxblock = yield deferral.retry('Error while calling merged getauxblock on %s:' % (merged_url,), 30)(merged_proxy.rpc_getauxblock)()
                target = auxblock['target'] if 'target' in auxblock else auxblock['_target']
                self.merged_work.set(math.merge_dicts(self.merged_work.value, {auxblock['chainid']: dict(
                    hash=int(auxblock['hash'], 16),
                    target='p2pool' if target == 'p2pool' else pack.IntType(256).unpack(target.decode('hex')),
                    merged_proxy=merged_proxy,
                )}))
                yield deferral.sleep(1)
        for merged_url, merged_userpass in merged_urls:
            set_merged_work(merged_url, merged_userpass)
        
        @self.merged_work.changed.watch
        def _(new_merged_work):
            print 'Got new merged mining work!'
        
        # COMBINE WORK
        
        self.current_work = variable.Variable(None)
        def compute_work():
            t = self.node.bitcoind_work.value
            bb = self.node.best_block_header.value
            if bb is not None and bb['previous_block'] == t['previous_block'] and self.node.net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(bb)) <= t['bits'].target:
                print 'Skipping from block %x to block %x!' % (bb['previous_block'],
                    bitcoin_data.hash256(bitcoin_data.block_header_type.pack(bb)))
                t = dict(
                    version=bb['version'],
                    previous_block=bitcoin_data.hash256(bitcoin_data.block_header_type.pack(bb)),
                    bits=bb['bits'], # not always true
                    coinbaseflags='',
                    height=t['height'] + 1,
                    time=bb['timestamp'] + 600, # better way?
                    transactions=[],
                    transaction_fees=[],
                    merkle_link=bitcoin_data.calculate_merkle_link([None], 0),
                    subsidy=self.node.net.PARENT.SUBSIDY_FUNC(self.node.bitcoind_work.value['height']),
                    last_update=self.node.bitcoind_work.value['last_update'],
                )
            
            self.current_work.set(t)
        self.node.bitcoind_work.changed.watch(lambda _: compute_work())
        self.node.best_block_header.changed.watch(lambda _: compute_work())
        compute_work()
        
        self.new_work_event = variable.Event()
        @self.current_work.transitioned.watch
        def _(before, after):
            # trigger LP if version/previous_block/bits changed or transactions changed from nothing
            if any(before[x] != after[x] for x in ['version', 'previous_block', 'bits']) or (not before['transactions'] and after['transactions']):
                self.new_work_event.happened()
        self.merged_work.changed.watch(lambda _: self.new_work_event.happened())
        self.node.best_share_var.changed.watch(lambda _: self.new_work_event.happened())