forked from nervosnetwork/ckb
/
cell.rs
268 lines (237 loc) · 6.68 KB
/
cell.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use crate::block::Block;
use crate::transaction::{CellOutput, OutPoint, Transaction};
use crate::Capacity;
use fnv::{FnvHashMap, FnvHashSet};
use numext_fixed_hash::H256;
use std::iter::Chain;
use std::slice;
#[derive(Clone, PartialEq, Debug)]
pub struct CellMeta {
pub cell_output: CellOutput,
pub block_number: Option<u64>,
}
#[derive(Clone, PartialEq, Debug)]
pub enum CellStatus {
/// Cell exists and has not been spent.
Live(CellMeta),
/// Cell exists and has been spent.
Dead,
/// Cell does not exist.
Unknown,
}
impl CellStatus {
pub fn is_live(&self) -> bool {
match *self {
CellStatus::Live(_) => true,
_ => false,
}
}
pub fn is_dead(&self) -> bool {
self == &CellStatus::Dead
}
pub fn is_unknown(&self) -> bool {
self == &CellStatus::Unknown
}
pub fn get_live(&self) -> Option<&CellMeta> {
match *self {
CellStatus::Live(ref output) => Some(output),
_ => None,
}
}
pub fn take_live(self) -> Option<CellMeta> {
match self {
CellStatus::Live(output) => Some(output),
_ => None,
}
}
}
/// Transaction with resolved input cells.
#[derive(Debug)]
pub struct ResolvedTransaction {
pub transaction: Transaction,
pub dep_cells: Vec<CellStatus>,
pub input_cells: Vec<CellStatus>,
}
pub trait CellProvider {
fn cell(&self, out_point: &OutPoint) -> CellStatus;
}
pub struct OverlayCellProvider<'a, O, CP> {
overlay: &'a O,
cell_provider: &'a CP,
}
impl<'a, O, CP> OverlayCellProvider<'a, O, CP> {
pub fn new(overlay: &'a O, cell_provider: &'a CP) -> Self {
OverlayCellProvider {
overlay,
cell_provider,
}
}
}
impl<'a, O, CP> CellProvider for OverlayCellProvider<'a, O, CP>
where
O: CellProvider,
CP: CellProvider,
{
fn cell(&self, out_point: &OutPoint) -> CellStatus {
match self.overlay.cell(out_point) {
CellStatus::Live(co) => CellStatus::Live(co),
CellStatus::Dead => CellStatus::Dead,
CellStatus::Unknown => self.cell_provider.cell(out_point),
}
}
}
pub struct BlockCellProvider<'a> {
output_indices: FnvHashMap<H256, usize>,
block: &'a Block,
}
impl<'a> BlockCellProvider<'a> {
pub fn new(block: &'a Block) -> Self {
let output_indices = block
.commit_transactions()
.iter()
.enumerate()
.map(|(idx, tx)| (tx.hash(), idx))
.collect();
Self {
output_indices,
block,
}
}
}
impl<'a> CellProvider for BlockCellProvider<'a> {
fn cell(&self, out_point: &OutPoint) -> CellStatus {
if let Some(i) = self.output_indices.get(&out_point.hash) {
match self.block.commit_transactions()[*i]
.outputs()
.get(out_point.index as usize)
{
Some(x) => CellStatus::Live(CellMeta {
cell_output: x.clone(),
block_number: Some(self.block.header().number()),
}),
None => CellStatus::Unknown,
}
} else {
CellStatus::Unknown
}
}
}
pub fn resolve_transaction<CP: CellProvider>(
transaction: &Transaction,
seen_inputs: &mut FnvHashSet<OutPoint>,
cell_provider: &CP,
) -> ResolvedTransaction {
let input_cells = transaction
.input_pts()
.iter()
.map(|input| {
if seen_inputs.insert(input.clone()) {
cell_provider.cell(input)
} else {
CellStatus::Dead
}
})
.collect();
let dep_cells = transaction
.dep_pts()
.iter()
.map(|dep| {
if seen_inputs.insert(dep.clone()) {
cell_provider.cell(dep)
} else {
CellStatus::Dead
}
})
.collect();
ResolvedTransaction {
transaction: transaction.clone(),
input_cells,
dep_cells,
}
}
impl ResolvedTransaction {
pub fn cells_iter(&self) -> Chain<slice::Iter<CellStatus>, slice::Iter<CellStatus>> {
self.dep_cells.iter().chain(&self.input_cells)
}
pub fn cells_iter_mut(
&mut self,
) -> Chain<slice::IterMut<CellStatus>, slice::IterMut<CellStatus>> {
self.dep_cells.iter_mut().chain(&mut self.input_cells)
}
pub fn is_double_spend(&self) -> bool {
self.cells_iter().any(|state| state.is_dead())
}
pub fn is_orphan(&self) -> bool {
self.cells_iter().any(|state| state.is_unknown())
}
pub fn is_fully_resolved(&self) -> bool {
self.cells_iter().all(|state| state.is_live())
}
pub fn fee(&self) -> Capacity {
self.inputs_capacity()
.saturating_sub(self.transaction.outputs_capacity())
}
pub fn inputs_capacity(&self) -> Capacity {
self.input_cells
.iter()
.filter_map(|cell_status| {
if let CellStatus::Live(cell_meta) = cell_status {
Some(cell_meta.cell_output.capacity)
} else {
None
}
})
.sum()
}
}
#[cfg(test)]
mod tests {
use super::super::script::Script;
use super::*;
use numext_fixed_hash::H256;
use std::collections::HashMap;
struct CellMemoryDb {
cells: HashMap<OutPoint, Option<CellMeta>>,
}
impl CellProvider for CellMemoryDb {
fn cell(&self, o: &OutPoint) -> CellStatus {
match self.cells.get(o) {
Some(&Some(ref cell)) => CellStatus::Live(cell.clone()),
Some(&None) => CellStatus::Dead,
None => CellStatus::Unknown,
}
}
}
#[test]
fn cell_provider_trait_works() {
let mut db = CellMemoryDb {
cells: HashMap::new(),
};
let p1 = OutPoint {
hash: H256::zero(),
index: 1,
};
let p2 = OutPoint {
hash: H256::zero(),
index: 2,
};
let p3 = OutPoint {
hash: H256::zero(),
index: 3,
};
let o = CellMeta {
block_number: Some(1),
cell_output: CellOutput {
capacity: 2,
data: vec![],
lock: Script::default(),
type_: None,
},
};
db.cells.insert(p1.clone(), Some(o.clone()));
db.cells.insert(p2.clone(), None);
assert_eq!(CellStatus::Live(o), db.cell(&p1));
assert_eq!(CellStatus::Dead, db.cell(&p2));
assert_eq!(CellStatus::Unknown, db.cell(&p3));
}
}