2 Implementation of Bitcoin's p2p protocol
5 from __future__ import division
12 from twisted.internet import defer, protocol, reactor, task
13 from twisted.python import log
16 from . import data as bitcoin_data, getwork
17 from p2pool.util import variable, datachunker, deferral, forest, pack
19 class TooLong(Exception):
22 class BaseProtocol(protocol.Protocol):
23 def connectionMade(self):
24 self.dataReceived = datachunker.DataChunker(self.dataReceiver())
26 def dataReceiver(self):
29 while start != self._prefix:
30 start = (start + (yield 1))[-len(self._prefix):]
32 command = (yield 12).rstrip('\0')
33 length, = struct.unpack('<I', (yield 4))
35 if length > self.max_payload_length:
36 print 'length too large'
44 payload = yield length
46 if checksum is not None:
47 if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum:
48 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')
51 type_ = getattr(self, 'message_' + command, None)
54 print 'no type for', repr(command)
58 payload2 = type_.unpack(payload)
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)')
64 self.packetReceived(command, payload2)
66 def packetReceived(self, command, payload2):
67 handler = getattr(self, 'handle_' + command, None)
70 print 'no handler for', repr(command)
76 print 'RECV', command, repr(payload2)[:100]
77 log.err(None, 'Error handling message: (see RECV line)')
79 def sendPacket(self, command, payload2):
80 if len(command) >= 12:
81 raise ValueError('command too long')
82 type_ = getattr(self, 'message_' + command, 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')
90 checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4]
93 data = self._prefix + struct.pack('<12sI', command, len(payload)) + checksum + payload
94 self.transport.write(data)
96 def __getattr__(self, attr):
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)
104 class Protocol(BaseProtocol):
105 def __init__(self, net):
106 self._prefix = net.P2P_PREFIX
110 max_payload_length = 1000000
113 def use_checksum(self):
114 return self.version >= 209
116 def connectionMade(self):
117 BaseProtocol.connectionMade(self)
122 time=int(time.time()),
125 address=self.transport.getPeer().host,
126 port=self.transport.getPeer().port,
130 address=self.transport.getHost().host,
131 port=self.transport.getHost().port,
133 nonce=random.randrange(2**64),
138 message_version = pack.ComposedType([
139 ('version', pack.IntType(32)),
140 ('services', pack.IntType(64)),
141 ('time', pack.IntType(64)),
142 ('addr_to', bitcoin_data.address_type),
143 ('addr_from', bitcoin_data.address_type),
144 ('nonce', pack.IntType(64)),
145 ('sub_version_num', pack.VarStrType()),
146 ('start_height', pack.IntType(32)),
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
153 message_verack = pack.ComposedType([])
154 def handle_verack(self):
155 self.version = self.version_after
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)]))
164 if hasattr(self.factory, 'resetDelay'):
165 self.factory.resetDelay()
166 if hasattr(self.factory, 'gotConnection'):
167 self.factory.gotConnection(self)
169 message_inv = pack.ComposedType([
170 ('invs', pack.ListType(pack.ComposedType([
171 ('type', pack.EnumType(pack.IntType(32), {'tx': 1, 'block': 2})),
172 ('hash', pack.IntType(256)),
175 def handle_inv(self, 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'])
182 print 'Unknown inv type', item
184 message_getdata = pack.ComposedType([
185 ('requests', pack.ListType(pack.ComposedType([
186 ('type', pack.EnumType(pack.IntType(32), {'tx': 1, 'block': 2})),
187 ('hash', pack.IntType(256)),
190 message_getblocks = pack.ComposedType([
191 ('version', pack.IntType(32)),
192 ('have', pack.ListType(pack.IntType(256))),
193 ('last', pack.PossiblyNoneType(0, pack.IntType(256))),
195 message_getheaders = pack.ComposedType([
196 ('version', pack.IntType(32)),
197 ('have', pack.ListType(pack.IntType(256))),
198 ('last', pack.PossiblyNoneType(0, pack.IntType(256))),
200 message_getaddr = pack.ComposedType([])
202 message_addr = pack.ComposedType([
203 ('addrs', pack.ListType(pack.ComposedType([
204 ('timestamp', pack.IntType(32)),
205 ('address', bitcoin_data.address_type),
208 def handle_addr(self, addrs):
212 message_tx = pack.ComposedType([
213 ('tx', bitcoin_data.tx_type),
215 def handle_tx(self, tx):
216 self.get_tx.got_response(bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx)), tx)
218 message_block = pack.ComposedType([
219 ('block', bitcoin_data.block_type),
221 def handle_block(self, block):
222 block_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(block['header']))
223 self.get_block.got_response(block_hash, block)
224 self.get_block_header.got_response(block_hash, block['header'])
226 message_headers = pack.ComposedType([
227 ('headers', pack.ListType(bitcoin_data.block_type)),
229 def handle_headers(self, headers):
230 for header in headers:
231 header = header['header']
232 self.get_block_header.got_response(bitcoin_data.hash256(bitcoin_data.block_header_type.pack(header)), header)
233 self.factory.new_headers.happened([header['header'] for header in headers])
235 message_ping = pack.ComposedType([])
236 def handle_ping(self):
239 message_alert = pack.ComposedType([
240 ('message', pack.VarStrType()),
241 ('signature', pack.VarStrType()),
243 def handle_alert(self, message, signature):
244 print 'ALERT:', (message, signature)
246 def connectionLost(self, reason):
247 if hasattr(self.factory, 'gotConnection'):
248 self.factory.gotConnection(None)
250 class ClientFactory(protocol.ReconnectingClientFactory):
255 def __init__(self, net):
257 self.conn = variable.Variable(None)
259 self.new_block = variable.Event()
260 self.new_tx = variable.Event()
261 self.new_headers = variable.Event()
263 def buildProtocol(self, addr):
264 p = self.protocol(self.net)
268 def gotConnection(self, conn):
271 def getProtocol(self):
272 return self.conn.get_not_none()
274 class HeaderWrapper(object):
276 __slots__ = 'hash previous_hash'.split(' ')
279 def from_header(cls, header):
280 return cls(bitcoin_data.hash256(bitcoin_data.block_header_type.pack(header)), header['previous_block'])
282 def __init__(self, hash, previous_hash):
283 self.hash, self.previous_hash = hash, previous_hash
285 class HeightTracker(object):
286 '''Point this at a factory and let it take care of getting block heights'''
288 def __init__(self, rpc_proxy, factory, backlog_needed=1000):
289 self._rpc_proxy = rpc_proxy
290 self._factory = factory
291 self._backlog_needed = backlog_needed
293 self._tracker = forest.Tracker()
295 self._watch1 = self._factory.new_headers.watch(self._heard_headers)
296 self._watch2 = self._factory.new_block.watch(self._heard_block)
298 self._requested = set()
299 self._clear_task = task.LoopingCall(self._requested.clear)
300 self._clear_task.start(60)
302 self._last_notified_size = 0
304 self.updated = variable.Event()
306 self._think_task = task.LoopingCall(self._think)
307 self._think_task.start(15)
308 self._think2_task = task.LoopingCall(self._think2)
309 self._think2_task.start(15)
313 highest_head = max(self._tracker.heads, key=lambda h: self._tracker.get_height_and_last(h)[0]) if self._tracker.heads else None
314 if highest_head is None:
315 return # wait for think2
316 height, last = self._tracker.get_height_and_last(highest_head)
317 if height < self._backlog_needed:
320 log.err(None, 'Error in HeightTracker._think:')
322 @defer.inlineCallbacks
325 ba = getwork.BlockAttempt.from_getwork((yield self._rpc_proxy.rpc_getwork()))
326 self._request(ba.previous_block)
328 log.err(None, 'Error in HeightTracker._think2:')
330 def _heard_headers(self, headers):
332 for header in headers:
333 hw = HeaderWrapper.from_header(header)
334 if hw.hash in self._tracker.shares:
337 self._tracker.add(hw)
339 self.updated.happened()
342 if len(self._tracker.shares) >= self._last_notified_size + 100:
343 print 'Have %i/%i block headers' % (len(self._tracker.shares), self._backlog_needed)
344 self._last_notified_size = len(self._tracker.shares)
346 def _heard_block(self, block_hash):
347 self._request(block_hash)
349 @defer.inlineCallbacks
350 def _request(self, last):
351 if last in self._tracker.shares:
353 if last in self._requested:
355 self._requested.add(last)
356 (yield self._factory.getProtocol()).send_getheaders(version=1, have=[], last=last)
358 def get_height_rel_highest(self, block_hash):
359 # callers: highest height can change during yields!
360 height, last = self._tracker.get_height_and_last(block_hash)
361 if last not in self._tracker.tails:
362 return -1000000000 # XXX hack
363 return height - max(self._tracker.get_height(head_hash) for head_hash in self._tracker.tails[last])
366 self._factory.new_headers.unwatch(self._watch1)
367 self._factory.new_block.unwatch(self._watch2)
368 self._clear_task.stop()
369 self._think_task.stop()
370 self._think2_task.stop()
372 if __name__ == '__main__':
373 from . import networks
374 factory = ClientFactory(networks.BitcoinMainnet)
375 reactor.connectTCP('127.0.0.1', 8333, factory)
379 @defer.inlineCallbacks
382 print (yield (yield factory.getProtocol()).get_block(0x000000000000003aaaf7638f9f9c0d0c60e8b0eb817dcdb55fd2b1964efc5175))
383 except defer.TimeoutError: