Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Mar 25, 2024
1 parent 48f17bc commit eaa98a5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 42 deletions.
2 changes: 0 additions & 2 deletions ckb-transaction-cobuild/src/lazy_reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::cmp::min;

use super::schemas2::blockchain;
use crate::log;
use alloc::boxed::Box;
use ckb_std::{ckb_constants::Source, error::SysError, syscalls};

Expand Down Expand Up @@ -30,7 +29,6 @@ fn read_data<F: Fn(&mut [u8], usize) -> Result<usize, SysError>>(
},
};
let read_len = min(buf.len(), actual_len);
log!("totally read {} bytes", read_len);
Ok(read_len)
}

Expand Down
24 changes: 12 additions & 12 deletions ckb-transaction-cobuild/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ extern crate alloc;
pub mod blake2b;
pub mod error;
pub mod lazy_reader;
pub mod log;
mod log;
// keep this as molecule builder is missing in lazy reader version
pub mod schemas;
// lazy reader version of molecule
pub mod schemas2;

use alloc::{collections::BTreeSet, vec::Vec};
Expand All @@ -54,13 +56,10 @@ use ckb_std::{
},
syscalls,
};
use core::convert::Into;
use error::Error;
use lazy_reader::{new_input_cell_data, new_transaction, new_witness};
use molecule::lazy_reader::Cursor;
use schemas2::basic::Message;

pub use error::Error;
pub use schemas2::{basic, blockchain, top_level};
use schemas2::{basic, blockchain, top_level};

