rewrote chain scoring function for better performance and more obvious behavior
[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
12 from twisted.internet import defer, protocol, reactor, task
13 from twisted.python import log
14
15 import p2pool
16 from . import data as bitcoin_data, getwork
17 from p2pool.util import variable, datachunker, deferral, forest
18
19 class TooLong(Exception):
20     pass
21
22 class BaseProtocol(protocol.Protocol):
23     def connectionMade(self):
24         self.dataReceived = datachunker.DataChunker(self.dataReceiver())
25     
26     def dataReceiver(self):
27         while True:
28             start = ''
29             while start != self._prefix:
30                 start = (start + (yield 1))[-len(self._prefix):]
31             
32             command = (yield 12).rstrip('\0')
33             length, = struct.unpack('<I', (yield 4))
34             
35             if length > self.max_payload_length:
36                 print 'length too large'
37                 continue
38             
39             if self.use_checksum:
40                 checksum = yield 4
41             else:
42                 checksum = None
43             
44             payload = yield length
45             
46             if checksum is not None:
47                 if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum:
48                     print 'invalid hash for', repr(command), checksum.encode('hex') if checksum is not None else None, repr(payload[:100].encode('hex')), len(payload)
49                     continue
50             
51             type_ = getattr(self, 'message_' + command, None)
52             if type_ is None:
53                 if p2pool.DEBUG:
54                     print 'no type for', repr(command)
55                 continue
56             
57             try:
58                 payload2 = type_.unpack(payload)
59             except:
60                 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
61                 log.err(None, 'Error parsing message: (see RECV line)')
62                 continue
63             
64             self.packetReceived(command, payload2)
65     
66     def packetReceived(self, command, payload2):
67         handler = getattr(self, 'handle_' + command, None)
68         if handler is None:
69             if p2pool.DEBUG:
70                 print 'no handler for', repr(command)
71             return
72         
73         try:
74             handler(**payload2)
75         except:
76             print 'RECV', command, repr(payload2)[:100]
77             log.err(None, 'Error handling message: (see RECV line)')
78     
79     def sendPacket(self, command, payload2):
80         if len(command) >= 12:
81             raise ValueError('command too long')
82         type_ = getattr(self, 'message_' + command, None)
83         if type_ is None:
84             raise ValueError('invalid command')
85         #print 'SEND', command, repr(payload2)[:500]
86         payload = type_.pack(payload2)
87         if len(payload) > self.max_payload_length:
88             raise TooLong('payload too long')
89         if self.use_checksum:
90             checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4]
91         else:
92             checksum = ''
93         data = self._prefix + struct.pack('<12sI', command, len(payload)) + checksum + payload
94         self.transport.write(data)
95     
96     def __getattr__(self, attr):
97         prefix = 'send_'
98         if attr.startswith(prefix):
99             command = attr[len(prefix):]
100             return lambda **payload2: self.sendPacket(command, payload2)
101         #return protocol.Protocol.__getattr__(self, attr)
102         raise AttributeError(attr)
103
104 class Protocol(BaseProtocol):
105     def __init__(self, net):
106         self._prefix = net.P2P_PREFIX
107     
108     version = 0
109     
110     max_payload_length = 1000000
111     
112     @property
113     def use_checksum(self):
114         return self.version >= 209
115     
116     def connectionMade(self):
117         BaseProtocol.connectionMade(self)
118         
119         self.send_version(
120             version=32200,
121             services=1,
122             time=int(time.time()),
123             addr_to=dict(
124                 services=1,
125                 address=self.transport.getPeer().host,
126                 port=self.transport.getPeer().port,
127             ),
128             addr_from=dict(
129                 services=1,
130                 address=self.transport.getHost().host,
131                 port=self.transport.getHost().port,
132             ),
133             nonce=random.randrange(2**64),
134             sub_version_num='',
135             start_height=0,
136         )
137     
138     message_version = bitcoin_data.ComposedType([
139         ('version', bitcoin_data.StructType('<I')),
140         ('services', bitcoin_data.StructType('<Q')),
141         ('time', bitcoin_data.StructType('<Q')),
142         ('addr_to', bitcoin_data.address_type),
143         ('addr_from', bitcoin_data.address_type),
144         ('nonce', bitcoin_data.StructType('<Q')),
145         ('sub_version_num', bitcoin_data.VarStrType()),
146         ('start_height', bitcoin_data.StructType('<I')),
147     ])
148     def handle_version(self, version, services, time, addr_to, addr_from, nonce, sub_version_num, start_height):
149         #print 'VERSION', locals()
150         self.version_after = version
151         self.send_verack()
152     
153     message_verack = bitcoin_data.ComposedType([])
154     def handle_verack(self):
155         self.version = self.version_after
156         
157         self.ready()
158     
159     def ready(self):
160         self.get_block = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='block', hash=hash)]))
161         self.get_block_header = deferral.ReplyMatcher(lambda hash: self.send_getheaders(version=1, have=[], last=hash))
162         self.get_tx = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='tx', hash=hash)]))
163         
164         if hasattr(self.factory, 'resetDelay'):
165             self.factory.resetDelay()
166         if hasattr(self.factory, 'gotConnection'):
167             self.factory.gotConnection(self)
168     
169     message_inv = bitcoin_data.ComposedType([
170         ('invs', bitcoin_data.ListType(bitcoin_data.ComposedType([
171             ('type', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'tx': 1, 'block': 2})),
172             ('hash', bitcoin_data.HashType()),
173         ]))),
174     ])
175     def handle_inv(self, invs):
176         for inv in invs:
177             if inv['type'] == 'tx':
178                 self.factory.new_tx.happened(inv['hash'])
179             elif inv['type'] == 'block':
180                 self.factory.new_block.happened(inv['hash'])
181             else:
182                 print 'Unknown inv type', item
183     
184     message_getdata = bitcoin_data.ComposedType([
185         ('requests', bitcoin_data.ListType(bitcoin_data.ComposedType([
186             ('type', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'tx': 1, 'block': 2})),
187             ('hash', bitcoin_data.HashType()),
188         ]))),
189     ])
190     message_getblocks = bitcoin_data.ComposedType([
191         ('version', bitcoin_data.StructType('<I')),
192         ('have', bitcoin_data.ListType(bitcoin_data.HashType())),
193         ('last', bitcoin_data.PossiblyNoneType(0, bitcoin_data.HashType())),
194     ])
195     message_getheaders = bitcoin_data.ComposedType([
196         ('version', bitcoin_data.StructType('<I')),
197         ('have', bitcoin_data.ListType(bitcoin_data.HashType())),
198         ('last', bitcoin_data.PossiblyNoneType(0, bitcoin_data.HashType())),
199     ])
200     message_getaddr = bitcoin_data.ComposedType([])
201     
202     message_addr = bitcoin_data.ComposedType([
203         ('addrs', bitcoin_data.ListType(bitcoin_data.ComposedType([
204             ('timestamp', bitcoin_data.StructType('<I')),
205             ('address', bitcoin_data.address_type),
206         ]))),
207     ])
208     def handle_addr(self, addrs):
209         for addr in addrs:
210             pass
211     
212     message_tx = bitcoin_data.ComposedType([
213         ('tx', bitcoin_data.tx_type),
214     ])
215     def handle_tx(self, tx):
216         self.get_tx.got_response(bitcoin_data.tx_type.hash256(tx), tx)
217     
218     message_block = bitcoin_data.ComposedType([
219         ('block', bitcoin_data.block_type),
220     ])
221     def handle_block(self, block):
222         block_hash = bitcoin_data.block_header_type.hash256(block['header'])
223         self.get_block.got_response(block_hash, block)
224         self.get_block_header.got_response(block_hash, block['header'])
225     
226     message_headers = bitcoin_data.ComposedType([
227         ('headers', bitcoin_data.ListType(bitcoin_data.block_type)),
228     ])
229     def handle_headers(self, headers):
230         for header in headers:
231             header = header['header']
232             self.get_block_header.got_response(bitcoin_data.block_header_type.hash256(header), header)
233         self.factory.new_headers.happened([header['header'] for header in headers])
234     
235     message_reply = bitcoin_data.ComposedType([
236         ('hash', bitcoin_data.HashType()),
237         ('reply',  bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'success': 0, 'failure': 1, 'denied': 2})),
238         ('script', bitcoin_data.PossiblyNoneType('', bitcoin_data.VarStrType())),
239     ])
240     
241     message_ping = bitcoin_data.ComposedType([])
242     def handle_ping(self):
243         pass
244     
245     message_alert = bitcoin_data.ComposedType([
246         ('message', bitcoin_data.VarStrType()),
247         ('signature', bitcoin_data.VarStrType()),
248     ])
249     def handle_alert(self, message, signature):
250         print 'ALERT:', (message, signature)
251     
252     def connectionLost(self, reason):
253         if hasattr(self.factory, 'gotConnection'):
254             self.factory.gotConnection(None)
255
256 class ClientFactory(protocol.ReconnectingClientFactory):
257     protocol = Protocol
258     
259     maxDelay = 1
260     
261     def __init__(self, net):
262         self.net = net
263         self.conn = variable.Variable(None)
264         
265         self.new_block = variable.Event()
266         self.new_tx = variable.Event()
267         self.new_headers = variable.Event()
268     
269     def buildProtocol(self, addr):
270         p = self.protocol(self.net)
271         p.factory = self
272         return p
273     
274     def gotConnection(self, conn):
275         self.conn.set(conn)
276     
277     def getProtocol(self):
278         return self.conn.get_not_none()
279
280 class HeaderWrapper(object):
281     target = 2**256 - 1
282     __slots__ = 'hash previous_hash'.split(' ')
283     
284     @classmethod
285     def from_header(cls, header):
286         return cls(bitcoin_data.block_header_type.hash256(header), header['previous_block'])
287     
288     def __init__(self, hash, previous_hash):
289         self.hash, self.previous_hash = hash, previous_hash
290
291 class HeightTracker(object):
292     '''Point this at a factory and let it take care of getting block heights'''
293     
294     def __init__(self, rpc_proxy, factory, backlog_needed=1000):
295         self._rpc_proxy = rpc_proxy
296         self._factory = factory
297         self._backlog_needed = backlog_needed
298         
299         self._tracker = forest.Tracker()
300         
301         self._watch1 = self._factory.new_headers.watch(self._heard_headers)
302         self._watch2 = self._factory.new_block.watch(self._heard_block)
303         
304         self._requested = set()
305         self._clear_task = task.LoopingCall(self._requested.clear)
306         self._clear_task.start(60)
307         
308         self._last_notified_size = 0
309         
310         self.updated = variable.Event()
311         
312         self._think_task = task.LoopingCall(self._think)
313         self._think_task.start(15)
314         self._think2_task = task.LoopingCall(self._think2)
315         self._think2_task.start(15)
316     
317     def _think(self):
318         try:
319             highest_head = max(self._tracker.heads, key=lambda h: self._tracker.get_height_and_last(h)[0]) if self._tracker.heads else None
320             if highest_head is None:
321                 return # wait for think2
322             height, last = self._tracker.get_height_and_last(highest_head)
323             if height < self._backlog_needed:
324                 self._request(last)
325         except:
326             log.err(None, 'Error in HeightTracker._think:')
327     
328     @defer.inlineCallbacks
329     def _think2(self):
330         try:
331             ba = getwork.BlockAttempt.from_getwork((yield self._rpc_proxy.rpc_getwork()))
332             self._request(ba.previous_block)
333         except:
334             log.err(None, 'Error in HeightTracker._think2:')
335     
336     def _heard_headers(self, headers):
337         changed = False
338         for header in headers:
339             hw = HeaderWrapper.from_header(header)
340             if hw.hash in self._tracker.shares:
341                 continue
342             changed = True
343             self._tracker.add(hw)
344         if changed:
345             self.updated.happened()
346         self._think()
347         
348         if len(self._tracker.shares) >= self._last_notified_size + 100:
349             print 'Have %i/%i block headers' % (len(self._tracker.shares), self._backlog_needed)
350             self._last_notified_size = len(self._tracker.shares)
351     
352     def _heard_block(self, block_hash):
353         self._request(block_hash)
354     
355     @defer.inlineCallbacks
356     def _request(self, last):
357         if last in self._tracker.shares:
358             return
359         if last in self._requested:
360             return
361         self._requested.add(last)
362         (yield self._factory.getProtocol()).send_getheaders(version=1, have=[], last=last)
363     
364     def get_height_rel_highest(self, block_hash):
365         # callers: highest height can change during yields!
366         height, last = self._tracker.get_height_and_last(block_hash)
367         if last not in self._tracker.tails:
368             return -1000000000 # XXX hack
369         return height - max(self._tracker.get_height(head_hash) for head_hash in self._tracker.tails[last])
370     
371     def stop(self):
372         self._factory.new_headers.unwatch(self._watch1)
373         self._factory.new_block.unwatch(self._watch2)
374         self._clear_task.stop()
375         self._think_task.stop()
376         self._think2_task.stop()
377
378 if __name__ == '__main__':
379     from . import networks
380     factory = ClientFactory(networks.BitcoinMainnet)
381     reactor.connectTCP('127.0.0.1', 8333, factory)
382     
383     @repr
384     @apply
385     @defer.inlineCallbacks
386     def think():
387         try:
388             print (yield (yield factory.getProtocol()).get_block(0x000000000000003aaaf7638f9f9c0d0c60e8b0eb817dcdb55fd2b1964efc5175))
389         except defer.TimeoutError:
390             print "timeout"
391         reactor.stop()
392     
393     reactor.run()