add detection of submitblock on init

This commit is contained in:
Alan Penner 2014-01-20 15:33:47 -08:00 committed by Ahmed Bodiwala
parent 47d827ec98
commit 009519e295
4 changed files with 84 additions and 1 deletions

View File

@ -39,7 +39,24 @@ class BitcoinRPC(object):
'params': params,
'id': '1',
}))
@defer.inlineCallbacks
def check_submitblock(self):
try:
log.info("Checking for submitblock")
resp = (yield self._call('submitblock', []))
defer.returnValue(None)
except Exception, e:
if (str(e) == "404 Not Found"):
log.debug("No submitblock detected.")
defer.returnValue(False)
elif (str(e) == "500 Internal Server Error"):
log.debug("submitblock detected.")
defer.returnValue(True)
else:
log.debug("unknown submitblock check result.")
defer.returnValue(None)
@defer.inlineCallbacks
def submitblock(self, block_hex, hash_hex):
# Try submitblock if that fails, go to getblocktemplate

View File

@ -89,6 +89,12 @@ class BitcoinRPCManager(object):
return self.conns[self.curr_conn]._call(method,params)
except:
self.next_connection()
def check_submitblock(self):
while True:
try:
return self.conns[self.curr_conn].check_submitblock()
except:
self.next_connection()
def submitblock(self, block_hex, hash_hex):
while True:

44
lib/ircbot.py.save Normal file
View File

@ -0,0 +1,44 @@
if args.irc_announce:
from twisted.words.protocols import irc
class IRCClient(irc.IRCClient):
nickname = 'xpool%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())

View File

@ -39,6 +39,22 @@ def setup(on_startup):
# - we are not still downloading the blockchain (Sleep)
log.info("Connecting to litecoind...")
while True:
try:
result = (yield bitcoin_rpc.check_submitblock())
if result == True:
log.info("Found submitblock")
elif result == False:
log.info("Did not find submitblock")
else:
log.info("unknown submitblock result")
except ConnectionRefusedError, e:
log.error("Connection refused while trying to connect to the coind (are your COIND_* settings correct?)")
reactor.stop()
break
except Exception, e:
log.debug(str(e))
try:
result = (yield bitcoin_rpc.getblocktemplate())
if isinstance(result, dict):