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