X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=p2pool%2Fbitcoin%2Fp2p.py;h=d6f50c22b54f2419825c6299a35955ce924115fd;hb=ae8740b9d8a417e6b4fd119f2cc646d86d0ae639;hp=b64772888b5984ce05186f1d593c24c52cb0f9e3;hpb=5b169530ddd46572b4fc76644e62dafc4f2d3eab;p=p2pool.git diff --git a/p2pool/bitcoin/p2p.py b/p2pool/bitcoin/p2p.py index b647728..d6f50c2 100644 --- a/p2pool/bitcoin/p2p.py +++ b/p2pool/bitcoin/p2p.py @@ -6,7 +6,7 @@ import random import sys import time -from twisted.internet import protocol, task +from twisted.internet import protocol import p2pool from . import data as bitcoin_data @@ -14,11 +14,11 @@ from p2pool.util import deferral, p2protocol, pack, variable class Protocol(p2protocol.Protocol): def __init__(self, net): - p2protocol.Protocol.__init__(self, net.P2P_PREFIX, 1000000) + p2protocol.Protocol.__init__(self, net.P2P_PREFIX, 1000000, ignore_trailing_payload=True) def connectionMade(self): self.send_version( - version=32200, + version=70002, services=1, time=int(time.time()), addr_to=dict( @@ -32,7 +32,7 @@ class Protocol(p2protocol.Protocol): port=self.transport.getHost().port, ), nonce=random.randrange(2**64), - sub_version_num='', + sub_version_num='/P2Pool:%s/' % (p2pool.__version__,), start_height=0, ) @@ -53,34 +53,33 @@ class Protocol(p2protocol.Protocol): def handle_verack(self): self.get_block = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='block', hash=hash)])) self.get_block_header = deferral.ReplyMatcher(lambda hash: self.send_getheaders(version=1, have=[], last=hash)) - self.get_tx = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='tx', hash=hash)])) if hasattr(self.factory, 'resetDelay'): self.factory.resetDelay() if hasattr(self.factory, 'gotConnection'): self.factory.gotConnection(self) - self.pinger = task.LoopingCall(self.send_ping) + self.pinger = deferral.RobustLoopingCall(self.send_ping, nonce=1234) self.pinger.start(30) message_inv = pack.ComposedType([ ('invs', pack.ListType(pack.ComposedType([ - ('type', pack.EnumType(pack.IntType(32), {'tx': 1, 'block': 2})), + ('type', pack.EnumType(pack.IntType(32), {1: 'tx', 2: 'block'})), ('hash', pack.IntType(256)), ]))), ]) def handle_inv(self, invs): for inv in invs: if inv['type'] == 'tx': - self.factory.new_tx.happened(inv['hash']) + self.send_getdata(requests=[inv]) elif inv['type'] == 'block': self.factory.new_block.happened(inv['hash']) else: - print 'Unknown inv type', item + print 'Unknown inv type', inv message_getdata = pack.ComposedType([ ('requests', pack.ListType(pack.ComposedType([ - ('type', pack.EnumType(pack.IntType(32), {'tx': 1, 'block': 2})), + ('type', pack.EnumType(pack.IntType(32), {1: 'tx', 2: 'block'})), ('hash', pack.IntType(256)), ]))), ]) @@ -110,7 +109,7 @@ class Protocol(p2protocol.Protocol): ('tx', bitcoin_data.tx_type), ]) def handle_tx(self, tx): - self.get_tx.got_response(bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx)), tx) + self.factory.new_tx.happened(tx) message_block = pack.ComposedType([ ('block', bitcoin_data.block_type), @@ -129,8 +128,16 @@ class Protocol(p2protocol.Protocol): self.get_block_header.got_response(bitcoin_data.hash256(bitcoin_data.block_header_type.pack(header)), header) self.factory.new_headers.happened([header['header'] for header in headers]) - message_ping = pack.ComposedType([]) - def handle_ping(self): + message_ping = pack.ComposedType([ + ('nonce', pack.IntType(64)), + ]) + def handle_ping(self, nonce): + self.send_pong(nonce=nonce) + + message_pong = pack.ComposedType([ + ('nonce', pack.IntType(64)), + ]) + def handle_pong(self, nonce): pass message_alert = pack.ComposedType([ @@ -138,7 +145,7 @@ class Protocol(p2protocol.Protocol): ('signature', pack.VarStrType()), ]) def handle_alert(self, message, signature): - print 'ALERT:', (message, signature) + pass # print 'ALERT:', (message, signature) def connectionLost(self, reason): if hasattr(self.factory, 'gotConnection'):