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
15 from . import data as bitcoin_data
16 from p2pool.util import variable, datachunker, deferral, forest
18 class TooLong(Exception):
21 class BaseProtocol(protocol.Protocol):
22 def connectionMade(self):
23 self.dataReceived = datachunker.DataChunker(self.dataReceiver())
25 def dataReceiver(self):
28 while start != self._prefix:
29 start = (start + (yield 1))[-len(self._prefix):]
31 command = (yield 12).rstrip('\0')
32 length, = struct.unpack('<I', (yield 4))
34 if length > self.max_payload_length:
35 print 'length too large'
43 payload = yield length
45 if checksum is not None:
46 if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum:
47 print 'invalid hash for', repr(command), checksum.encode('hex') if checksum is not None else None, repr(payload[:100].encode('hex')), len(payload)
50 type_ = getattr(self, 'message_' + command, None)
52 print 'no type for', repr(command)
56 payload2 = type_.unpack(payload)
58 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
59 log.err(None, 'Error parsing message: (see RECV line)')
62 handler = getattr(self, 'handle_' + command, None)
64 print 'no handler for', repr(command)
70 print 'RECV', command, repr(payload2)[:100]
71 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.BITCOIN_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 = 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')),
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 = bitcoin_data.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 = 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()),
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 = 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()),
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())),
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())),
200 message_getaddr = bitcoin_data.ComposedType([])
201 message_checkorder = bitcoin_data.ComposedType([
202 ('id', bitcoin_data.HashType()),
203 ('order', bitcoin_data.FixedStrType(60)), # XXX
205 message_submitorder = bitcoin_data.ComposedType([
206 ('id', bitcoin_data.HashType()),
207 ('order', bitcoin_data.FixedStrType(60)), # XXX
210 message_addr = bitcoin_data.ComposedType([
211 ('addrs', bitcoin_data.ListType(bitcoin_data.ComposedType([
212 ('timestamp', bitcoin_data.StructType('<I')),
213 ('address', bitcoin_data.address_type),
216 def handle_addr(self, addrs):
220 message_tx = bitcoin_data.ComposedType([
221 ('tx', bitcoin_data.tx_type),
223 def handle_tx(self, tx):
224 self.get_tx.got_response(bitcoin_data.tx_type.hash256(tx), tx)
226 message_block = bitcoin_data.ComposedType([
227 ('block', bitcoin_data.block_type),
229 def handle_block(self, block):
230 block_hash = bitcoin_data.block_header_type.hash256(block['header'])
231 self.get_block.got_response(block_hash, block)
232 self.get_block_header.got_response(block_hash, block['header'])
234 message_headers = bitcoin_data.ComposedType([
235 ('headers', bitcoin_data.ListType(bitcoin_data.block_type)),
237 def handle_headers(self, headers):
238 for header in headers:
239 header = header['header']
240 self.get_block_header.got_response(bitcoin_data.block_header_type.hash256(header), header)
241 self.factory.new_headers.happened([header['header'] for header in headers])
243 message_reply = bitcoin_data.ComposedType([
244 ('hash', bitcoin_data.HashType()),
245 ('reply', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'success': 0, 'failure': 1, 'denied': 2})),
246 ('script', bitcoin_data.PossiblyNoneType('', bitcoin_data.VarStrType())),
249 message_ping = bitcoin_data.ComposedType([])
250 def handle_ping(self):
253 message_alert = bitcoin_data.ComposedType([
254 ('message', bitcoin_data.VarStrType()),
255 ('signature', bitcoin_data.VarStrType()),
257 def handle_alert(self, message, signature):
258 print 'ALERT:', (message, signature)
260 def connectionLost(self, reason):
261 if hasattr(self.factory, 'gotConnection'):
262 self.factory.gotConnection(None)
264 class ClientFactory(protocol.ReconnectingClientFactory):
269 def __init__(self, net):
271 self.conn = variable.Variable(None)
273 self.new_block = variable.Event()
274 self.new_tx = variable.Event()
275 self.new_headers = variable.Event()
277 def buildProtocol(self, addr):
278 p = self.protocol(self.net)
282 def gotConnection(self, conn):
285 def getProtocol(self):
286 return self.conn.get_not_none()
288 class HeaderWrapper(object):
290 __slots__ = 'hash previous_hash'.split(' ')
293 def from_header(cls, header):
294 return cls(bitcoin_data.block_header_type.hash256(header), header['previous_block'])
296 def __init__(self, hash, previous_hash):
297 self.hash, self.previous_hash = hash, previous_hash
299 class HeightTracker(object):
300 '''Point this at a factory and let it take care of getting block heights'''
302 def __init__(self, factory, backing):
303 self.factory = factory
304 self.tracker = forest.Tracker()
305 self.backing = backing
306 self.most_recent = None
308 self._watch1 = self.factory.new_headers.watch(self.heard_headers)
309 self._watch2 = self.factory.new_block.watch(self.heard_block)
311 self.requested = set()
312 self._clear_task = task.LoopingCall(self.requested.clear)
313 self._clear_task.start(60)
315 self.last_notified_size = 0
317 self.updated = variable.Event()
323 def _load_backing(self):
324 open(self.backing, 'ab').close()
325 with open(self.backing, 'rb') as f:
329 hash, previous_hash, checksum = (int(x, 16) for x in line.strip().split(' '))
331 print "skipping over bad data in headers.dat"
333 if (hash - previous_hash) % 2**256 != checksum:
334 print "checksum failed"
336 if previous_hash == 0: previous_hash = None
338 if count % 10000 == 0 and count: print count
339 if hash not in self.tracker.shares:
340 self.tracker.add(HeaderWrapper(hash, previous_hash))
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)
349 while cur is not None:
351 if step > cur_height:
353 cur = self.tracker.get_nth_parent_hash(cur, step)
358 have.append(self.tracker.get_nth_parent_hash(highest_head, height - 1))
361 self.request(have, None)
363 for tail in self.tracker.tails:
366 self.request([], tail)
367 for head in self.tracker.heads:
368 if head == highest_head:
370 self.request([head], None)
372 def heard_headers(self, headers):
374 b = open(self.backing, 'ab')
375 for header in headers:
376 hw = HeaderWrapper.from_header(header)
377 if hw.hash in self.tracker.shares:
381 hash, prev = hw.hash, 0 if hw.previous_hash is None else hw.previous_hash
382 b.write('%x %x %x\n' % (hash, prev, (hash - prev) % 2**256))
385 self.updated.happened()
388 if len(self.tracker.shares) > self.last_notified_size + 10:
389 print 'Have %i block headers' % len(self.tracker.shares)
390 self.last_notified_size = len(self.tracker.shares)
392 def heard_block(self, block_hash):
393 self.request([], block_hash)
395 @defer.inlineCallbacks
396 def request(self, have, last):
397 if (tuple(have), last) in self.requested:
399 self.requested.add((tuple(have), last))
400 (yield self.factory.getProtocol()).send_getheaders(version=1, have=have, last=last)
402 def getHeight(self, block_hash):
403 height, last = self.tracker.get_height_and_last(block_hash)
405 #self.request([], last)
409 def get_min_height(self, block_hash):
410 height, last = self.tracker.get_height_and_last(block_hash)
411 #if last is not None:
412 # self.request([], last)
415 def get_highest_height(self):
416 return self.tracker.get_highest_height()
419 self.factory.new_headers.unwatch(self._watch1)
420 self.factory.new_block.unwatch(self._watch2)
421 self._clear_task.stop()
423 if __name__ == '__main__':
424 factory = ClientFactory(bitcoin_data.BitcoinMainnet)
425 reactor.connectTCP('127.0.0.1', 8333, factory)
426 h = HeightTracker(factory)
430 @defer.inlineCallbacks
433 yield deferral.sleep(1)
434 print h.get_min_height(0xa285c3cb2a90ac7194cca034512748289e2526d9d7ae6ee7523)