///
/// This is the callback trait should be implemented in lock script by
Expand Down Expand Up @@ -115,7 +114,7 @@ fn fetch_seal() -> Result<Vec<u8>, Error> {
/// - Returns `Error::WrongWitnessLayout` if multiple `SighashAll` witnesses are
/// found. This function is intended for use within type scripts and lock
/// scripts.
pub fn fetch_message() -> Result<Option<schemas2::basic::Message>, Error> {
pub fn fetch_message() -> Result<Option<basic::Message>, Error> {
let tx = new_transaction();
let (witness_layouts, _) = parse_witness_layouts(&tx)?;

Expand Down Expand Up @@ -157,7 +156,7 @@ fn check_others_in_group() -> Result<(), Error> {
///
/// Generate signing message hash for SighashAll or SighashAllOnly.
///
fn generate_signing_message_hash(message: &Option<Message>) -> Result<[u8; 32], Error> {
fn generate_signing_message_hash(message: &Option<basic::Message>) -> Result<[u8; 32], Error> {
let tx = new_transaction();

// message
Expand Down Expand Up @@ -288,7 +287,7 @@ pub fn cobuild_entry<F: Callback>(verifier: F) -> Result<bool, Error> {
break;
}
match witness {
Some(schemas2::top_level::WitnessLayout::Otx(ref otx)) => {
Some(top_level::WitnessLayout::Otx(ref otx)) => {
otx_count += 1;
let input_cells: u32 = otx.input_cells()?;
let output_cells: u32 = otx.output_cells()?;
Expand Down Expand Up @@ -325,6 +324,7 @@ pub fn cobuild_entry<F: Callback>(verifier: F) -> Result<bool, Error> {
for seal_pair in otx.seals()?.iter() {
if seal_pair.script_hash()? == current_script_hash.as_slice() {
let seal: Vec<u8> = seal_pair.seal()?.try_into()?;
log!("invoke OTX verifier");
verifier.invoke(&seal, &smh)?;
seal_found = true;
execution_count += 1;
Expand Down Expand Up @@ -361,7 +361,7 @@ pub fn cobuild_entry<F: Callback>(verifier: F) -> Result<bool, Error> {
if loop_index < i || loop_index >= j {
if let Some(r) = &witness_layouts.get(loop_index) {
match r {
Some(schemas2::top_level::WitnessLayout::Otx(_)) => {
Some(top_level::WitnessLayout::Otx(_)) => {
log!(
"WrongWitnessLayout at index = {} (i = {}, j = {}, otx_count = {})",
loop_index,
Expand Down Expand Up @@ -400,7 +400,7 @@ pub fn cobuild_entry<F: Callback>(verifier: F) -> Result<bool, Error> {

/// generate OTX signing message hash
fn generate_otx_smh(
otx: &schemas2::basic::Otx,
otx: &basic::Otx,
raw_tx: &blockchain::RawTransaction,
ie: usize,
oe: usize,
Expand Down Expand Up @@ -465,7 +465,7 @@ fn generate_otx_smh(
let count = hasher.count();
hasher.finalize(&mut result);
log!(
"generate_otx_smh2 totally hashed {} bytes and hash is {:?}",
"generate_otx_smh totally hashed {} bytes and hash is {:?}",
count,
result
);
Expand Down
1 change: 0 additions & 1 deletion tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl Loader {
pub fn load_binary(&self, name: &str) -> Bytes {
let mut path = self.0.clone();
path.push(name);
println!("path = {}", path.display());
fs::read(path).expect("binary").into()
}
}
59 changes: 32 additions & 27 deletions tests/src/test_otx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ impl Verifier {
let str = format!("Script({})", hex::encode(&script.as_slice()[..4]));
println!("{}: {}", str, msg);
});
verifier.verify(u64::MAX)
let result = verifier.verify(u64::MAX);
if result.is_ok() {
let cycles = (*result.as_ref().unwrap() as f64) / 1024.0 / 1024.0;
println!("cycles = {:.1} M ", cycles);
}
result
}
}

Expand Down Expand Up @@ -369,12 +374,12 @@ fn generate_otx_a0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra

// Create cell meta
let cell_meta_always_success = px.insert_cell_data(dl, &BINARY_ALWAYS_SUCCESS);
let cell_meta_omni_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_demo_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_auth = px.insert_cell_data(dl, &BINARY_AUTH);
let cell_meta_secp256k1_data = px.insert_cell_data(dl, &BINARY_SECP256K1_DATA);
let cell_meta_i = px.insert_cell_fund(
dl,
px.create_script(&cell_meta_omni_lock, &args),
px.create_script(&cell_meta_demo_lock, &args),
None,
&[0u8; 3000],
);
Expand All @@ -383,7 +388,7 @@ fn generate_otx_a0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_always_success));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_auth));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_secp256k1_data));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_demo_lock));
let tx_builder = tx_builder
.header_dep(ckb_types::packed::Byte32::from_slice(&[11u8; 32]).unwrap())
.header_dep(ckb_types::packed::Byte32::from_slice(&[12u8; 32]).unwrap());
Expand Down Expand Up @@ -424,7 +429,7 @@ fn generate_otx_a0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
println_hex("seal", seal.as_slice());
let seal = schemas::basic::SealPair::new_builder()
.script_hash(
px.create_script(&cell_meta_omni_lock, &args)
px.create_script(&cell_meta_demo_lock, &args)
.calc_script_hash(),
)
.seal(seal.pack())
Expand Down Expand Up @@ -457,12 +462,12 @@ fn generate_otx_b0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra

// Create cell meta
let cell_meta_always_success = px.insert_cell_data(dl, &BINARY_ALWAYS_SUCCESS);
let cell_meta_omni_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_demo_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_auth = px.insert_cell_data(dl, &BINARY_AUTH);
let cell_meta_secp256k1_data = px.insert_cell_data(dl, &BINARY_SECP256K1_DATA);
let cell_meta_i = px.insert_cell_fund(
dl,
px.create_script(&cell_meta_omni_lock, &args),
px.create_script(&cell_meta_demo_lock, &args),
None,
&[0u8; 1024],
);
Expand All @@ -472,7 +477,7 @@ fn generate_otx_b0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
let tx_builder: ckb_types::core::TransactionBuilder =
tx_builder.cell_dep(px.create_cell_dep(&cell_meta_auth));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_secp256k1_data));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_demo_lock));
let tx_builder = tx_builder
.header_dep(ckb_types::packed::Byte32::from_slice(&[11u8; 32]).unwrap())
.header_dep(ckb_types::packed::Byte32::from_slice(&[12u8; 32]).unwrap());
Expand Down Expand Up @@ -513,7 +518,7 @@ fn generate_otx_b0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
println_hex("seal", seal.as_slice());
let seal = schemas::basic::SealPair::new_builder()
.script_hash(
px.create_script(&cell_meta_omni_lock, &args)
px.create_script(&cell_meta_demo_lock, &args)
.calc_script_hash(),
)
.seal(seal.pack())
Expand Down Expand Up @@ -546,12 +551,12 @@ fn generate_otx_c0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra

// Create cell meta
let cell_meta_always_success = px.insert_cell_data(dl, &BINARY_ALWAYS_SUCCESS);
let cell_meta_omni_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_demo_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_auth = px.insert_cell_data(dl, &BINARY_AUTH);
let cell_meta_secp256k1_data = px.insert_cell_data(dl, &BINARY_SECP256K1_DATA);
let cell_meta_i = px.insert_cell_fund(
dl,
px.create_script(&cell_meta_omni_lock, &args),
px.create_script(&cell_meta_demo_lock, &args),
None,
&[0u8; 4100],
);
Expand All @@ -562,7 +567,7 @@ fn generate_otx_c0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
let tx_builder: ckb_types::core::TransactionBuilder =
tx_builder.cell_dep(px.create_cell_dep(&cell_meta_auth));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_secp256k1_data));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_demo_lock));

// Create input
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
Expand Down Expand Up @@ -600,7 +605,7 @@ fn generate_otx_c0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
println_hex("seal", seal.as_slice());
let seal = schemas::basic::SealPair::new_builder()
.script_hash(
px.create_script(&cell_meta_omni_lock, &args)
px.create_script(&cell_meta_demo_lock, &args)
.calc_script_hash(),
)
.seal(seal.pack())
Expand Down Expand Up @@ -633,19 +638,19 @@ fn generate_otx_d0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra

// Create cell meta
let cell_meta_always_success = px.insert_cell_data(dl, &BINARY_ALWAYS_SUCCESS);
let cell_meta_omni_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_demo_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_auth = px.insert_cell_data(dl, &BINARY_AUTH);
let cell_meta_secp256k1_data = px.insert_cell_data(dl, &BINARY_SECP256K1_DATA);
let cell_meta_i =
px.insert_cell_fund(dl, px.create_script(&cell_meta_omni_lock, &args), None, &[]);
px.insert_cell_fund(dl, px.create_script(&cell_meta_demo_lock, &args), None, &[]);

// Create cell dep
let tx_builder: ckb_types::core::TransactionBuilder =
tx_builder.cell_dep(px.create_cell_dep(&cell_meta_always_success));
let tx_builder: ckb_types::core::TransactionBuilder =
tx_builder.cell_dep(px.create_cell_dep(&cell_meta_auth));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_secp256k1_data));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_demo_lock));

