forked from nervosnetwork/ckb
/
transaction_meta.rs
98 lines (80 loc) · 2.41 KB
/
transaction_meta.rs
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
91
92
93
94
95
96
97
98
use bit_vec::BitVec;
use serde_derive::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[serde(remote = "BitVec")]
struct BitVecSerde {
#[serde(getter = "BitVec::to_bytes")]
bits: Vec<u8>,
}
impl From<BitVecSerde> for BitVec {
fn from(bv: BitVecSerde) -> BitVec {
BitVec::from_bytes(&bv.bits)
}
}
#[derive(Default, Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct TransactionMeta {
block_number: u64,
/// first bit indicate if transaction is a cellbase transaction
/// next bits indicate if transaction has dead cells
#[serde(with = "BitVecSerde")]
dead_cell: BitVec,
}
impl TransactionMeta {
pub fn new(block_number: u64, outputs_count: usize) -> TransactionMeta {
TransactionMeta {
block_number,
dead_cell: BitVec::from_elem(outputs_count + 1, false),
}
}
/// New cellbase transaction
pub fn new_cellbase(block_number: u64, outputs: usize) -> Self {
let mut result = Self::new(block_number, outputs);
result.dead_cell.set(0, true);
result
}
/// Returns true if it is a cellbase transaction
pub fn is_cellbase(&self) -> bool {
self.dead_cell.get(0).expect("One bit should always exists")
}
pub fn len(&self) -> usize {
self.dead_cell.len()
}
pub fn block_number(&self) -> u64 {
self.block_number
}
pub fn is_empty(&self) -> bool {
self.dead_cell.is_empty()
}
pub fn is_new(&self) -> bool {
self.dead_cell.none()
}
pub fn is_all_dead(&self) -> bool {
self.dead_cell.all()
}
pub fn is_dead(&self, index: usize) -> bool {
self.dead_cell.get(index + 1).unwrap_or(true)
}
pub fn set_dead(&mut self, index: usize) {
self.dead_cell.set(index + 1, true);
}
pub fn unset_dead(&mut self, index: usize) {
self.dead_cell.set(index + 1, false);
}
}
#[cfg(test)]
mod tests {
use super::*;
use bincode;
#[test]
fn transaction_meta_serde() {
let mut original = TransactionMeta::new(0, 4);
original.set_dead(1);
original.set_dead(3);
let decoded: TransactionMeta =
bincode::deserialize(&(bincode::serialize(&original).unwrap())[..]).unwrap();
assert!(!decoded.is_dead(0));
assert!(decoded.is_dead(1));
assert!(!decoded.is_dead(2));
assert!(decoded.is_dead(3));
}
}