mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-06-13 08:01:49 +08:00
refactor: move macro
This commit is contained in:
@@ -13,3 +13,25 @@ macro_rules! impl_stacks_message_codec_for_int {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! impl_byte_array_message_codec {
|
||||
($thing:ident, $len:expr) => {
|
||||
impl ::codec::StacksMessageCodec for $thing {
|
||||
fn consensus_serialize<W: std::io::Write>(
|
||||
&self,
|
||||
fd: &mut W,
|
||||
) -> Result<(), ::codec::Error> {
|
||||
fd.write_all(self.as_bytes())
|
||||
.map_err(::codec::Error::WriteError)
|
||||
}
|
||||
fn consensus_deserialize<R: std::io::Read>(
|
||||
fd: &mut R,
|
||||
) -> Result<$thing, ::codec::Error> {
|
||||
let mut buf = [0u8; ($len as usize)];
|
||||
fd.read_exact(&mut buf).map_err(::codec::Error::ReadError)?;
|
||||
let ret = $thing::from_bytes(&buf).expect("BUG: buffer is not the right size");
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1515,27 +1515,6 @@ pub const GETPOXINV_MAX_BITLEN: u64 = 8;
|
||||
// message.
|
||||
pub const BLOCKS_PUSHED_MAX: u32 = 32;
|
||||
|
||||
macro_rules! impl_byte_array_message_codec {
|
||||
($thing:ident, $len:expr) => {
|
||||
impl ::codec::StacksMessageCodec for $thing {
|
||||
fn consensus_serialize<W: std::io::Write>(
|
||||
&self,
|
||||
fd: &mut W,
|
||||
) -> Result<(), ::codec::Error> {
|
||||
fd.write_all(self.as_bytes())
|
||||
.map_err(::codec::Error::WriteError)
|
||||
}
|
||||
fn consensus_deserialize<R: std::io::Read>(
|
||||
fd: &mut R,
|
||||
) -> Result<$thing, ::codec::Error> {
|
||||
let mut buf = [0u8; ($len as usize)];
|
||||
fd.read_exact(&mut buf).map_err(::codec::Error::ReadError)?;
|
||||
let ret = $thing::from_bytes(&buf).expect("BUG: buffer is not the right size");
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
impl_byte_array_message_codec!(ConsensusHash, 20);
|
||||
impl_byte_array_message_codec!(Hash160, 20);
|
||||
impl_byte_array_message_codec!(BurnchainHeaderHash, 32);
|
||||
|
||||
Reference in New Issue
Block a user