// Create input
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
Expand Down Expand Up @@ -683,7 +688,7 @@ fn generate_otx_d0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
println_hex("seal", seal.as_slice());
let seal = schemas::basic::SealPair::new_builder()
.script_hash(
px.create_script(&cell_meta_omni_lock, &args)
px.create_script(&cell_meta_demo_lock, &args)
.calc_script_hash(),
)
.seal(seal.pack())
Expand Down Expand Up @@ -717,19 +722,19 @@ fn generate_otx_e0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra

// Create cell meta
let cell_meta_always_success = px.insert_cell_data(dl, &BINARY_ALWAYS_SUCCESS);
let cell_meta_omni_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_demo_lock = px.insert_cell_data(dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_auth = px.insert_cell_data(dl, &BINARY_AUTH);
let cell_meta_secp256k1_data = px.insert_cell_data(dl, &BINARY_SECP256K1_DATA);
let cell_meta_i =
px.insert_cell_fund(dl, px.create_script(&cell_meta_omni_lock, &args), None, &[]);
px.insert_cell_fund(dl, px.create_script(&cell_meta_demo_lock, &args), None, &[]);

// Create cell dep
let tx_builder: ckb_types::core::TransactionBuilder =
tx_builder.cell_dep(px.create_cell_dep(&cell_meta_always_success));
let tx_builder: ckb_types::core::TransactionBuilder =
tx_builder.cell_dep(px.create_cell_dep(&cell_meta_auth));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_secp256k1_data));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_demo_lock));

// Create input
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
Expand Down Expand Up @@ -771,7 +776,7 @@ fn generate_otx_e0(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::Tra
println_hex("seal", seal.as_slice());
let seal = schemas::basic::SealPair::new_builder()
.script_hash(
px.create_script(&cell_meta_omni_lock, &args)
px.create_script(&cell_meta_demo_lock, &args)
.calc_script_hash(),
)
.seal(seal.pack())
Expand Down Expand Up @@ -1066,15 +1071,15 @@ fn test_cobuild_otx_prefix() {
let prikey = "000000000000000000000000000000000000000000000000000000000000000f";
let (prikey, args) = get_ckb_key(prikey);
let cell_meta_always_success = px.insert_cell_data(&mut dl, &BINARY_ALWAYS_SUCCESS);
let cell_meta_omni_lock = px.insert_cell_data(&mut dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_demo_lock = px.insert_cell_data(&mut dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_i = px.insert_cell_fund(
&mut dl,
px.create_script(&cell_meta_omni_lock, &args),
px.create_script(&cell_meta_demo_lock, &args),
None,
&[],
);
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_always_success));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_demo_lock));
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
let tx_builder = tx_builder
.output(px.create_cell_output(px.create_script(&cell_meta_always_success, &[]), None));
Expand Down Expand Up @@ -1203,15 +1208,15 @@ fn test_cobuild_otx_prefix_and_suffix() {
let prikey = "000000000000000000000000000000000000000000000000000000000000000f";
let (prikey, args) = get_ckb_key(prikey);
let cell_meta_always_success = px.insert_cell_data(&mut dl, &BINARY_ALWAYS_SUCCESS);
let cell_meta_omni_lock = px.insert_cell_data(&mut dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_demo_lock = px.insert_cell_data(&mut dl, &BINARY_TRANSACTION_COBUILD_LOCK_DEMO);
let cell_meta_i = px.insert_cell_fund(
&mut dl,
px.create_script(&cell_meta_omni_lock, &args),
px.create_script(&cell_meta_demo_lock, &args),
None,
&[],
);
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_always_success));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock));
let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_demo_lock));
let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i));
let tx_builder = tx_builder
.output(px.create_cell_output(px.create_script(&cell_meta_always_success, &[]), None));
Expand Down

0 comments on commit eaa98a5

Please sign in to comment.