Skip to content

Commit

Permalink
refactor: rename CellOutput#contract to CellOutput#_type
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Dec 26, 2018
1 parent f348821 commit 6e128c1
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 49 deletions.
2 changes: 1 addition & 1 deletion core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ mod tests {
capacity: 2,
data: vec![],
lock: H256::default(),
contract: None,
type_: None,
};

db.cells.insert(p1.clone(), Some(o.clone()));
Expand Down
8 changes: 0 additions & 8 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,3 @@ impl From<CrypError> for Error {
Error::InvalidSignature(e)
}
}

#[derive(Debug, PartialEq, Eq)]
pub enum TxError {
OutOfBound,
NotMatch,
EmptyGroup,
WrongFormat,
}
9 changes: 5 additions & 4 deletions core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ pub struct CellOutput {
pub capacity: Capacity,
pub data: Vec<u8>,
pub lock: H256,
pub contract: Option<Script>,
#[serde(rename = "type")]
pub type_: Option<Script>,
}

impl CellOutput {
pub fn new(capacity: Capacity, data: Vec<u8>, lock: H256, contract: Option<Script>) -> Self {
pub fn new(capacity: Capacity, data: Vec<u8>, lock: H256, type_: Option<Script>) -> Self {
CellOutput {
capacity,
data,
lock,
contract,
type_,
}
}

Expand All @@ -110,7 +111,7 @@ impl CellOutput {
mem::size_of::<Capacity>()
+ self.data.len()
+ H256::size_of()
+ self.contract.as_ref().map_or(0, |s| s.bytes_len())
+ self.type_.as_ref().map_or(0, |s| s.bytes_len())
}
}

Expand Down
9 changes: 3 additions & 6 deletions protocol/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,13 @@ impl<'a> FbsCellOutput<'a> {
) -> WIPOffset<FbsCellOutput<'b>> {
let data = FbsBytes::build(fbb, &cell_output.data);
let lock = FbsBytes::build(fbb, &cell_output.lock.as_bytes());
let contract = cell_output
.contract
.as_ref()
.map(|s| FbsScript::build(fbb, s));
let type_ = cell_output.type_.as_ref().map(|s| FbsScript::build(fbb, s));
let mut builder = CellOutputBuilder::new(fbb);
builder.add_capacity(cell_output.capacity);
builder.add_data(data);
builder.add_lock(lock);
if let Some(s) = contract {
builder.add_contract(s);
if let Some(s) = type_ {
builder.add_type_(s);
}
builder.finish()
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<'a> From<ckb_protocol::CellOutput<'a>> for ckb_core::transaction::CellOutpu
capacity: cell_output.capacity(),
data: cell_output.data().and_then(|b| b.seq()).unwrap().to_vec(),
lock: H256::from_slice(cell_output.lock().and_then(|b| b.seq()).unwrap()).unwrap(),
contract: cell_output.contract().map(Into::into),
type_: cell_output.type_().map(Into::into),
}
}
}
2 changes: 1 addition & 1 deletion protocol/src/protocol.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ table CellOutput {
capacity: uint64;
data: Bytes;
lock: Bytes;
contract: Script;
type: Script;
}

