`addrv2` and `sendaddrv2` network message

This commit is contained in:
Jake Rawsthorne 2020-10-11 19:40:29 +01:00
parent cf8e290c99
commit 921f64699c
1 changed files with 15 additions and 2 deletions

View File

@ -25,7 +25,7 @@ use std::io::Cursor;
use blockdata::block; use blockdata::block;
use blockdata::transaction; use blockdata::transaction;
use network::address::Address; use network::address::{Address, AddrV2Message};
use network::message_network; use network::message_network;
use network::message_blockdata; use network::message_blockdata;
use network::message_filter; use network::message_filter;
@ -159,6 +159,10 @@ pub enum NetworkMessage {
FeeFilter(i64), FeeFilter(i64),
/// `wtxidrelay` /// `wtxidrelay`
WtxidRelay, WtxidRelay,
/// `addrv2`
AddrV2(Vec<AddrV2Message>),
/// `sendaddrv2`
SendAddrV2,
} }
impl NetworkMessage { impl NetworkMessage {
@ -191,6 +195,8 @@ impl NetworkMessage {
NetworkMessage::Reject(_) => "reject", NetworkMessage::Reject(_) => "reject",
NetworkMessage::FeeFilter(_) => "feefilter", NetworkMessage::FeeFilter(_) => "feefilter",
NetworkMessage::WtxidRelay => "wtxidrelay", NetworkMessage::WtxidRelay => "wtxidrelay",
NetworkMessage::AddrV2(_) => "addrv2",
NetworkMessage::SendAddrV2 => "sendaddrv2",
} }
} }
@ -260,11 +266,13 @@ impl Encodable for RawNetworkMessage {
NetworkMessage::Alert(ref dat) => serialize(dat), NetworkMessage::Alert(ref dat) => serialize(dat),
NetworkMessage::Reject(ref dat) => serialize(dat), NetworkMessage::Reject(ref dat) => serialize(dat),
NetworkMessage::FeeFilter(ref data) => serialize(data), NetworkMessage::FeeFilter(ref data) => serialize(data),
NetworkMessage::AddrV2(ref dat) => serialize(dat),
NetworkMessage::Verack NetworkMessage::Verack
| NetworkMessage::SendHeaders | NetworkMessage::SendHeaders
| NetworkMessage::MemPool | NetworkMessage::MemPool
| NetworkMessage::GetAddr | NetworkMessage::GetAddr
| NetworkMessage::WtxidRelay => vec![], | NetworkMessage::WtxidRelay => vec![],
| NetworkMessage::SendAddrV2 => vec![],
}).consensus_encode(&mut s)?; }).consensus_encode(&mut s)?;
Ok(len) Ok(len)
} }
@ -329,6 +337,8 @@ impl Decodable for RawNetworkMessage {
"alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut mem_d)?), "alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut mem_d)?),
"feefilter" => NetworkMessage::FeeFilter(Decodable::consensus_decode(&mut mem_d)?), "feefilter" => NetworkMessage::FeeFilter(Decodable::consensus_decode(&mut mem_d)?),
"wtxidrelay" => NetworkMessage::WtxidRelay, "wtxidrelay" => NetworkMessage::WtxidRelay,
"addrv2" => NetworkMessage::AddrV2(Decodable::consensus_decode(&mut mem_d)?),
"sendaddrv2" => NetworkMessage::SendAddrV2,
_ => return Err(encode::Error::UnrecognizedNetworkCommand(cmd.into_owned())), _ => return Err(encode::Error::UnrecognizedNetworkCommand(cmd.into_owned())),
}; };
Ok(RawNetworkMessage { Ok(RawNetworkMessage {
@ -341,13 +351,14 @@ impl Decodable for RawNetworkMessage {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::io; use std::io;
use std::net::Ipv4Addr;
use super::{RawNetworkMessage, NetworkMessage, CommandString}; use super::{RawNetworkMessage, NetworkMessage, CommandString};
use network::constants::ServiceFlags; use network::constants::ServiceFlags;
use consensus::encode::{Encodable, deserialize, deserialize_partial, serialize}; use consensus::encode::{Encodable, deserialize, deserialize_partial, serialize};
use hashes::hex::FromHex; use hashes::hex::FromHex;
use hashes::sha256d::Hash; use hashes::sha256d::Hash;
use hashes::Hash as HashTrait; use hashes::Hash as HashTrait;
use network::address::Address; use network::address::{Address, AddrV2, AddrV2Message};
use super::message_network::{Reject, RejectReason, VersionMessage}; use super::message_network::{Reject, RejectReason, VersionMessage};
use network::message_blockdata::{Inventory, GetBlocksMessage, GetHeadersMessage}; use network::message_blockdata::{Inventory, GetBlocksMessage, GetHeadersMessage};
use blockdata::block::{Block, BlockHeader}; use blockdata::block::{Block, BlockHeader};
@ -393,6 +404,8 @@ mod test {
NetworkMessage::Reject(Reject{message: "Test reject".into(), ccode: RejectReason::Duplicate, reason: "Cause".into(), hash: hash([255u8; 32])}), NetworkMessage::Reject(Reject{message: "Test reject".into(), ccode: RejectReason::Duplicate, reason: "Cause".into(), hash: hash([255u8; 32])}),
NetworkMessage::FeeFilter(1000), NetworkMessage::FeeFilter(1000),
NetworkMessage::WtxidRelay, NetworkMessage::WtxidRelay,
NetworkMessage::AddrV2(vec![AddrV2Message{ addr: AddrV2::Ipv4(Ipv4Addr::new(127, 0, 0, 1)), port: 0, services: ServiceFlags::NONE, time: 0 }]),
NetworkMessage::SendAddrV2,
]; ];
for msg in msgs { for msg in msgs {