forked from kokjo/pycoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.py
314 lines (288 loc) · 7.56 KB
/
msgs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""Contains the objects representing each message in the protocol
Serialization is provided by the jserialize (for json) and bserialize (for the
wire protocol) modules"""
import struct
import time
import random
import binhex
import jserialize as js
import bserialize as bs
from utils import *
MAGIC_MAINNET = 0xd9b4bef9
TYPE_TX = 1
TYPE_BLOCK = 2
class Header():
def __init__(self, data, magic=MAGIC_MAINNET):
self.magic, self.cmd, self.len = struct.unpack("<L12sL", data)
if self.magic != magic:
raise ProtocolViolation("wrong magic") # Client shutdown
self.cmd = self.cmd.strip("\x00")
try:
self.type = msgtable[self.cmd]
except KeyError:
print "??",self.cmd
raise ProtocolViolation # Unrecognized message type
if self.cmd not in ("version", "verack"):
self.len += 4
def deserialize(self, data):
if self.cmd not in ("version", "verack"):
self.cksum, data = data[:4], data[4:]
if self.cksum != checksum(data):
raise ProtocolViolation
return self.type.frombinary(data)[0]
@staticmethod
def serialize(msg, magic=MAGIC_MAINNET):
data = msg.tobinary()
if msg.type not in ("version", "verack"):
return struct.pack("<L12sL4s", magic, msg.type.encode('ascii'), len(data), checksum(data)[:4]) + data
else: # No checksum in version and verack
return struct.pack("<L12sL", magic, msg.type.encode('ascii'), len(data)) + data
class Address(js.Entity, bs.Entity):
fields = {
"services":js.Int,
"ip":js.IPv4,
"port":js.Int
}
bfields = [
("services", bs.structfmt("<Q")),
("ip", bs.IPv4Inv6),
("port", bs.structfmt("!H")),
]
@constructor
def make(self, ip, port):
self.services = 1 # Always 1 in current protocol
self.ip = ip
self.port = port
class Version(js.Entity, bs.Entity):
type = "version"
fields = {
"type":js.Str,
"version":js.Int,
"services":js.Int,
"time":js.Int,
"reciever":Address,
"sender":Address,
"nonce":js.Int,
"subverinfo":js.Str,
"finalblock":js.Int
}
bfields = [
("version", bs.structfmt("<I")),
("services", bs.structfmt("<Q")),
("time", bs.structfmt("<Q")),
("reciever", Address),
("sender", Address),
("nonce", bs.structfmt("<Q")),
("subverinfo", bs.Str),
("finalblock", bs.structfmt("<I")),
]
@constructor
def make(self, version=31901, sender=Address.make("0.0.0.0",0), reciever=None):
self.version = version
self.services = 1
self.time = int(time.time())
self.sender = sender
self.reciever = reciever
self.nonce = 1234134124
self.subverinfo = ""
self.finalblock = 1
class Verack(js.Entity, bs.Entity):
type = "verack"
fields = {
"type":js.Str
}
bfields = []
@constructor
def make(self):
pass
class Getblocks(js.Entity, bs.Entity):
type = "getblocks"
fields = {
"type":js.Str,
"version":js.Int,
"starts":js.List(js.Hash),
"end":js.Hash,
}
bfields = [
("version", bs.structfmt("<I")),
("starts", bs.VarList(bs.Hash)),
("end", bs.Hash),
]
@constructor
def make(self, starts, end=b"\x00"*32):
self.version = 31900
self.starts = starts
self.end = end
class TimedAddress(js.Entity, bs.Entity):
fields = {
"lastseen":js.Int,
"address":Address,
}
bfields = [
("lastseen", bs.structfmt("<I")),
("address", Address),
]
class Addr(js.Entity, bs.Entity):
type = "addr"
fields = {
"type":js.Str,
"addrs":js.List(TimedAddress),
}
bfields = [
("addrs", bs.VarList(TimedAddress)),
]
class Getaddr(js.Entity, bs.Entity):
type = "getaddr"
fields = {
"type":js.Str
}
bfields = []
@constructor
def make(self):
pass
class InvVect(js.Entity, bs.Entity):
fields = {
"objtype":js.Int,
"hash":js.Hash,
}
bfields = [
("objtype", bs.structfmt("<I")),
("hash", bs.Hash),
]
def __eq__(self, other):
if self.__class__ == other.__class__:
return self.hash == other.hash and self.objtype == other.objtype
return NotImplemented
def __hash__(self):
return hash(self.hash)
@constructor
def make(self, objtype, hash):
self.objtype, self.hash = objtype, hash
class Inv(js.Entity, bs.Entity):
type = "inv"
fields = {
"type":js.Str,
"objs":js.List(InvVect),
}
bfields = [
("objs", bs.VarList(InvVect))
]
@constructor
def make(self, objs):
self.objs = objs
class Getdata(js.Entity, bs.Entity):
type = "getdata"
# Content-wise identical to "inv"
fields = {
"type":js.Str,
"objs":js.List(InvVect),
}
bfields = [
("objs", bs.VarList(InvVect))
]
@constructor
def make(self, objs):
self.objs = objs
class TxOutpoint(js.Entity, bs.Entity):
fields = {
"tx":js.Hash,
"index":js.Int,
}
bfields = [
("tx", bs.Hash),
("index", bs.structfmt("<I")),
]
class TxInput(js.Entity, bs.Entity):
fields = {
"outpoint":TxOutpoint,
"script":js.Bytes,
"sequence":js.Int,
}
bfields = [
("outpoint", TxOutpoint),
("script", bs.VarBytes),
("sequence", bs.structfmt("<I")),
]
class TxOutput(js.Entity, bs.Entity):
fields = {
"amount":js.Int,
"script":js.Bytes,
}
bfields = [
("amount", bs.structfmt("<Q")),
("script", bs.VarBytes),
]
class Tx(js.Entity, bs.Entity):
type = "tx"
fields = {
"version":js.Int,
"inputs":js.List(TxInput),
"outputs":js.List(TxOutput),
"locktime":js.Int,
}
bfields = [
("version", bs.structfmt("<I")),
("inputs", bs.VarList(TxInput)),
("outputs", bs.VarList(TxOutput)),
("locktime", bs.structfmt("<I")),
]
@cachedproperty
def hash(self):
return doublesha(self.tobinary())
class TxAux(js.Entity, bs.Entity):
fields = {
"tx":Tx,
"block":js.Hash,
"redeemed":js.List(js.Hash),
}
bfields = [
("tx", Tx),
("block", bs.Hash),
("redeemed", bs.VarList(bs.Hash)),
]
@constructor
def make(self, tx):
self.tx, self.block = tx, nullhash
self.redeemed = [nullhash] * len(tx.outputs)
class Block(js.Entity, bs.Entity):
fields = {
"version":js.Int,
"prev":js.Hash,
"merkle":js.Hash,
"time":js.Int,
"bits":js.Int,
"nonce":js.Int,
}
bfields = [
("version", bs.structfmt("<I")),
("prev", bs.Hash),
("merkle", bs.Hash),
("time", bs.structfmt("<I")),
("bits", bs.structfmt("<I")),
("nonce", bs.structfmt("<I")),
]
@cachedproperty
def hash(self):
return doublesha(self.tobinary())
class Blockmsg(js.Entity, bs.Entity):
type = "block"
fields = {
"type":js.Str,
"block":Block,
"txs":js.List(Tx)
}
bfields = [
("block", Block),
("txs", bs.VarList(Tx)),
]
msgtable = {
'version':Version,
'verack':Verack,
'getblocks':Getblocks,
'getaddr':Getaddr,
'addr':Addr,
'inv':Inv,
'getdata':Getdata,
'block':Blockmsg,
'tx':Tx,
}