table Script {
Expand Down
16 changes: 8 additions & 8 deletions protocol/src/protocol_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ impl<'a> CellOutput<'a> {
args: &'args CellOutputArgs<'args>) -> flatbuffers::WIPOffset<CellOutput<'bldr>> {
let mut builder = CellOutputBuilder::new(_fbb);
builder.add_capacity(args.capacity);
if let Some(x) = args.contract { builder.add_contract(x); }
if let Some(x) = args.type_ { builder.add_type_(x); }
if let Some(x) = args.lock { builder.add_lock(x); }
if let Some(x) = args.data { builder.add_data(x); }
builder.finish()
Expand All @@ -1385,7 +1385,7 @@ impl<'a> CellOutput<'a> {
pub const VT_CAPACITY: flatbuffers::VOffsetT = 4;
pub const VT_DATA: flatbuffers::VOffsetT = 6;
pub const VT_LOCK: flatbuffers::VOffsetT = 8;
pub const VT_CONTRACT: flatbuffers::VOffsetT = 10;
pub const VT_TYPE_: flatbuffers::VOffsetT = 10;

#[inline]
pub fn capacity(&self) -> u64 {
Expand All @@ -1400,16 +1400,16 @@ impl<'a> CellOutput<'a> {
self._tab.get::<flatbuffers::ForwardsUOffset<Bytes<'a>>>(CellOutput::VT_LOCK, None)
}
#[inline]
pub fn contract(&self) -> Option<Script<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<Script<'a>>>(CellOutput::VT_CONTRACT, None)
pub fn type_(&self) -> Option<Script<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<Script<'a>>>(CellOutput::VT_TYPE_, None)
}
}

pub struct CellOutputArgs<'a> {
pub capacity: u64,
pub data: Option<flatbuffers::WIPOffset<Bytes<'a >>>,
pub lock: Option<flatbuffers::WIPOffset<Bytes<'a >>>,
pub contract: Option<flatbuffers::WIPOffset<Script<'a >>>,
pub type_: Option<flatbuffers::WIPOffset<Script<'a >>>,
}
impl<'a> Default for CellOutputArgs<'a> {
#[inline]
Expand All @@ -1418,7 +1418,7 @@ impl<'a> Default for CellOutputArgs<'a> {
capacity: 0,
data: None,
lock: None,
contract: None,
type_: None,
}
}
}
Expand All @@ -1440,8 +1440,8 @@ impl<'a: 'b, 'b> CellOutputBuilder<'a, 'b> {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Bytes>>(CellOutput::VT_LOCK, lock);
}
#[inline]
pub fn add_contract(&mut self, contract: flatbuffers::WIPOffset<Script<'b >>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Script>>(CellOutput::VT_CONTRACT, contract);
pub fn add_type_(&mut self, type_: flatbuffers::WIPOffset<Script<'b >>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Script>>(CellOutput::VT_TYPE_, type_);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> CellOutputBuilder<'a, 'b> {
Expand Down
6 changes: 3 additions & 3 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,
"contract": 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,
"contract": null,
"type_": null,
"data": [],
"lock": "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff"
}
Expand Down Expand Up @@ -267,7 +267,7 @@ curl -d '{"id": 2, "jsonrpc": "2.0", "method":"get_current_cell","params": [{"ha
"result": {
"cell": {
"capacity": 50000,
"contract": null,
"type_": null,
"data": [],
"lock": "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff"
},
Expand Down
8 changes: 4 additions & 4 deletions script/src/syscalls/fetch_script_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ impl<'a> FetchScriptHash<'a> {
(Source::INPUT, Category::CONTRACT) => {
self.input_cells.get(index).and_then(|input_cell| {
input_cell
.contract
.type_
.as_ref()
.map(|contract| contract.type_hash())
.map(|type_| type_.type_hash())
})
}
(Source::OUTPUT, Category::LOCK) => {
self.outputs.get(index).map(|output| output.lock.clone())
}
(Source::OUTPUT, Category::CONTRACT) => self.outputs.get(index).and_then(|output| {
output
.contract
.type_
.as_ref()
.map(|contract| contract.type_hash())
.map(|type_| type_.type_hash())
}),
}
}
Expand Down
12 changes: 6 additions & 6 deletions script/src/syscalls/load_cell_by_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCellByField<'a> {
store_data(machine, &cell.lock.as_bytes())?;
SUCCESS
}
CellField::Contract => match cell.contract {
Some(ref contract) => {
CellField::Type => match cell.type_ {
Some(ref type_) => {
let mut builder = FlatBufferBuilder::new();
let offset = FbsScript::build(&mut builder, &contract);
let offset = FbsScript::build(&mut builder, &type_);
builder.finish(offset, None);
let data = builder.finished_data();
store_data(machine, data)?;
SUCCESS
}
None => ITEM_MISSING,
},
CellField::ContractHash => match cell.contract {
Some(ref contract) => {
store_data(machine, &contract.type_hash().as_bytes())?;
CellField::TypeHash => match cell.type_ {
Some(ref type_) => {
store_data(machine, &type_.type_hash().as_bytes())?;
SUCCESS
}
None => ITEM_MISSING,
Expand Down
10 changes: 5 additions & 5 deletions script/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ enum CellField {
Data = 1,
DataHash = 2,
LockHash = 3,
Contract = 4,
ContractHash = 5,
Type = 4,
TypeHash = 5,
}

impl CellField {
Expand All @@ -41,8 +41,8 @@ impl CellField {
1 => Ok(CellField::Data),
2 => Ok(CellField::DataHash),
3 => Ok(CellField::LockHash),
4 => Ok(CellField::Contract),
5 => Ok(CellField::ContractHash),
4 => Ok(CellField::Type),
5 => Ok(CellField::TypeHash),
_ => Err(Error::ParseError),
}
}
Expand Down Expand Up @@ -594,7 +594,7 @@ mod tests {
machine.registers_mut()[A2] = 0; // offset
machine.registers_mut()[A3] = 0; //index
machine.registers_mut()[A4] = Source::Output as u64; //source: 2 output
machine.registers_mut()[A5] = CellField::Contract as u64; //field: 4 contract
machine.registers_mut()[A5] = CellField::Type as u64; //field: 4 type
machine.registers_mut()[A7] = LOAD_CELL_BY_FIELD_SYSCALL_NUMBER; // syscall number

let output_cell = CellOutput::new(100, vec![], H256::default(), None);
Expand Down
4 changes: 2 additions & 2 deletions script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ impl<'a> TransactionScriptsVerifier<'a> {
})?;
}
for (i, output) in self.outputs.iter().enumerate() {
if let Some(ref contract) = output.contract {
if let Some(ref type_) = output.type_ {
let prefix = format!("Transaction {}, output {}", self.hash, i);
self.verify_script(contract, &prefix, output, None).map_err(|e| {
self.verify_script(type_, &prefix, output, None).map_err(|e| {
info!(target: "script", "Error validating output {} of transaction {}: {:?}", i, self.hash, e);
e
})?;
Expand Down

0 comments on commit 6e128c1

Please sign in to comment.