2 Implementation of Bitcoin's p2p protocol
5 from __future__ import division
13 from twisted.internet import protocol, reactor
15 from . import data as bitcoin_data
16 from p2pool.util import variable, datachunker, deferral
18 class BaseProtocol(protocol.Protocol):
19 def connectionMade(self):
20 self.dataReceived = datachunker.DataChunker(self.dataReceiver())
22 def dataReceiver(self):
25 while start != self._prefix:
26 start = (start + (yield 1))[-len(self._prefix):]
28 command = (yield 12).rstrip('\0')
29 length, = struct.unpack('<I', (yield 4))
36 payload = yield length
38 if checksum is not None:
39 if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum:
40 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
44 type_ = getattr(self, "message_" + command, None)
46 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
47 print 'NO TYPE FOR', repr(command)
51 payload2 = type_.unpack(payload)
53 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
57 handler = getattr(self, 'handle_' + command, None)
59 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
60 print 'NO HANDLER FOR', command
63 #print 'RECV', command, payload2
68 print 'RECV', command, checksum.encode('hex') if checksum is not None else None, repr(payload.encode('hex')), len(payload)
72 def sendPacket(self, command, payload2={}):
73 type_ = getattr(self, "message_" + command, None)
75 raise ValueError('invalid command')
76 payload = type_.pack(payload2)
77 if len(command) >= 12:
78 raise ValueError('command too long')
80 checksum = hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4]
83 data = self._prefix + struct.pack('<12sI', command, len(payload)) + checksum + payload
84 self.transport.write(data)
85 #print 'SEND', command, payload2
87 def __getattr__(self, attr):
89 if attr.startswith(prefix):
90 command = attr[len(prefix):]
91 return lambda **payload2: self.sendPacket(command, payload2)
92 #return protocol.Protocol.__getattr__(self, attr)
93 raise AttributeError(attr)
95 class Protocol(BaseProtocol):
96 def __init__(self, net):
97 self._prefix = net.BITCOIN_P2P_PREFIX
102 def use_checksum(self):
103 return self.version >= 209
108 def connectionMade(self):
109 BaseProtocol.connectionMade(self)
114 time=int(time.time()),
117 address=self.transport.getPeer().host,
118 port=self.transport.getPeer().port,
122 address=self.transport.getHost().host,
123 port=self.transport.getHost().port,
125 nonce=random.randrange(2**64),
130 message_version = bitcoin_data.ComposedType([
131 ('version', bitcoin_data.StructType('<I')),
132 ('services', bitcoin_data.StructType('<Q')),
133 ('time', bitcoin_data.StructType('<Q')),
134 ('addr_to', bitcoin_data.address_type),
135 ('addr_from', bitcoin_data.address_type),
136 ('nonce', bitcoin_data.StructType('<Q')),
137 ('sub_version_num', bitcoin_data.VarStrType()),
138 ('start_height', bitcoin_data.StructType('<I')),
140 def handle_version(self, version, services, time, addr_to, addr_from, nonce, sub_version_num, start_height):
141 #print 'VERSION', locals()
142 self.version_after = version
145 message_verack = bitcoin_data.ComposedType([])
146 def handle_verack(self):
147 self.version = self.version_after
150 self.check_order = deferral.GenericDeferrer(2**256, lambda id, order: self.send_checkorder(id=id, order=order))
151 self.submit_order = deferral.GenericDeferrer(2**256, lambda id, order: self.send_submitorder(id=id, order=order))
152 self.get_block = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='block', hash=hash)]))
153 self.get_block_header = deferral.ReplyMatcher(lambda hash: self.send_getheaders(version=1, have=[], last=hash))
154 self.get_tx = deferral.ReplyMatcher(lambda hash: self.send_getdata(requests=[dict(type='tx', hash=hash)]))
156 if hasattr(self.factory, 'resetDelay'):
157 self.factory.resetDelay()
158 if hasattr(self.factory, 'gotConnection'):
159 self.factory.gotConnection(self)
161 message_inv = bitcoin_data.ComposedType([
162 ('invs', bitcoin_data.ListType(bitcoin_data.ComposedType([
163 ('type', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'tx': 1, 'block': 2})),
164 ('hash', bitcoin_data.HashType()),
167 def handle_inv(self, invs):
169 if inv['type'] == 'tx':
170 self.factory.new_tx.happened(inv['hash'])
171 elif inv['type'] == 'block':
172 self.factory.new_block.happened(inv['hash'])
174 print "Unknown inv type", item
176 message_getdata = bitcoin_data.ComposedType([
177 ('requests', bitcoin_data.ListType(bitcoin_data.ComposedType([
178 ('type', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'tx': 1, 'block': 2})),
179 ('hash', bitcoin_data.HashType()),
182 message_getblocks = bitcoin_data.ComposedType([
183 ('version', bitcoin_data.StructType('<I')),
184 ('have', bitcoin_data.ListType(bitcoin_data.HashType())),
185 ('last', bitcoin_data.HashType()),
187 message_getheaders = bitcoin_data.ComposedType([
188 ('version', bitcoin_data.StructType('<I')),
189 ('have', bitcoin_data.ListType(bitcoin_data.HashType())),
190 ('last', bitcoin_data.HashType()),
192 message_getaddr = bitcoin_data.ComposedType([])
193 message_checkorder = bitcoin_data.ComposedType([
194 ('id', bitcoin_data.HashType()),
195 ('order', bitcoin_data.FixedStrType(60)), # XXX
197 message_submitorder = bitcoin_data.ComposedType([
198 ('id', bitcoin_data.HashType()),
199 ('order', bitcoin_data.FixedStrType(60)), # XXX
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),
208 def handle_addr(self, addrs):
212 message_tx = bitcoin_data.ComposedType([
213 ('tx', bitcoin_data.tx_type),
215 def handle_tx(self, tx):
216 self.get_block.got_response(bitcoin_data.tx_type.hash256(tx), tx)
218 message_block = bitcoin_data.ComposedType([
219 ('block', bitcoin_data.block_type),
221 def handle_block(self, block):
222 self.get_block.got_response(bitcoin_data.block_header_type.hash256(block['header']), block)
224 message_headers = bitcoin_data.ComposedType([
225 ('headers', bitcoin_data.ListType(bitcoin_data.block_header_type)),
227 def handle_headers(self, headers):
228 for header in headers:
229 self.get_block_header.got_response(bitcoin_data.block_hash(header), header)
231 message_reply = bitcoin_data.ComposedType([
232 ('hash', bitcoin_data.HashType()),
233 ('reply', bitcoin_data.EnumType(bitcoin_data.StructType('<I'), {'success': 0, 'failure': 1, 'denied': 2})),
234 ('script', bitcoin_data.VarStrType()),
236 def handle_reply(self, hash, reply, script):
237 self.check_order.got_response(hash, dict(reply=reply, script=script))
238 self.submit_order.got_response(hash, dict(reply=reply, script=script))
240 message_ping = bitcoin_data.ComposedType([])
241 def handle_ping(self):
244 message_alert = bitcoin_data.ComposedType([
245 ('message', bitcoin_data.VarStrType()),
246 ('signature', bitcoin_data.VarStrType()),
248 def handle_alert(self, message, signature):
249 print "ALERT:", (message, signature)
251 def connectionLost(self, reason):
252 if hasattr(self.factory, 'gotConnection'):
253 self.factory.gotConnection(None)
255 class ClientFactory(protocol.ReconnectingClientFactory):
260 def __init__(self, net):
262 self.conn = variable.Variable(None)
264 self.new_block = variable.Event()
265 self.new_tx = variable.Event()
267 def buildProtocol(self, addr):
268 p = self.protocol(self.net)
272 def gotConnection(self, conn):
275 def getProtocol(self):
276 return self.conn.get_not_none()
278 if __name__ == '__main__':
279 factory = ClientFactory()
280 reactor.connectTCP('127.0.0.1', 8333, factory)
284 @defer.inlineCallbacks
286 (yield factory.getProtocol())