|
33 | 33 | //! assert_eq!(&bytes[..], &[0xBF, 0x0C, 0x6B, 0xBD]); |
34 | 34 | //! ``` |
35 | 35 |
|
36 | | -use core::convert::From; |
37 | | -use core::{fmt, ops}; |
| 36 | +use core::fmt; |
38 | 37 |
|
39 | 38 | use hashes::Hash; |
40 | 39 |
|
41 | 40 | use crate::consensus::encode::{self, Decodable, Encodable}; |
42 | | -use crate::{BlockHash, io}; |
43 | | - |
44 | | -// Re-export NODE_HEADERS_COMPRESSED for convenience |
45 | | -pub const NODE_HEADERS_COMPRESSED: ServiceFlags = ServiceFlags::NODE_HEADERS_COMPRESSED; |
| 41 | +use crate::{BlockHash, VarInt, io}; |
46 | 42 |
|
47 | 43 | /// Version of the protocol as appearing in network message headers |
48 | 44 | /// This constant is used to signal to other peers which features you support. |
@@ -307,57 +303,18 @@ impl fmt::Display for ServiceFlags { |
307 | 303 | } |
308 | 304 | } |
309 | 305 |
|
310 | | -impl From<u64> for ServiceFlags { |
311 | | - fn from(f: u64) -> Self { |
312 | | - ServiceFlags(f) |
313 | | - } |
314 | | -} |
315 | | - |
316 | | -impl From<ServiceFlags> for u64 { |
317 | | - fn from(val: ServiceFlags) -> Self { |
318 | | - val.0 |
319 | | - } |
320 | | -} |
321 | | - |
322 | | -impl ops::BitOr for ServiceFlags { |
323 | | - type Output = Self; |
324 | | - |
325 | | - fn bitor(mut self, rhs: Self) -> Self { |
326 | | - self.add(rhs) |
327 | | - } |
328 | | -} |
329 | | - |
330 | | -impl ops::BitOrAssign for ServiceFlags { |
331 | | - fn bitor_assign(&mut self, rhs: Self) { |
332 | | - self.add(rhs); |
333 | | - } |
334 | | -} |
335 | | - |
336 | | -impl ops::BitXor for ServiceFlags { |
337 | | - type Output = Self; |
338 | | - |
339 | | - fn bitxor(mut self, rhs: Self) -> Self { |
340 | | - self.remove(rhs) |
341 | | - } |
342 | | -} |
343 | | - |
344 | | -impl ops::BitXorAssign for ServiceFlags { |
345 | | - fn bitxor_assign(&mut self, rhs: Self) { |
346 | | - self.remove(rhs); |
347 | | - } |
348 | | -} |
349 | | - |
350 | 306 | impl Encodable for ServiceFlags { |
351 | 307 | #[inline] |
352 | 308 | fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> { |
353 | | - self.0.consensus_encode(w) |
| 309 | + VarInt(self.0).consensus_encode(w) |
354 | 310 | } |
355 | 311 | } |
356 | 312 |
|
357 | 313 | impl Decodable for ServiceFlags { |
358 | 314 | #[inline] |
359 | 315 | fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> { |
360 | | - Ok(ServiceFlags(Decodable::consensus_decode(r)?)) |
| 316 | + let services = VarInt::consensus_decode(r)?; |
| 317 | + Ok(ServiceFlags(services.0)) |
361 | 318 | } |
362 | 319 | } |
363 | 320 |
|
@@ -434,27 +391,27 @@ mod tests { |
434 | 391 | assert!(!flags.has(*f)); |
435 | 392 | } |
436 | 393 |
|
437 | | - flags |= ServiceFlags::WITNESS; |
| 394 | + flags.add(ServiceFlags::WITNESS); |
438 | 395 | assert_eq!(flags, ServiceFlags::WITNESS); |
439 | 396 |
|
440 | | - let mut flags2 = flags | ServiceFlags::GETUTXO; |
| 397 | + flags.add(ServiceFlags::GETUTXO); |
441 | 398 | for f in all.iter() { |
442 | | - assert_eq!(flags2.has(*f), *f == ServiceFlags::WITNESS || *f == ServiceFlags::GETUTXO); |
| 399 | + assert_eq!(flags.has(*f), *f == ServiceFlags::WITNESS || *f == ServiceFlags::GETUTXO); |
443 | 400 | } |
444 | 401 |
|
445 | | - flags2 ^= ServiceFlags::WITNESS; |
446 | | - assert_eq!(flags2, ServiceFlags::GETUTXO); |
| 402 | + flags.remove(ServiceFlags::WITNESS); |
| 403 | + assert_eq!(flags, ServiceFlags::GETUTXO); |
447 | 404 |
|
448 | | - flags2 |= ServiceFlags::COMPACT_FILTERS; |
449 | | - flags2 ^= ServiceFlags::GETUTXO; |
450 | | - assert_eq!(flags2, ServiceFlags::COMPACT_FILTERS); |
| 405 | + flags.add(ServiceFlags::COMPACT_FILTERS); |
| 406 | + flags.remove(ServiceFlags::GETUTXO); |
| 407 | + assert_eq!(flags, ServiceFlags::COMPACT_FILTERS); |
451 | 408 |
|
452 | 409 | // Test formatting. |
453 | 410 | assert_eq!("ServiceFlags(NONE)", ServiceFlags::NONE.to_string()); |
454 | 411 | assert_eq!("ServiceFlags(WITNESS)", ServiceFlags::WITNESS.to_string()); |
455 | | - let flag = ServiceFlags::WITNESS | ServiceFlags::BLOOM | ServiceFlags::NETWORK; |
456 | | - assert_eq!("ServiceFlags(NETWORK|BLOOM|WITNESS)", flag.to_string()); |
457 | | - let flag = ServiceFlags::WITNESS | 0xf0.into(); |
458 | | - assert_eq!("ServiceFlags(WITNESS|COMPACT_FILTERS|0xb0)", flag.to_string()); |
| 412 | + |
| 413 | + let mut flags = ServiceFlags::NONE; |
| 414 | + flags.add(ServiceFlags::WITNESS).add(ServiceFlags::BLOOM).add(ServiceFlags::NETWORK); |
| 415 | + assert_eq!("ServiceFlags(NETWORK|BLOOM|WITNESS)", flags.to_string()); |
459 | 416 | } |
460 | 417 | } |
0 commit comments