Skip to content

Commit

Permalink
chore: compile success
Browse files Browse the repository at this point in the history
  • Loading branch information
liyukun committed Jan 6, 2024
1 parent e0fb7cc commit 575db11
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 64 deletions.
2 changes: 1 addition & 1 deletion contracts/cluster/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn process_creation(index: usize) -> Result<(), Error> {
if cluster_data.name().is_empty() {
return Err(Error::EmptyName);
}
let Some(cluster_id) = verify_type_id(index, Output) else {
let Some(cluster_id) = verify_type_id(index) else {
return Err(Error::InvalidClusterID);
};

Expand Down
2 changes: 1 addition & 1 deletion contracts/cluster_proxy/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn process_creation(index: usize) -> Result<(), Error> {
.ok_or(Error::ClusterCellNotInDep)?;

// verify Proxy ID
let Some(proxy_id) = verify_type_id(index, Output) else {
let Some(proxy_id) = verify_type_id(index) else {
return Err(Error::InvalidProxyID);
};

Expand Down
2 changes: 1 addition & 1 deletion contracts/spore/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn process_creation(index: usize) -> Result<(), Error> {
}

// verify NFT ID
let Some(spore_id) = verify_type_id(index, Output) else {
let Some(spore_id) = verify_type_id(index) else {
return Err(Error::InvalidNFTID);
};

Expand Down
2 changes: 1 addition & 1 deletion contracts/spore_extension_lua/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl CKBLuaLib {
}

fn process_creation(index: usize) -> Result<(), WrappedError> {
if verify_type_id(index, Output).is_none() {
if verify_type_id(index).is_none() {
return Err(Error::InvalidExtensionID.into());
}
let args = load_cell_type(index, Output)?
Expand Down
18 changes: 11 additions & 7 deletions lib/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub fn load_type_args(index: usize, source: Source) -> Bytes {
}

// only be avaliable in mint/create like ckb transaction
pub fn verify_type_id(output_index: usize, source: Source) -> Option<[u8; 32]> {
pub fn verify_type_id(output_index: usize) -> Option<[u8; 32]> {
let first_input = match load_input(0, Source::Input) {
Ok(cell_input) => cell_input,
Err(_) => return None,
};

let expected_id = calc_type_id(first_input.as_slice(), output_index);
let type_id_args = load_type_args(output_index, source);
let type_id_args = load_type_args(output_index, Source::Output);

debug!("wanted: {:?}, got: {:?}", expected_id, type_id_args);
if type_id_args.len() < 32 {
Expand All @@ -60,7 +60,7 @@ pub fn calc_type_id(tx_first_input: &[u8], output_index: usize) -> [u8; 32] {
let mut blake2b = Blake2bBuilder::new(32)
.personal(b"ckb-default-hash")
.build();
blake2b.update(prevout_hash);
blake2b.update(tx_first_input);
blake2b.update(&(output_index as u64).to_le_bytes());
let mut verify_id = [0; 32];
blake2b.finalize(&mut verify_id);
Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn check_spore_address(
group_source: Source,
spore_address: action::Address,
) -> Result<(), Error> {
let address = load_cell_lock(0, gourp_source)?;
let address = load_cell_lock(0, group_source)?;
let action::AddressUnion::Script(expected_script) = spore_address.to_enum();
if address.as_slice() != expected_script.as_slice() {
return Err(Error::SporeActionFieldMismatch);
Expand All @@ -165,9 +165,13 @@ pub fn extract_spore_action() -> Result<action::SporeAction, Error> {
.ok_or(Error::InvliadCoBuildWitnessLayout)?;
let script_hash = load_script_hash()?;

let mut iter = message.actions().into_iter().filter(|value| value.script_hash().as_slice() == script_hash.as_slice());
let mut iter = message
.actions()
.into_iter()
.filter(|value| value.script_hash().as_slice() == script_hash.as_slice());
match (iter.next(), iter.next()) {
(Some(action), None) => action::SporeAction::from_slice(&action.data().raw_data()).map_err(|_| Error::InvliadCoBuildMessage),
_ => Err(Error::InvliadCoBuildMessage)
(Some(action), None) => action::SporeAction::from_slice(&action.data().raw_data())
.map_err(|_| Error::InvliadCoBuildMessage),
_ => Err(Error::InvliadCoBuildMessage),
}
}
33 changes: 5 additions & 28 deletions tests/src/tests/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use ckb_testtool::ckb_types::H256;
use ckb_testtool::ckb_types::{
bytes::Bytes, core::TransactionBuilder, packed, packed::*, prelude::*,
};
use ckb_testtool::ckb_types::{core::TransactionBuilder, packed, prelude::*};
use ckb_testtool::context::Context;

use crate::utils::co_build::*;
use crate::utils::*;
use crate::Loader;
use crate::MAX_CYCLES;

#[test]
fn test_simple_cluster_mint() {
let mut context = Context::default();

let cluster = build_serialized_cluster_data("Spore Cluster", "Test Cluster");

// always success lock
let cluster_bin: Bytes = Loader::default().load_binary("cluster");
let cluster_out_point = context.deploy_cell(cluster_bin);
let cluster_script_dep = CellDep::new_builder()
.out_point(cluster_out_point.clone())
.build();
let (cluster_out_point, cluster_script_dep) = build_spore_materials(&mut context, "cluster");
let input_cell = build_normal_input(&mut context);
let cluster_type_id = build_type_id(&input_cell, 0);
let type_ = build_spore_type_script(
Expand Down Expand Up @@ -131,23 +122,14 @@ fn test_cluster_proxy_mint() {

// cluster
let cluster = build_serialized_cluster_data("Spore Cluster", "Test Cluster");
let cluster_bin: Bytes = Loader::default().load_binary("cluster");
let cluster_out_point = context.deploy_cell(cluster_bin);
let cluster_script_dep = CellDep::new_builder()
.out_point(cluster_out_point.clone())
.build();
let (cluster_out_point, cluster_script_dep) = build_spore_materials(&mut context, "cluster");
let cluster_id = build_type_id(&input_cell, 0);
let cluster_type =
build_spore_type_script(&mut context, &cluster_out_point, cluster_id.to_vec().into());
let cluster_dep = build_normal_cell_dep(&mut context, cluster.as_slice(), cluster_type);

// proxy
let proxy_bin: Bytes = Loader::default().load_binary("cluster_proxy");
let proxy_out_point = context.deploy_cell(proxy_bin);
let proxy_script_dep = CellDep::new_builder()
.out_point(proxy_out_point.clone())
.build();

let (proxy_out_point, proxy_script_dep) = build_spore_materials(&mut context, "cluster_proxy");
let proxy_id = build_type_id(&input_cell, 0);
let proxy_type =
build_spore_type_script(&mut context, &proxy_out_point, proxy_id.to_vec().into());
Expand Down Expand Up @@ -177,12 +159,7 @@ fn test_simple_cluster_destroy_failed() {

let cluster = build_serialized_cluster_data("Spore Cluster", "Test Cluster");

// always success lock
let cluster_bin: Bytes = Loader::default().load_binary("cluster");
let cluster_out_point = context.deploy_cell(cluster_bin);
let cluster_script_dep = CellDep::new_builder()
.out_point(cluster_out_point.clone())
.build();
let (cluster_out_point, cluster_script_dep) = build_spore_materials(&mut context, "cluster");
let cluster_id = build_type_id(&build_normal_input(&mut context), 0);
let type_ =
build_spore_type_script(&mut context, &cluster_out_point, cluster_id.to_vec().into());
Expand Down
18 changes: 4 additions & 14 deletions tests/src/tests/mutant.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
use ckb_testtool::ckb_types::{bytes::Bytes, core::TransactionBuilder, packed::*, prelude::*};
use ckb_testtool::ckb_types::{core::TransactionBuilder, prelude::*};
use ckb_testtool::context::Context;

use crate::utils::*;
use crate::Loader;
use crate::MAX_CYCLES;

#[test]
fn test_simple_mutant_mint() {
let mut context = Context::default();

// always success lock
let lua_lib_bin: Bytes = Loader::default().load_binary("libckblua.so");
let lua_lib_out_point = context.deploy_cell(lua_lib_bin.clone());
let lua_lib_dep = CellDep::new_builder()
.out_point(lua_lib_out_point.clone())
.build();

let spore_extension_bin: Bytes = Loader::default().load_binary("spore_extension_lua");
let spore_extension_out_point = context.deploy_cell(spore_extension_bin);
let spore_extension_script_dep = CellDep::new_builder()
.out_point(spore_extension_out_point.clone())
.build();
let (_, lua_lib_dep) = build_spore_materials(&mut context, "libckblua.so");
let (spore_extension_out_point, spore_extension_script_dep) =
build_spore_materials(&mut context, "spore_extension_lua");

let lua_code = String::from("print('hello world')");
let input_cell = build_normal_input(&mut context);
Expand Down
4 changes: 0 additions & 4 deletions tests/src/tests/spore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn test_multi_spores_mint() {
let serialized =
build_serialized_spore_data("Hello Spore!".as_bytes().to_vec(), "plain/text", None);

// always success lock
let (spore_out_point, spore_script_dep) = build_spore_materials(&mut context, "spore");

let input_cell_1 = build_normal_input(&mut context);
Expand Down Expand Up @@ -127,7 +126,6 @@ fn test_simple_spore_transfer() {
build_serialized_spore_data("Hello Spore!".as_bytes().to_vec(), "plain/text", None);
let mut context = Context::default();

// always success lock
let (spore_out_point, spore_script_dep) = build_spore_materials(&mut context, "spore");
let spore_type_id = build_type_id(&build_normal_input(&mut context), 0);
let spore_type = build_spore_type_script(
Expand Down Expand Up @@ -191,7 +189,6 @@ fn test_simple_spore_destroy() {
build_serialized_spore_data("Hello Spore!".as_bytes().to_vec(), "plain/text", None);
let mut context = Context::default();

// always success lock
let (spore_out_point, spore_script_dep) = build_spore_materials(&mut context, "spore");
let input = build_normal_input(&mut context);
let spore_type_id = build_type_id(&input, 0);
Expand Down Expand Up @@ -346,7 +343,6 @@ fn test_simple_spore_destroy_failed_with_immortal() {
);
let mut context = Context::default();

// always success lock
let (spore_out_point, spore_script_dep) = build_spore_materials(&mut context, "spore");
let input = build_normal_input(&mut context);
let spore_type_id = build_type_id(&input, 0);
Expand Down
14 changes: 8 additions & 6 deletions tests/src/utils/co_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ pub fn build_proxy_transfer_action(
proxy_id: [u8; 32],
) -> SporeActionUnion {
let script = internal::build_always_success_script(context);
let address = script_to_address(script);
let from = script_to_address(script);
let to = from.clone();
let proxy_transfer = ProxyTransfer::new_builder()
.cluster_id(h256_to_byte32(cluster_id))
.proxy_id(h256_to_byte32(proxy_id))
.from(address.clone())
.to(address)
.from(from)
.to(to)
.build();
SporeActionUnion::ProxyTransfer(proxy_transfer)
}
Expand All @@ -184,11 +185,12 @@ pub fn build_agent_transfer_action(
cluster_id: [u8; 32],
) -> SporeActionUnion {
let script = internal::build_always_success_script(context);
let address = script_to_address(script);
let from = script_to_address(script);
let to = from.clone();
let agent_transfer = AgentTransfer::new_builder()
.cluster_id(h256_to_byte32(cluster_id))
.from(address.clone())
.to(address)
.from(from)
.to(to)
.build();
SporeActionUnion::AgentTransfer(agent_transfer)
}
1 change: 0 additions & 1 deletion tests/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ pub fn build_single_spore_mint_tx(
let output_data =
build_serialized_spore_data(output_data, content_type, cluster_id.map(|v| v.to_vec()));

// always success lock
let (spore_out_point, spore_script_dep) = build_spore_materials(context, "spore");
let (input, type_id) = match input_data {
None => {
Expand Down

0 comments on commit 575db11

Please sign in to comment.