Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove version from Script #422

Merged
merged 1 commit into from Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 17 additions & 31 deletions core/src/script.rs
Expand Up @@ -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<Vec<u8>>,
// 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
Expand All @@ -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()?;
Expand All @@ -42,47 +41,34 @@ impl fmt::Debug for Script {
}
}

type ScriptTuple = (u8, Vec<Vec<u8>>, H256);
type ScriptTuple = (Vec<Vec<u8>>, 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<Vec<u8>>, binary_hash: H256) -> Self {
Script {
version,
args,
binary_hash,
}
pub fn new(args: Vec<Vec<u8>>, 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()
}
}

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions core/src/transaction.rs
Expand Up @@ -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()
);
}
Expand Down
3 changes: 1 addition & 2 deletions miner/src/block_assembler.rs
Expand Up @@ -267,8 +267,7 @@ impl<CI: ChainIndex + 'static> BlockAssembler<CI> {
}

// 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)?;

Expand Down
1 change: 0 additions & 1 deletion protocol/src/builder.rs
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion protocol/src/convert.rs
Expand Up @@ -260,7 +260,6 @@ impl<'a> TryFrom<ckb_protocol::Script<'a>> for ckb_core::script::Script {
};

Ok(ckb_core::script::Script {
version: script.version(),
args: cast!(args)?,
binary_hash: cast!(binary_hash)?,
})
Expand Down
1 change: 0 additions & 1 deletion protocol/src/protocol.fbs
Expand Up @@ -91,7 +91,6 @@ table CellOutput {
}

table Script {
version: uint8;
args: [Bytes];
binary_hash: H256;
}
Expand Down
16 changes: 2 additions & 14 deletions protocol/src/protocol_generated.rs
Expand Up @@ -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::<u8>(Script::VT_VERSION, Some(0)).unwrap()
}
#[inline]
pub fn args(&self) -> Option<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Bytes<'a>>>> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Bytes<'a>>>>>(Script::VT_ARGS, None)
Expand All @@ -1948,15 +1942,13 @@ impl<'a> Script<'a> {
}

pub struct ScriptArgs<'a> {
pub version: u8,
pub args: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , flatbuffers::ForwardsUOffset<Bytes<'a >>>>>,
pub binary_hash: Option<&'a H256>,
}
impl<'a> Default for ScriptArgs<'a> {
#[inline]
fn default() -> Self {
ScriptArgs {
version: 0,
args: None,
binary_hash: None,
}
Expand All @@ -1967,10 +1959,6 @@ pub struct ScriptBuilder<'a: 'b, 'b> {
start_: flatbuffers::WIPOffset<flatbuffers::TableUnfinishedWIPOffset>,
}
impl<'a: 'b, 'b> ScriptBuilder<'a, 'b> {
#[inline]
pub fn add_version(&mut self, version: u8) {
self.fbb_.push_slot::<u8>(Script::VT_VERSION, version, 0);
}
#[inline]
pub fn add_args(&mut self, args: flatbuffers::WIPOffset<flatbuffers::Vector<'b , flatbuffers::ForwardsUOffset<Bytes<'b >>>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(Script::VT_ARGS, args);
Expand Down
9 changes: 0 additions & 9 deletions protocol/src/protocol_generated_verifier.rs
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion script/src/syscalls/mod.rs
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions script/src/verify.rs
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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),
);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions spec/src/lib.rs
Expand Up @@ -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();
Expand All @@ -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"
Expand Down
13 changes: 3 additions & 10 deletions util/jsonrpc-types/src/blockchain.rs
Expand Up @@ -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<Bytes>,
pub binary_hash: H256,
}

impl From<Script> for CoreScript {
fn from(json: Script) -> CoreScript {
let Script {
version,
args,
binary_hash,
} = json;
let Script { args, binary_hash } = json;
CoreScript::new(
version,
args.into_iter().map(|arg| arg.into_vec()).collect(),
binary_hash,
)
Expand All @@ -37,9 +31,8 @@ impl From<Script> for CoreScript {

impl From<CoreScript> for Script {
fn from(core: CoreScript) -> Script {
let (version, args, binary_hash) = core.destruct();
let (args, binary_hash) = core.destruct();
Script {
version,
binary_hash,
args: args.into_iter().map(Bytes::new).collect(),
}
Expand Down Expand Up @@ -373,7 +366,7 @@ mod tests {
use proptest::{collection::size_range, prelude::*};

fn mock_script(arg: Vec<u8>) -> CoreScript {
CoreScript::new(0, vec![arg], H256::default())
CoreScript::new(vec![arg], H256::default())
}

fn mock_cell_output(data: Vec<u8>, arg: Vec<u8>) -> CoreCellOutput {
Expand Down