MCPcopy Index your code
hub / github.com/jimmysong/programmingbitcoin / serialize

Method serialize

code-ch13/network.py:147–179  ·  view source on GitHub ↗

Serialize this message to send over the network

(self)

Source from the content-addressed store, hash-verified

145 self.relay = relay
146
147 def serialize(self):
148 '''Serialize this message to send over the network'''
149 # version is 4 bytes little endian
150 result = int_to_little_endian(self.version, 4)
151 # services is 8 bytes little endian
152 result += int_to_little_endian(self.services, 8)
153 # timestamp is 8 bytes little endian
154 result += int_to_little_endian(self.timestamp, 8)
155 # receiver services is 8 bytes little endian
156 result += int_to_little_endian(self.receiver_services, 8)
157 # IPV4 is 10 00 bytes and 2 ff bytes then receiver ip
158 result += b'\x00' * 10 + b'\xff\xff' + self.receiver_ip
159 # receiver port is 2 bytes, big endian
160 result += self.receiver_port.to_bytes(2, 'big')
161 # sender services is 8 bytes little endian
162 result += int_to_little_endian(self.sender_services, 8)
163 # IPV4 is 10 00 bytes and 2 ff bytes then sender ip
164 result += b'\x00' * 10 + b'\xff\xff' + self.sender_ip
165 # sender port is 2 bytes, big endian
166 result += self.sender_port.to_bytes(2, 'big')
167 # nonce should be 8 bytes
168 result += self.nonce
169 # useragent is a variable string, so varint first
170 result += encode_varint(len(self.user_agent))
171 result += self.user_agent
172 # latest block is 4 bytes little endian
173 result += int_to_little_endian(self.latest_block, 4)
174 # relay is 00 if false, 01 if true
175 if self.relay:
176 result += b'\x01'
177 else:
178 result += b'\x00'
179 return result
180
181
182class VersionMessageTest(TestCase):

Callers 1

test_serializeMethod · 0.95

Calls 2

int_to_little_endianFunction · 0.90
encode_varintFunction · 0.90

Tested by 1

test_serializeMethod · 0.76