2022-07-15 05:26:07 +00:00
|
|
|
use crate::{serialize::*, transaction::Transaction};
|
2022-05-22 01:35:25 +00:00
|
|
|
|
2022-07-22 06:34:36 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2022-05-22 01:35:25 +00:00
|
|
|
pub struct BlockHeader {
|
|
|
|
pub major_version: u64,
|
|
|
|
pub minor_version: u64,
|
|
|
|
pub timestamp: u64,
|
|
|
|
pub previous: [u8; 32],
|
2022-07-15 05:26:07 +00:00
|
|
|
pub nonce: u32,
|
2022-05-22 01:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockHeader {
|
|
|
|
pub fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
write_varint(&self.major_version, w)?;
|
|
|
|
write_varint(&self.minor_version, w)?;
|
|
|
|
write_varint(&self.timestamp, w)?;
|
|
|
|
w.write_all(&self.previous)?;
|
|
|
|
w.write_all(&self.nonce.to_le_bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<BlockHeader> {
|
2022-07-15 05:26:07 +00:00
|
|
|
Ok(BlockHeader {
|
|
|
|
major_version: read_varint(r)?,
|
|
|
|
minor_version: read_varint(r)?,
|
|
|
|
timestamp: read_varint(r)?,
|
2022-08-21 08:41:55 +00:00
|
|
|
previous: read_bytes(r)?,
|
|
|
|
nonce: read_bytes(r).map(u32::from_le_bytes)?,
|
2022-07-15 05:26:07 +00:00
|
|
|
})
|
2022-05-22 01:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 06:34:36 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2022-05-22 01:35:25 +00:00
|
|
|
pub struct Block {
|
|
|
|
pub header: BlockHeader,
|
|
|
|
pub miner_tx: Transaction,
|
2022-07-15 05:26:07 +00:00
|
|
|
pub txs: Vec<[u8; 32]>,
|
2022-05-22 01:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Block {
|
|
|
|
pub fn serialize<W: std::io::Write>(&self, w: &mut W) -> std::io::Result<()> {
|
|
|
|
self.header.serialize(w)?;
|
|
|
|
self.miner_tx.serialize(w)?;
|
|
|
|
write_varint(&self.txs.len().try_into().unwrap(), w)?;
|
|
|
|
for tx in &self.txs {
|
|
|
|
w.write_all(tx)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deserialize<R: std::io::Read>(r: &mut R) -> std::io::Result<Block> {
|
2022-07-15 05:26:07 +00:00
|
|
|
Ok(Block {
|
|
|
|
header: BlockHeader::deserialize(r)?,
|
|
|
|
miner_tx: Transaction::deserialize(r)?,
|
2022-08-21 08:41:55 +00:00
|
|
|
txs: (0 .. read_varint(r)?).map(|_| read_bytes(r)).collect::<Result<_, _>>()?,
|
2022-07-15 05:26:07 +00:00
|
|
|
})
|
2022-05-22 01:35:25 +00:00
|
|
|
}
|
|
|
|
}
|