MCPcopy
hub / github.com/petertodd/python-bitcoinlib / stream_deserialize

Method stream_deserialize

bitcoin/messages.py:76–105  ·  view source on GitHub ↗
(cls, f, protover=PROTO_VERSION)

Source from the content-addressed store, hash-verified

74
75 @classmethod
76 def stream_deserialize(cls, f, protover=PROTO_VERSION):
77 recvbuf = ser_read(f, 4 + 12 + 4 + 4)
78
79 # check magic
80 if recvbuf[:4] != bitcoin.params.MESSAGE_START:
81 raise ValueError("Invalid message start '%s', expected '%s'" %
82 (b2x(recvbuf[:4]), b2x(bitcoin.params.MESSAGE_START)))
83
84 # remaining header fields: command, msg length, checksum
85 command = recvbuf[4:4+12].split(b"\x00", 1)[0]
86 msglen = struct.unpack(b"<i", recvbuf[4+12:4+12+4])[0]
87 checksum = recvbuf[4+12+4:4+12+4+4]
88
89 # read message body
90 recvbuf += ser_read(f, msglen)
91
92 msg = recvbuf[4+12+4+4:4+12+4+4+msglen]
93 th = hashlib.sha256(msg).digest()
94 h = hashlib.sha256(th).digest()
95 if checksum != h[:4]:
96 raise ValueError("got bad checksum %s" % repr(recvbuf))
97 recvbuf = recvbuf[4+12+4+4+msglen:]
98
99 if command in messagemap:
100 cls = messagemap[command]
101 # print("Going to deserialize '%s'" % msg)
102 return cls.msg_deser(BytesIO(msg))
103 else:
104 print("Command '%s' not in messagemap" % repr(command))
105 return None
106
107 def stream_serialize(self, f):
108 data = self.to_bytes()

Callers 13

from_bytesMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45
msg_deserMethod · 0.45

Calls 3

ser_readFunction · 0.85
b2xFunction · 0.85
msg_deserMethod · 0.45

Tested by

no test coverage detected