From c581ea3dfc1f40f2396dcee9e8517ba4526f057c Mon Sep 17 00:00:00 2001 From: Xuejie Xiao Date: Tue, 9 Apr 2019 08:28:58 +0000 Subject: [PATCH] feat: Remove version from Script NOTE: This is a breaking change --- core/src/script.rs | 48 ++++++++------------- core/src/transaction.rs | 4 +- miner/src/block_assembler.rs | 3 +- protocol/src/builder.rs | 1 - protocol/src/convert.rs | 1 - protocol/src/protocol.fbs | 1 - protocol/src/protocol_generated.rs | 16 +------ protocol/src/protocol_generated_verifier.rs | 9 ---- script/src/syscalls/mod.rs | 2 +- script/src/verify.rs | 14 +++--- spec/src/lib.rs | 4 +- util/jsonrpc-types/src/blockchain.rs | 13 ++---- 12 files changed, 35 insertions(+), 81 deletions(-) diff --git a/core/src/script.rs b/core/src/script.rs index 79fd464f9b..e18a4ed716 100644 --- a/core/src/script.rs +++ b/core/src/script.rs @@ -13,7 +13,6 @@ pub const ALWAYS_SUCCESS_HASH: H256 = h256!("0x1"); // implement proper From trait #[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash)] pub struct Script { - pub version: u8, pub args: Vec>, // Binary hash here can be used to refer to binary in one of the dep // cells of current transaction. The hash here must match the hash of @@ -31,7 +30,7 @@ fn prefix_hex(bytes: &[u8]) -> String { impl fmt::Debug for Script { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Script {{ version: {}, args: ", self.version,)?; + write!(f, "Script {{ args: ")?; f.debug_list() .entries(self.args.iter().map(|arg| prefix_hex(arg))) .finish()?; @@ -42,47 +41,34 @@ impl fmt::Debug for Script { } } -type ScriptTuple = (u8, Vec>, H256); +type ScriptTuple = (Vec>, H256); const VEC_WRITE_ALL_EXPECT: &str = "Essentially, Vec::write_all invoke extend_from_slice, should not fail"; impl Script { - pub fn new(version: u8, args: Vec>, binary_hash: H256) -> Self { - Script { - version, - args, - binary_hash, - } + pub fn new(args: Vec>, binary_hash: H256) -> Self { + Script { args, binary_hash } } pub fn always_success() -> Self { - Self::new(0, vec![], ALWAYS_SUCCESS_HASH) + Self::new(vec![], ALWAYS_SUCCESS_HASH) } pub fn destruct(self) -> ScriptTuple { - let Script { - version, - args, - binary_hash, - } = self; - (version, args, binary_hash) + let Script { args, binary_hash } = self; + (args, binary_hash) } pub fn hash(&self) -> H256 { - match self.version { - 0 => { - let mut bytes = vec![]; - bytes - .write_all(self.binary_hash.as_bytes()) - .expect(VEC_WRITE_ALL_EXPECT); - for argument in &self.args { - bytes.write_all(argument).expect(VEC_WRITE_ALL_EXPECT); - } - blake2b_256(bytes).into() - } - _ => H256::zero(), + let mut bytes = vec![]; + bytes + .write_all(self.binary_hash.as_bytes()) + .expect(VEC_WRITE_ALL_EXPECT); + for argument in &self.args { + bytes.write_all(argument).expect(VEC_WRITE_ALL_EXPECT); } + blake2b_256(bytes).into() } } @@ -99,7 +85,7 @@ mod tests { #[test] fn empty_script_hash() { - let script = Script::new(0, vec![], H256::zero()); + let script = Script::new(vec![], H256::zero()); let expect = H256::from_hex_str("266cec97cbede2cfbce73666f08deed9560bdf7841a7a5a51b3a3f09da249e21") .unwrap(); @@ -111,7 +97,7 @@ mod tests { let always_success = include_bytes!("../../nodes_template/spec/cells/always_success"); let always_success_hash: H256 = (&blake2b_256(&always_success[..])).into(); - let script = Script::new(0, vec![], always_success_hash); + let script = Script::new(vec![], always_success_hash); let expect = H256::from_hex_str("9a9a6bdbc38d4905eace1822f85237e3a1e238bb3f277aa7b7c8903441123510") .unwrap(); @@ -120,7 +106,7 @@ mod tests { #[test] fn one_script_hash() { - let one = Script::new(0, vec![vec![1]], H256::zero()); + let one = Script::new(vec![vec![1]], H256::zero()); let expect = H256::from_hex_str("dade0e507e27e2a5995cf39c8cf454b6e70fa80d03c1187db7a4cb2c9eab79da") .unwrap(); diff --git a/core/src/transaction.rs b/core/src/transaction.rs index 6fe703a35a..ae2bc76a7d 100644 --- a/core/src/transaction.rs +++ b/core/src/transaction.rs @@ -430,12 +430,12 @@ mod test { assert_eq!( tx.hash(), - H256::from_hex_str("7e1e256d6882809b7dfb55d002e54c5b4fbdbbbe8ce906aa6eae1b429de4d3d8") + H256::from_hex_str("3a4238c3fda565d6e76e76b5b05d3403b37b94d53c1644d5ff58d4e9293ca468") .unwrap() ); assert_eq!( tx.witness_hash(), - H256::from_hex_str("93d363096f3901651b718d093751aa78ce7b56274850841a3e4134fe14ffb120") + H256::from_hex_str("997f0627d2c1ef00fc98311357aa097c3fff5ed0a0408e14ea26656f5beae6b3") .unwrap() ); } diff --git a/miner/src/block_assembler.rs b/miner/src/block_assembler.rs index a0ff429a49..9cdb975023 100644 --- a/miner/src/block_assembler.rs +++ b/miner/src/block_assembler.rs @@ -267,8 +267,7 @@ impl BlockAssembler { } // dummy cellbase - let cellbase_lock = - Script::new(0, self.config.args.clone(), self.config.binary_hash.clone()); + let cellbase_lock = Script::new(self.config.args.clone(), self.config.binary_hash.clone()); let cellbase = self.create_cellbase_transaction(header, &commit_transactions, cellbase_lock)?; diff --git a/protocol/src/builder.rs b/protocol/src/builder.rs index 3e4f911ecd..2d8ff9f8e3 100644 --- a/protocol/src/builder.rs +++ b/protocol/src/builder.rs @@ -173,7 +173,6 @@ impl<'a> FbsScript<'a> { let binary_hash = (&script.binary_hash).into(); let mut builder = ScriptBuilder::new(fbb); - builder.add_version(script.version); builder.add_args(args); builder.add_binary_hash(&binary_hash); builder.finish() diff --git a/protocol/src/convert.rs b/protocol/src/convert.rs index 84c6be1a40..5492b42b14 100644 --- a/protocol/src/convert.rs +++ b/protocol/src/convert.rs @@ -260,7 +260,6 @@ impl<'a> TryFrom> for ckb_core::script::Script { }; Ok(ckb_core::script::Script { - version: script.version(), args: cast!(args)?, binary_hash: cast!(binary_hash)?, }) diff --git a/protocol/src/protocol.fbs b/protocol/src/protocol.fbs index e546e68828..c5f67af9f5 100644 --- a/protocol/src/protocol.fbs +++ b/protocol/src/protocol.fbs @@ -91,7 +91,6 @@ table CellOutput { } table Script { - version: uint8; args: [Bytes]; binary_hash: H256; } diff --git a/protocol/src/protocol_generated.rs b/protocol/src/protocol_generated.rs index 8c6f12dc3b..798449a7f7 100644 --- a/protocol/src/protocol_generated.rs +++ b/protocol/src/protocol_generated.rs @@ -1925,18 +1925,12 @@ impl<'a> Script<'a> { let mut builder = ScriptBuilder::new(_fbb); if let Some(x) = args.binary_hash { builder.add_binary_hash(x); } if let Some(x) = args.args { builder.add_args(x); } - builder.add_version(args.version); builder.finish() } - pub const VT_VERSION: flatbuffers::VOffsetT = 4; - pub const VT_ARGS: flatbuffers::VOffsetT = 6; - pub const VT_BINARY_HASH: flatbuffers::VOffsetT = 8; + pub const VT_ARGS: flatbuffers::VOffsetT = 4; + pub const VT_BINARY_HASH: flatbuffers::VOffsetT = 6; - #[inline] - pub fn version(&self) -> u8 { - self._tab.get::(Script::VT_VERSION, Some(0)).unwrap() - } #[inline] pub fn args(&self) -> Option>>> { self._tab.get::>>>>(Script::VT_ARGS, None) @@ -1948,7 +1942,6 @@ impl<'a> Script<'a> { } pub struct ScriptArgs<'a> { - pub version: u8, pub args: Option>>>>, pub binary_hash: Option<&'a H256>, } @@ -1956,7 +1949,6 @@ impl<'a> Default for ScriptArgs<'a> { #[inline] fn default() -> Self { ScriptArgs { - version: 0, args: None, binary_hash: None, } @@ -1967,10 +1959,6 @@ pub struct ScriptBuilder<'a: 'b, 'b> { start_: flatbuffers::WIPOffset, } impl<'a: 'b, 'b> ScriptBuilder<'a, 'b> { - #[inline] - pub fn add_version(&mut self, version: u8) { - self.fbb_.push_slot::(Script::VT_VERSION, version, 0); - } #[inline] pub fn add_args(&mut self, args: flatbuffers::WIPOffset>>>) { self.fbb_.push_slot_always::>(Script::VT_ARGS, args); diff --git a/protocol/src/protocol_generated_verifier.rs b/protocol/src/protocol_generated_verifier.rs index c2699776fc..e8c1e8ffc2 100644 --- a/protocol/src/protocol_generated_verifier.rs +++ b/protocol/src/protocol_generated_verifier.rs @@ -2169,15 +2169,6 @@ pub mod ckb { } } - if Self::VT_VERSION as usize + flatbuffers::SIZE_VOFFSET - <= vtab_num_bytes - { - let voffset = vtab.get(Self::VT_VERSION) as usize; - if voffset > 0 && object_inline_num_bytes - voffset < 1 { - return Err(Error::OutOfBounds); - } - } - if Self::VT_ARGS as usize + flatbuffers::SIZE_VOFFSET <= vtab_num_bytes { diff --git a/script/src/syscalls/mod.rs b/script/src/syscalls/mod.rs index dc5c90a7a7..f2e5ec6fd3 100644 --- a/script/src/syscalls/mod.rs +++ b/script/src/syscalls/mod.rs @@ -561,7 +561,7 @@ mod tests { machine.set_register(A5, CellField::LockHash as u64); //field: 3 lock hash machine.set_register(A7, LOAD_CELL_BY_FIELD_SYSCALL_NUMBER); // syscall number - let script = Script::new(0, vec![data.to_vec()], H256::zero()); + let script = Script::new(vec![data.to_vec()], H256::zero()); let h = script.hash(); let hash = h.as_bytes(); let input_cell = CellOutput::new(100, vec![], script, None); diff --git a/script/src/verify.rs b/script/src/verify.rs index 2f621eb148..db7545dab9 100644 --- a/script/src/verify.rs +++ b/script/src/verify.rs @@ -264,7 +264,7 @@ mod tests { let dep_out_point = OutPoint::new(H256::from_trimmed_hex_str("123").unwrap(), 8); let dep_cell = CellOutput::new(buffer.len() as Capacity, buffer, Script::default(), None); - let script = Script::new(0, args, binary_hash); + let script = Script::new(args, binary_hash); let input = CellInput::new(OutPoint::null(), vec![]); let transaction = TransactionBuilder::default() @@ -319,7 +319,7 @@ mod tests { let dep_out_point = OutPoint::new(H256::from_trimmed_hex_str("123").unwrap(), 8); let dep_cell = CellOutput::new(buffer.len() as Capacity, buffer, Script::default(), None); - let script = Script::new(0, args, binary_hash); + let script = Script::new(args, binary_hash); let input = CellInput::new(OutPoint::null(), vec![]); let transaction = TransactionBuilder::default() @@ -376,7 +376,7 @@ mod tests { let dep_out_point = OutPoint::new(H256::from_trimmed_hex_str("123").unwrap(), 8); let dep_cell = CellOutput::new(buffer.len() as Capacity, buffer, Script::default(), None); - let script = Script::new(0, args, binary_hash); + let script = Script::new(args, binary_hash); let input = CellInput::new(OutPoint::null(), vec![]); let transaction = TransactionBuilder::default() @@ -429,7 +429,7 @@ mod tests { witness_data.insert(0, hex_pubkey); let binary_hash: H256 = (&blake2b_256(&buffer)).into(); - let script = Script::new(0, args, binary_hash); + let script = Script::new(args, binary_hash); let input = CellInput::new(OutPoint::null(), vec![]); let transaction = TransactionBuilder::default() @@ -482,11 +482,11 @@ mod tests { let input = CellInput::new(OutPoint::null(), vec![]); let dummy_cell = CellOutput::new(100, vec![], Script::always_success(), None); - let script = Script::new(0, args, (&blake2b_256(&buffer)).into()); + let script = Script::new(args, (&blake2b_256(&buffer)).into()); let output = CellOutput::new( 0, Vec::new(), - Script::new(0, vec![], H256::zero()), + Script::new(vec![], H256::zero()), Some(script), ); @@ -543,7 +543,7 @@ mod tests { let input = CellInput::new(OutPoint::null(), vec![]); let dummy_cell = CellOutput::new(100, vec![], Script::always_success(), None); - let script = Script::new(0, args, (&blake2b_256(&buffer)).into()); + let script = Script::new(args, (&blake2b_256(&buffer)).into()); let output = CellOutput::new(0, Vec::new(), Script::default(), Some(script)); let dep_out_point = OutPoint::new(H256::from_trimmed_hex_str("123").unwrap(), 8); diff --git a/spec/src/lib.rs b/spec/src/lib.rs index 76f599871d..f2281f9a33 100644 --- a/spec/src/lib.rs +++ b/spec/src/lib.rs @@ -230,7 +230,7 @@ pub mod test { // Tx and Output hash will be used in some test cases directly, assert here for convenience assert_eq!( format!("{:x}", tx.hash()), - "9c3c3cc1a11966ff78a739a1ddb5e4b94fdcaa4e63e3e341c6f8126de2dfa2ac" + "913a98b7a6521b879e60a02a2c38ea14355f3e98beb60215e67e2512b6e0a235" ); let reference = tx.outputs()[0].data_hash(); @@ -239,7 +239,7 @@ pub mod test { "28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5" ); - let script = Script::new(0, vec![], reference); + let script = Script::new(vec![], reference); assert_eq!( format!("{:x}", script.hash()), "9a9a6bdbc38d4905eace1822f85237e3a1e238bb3f277aa7b7c8903441123510" diff --git a/util/jsonrpc-types/src/blockchain.rs b/util/jsonrpc-types/src/blockchain.rs index ddcb173ca3..d6783cf7d0 100644 --- a/util/jsonrpc-types/src/blockchain.rs +++ b/util/jsonrpc-types/src/blockchain.rs @@ -15,20 +15,14 @@ use serde_derive::{Deserialize, Serialize}; #[derive(Clone, Default, Serialize, Deserialize, PartialEq, Eq, Hash, Debug)] pub struct Script { - pub version: u8, pub args: Vec, pub binary_hash: H256, } impl From