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

refactor: Revise script structure #344

Merged
merged 14 commits into from
Mar 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heap
rand = "0.6"
tempfile = "3.0"
ckb-traits = { path = "../traits" }
hash = {path = "../util/hash"}

[[bench]]
name = "cuckoo"
Expand Down
39 changes: 16 additions & 23 deletions benches/benches/process_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ use criterion::{criterion_group, criterion_main, Criterion};
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
use rand::random;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::sync::Arc;
use tempfile::{tempdir, TempDir};

Expand Down Expand Up @@ -139,17 +136,20 @@ fn new_chain() -> (
) {
let cellbase = TransactionBuilder::default()
.input(CellInput::new_cellbase_input(0))
.output(CellOutput::new(0, vec![], H256::zero(), None))
.output(CellOutput::new(0, vec![], Script::default(), None))
.build();

let script = create_script();

// create genesis block with 100 tx
let commit_transactions: Vec<Transaction> = (0..100)
.map(|i| {
TransactionBuilder::default()
.input(CellInput::new(OutPoint::null(), script.clone()))
.output(CellOutput::new(50000, vec![i], script.type_hash(), None))
.input(CellInput::new(OutPoint::null(), vec![]))
.output(CellOutput::new(
50000,
vec![i],
Script::always_success(),
None,
))
.build()
})
.collect();
Expand Down Expand Up @@ -185,7 +185,7 @@ fn gen_block(blocks: &mut Vec<Block>, parent_index: usize) {

let cellbase = TransactionBuilder::default()
.input(CellInput::new_cellbase_input(number))
.output(CellOutput::new(0, vec![], H256::zero(), None))
.output(CellOutput::new(0, vec![], Script::default(), None))
.build();

// spent n-2 block's tx and proposal n-1 block's tx
Expand Down Expand Up @@ -225,20 +225,13 @@ fn gen_block(blocks: &mut Vec<Block>, parent_index: usize) {
}

fn create_transaction(hash: H256) -> Transaction {
let script = create_script();
TransactionBuilder::default()
.output(CellOutput::new(50000, vec![], script.type_hash(), None))
.input(CellInput::new(OutPoint::new(hash, 0), script))
.output(CellOutput::new(
50000,
vec![],
Script::always_success(),
None,
))
.input(CellInput::new(OutPoint::new(hash, 0), vec![]))
.build()
}

fn create_script() -> Script {
let mut file = File::open(
Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/cells/always_success"),
)
.unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();

Script::new(0, Vec::new(), None, Some(buffer), Vec::new())
}
1 change: 1 addition & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lru-cache = { git = "https://github.com/nervosnetwork/lru-cache" }
serde = "1.0"
ckb-traits = { path = "../traits" }
failure = "0.1.5"
hash = {path = "../util/hash"}

[dev-dependencies]
env_logger = "0.6"
Expand Down
6 changes: 3 additions & 3 deletions chain/src/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use ckb_core::block::Block;
use ckb_core::block::BlockBuilder;
use ckb_core::cell::{CellProvider, CellStatus};
use ckb_core::header::HeaderBuilder;
use ckb_core::script::Script;
use ckb_core::transaction::{CellInput, CellOutput, OutPoint, TransactionBuilder};
use ckb_shared::error::SharedError;
use ckb_traits::ChainProvider;
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
use std::sync::Arc;

Expand All @@ -19,7 +19,7 @@ fn test_genesis_transaction_spend() {
CellOutput::new(
100_000_000,
vec![],
H256::default(),
Script::default(),
None
);
100
Expand Down Expand Up @@ -349,7 +349,7 @@ fn test_genesis_transaction_fetch() {
CellOutput::new(
100_000_000,
vec![],
H256::default(),
Script::default(),
None
);
100
Expand Down
20 changes: 3 additions & 17 deletions chain/src/tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ use ckb_shared::store::ChainKVStore;
use faketime::unix_time_as_millis;
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
use std::fs::File;
use std::io::Read;
use std::path::Path;

pub(crate) fn start_chain(
consensus: Option<Consensus>,
Expand All @@ -42,7 +39,7 @@ fn create_cellbase(number: BlockNumber) -> Transaction {
.output(CellOutput::new(
5000,
vec![],
create_script().type_hash(),
Script::always_success(),
None,
))
.build()
Expand Down Expand Up @@ -78,24 +75,13 @@ pub(crate) fn gen_block(
}

pub(crate) fn create_transaction(parent: H256, unique_data: u8) -> Transaction {
let script = create_script();
TransactionBuilder::default()
.output(CellOutput::new(
5000,
vec![unique_data],
script.type_hash(),
Script::always_success(),
None,
))
.input(CellInput::new(OutPoint::new(parent, 0), script))
.input(CellInput::new(OutPoint::new(parent, 0), vec![]))
.build()
}

fn create_script() -> Script {
let mut file = File::open(
Path::new(env!("CARGO_MANIFEST_DIR")).join("../nodes_template/spec/cells/always_success"),
)
.unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).unwrap();
Script::new(0, Vec::new(), None, Some(buffer), Vec::new())
}
3 changes: 2 additions & 1 deletion core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl ResolvedTransaction {

#[cfg(test)]
mod tests {
use super::super::script::Script;
use super::*;
use numext_fixed_hash::H256;
use std::collections::HashMap;
Expand Down Expand Up @@ -241,7 +242,7 @@ mod tests {
let o = CellOutput {
capacity: 2,
data: vec![],
lock: H256::default(),
lock: Script::default(),
type_: None,
};

Expand Down
Loading