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
use std::ops;
use primitives::hash::H256;
use chain::{IndexedBlock, IndexedTransaction, IndexedBlockHeader};

/// Blocks whose parents are known to be in the chain
#[derive(Clone, Copy)]
pub struct CanonBlock<'a> {
	block: &'a IndexedBlock,
}

impl<'a> CanonBlock<'a> {
	pub fn new(block: &'a IndexedBlock) -> Self {
		CanonBlock {
			block: block,
		}
	}

	pub fn hash<'b>(&'b self) -> &'a H256 where 'a: 'b {
		&self.block.header.hash
	}

	pub fn raw<'b>(&'b self) -> &'a IndexedBlock where 'a: 'b {
		self.block
	}

	pub fn header<'b>(&'b self) -> CanonHeader<'a> where 'a: 'b {
		CanonHeader::new(&self.block.header)
	}

	pub fn transactions<'b>(&'b self) -> Vec<CanonTransaction<'a>> where 'a: 'b {
		self.block.transactions.iter().map(CanonTransaction::new).collect()
	}
}

impl<'a> ops::Deref for CanonBlock<'a> {
	type Target = IndexedBlock;

	fn deref(&self) -> &Self::Target {
		self.block
	}
}

#[derive(Clone, Copy)]
pub struct CanonHeader<'a> {
	header: &'a IndexedBlockHeader,
}

impl<'a> CanonHeader<'a> {
	pub fn new(header: &'a IndexedBlockHeader) -> Self {
		CanonHeader {
			header: header,
		}
	}
}

impl<'a> ops::Deref for CanonHeader<'a> {
	type Target = IndexedBlockHeader;

	fn deref(&self) -> &Self::Target {
		self.header
	}
}

#[derive(Clone, Copy)]
pub struct CanonTransaction<'a> {
	transaction: &'a IndexedTransaction,
}

impl<'a> CanonTransaction<'a> {
	pub fn new(transaction: &'a IndexedTransaction) -> Self {
		CanonTransaction {
			transaction: transaction,
		}
	}
}

impl<'a> ops::Deref for CanonTransaction<'a> {
	type Target = IndexedTransaction;

	fn deref(&self) -> &Self::Target {
		self.transaction
	}
}