1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::block_header::BlockHeader;
use crate::transaction::Transaction;
use crate::{self as shared, Deserializable, DeserializationError};
use crate::{CompactInt, Serializable};
use serde_derive::Serializable;
#[derive(Serializable, Debug)]
pub struct Block {
block_header: BlockHeader,
transactions: Vec<Transaction>,
}
impl Block {
pub fn new(header: BlockHeader, txs: Vec<Transaction>) -> Block {
let message = Block {
block_header: header,
transactions: txs,
};
message
}
fn serialized_size(&self) -> usize {
let mut size = CompactInt::size(self.transactions.len());
size += BlockHeader::len();
for transaction in self.transactions.iter() {
size += transaction.len();
}
size
}
fn to_bytes(&self) -> Result<Vec<u8>, std::io::Error> {
let mut target = Vec::with_capacity(self.serialized_size());
self.serialize(&mut target)?;
Ok(target)
}
fn deserialize<R>(src: &mut R) -> Result<Self, DeserializationError>
where
R: std::io::Read,
{
let header = BlockHeader::deserialize(src)?;
let tx_count = CompactInt::deserialize(src)?;
if tx_count.value() == 0 {
return Err(DeserializationError::Parse(String::from(
"Block contains no transactions",
)));
}
let first_tx = Transaction::deserialize(src)?;
todo!()
}
}
#[test]
fn serial_size() {
let previous_outpoint = crate::TxOutpoint::new(crate::u256::from(1), 438);
let txin1 = crate::TxInput::new(previous_outpoint, Vec::from([8u8; 21]), 1);
let txin2 = crate::TxInput::new(crate::TxOutpoint::new(crate::u256::new(), 0), Vec::new(), 2);
let mut txins = Vec::new();
txins.push(txin1);
txins.push(txin2);
let mut outputs = Vec::new();
let out1 = crate::TxOutput::new(1, Vec::from([3u8; 11]));
let out2 = crate::TxOutput::new(0, Vec::new());
outputs.push(out1);
outputs.push(out2);
let tx1 = Transaction::new(0, txins, outputs);
let tx2 = Transaction::new(1, Vec::new(), Vec::new());
let mut txs = Vec::with_capacity(2);
txs.push(tx1);
txs.push(tx2);
let block_header = BlockHeader::new(
23,
crate::u256::from(12345678),
crate::u256::from(9876543),
2342,
crate::block_header::Nbits::new(crate::u256::from(8719)),
99,
);
let msg = Block {
block_header,
transactions: txs,
};
let serial = msg.to_bytes().expect("Serializing into vec shouldn't fail");
assert_eq!(serial.len(), msg.serialized_size());
assert_eq!(serial.len(), serial.capacity())
}