code style fixes
[p2pool.git] / p2pool / bitcoin / p2p.py
1 '''
2 Implementation of Bitcoin's p2p protocol
3 '''
4
5 from __future__ import division
6
7 import hashlib
8 import random
9 import struct
10 import time
11 import zlib
12
13 from twisted.internet import defer, protocol, reactor, task
14 from twisted.python import log
15
16 from . import data as bitcoin_data
17 from p2pool.util import variable, datachunker, deferral
18
19 class BaseProtocol(protocol.Protocol):
20     def connectionMade(self):
21         self.dataReceived = datachunker.DataChunker(self.dataReceiver())
22     
23     def dataReceiver(self):
24         while True:
25             start = ''
26             while start != self._prefix:
27                 start = (start + (yield 1))[-len(self._prefix):]
28             
29             command = (yield 12).rstrip('\0')
30             length, = struct.unpack('<I', (yield 4))
31             
32             if length > self.max_net_payload_length:
33                 print 'length too long'
34                 continue
35             
36             if self.use_checksum:
37                 checksum = yield 4
38             else:
39                 checksum = None
40             
41             compressed_payload = yield length
42             
43             if self.compress:
44                 try:
45                     d = zlib.decompressobj()
46                     payload = d.decompress(compressed_payload, self.max_payload_length)
47                     if d.unconsumed_tail:
48                         print 'compressed payload expanded too much'
49                         continue
50                     assert not len(payload) > self.max_payload_length
51                 except:
52                     log.err(None, 'Failure decompressing message:')
53                     continue
54             else:
55                 if len(compressed_payload) > self.max_payload_length:
56                     print 'compressed payload expanded too much'
57                     continue
58                 payload = compressed_payload
59             
60             if checksum is not None:
61                 if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum:
62                     print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
63                     print 'INVALID HASH'
64                     continue
65             
66             type_ = getattr(self, 'message_' + command, None)
67             if type_ is None:
68                 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
69                 print 'NO TYPE FOR', repr(command)
70                 continue
71             
72             try:
73                 payload2 = type_.unpack(payload)
74             except:
75                 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
76                 log.err(None, 'Error parsing message: (see RECV line)')
77                 continue
78             
79             handler = getattr(self, 'handle_' + command, None)
80             if handler is None:
81                 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
82                 print 'NO HANDLER FOR', command
83                 continue
84             
85             #print 'RECV', command, repr(payload2)[:500]
86             
87             try:
88                 handler(**payload2)
89             except:
90                 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
91                 log.err(None, 'Error handling message: (see RECV line)')
92                 continue
93     
94     def sendPacket(self, command, payload2):
95         if len(command) >= 12:
96             raise ValueError('command too long')
97         type_ = getattr(self, 'message_' + command, None)
98         if type_ is None:
99             raise ValueError('invalid command')
100         #print 'SEND', command, repr(payload2)[:500]
101         payload = type_.pack(payload2)
102         if len(payload) > self.max_payload_length:
103             raise ValueError('payload too long')
104         if self.use_checksum:
105             checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4]
106         else:
107             checksum = ''
108         compressed_payload = zlib.compress(payload) if self.compress else payload
109         if len(compressed_payload) > self.max_net_payload_length:
110             raise ValueError('compressed payload too long')
111         data = self._prefix + struct.pack('<12sI', command, len(compressed_payload)) + checksum + compressed_payload
112         self.transport.write(data)
113     
114     def __getattr__(self, attr):
115         prefix = 'send_'
116         if attr.startswith(prefix):
117             command = attr[len(prefix):]
118             return lambda **payload2: self.sendPacket(command, payload2)
119         #return protocol.Protocol.__getattr__(self, attr)
120         raise AttributeError(attr)
121
122 class Protocol(BaseProtocol):
123     def __init__(self, net):
124         self._prefix = net.BITCOIN_P2P_PREFIX
125     
126     version = 0
127     
128     max_payload_length = max_net_payload_length = 1000000
129     
130     compress = False
131     @property
132     def use_checksum(self):
133         return self.version >= 209
134     
135     
136     null_order = '\0'*60
137     
138     def connectionMade(self):
139         BaseProtocol.connectionMade(self)
140         
141         self.send_version(
142             version=32200,
143             services=1,
144             time=int(time.time()),
145             addr_to=dict(
146                 services=1,
147                 address=self.transport.getPeer().host,
148                 port=self.transport.getPeer().port,
149             ),
150             addr_from=dict(
151                 services=1,
152                 address=self.transport.getHost().host,
153                 port=self.transport.getHost().port,
154             ),
155             nonce=random.randrange(2**64),
156             sub_version_num='',
157             start_height=0,
158         )
159     
160     message_version = bitcoin_data.ComposedType([
161         ('version', bitcoin_data.StructType('<I')),
162         ('services', bitcoin_data.StructType('<Q')),
163         ('time', bitcoin_data.StructType('<Q')),
164         ('addr_to', bitcoin_data.address_type),
165         ('addr_from', bitcoin_data.address_type),
166         ('nonce', bitcoin_data.StructType('<Q')),
167         ('sub_version_num', bitcoin_data.VarStrType()),
168         ('start_height', bitcoin_data.StructType('<I')),
169     ])
170     def handle_version(self, version, services, time, addr_to, addr_from, nonce, sub_version_num, start_height):
171         #print 'VERSION', locals()
172         self.version_after = version
173         self.send_verack()
174     
175     message_verack = bitcoin_data.ComposedType([])
176     def handle_verack(self):
177         self.version = self.version_after
178         
179         self.ready()
180     
181     def ready(self):
182         self.check_order = deferral.GenericDeferrer(2**256, lambda id, order: self.send_checkorder(id=id, order=order))
183         self.submit_order = deferral.GenericDeferrer(2**256, lambda id, order: self.send_submitorder(id=id, order=order))
184         self.get_block = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='block', hash=hash)]))
185         self.get_block_header = deferral.ReplyMatcher(lambda hash: self.send_getheaders(version=1, have=[], last=hash))
186         self.get_tx = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='tx', hash=hash)]))
187         
188         if hasattr(self.factory, 'resetDelay'):
189             self.factory.resetDelay()
190         if hasattr(self.factory, 'gotConnection'):
191             self.factory.gotConnection(self)
192     
193     message_inv = bitcoin_data.ComposedType([
194         ('invs', bitcoin_data.ListType(bitcoin_data.ComposedType([
195             ('type', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'tx': 1, 'block': 2})),
196             ('hash', bitcoin_data.HashType()),
197         ]))),
198     ])
199     def handle_inv(self, invs):
200         for inv in invs:
201             if inv['type'] == 'tx':
202                 self.factory.new_tx.happened(inv['hash'])
203             elif inv['type'] == 'block':
204                 self.factory.new_block.happened(inv['hash'])
205             else:
206                 print 'Unknown inv type', item
207     
208     message_getdata = bitcoin_data.ComposedType([
209         ('requests', bitcoin_data.ListType(bitcoin_data.ComposedType([
210             ('type', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'tx': 1, 'block': 2})),
211             ('hash', bitcoin_data.HashType()),
212         ]))),
213     ])
214     message_getblocks = bitcoin_data.ComposedType([
215         ('version', bitcoin_data.StructType('<I')),
216         ('have', bitcoin_data.ListType(bitcoin_data.HashType())),
217         ('last', bitcoin_data.PossiblyNone(0, bitcoin_data.HashType())),
218     ])
219     message_getheaders = bitcoin_data.ComposedType([
220         ('version', bitcoin_data.StructType('<I')),
221         ('have', bitcoin_data.ListType(bitcoin_data.HashType())),
222         ('last', bitcoin_data.PossiblyNone(0, bitcoin_data.HashType())),
223     ])
224     message_getaddr = bitcoin_data.ComposedType([])
225     message_checkorder = bitcoin_data.ComposedType([
226         ('id', bitcoin_data.HashType()),
227         ('order', bitcoin_data.FixedStrType(60)), # XXX
228     ])
229     message_submitorder = bitcoin_data.ComposedType([
230         ('id', bitcoin_data.HashType()),
231         ('order', bitcoin_data.FixedStrType(60)), # XXX
232     ])
233     
234     message_addr = bitcoin_data.ComposedType([
235         ('addrs', bitcoin_data.ListType(bitcoin_data.ComposedType([
236             ('timestamp', bitcoin_data.StructType('<I')),
237             ('address', bitcoin_data.address_type),
238         ]))),
239     ])
240     def handle_addr(self, addrs):
241         for addr in addrs:
242             pass
243     
244     message_tx = bitcoin_data.ComposedType([
245         ('tx', bitcoin_data.tx_type),
246     ])
247     def handle_tx(self, tx):
248         self.get_tx.got_response(bitcoin_data.tx_type.hash256(tx), tx)
249     
250     message_block = bitcoin_data.ComposedType([
251         ('block', bitcoin_data.block_type),
252     ])
253     def handle_block(self, block):
254         block_hash = bitcoin_data.block_header_type.hash256(block['header'])
255         self.get_block.got_response(block_hash, block)
256         self.get_block_header.got_response(block_hash, block['header'])
257     
258     message_headers = bitcoin_data.ComposedType([
259         ('headers', bitcoin_data.ListType(bitcoin_data.block_type)),
260     ])
261     def handle_headers(self, headers):
262         for header in headers:
263             header = header['header']
264             self.get_block_header.got_response(bitcoin_data.block_header_type.hash256(header), header)
265         self.factory.new_headers.happened([header['header'] for header in headers])
266     
267     message_reply = bitcoin_data.ComposedType([
268         ('hash', bitcoin_data.HashType()),
269         ('reply',  bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'success': 0, 'failure': 1, 'denied': 2})),
270         ('script', bitcoin_data.PossiblyNone('', bitcoin_data.VarStrType())),
271     ])
272     def handle_reply(self, hash, reply, script):
273         self.check_order.got_response(hash, dict(reply=reply, script=script))
274         self.submit_order.got_response(hash, dict(reply=reply, script=script))
275     
276     message_ping = bitcoin_data.ComposedType([])
277     def handle_ping(self):
278         pass
279     
280     message_alert = bitcoin_data.ComposedType([
281         ('message', bitcoin_data.VarStrType()),
282         ('signature', bitcoin_data.VarStrType()),
283     ])
284     def handle_alert(self, message, signature):
285         print 'ALERT:', (message, signature)
286     
287     def connectionLost(self, reason):
288         if hasattr(self.factory, 'gotConnection'):
289             self.factory.gotConnection(None)
290
291 class ClientFactory(protocol.ReconnectingClientFactory):
292     protocol = Protocol
293     
294     maxDelay = 15
295     
296     def __init__(self, net):
297         self.net = net
298         self.conn = variable.Variable(None)
299         
300         self.new_block = variable.Event()
301         self.new_tx = variable.Event()
302         self.new_headers = variable.Event()
303     
304     def buildProtocol(self, addr):
305         p = self.protocol(self.net)
306         p.factory = self
307         return p
308     
309     def gotConnection(self, conn):
310         self.conn.set(conn)
311     
312     def getProtocol(self):
313         return self.conn.get_not_none()
314
315 class HeaderWrapper(object):
316     target = 0
317     __slots__ = 'hash previous_hash'.split(' ')
318     
319     def __init__(self, header):
320         self.hash = bitcoin_data.block_header_type.hash256(header)
321         self.previous_hash = header['previous_block']
322
323 class HeightTracker(object):
324     '''Point this at a factory and let it take care of getting block heights'''
325     
326     def __init__(self, factory):
327         self.factory = factory
328         self.tracker = bitcoin_data.Tracker()
329         self.most_recent = None
330         
331         self._watch1 = self.factory.new_headers.watch(self.heard_headers)
332         self._watch2 = self.factory.new_block.watch(self.heard_block)
333         
334         self.requested = set()
335         self._clear_task = task.LoopingCall(self.requested.clear)
336         self._clear_task.start(60)
337         
338         self.last_notified_size = 0
339         
340         self.think()
341     
342     def think(self):
343         highest_head = max(self.tracker.heads, key=lambda h: self.tracker.get_height_and_last(h)[0]) if self.tracker.heads else None
344         height, last = self.tracker.get_height_and_last(highest_head)
345         cur = highest_head
346         cur_height = height
347         have = []
348         step = 1
349         while cur is not None:
350             have.append(cur)
351             if step > cur_height:
352                 break
353             cur = self.tracker.get_nth_parent_hash(cur, step)
354             cur_height -= step
355             if len(have) > 10:
356                 step *= 2
357         if height:
358             have.append(self.tracker.get_nth_parent_hash(highest_head, height - 1))
359         if not have:
360             have.append(0)
361         self.request(have, None)
362         
363         for tail in self.tracker.tails:
364             if tail is None:
365                 continue
366             self.request([], tail)
367         for head in self.tracker.heads:
368             if head == highest_head:
369                 continue
370             self.request([head], None)
371     
372     def heard_headers(self, headers):
373         for header in headers:
374             self.tracker.add(HeaderWrapper(header))
375         self.think()
376         
377         if len(self.tracker.shares) > self.last_notified_size + 10:
378             print 'Have %i block headers' % len(self.tracker.shares)
379             self.last_notified_size = len(self.tracker.shares)
380     
381     def heard_block(self, block_hash):
382         self.request([], block_hash)
383     
384     @defer.inlineCallbacks
385     def request(self, have, last):
386         if (tuple(have), last) in self.requested:
387             return
388         self.requested.add((tuple(have), last))
389         (yield self.factory.getProtocol()).send_getheaders(version=1, have=have, last=last)
390     
391     #@defer.inlineCallbacks
392     #XXX should defer?
393     def getHeight(self, block_hash):
394         height, last = self.tracker.get_height_and_last(block_hash)
395         if last is not None:
396             self.request([], last)
397             raise ValueError()
398         return height
399     
400     def get_min_height(self, block_hash):
401         height, last = self.tracker.get_height_and_last(block_hash)
402         if last is not None:
403             self.request([], last)
404         return height
405     
406     def get_highest_height(self):
407         return self.tracker.get_highest_height()
408     
409     def stop(self):
410         self.factory.new_headers.unwatch(self._watch1)
411         self.factory.new_block.unwatch(self._watch2)
412         self._clear_task.stop()
413
414 if __name__ == '__main__':
415     factory = ClientFactory(bitcoin_data.Mainnet)
416     reactor.connectTCP('127.0.0.1', 8333, factory)
417     h = HeightTracker(factory)
418     
419     @repr
420     @apply
421     @defer.inlineCallbacks
422     def think():
423         while True:
424             yield deferral.sleep(1)
425             print h.get_min_height(0xa285c3cb2a90ac7194cca034512748289e2526d9d7ae6ee7523)
426     
427     reactor.run()