MCPcopy Create free account
hub / github.com/ElementsProject/elements / CAddress

Class CAddress

test/functional/test_framework/messages.py:214–307  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

212
213
214class CAddress:
215 __slots__ = ("net", "ip", "nServices", "port", "time")
216
217 # see https://github.com/bitcoin/bips/blob/master/bip-0155.mediawiki
218 NET_IPV4 = 1
219 NET_I2P = 5
220
221 ADDRV2_NET_NAME = {
222 NET_IPV4: "IPv4",
223 NET_I2P: "I2P"
224 }
225
226 ADDRV2_ADDRESS_LENGTH = {
227 NET_IPV4: 4,
228 NET_I2P: 32
229 }
230
231 I2P_PAD = "===="
232
233 def __init__(self):
234 self.time = 0
235 self.nServices = 1
236 self.net = self.NET_IPV4
237 self.ip = "0.0.0.0"
238 self.port = 0
239
240 def __eq__(self, other):
241 return self.net == other.net and self.ip == other.ip and self.nServices == other.nServices and self.port == other.port and self.time == other.time
242
243 def deserialize(self, f, *, with_time=True):
244 """Deserialize from addrv1 format (pre-BIP155)"""
245 if with_time:
246 # VERSION messages serialize CAddress objects without time
247 self.time = struct.unpack("<I", f.read(4))[0]
248 self.nServices = struct.unpack("<Q", f.read(8))[0]
249 # We only support IPv4 which means skip 12 bytes and read the next 4 as IPv4 address.
250 f.read(12)
251 self.net = self.NET_IPV4
252 self.ip = socket.inet_ntoa(f.read(4))
253 self.port = struct.unpack(">H", f.read(2))[0]
254
255 def serialize(self, *, with_time=True):
256 """Serialize in addrv1 format (pre-BIP155)"""
257 assert self.net == self.NET_IPV4
258 r = b""
259 if with_time:
260 # VERSION messages serialize CAddress objects without time
261 r += struct.pack("<I", self.time)
262 r += struct.pack("<Q", self.nServices)
263 r += b"\x00" * 10 + b"\xff" * 2
264 r += socket.inet_aton(self.ip)
265 r += struct.pack(">H", self.port)
266 return r
267
268 def deserialize_v2(self, f):
269 """Deserialize from addrv2 format (BIP155)"""
270 self.time = struct.unpack("<I", f.read(4))[0]
271

Callers 5

setup_addr_msgMethod · 0.90
p2p_addrfetch.pyFile · 0.90
__init__Method · 0.70
deserializeMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected