Skip to content

Commit

Permalink
refactor: rename CellStatus old -> dead, current -> live
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Dec 26, 2018
1 parent 6e128c1 commit ede5108
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 66 deletions.
2 changes: 1 addition & 1 deletion chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ pub mod test {

let out_point = OutPoint::new(root_hash, 0);
let state = shared.cell(&out_point);
assert!(state.is_current());
assert!(state.is_live());
}

#[test]
Expand Down
44 changes: 22 additions & 22 deletions core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ use std::slice;
#[derive(Clone, PartialEq, Debug)]
pub enum CellStatus {
/// Cell exists and has not been spent.
Current(CellOutput),
Live(CellOutput),
/// Cell exists and has been spent.
Old,
Dead,
/// Cell does not exist.
Unknown,
}

impl CellStatus {
pub fn is_current(&self) -> bool {
pub fn is_live(&self) -> bool {
match *self {
CellStatus::Current(_) => true,
CellStatus::Live(_) => true,
_ => false,
}
}

pub fn is_old(&self) -> bool {
self == &CellStatus::Old
pub fn is_dead(&self) -> bool {
self == &CellStatus::Dead
}

pub fn is_unknown(&self) -> bool {
self == &CellStatus::Unknown
}

pub fn get_current(&self) -> Option<&CellOutput> {
pub fn get_live(&self) -> Option<&CellOutput> {
match *self {
CellStatus::Current(ref output) => Some(output),
CellStatus::Live(ref output) => Some(output),
_ => None,
}
}

pub fn take_current(self) -> Option<CellOutput> {
pub fn take_live(self) -> Option<CellOutput> {
match self {
CellStatus::Current(output) => Some(output),
CellStatus::Live(output) => Some(output),
_ => None,
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ pub trait CellProvider {
if seen_inputs.insert(input.clone()) {
self.cell(input)
} else {
CellStatus::Old
CellStatus::Dead
}
})
.collect();
Expand All @@ -80,7 +80,7 @@ pub trait CellProvider {
if seen_inputs.insert(dep.clone()) {
self.cell(dep)
} else {
CellStatus::Old
CellStatus::Dead
}
})
.collect();
Expand All @@ -106,7 +106,7 @@ pub trait CellProvider {
if seen_inputs.insert(input.clone()) {
self.cell_at(input, parent)
} else {
CellStatus::Old
CellStatus::Dead
}
})
.collect();
Expand All @@ -118,7 +118,7 @@ pub trait CellProvider {
if seen_inputs.insert(dep.clone()) {
self.cell_at(dep, parent)
} else {
CellStatus::Old
CellStatus::Dead
}
})
.collect();
Expand Down Expand Up @@ -156,15 +156,15 @@ impl ResolvedTransaction {
}

pub fn is_double_spend(&self) -> bool {
self.cells_iter().any(|state| state.is_old())
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_current())
self.cells_iter().all(|state| state.is_live())
}
}

Expand All @@ -180,16 +180,16 @@ mod tests {
impl CellProvider for CellMemoryDb {
fn cell(&self, o: &OutPoint) -> CellStatus {
match self.cells.get(o) {
Some(&Some(ref cell_output)) => CellStatus::Current(cell_output.clone()),
Some(&None) => CellStatus::Old,
Some(&Some(ref cell_output)) => CellStatus::Live(cell_output.clone()),
Some(&None) => CellStatus::Dead,
None => CellStatus::Unknown,
}
}

fn cell_at(&self, o: &OutPoint, _: &H256) -> CellStatus {
match self.cells.get(o) {
Some(&Some(ref cell_output)) => CellStatus::Current(cell_output.clone()),
Some(&None) => CellStatus::Old,
Some(&Some(ref cell_output)) => CellStatus::Live(cell_output.clone()),
Some(&None) => CellStatus::Dead,
None => CellStatus::Unknown,
}
}
Expand Down Expand Up @@ -223,8 +223,8 @@ mod tests {
db.cells.insert(p1.clone(), Some(o.clone()));
db.cells.insert(p2.clone(), None);

assert_eq!(CellStatus::Current(o), db.cell(&p1));
assert_eq!(CellStatus::Old, db.cell(&p2));
assert_eq!(CellStatus::Live(o), db.cell(&p1));
assert_eq!(CellStatus::Dead, db.cell(&p2));
assert_eq!(CellStatus::Unknown, db.cell(&p3));
}
}
6 changes: 3 additions & 3 deletions pool/src/tests/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,18 @@ fn test_add_pool() {
assert_eq!(pool.service.total_size(), 2);
expect_output_parent!(
pool.service,
CellStatus::Current(_),
CellStatus::Live(_),
OutPoint::new(child_tx_hash.clone(), 0)
);
expect_output_parent!(
pool.service,
CellStatus::Old,
CellStatus::Dead,
OutPoint::new(parent_tx_hash.clone(), 0),
OutPoint::new(parent_tx_hash.clone(), 1)
);
expect_output_parent!(
pool.service,
CellStatus::Current(_),
CellStatus::Live(_),
OutPoint::new(pool.tx_hash.clone(), 8)
);
expect_output_parent!(
Expand Down
8 changes: 4 additions & 4 deletions pool/src/txs_pool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ where
{
fn cell(&self, o: &OutPoint) -> CellStatus {
match { self.pool.txo_status(o) } {
TxoStatus::Spent => CellStatus::Old,
TxoStatus::InPool => CellStatus::Current(self.pool.get_output(o).unwrap()),
TxoStatus::Spent => CellStatus::Dead,
TxoStatus::InPool => CellStatus::Live(self.pool.get_output(o).unwrap()),
TxoStatus::Unknown => self.shared.cell(o),
}
}
Expand Down Expand Up @@ -433,7 +433,7 @@ where
CellStatus::Unknown => {
unknowns.push(inputs[i].clone());
}
CellStatus::Old => {
CellStatus::Dead => {
self.cache.insert(tx.proposal_short_id(), tx);
return Err(PoolError::DoubleSpent);
}
Expand All @@ -446,7 +446,7 @@ where
CellStatus::Unknown => {
unknowns.push(deps[i].clone());
}
CellStatus::Old => {
CellStatus::Dead => {
self.cache.insert(tx.proposal_short_id(), tx);
return Err(PoolError::DoubleSpent);
}
Expand Down
10 changes: 5 additions & 5 deletions rpc/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_block","params": ["0x7643567c
"outputs": [
{
"capacity": 50000,
"type_": null,
"type": null,
"data": [],
"lock": "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff"
}
Expand Down Expand Up @@ -111,7 +111,7 @@ curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_transaction","params": ["0xbd
"outputs": [
{
"capacity": 50000,
"type_": null,
"type": null,
"data": [],
"lock": "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff"
}
Expand Down Expand Up @@ -247,7 +247,7 @@ curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_cells_by_type_hash","params":
```


# get_current_cell
# get_live_cell

Returns the information about a cell by out_point.

Expand All @@ -258,7 +258,7 @@ Returns the information about a cell by out_point.
## Examples

```shell
curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_current_cell","params": [{"hash": "0xbddb7c2559c2c3cdfc8f3cae2697ca75489521c352265cc9e60b4b2416ad5929", "index": 0}]}' -H 'content-type:application/json' 'http://localhost:8114'
curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_live_cell","params": [{"hash": "0xbddb7c2559c2c3cdfc8f3cae2697ca75489521c352265cc9e60b4b2416ad5929", "index": 0}]}' -H 'content-type:application/json' 'http://localhost:8114'
```

```json
Expand All @@ -267,7 +267,7 @@ curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_current_cell","params": [{"ha
"result": {
"cell": {
"capacity": 50000,
"type_": null,
"type": null,
"data": [],
"lock": "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff"
},
Expand Down
6 changes: 3 additions & 3 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ build_rpc_trait! {
_to: BlockNumber
) -> Result<Vec<CellOutputWithOutPoint>>;

#[rpc(name = "get_current_cell")]
fn get_current_cell(&self, _out_point: OutPoint) -> Result<CellWithStatus>;
#[rpc(name = "get_live_cell")]
fn get_live_cell(&self, _out_point: OutPoint) -> Result<CellWithStatus>;

#[rpc(name = "get_tip_block_number")]
fn get_tip_block_number(&self) -> Result<BlockNumber>;
Expand Down Expand Up @@ -96,7 +96,7 @@ impl<CI: ChainIndex + 'static> ChainRpc for ChainRpcImpl<CI> {
Ok(result)
}

fn get_current_cell(&self, out_point: OutPoint) -> Result<CellWithStatus> {
fn get_live_cell(&self, out_point: OutPoint) -> Result<CellWithStatus> {
Ok(self.shared.cell(&out_point).into())
}

Expand Down
4 changes: 2 additions & 2 deletions rpc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub struct CellWithStatus {
impl From<CellStatus> for CellWithStatus {
fn from(status: CellStatus) -> Self {
let (cell, status) = match status {
CellStatus::Current(cell) => (Some(cell), "current"),
CellStatus::Old => (None, "old"),
CellStatus::Live(cell) => (Some(cell), "live"),
CellStatus::Dead => (None, "dead"),
CellStatus::Unknown => (None, "unknown"),
};
Self {
Expand Down
18 changes: 9 additions & 9 deletions script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a> TransactionScriptsVerifier<'a> {
.dep_cells
.iter()
.map(|cell| {
cell.get_current()
cell.get_live()
.expect("already verifies that all dep cells are valid")
})
.collect();
Expand All @@ -48,7 +48,7 @@ impl<'a> TransactionScriptsVerifier<'a> {
.input_cells
.iter()
.map(|cell| {
cell.get_current()
cell.get_live()
.expect("already verifies that all input cells are valid")
})
.collect();
Expand Down Expand Up @@ -250,7 +250,7 @@ mod tests {
let rtx = ResolvedTransaction {
transaction,
dep_cells: vec![],
input_cells: vec![CellStatus::Current(dummy_cell)],
input_cells: vec![CellStatus::Live(dummy_cell)],
};

let verifier = TransactionScriptsVerifier::new(&rtx);
Expand Down Expand Up @@ -297,7 +297,7 @@ mod tests {
let rtx = ResolvedTransaction {
transaction,
dep_cells: vec![],
input_cells: vec![CellStatus::Current(dummy_cell)],
input_cells: vec![CellStatus::Live(dummy_cell)],
};

let verifier = TransactionScriptsVerifier::new(&rtx);
Expand Down Expand Up @@ -351,8 +351,8 @@ mod tests {

let rtx = ResolvedTransaction {
transaction,
dep_cells: vec![CellStatus::Current(dep_cell.clone())],
input_cells: vec![CellStatus::Current(dummy_cell)],
dep_cells: vec![CellStatus::Live(dep_cell.clone())],
input_cells: vec![CellStatus::Live(dummy_cell)],
};

let verifier = TransactionScriptsVerifier::new(&rtx);
Expand Down Expand Up @@ -403,7 +403,7 @@ mod tests {
let rtx = ResolvedTransaction {
transaction,
dep_cells: vec![],
input_cells: vec![CellStatus::Current(dummy_cell)],
input_cells: vec![CellStatus::Live(dummy_cell)],
};

let verifier = TransactionScriptsVerifier::new(&rtx);
Expand Down Expand Up @@ -460,7 +460,7 @@ mod tests {
let rtx = ResolvedTransaction {
transaction,
dep_cells: vec![],
input_cells: vec![CellStatus::Current(dummy_cell)],
input_cells: vec![CellStatus::Live(dummy_cell)],
};

let verifier = TransactionScriptsVerifier::new(&rtx);
Expand Down Expand Up @@ -511,7 +511,7 @@ mod tests {
let rtx = ResolvedTransaction {
transaction,
dep_cells: vec![],
input_cells: vec![CellStatus::Current(dummy_cell)],
input_cells: vec![CellStatus::Live(dummy_cell)],
};

let verifier = TransactionScriptsVerifier::new(&rtx);
Expand Down
8 changes: 4 additions & 4 deletions shared/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ impl<CI: ChainIndex> CellProvider for Shared<CI> {
.store
.get_transaction(&out_point.hash)
.expect("transaction must exist");
CellStatus::Current(transaction.outputs()[index].clone())
CellStatus::Live(transaction.outputs()[index].clone())
} else {
CellStatus::Old
CellStatus::Dead
}
} else {
CellStatus::Unknown
Expand All @@ -159,9 +159,9 @@ impl<CI: ChainIndex> CellProvider for Shared<CI> {
.store
.get_transaction(&out_point.hash)
.expect("transaction must exist");
CellStatus::Current(transaction.outputs()[index].clone())
CellStatus::Live(transaction.outputs()[index].clone())
} else {
CellStatus::Old
CellStatus::Dead
}
} else {
CellStatus::Unknown
Expand Down
10 changes: 5 additions & 5 deletions verification/src/block_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ impl<'a, P: CellProvider> CellProvider for TransactionsVerifierWrapper<'a, P> {
.outputs()
.get(o.index as usize)
{
Some(x) => CellStatus::Current(x.clone()),
Some(x) => CellStatus::Live(x.clone()),
None => CellStatus::Unknown,
}
} else {
let chain_cell_state = self.verifier.provider.cell_at(o, parent);
if chain_cell_state.is_current() {
CellStatus::Current(chain_cell_state.take_current().expect("state checked"))
} else if chain_cell_state.is_old() {
CellStatus::Old
if chain_cell_state.is_live() {
CellStatus::Live(chain_cell_state.take_live().expect("state checked"))
} else if chain_cell_state.is_dead() {
CellStatus::Dead
} else {
CellStatus::Unknown
}
Expand Down
Loading

0 comments on commit ede5108

Please sign in to comment.