From 884923a20cceefa53bcaa59627fdd22b5c886396 Mon Sep 17 00:00:00 2001 From: liyukun Date: Thu, 4 Jan 2024 14:14:18 +0800 Subject: [PATCH] feat: complete co-build test cases --- lib/types/Cargo.toml | 2 +- lib/utils/src/lib.rs | 4 + tests/src/tests.rs | 317 +++++++++++---------------- tests/src/utils/co_build.rs | 140 ++++++++++++ tests/src/utils/internal.rs | 48 ++++ tests/src/{utils.rs => utils/mod.rs} | 138 ++++-------- 6 files changed, 373 insertions(+), 276 deletions(-) create mode 100644 tests/src/utils/co_build.rs create mode 100644 tests/src/utils/internal.rs rename tests/src/{utils.rs => utils/mod.rs} (73%) diff --git a/lib/types/Cargo.toml b/lib/types/Cargo.toml index 168eaeb..e2a6927 100644 --- a/lib/types/Cargo.toml +++ b/lib/types/Cargo.toml @@ -9,4 +9,4 @@ description = "Spore Protocol types serilization/deserilize utilities (based on # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -molecule = { version = "0.7.3", default-features = false } +molecule = { version = "0.7.5", default-features = false } diff --git a/lib/utils/src/lib.rs b/lib/utils/src/lib.rs index 6ca0fbb..3aee9fe 100644 --- a/lib/utils/src/lib.rs +++ b/lib/utils/src/lib.rs @@ -17,6 +17,10 @@ use spore_errors::error::Error; use spore_types::generated::action; pub use mime::MIME; +pub mod co_build_types { + pub use ckb_transaction_cobuild::schemas::basic::*; + pub use ckb_transaction_cobuild::schemas::top_level::*; +} mod mime; diff --git a/tests/src/tests.rs b/tests/src/tests.rs index 3136cd0..d34feb2 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -8,51 +8,12 @@ use hex::encode; use spore_types::generated::spore_types::{ClusterData, SporeData}; use spore_types::NativeNFTData; +use crate::utils::co_build::*; use crate::utils::*; use crate::Loader; const MAX_CYCLES: u64 = 10_000_000; -#[test] -fn test_simple_spore_mint() { - let serialized = build_serialized_spore("Hello Spore!", "plain/text"); - let capacity = serialized.total_size() as u64; - - let mut context = Context::default(); - - // always success lock - let spore_bin: Bytes = Loader::default().load_binary("spore"); - let spore_out_point = context.deploy_cell(spore_bin); - let spore_script_dep = CellDep::new_builder() - .out_point(spore_out_point.clone()) - .build(); - - let input_cell = build_normal_input(&mut context, capacity); - - println!( - "input cell hash: {:?}, out_index: {}", - input_cell.previous_output().tx_hash().unpack().to_string(), - 0 - ); - let spore_type_id = build_script_args(&input_cell, 0); - let type_ = build_spore_type_script(&mut context, &spore_out_point, spore_type_id.clone()); - - let spore_out_cell = build_output_cell_with_type_id(&mut context, capacity, type_.clone()); - - let tx = TransactionBuilder::default() - .input(input_cell) - .output(spore_out_cell) - .output_data(serialized.as_slice().pack()) - .cell_dep(spore_script_dep) - .build(); - - let tx = context.complete_tx(tx); - - context - .verify_tx(&tx, MAX_CYCLES) - .expect("test simple spore mint"); -} - #[test] fn test_simple_cluster_mint() { let cluster = ClusterData::new_builder() @@ -70,8 +31,12 @@ fn test_simple_cluster_mint() { .out_point(cluster_out_point.clone()) .build(); let input_cell = build_normal_input(&mut context, capacity); - let cluster_type_id = build_script_args(&input_cell, 0); - let type_ = build_spore_type_script(&mut context, &cluster_out_point, cluster_type_id.clone()); + let cluster_type_id = build_type_id(&input_cell, 0); + let type_ = build_spore_type_script( + &mut context, + &cluster_out_point, + cluster_type_id.to_vec().into(), + ); let cluster_out_cell = build_output_cell_with_type_id(&mut context, capacity, type_.clone()); let tx = TransactionBuilder::default() @@ -80,75 +45,10 @@ fn test_simple_cluster_mint() { .output_data(cluster.as_slice().pack()) .cell_dep(cluster_script_dep) .build(); - let tx = context.complete_tx(tx); - - context - .verify_tx(&tx, MAX_CYCLES) - .expect("test simple spore mint"); -} - -#[test] -fn test_simple_spore_mint_with_cluster() { - let mut context = Context::default(); - - // cluster - let cluster = ClusterData::new_builder() - .name("Spore Cluster".as_bytes().into()) - .description("Test Cluster".as_bytes().into()) - .build(); - let cluster_capacity = cluster.total_size() as u64; - 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 input_cell = build_normal_input(&mut context, cluster_capacity); - let cluster_type_id = build_script_args(&input_cell, 0); - let cluster_type = - build_spore_type_script(&mut context, &cluster_out_point, cluster_type_id.clone()); - let cluster_input_cell = - build_cluster_input(&mut context, cluster.clone(), cluster_type.clone()); - let cluster_output_cell = - build_output_cell_with_type_id(&mut context, cluster_capacity, cluster_type.clone()); + let action = build_cluster_create_action(&mut context, cluster_type_id, cluster.as_slice()); + let tx = complete_co_build_message_with_actions(tx, &[(type_, action)]); - // spore - let spore_content: Vec = "Hello Spore!".as_bytes().to_vec(); - let spore_type = String::from("plain/text"); - let spore_data: NativeNFTData = NativeNFTData { - content: spore_content.clone(), - content_type: spore_type.clone(), - cluster_id: Some(cluster_type_id.to_vec().clone()), - }; - let serialized = SporeData::from(spore_data); - let capacity = serialized.total_size() as u64; - let spore_bin: Bytes = Loader::default().load_binary("spore"); - let spore_out_point = context.deploy_cell(spore_bin); - let spore_script_dep = CellDep::new_builder() - .out_point(spore_out_point.clone()) - .build(); - let spore_cluster_dep = CellDep::new_builder() - .out_point(cluster_input_cell.previous_output()) - .build(); - - let input_cell = build_normal_input(&mut context, capacity); - let spore_type_id = build_script_args(&input_cell, 1); - let spore_type = build_spore_type_script(&mut context, &spore_out_point, spore_type_id.clone()); - let spore_out_cell = build_output_cell_with_type_id(&mut context, capacity, spore_type.clone()); - - let tx = TransactionBuilder::default() - .inputs(vec![input_cell, cluster_input_cell]) - .outputs(vec![cluster_output_cell, spore_out_cell]) - .outputs_data(vec![ - cluster.as_slice().pack(), - serialized.as_slice().pack(), - ]) - .cell_deps(vec![ - cluster_script_dep, - spore_cluster_dep, - spore_script_dep, - ]) - .build(); let tx = context.complete_tx(tx); context @@ -173,9 +73,12 @@ fn test_spore_mint_with_lock_proxy() { .out_point(cluster_out_point.clone()) .build(); let input_cell = build_normal_input(&mut context, cluster_capacity); - let cluster_type_id = build_script_args(&input_cell, 0); - let cluster_type = - build_spore_type_script(&mut context, &cluster_out_point, cluster_type_id.clone()); + let cluster_type_id = build_type_id(&input_cell, 0); + let cluster_type = build_spore_type_script( + &mut context, + &cluster_out_point, + cluster_type_id.to_vec().into(), + ); let cluster_dep = build_normal_cell_dep(&mut context, cluster.as_slice(), cluster_type); // spore @@ -195,8 +98,12 @@ fn test_spore_mint_with_lock_proxy() { .build(); let input_cell = build_normal_input(&mut context, capacity); - let spore_type_id = build_script_args(&input_cell, 0); - let spore_type = build_spore_type_script(&mut context, &spore_out_point, spore_type_id.clone()); + let spore_type_id = build_type_id(&input_cell, 0); + let spore_type = build_spore_type_script( + &mut context, + &spore_out_point, + spore_type_id.to_vec().into(), + ); let spore_out_cell = build_output_cell_with_type_id(&mut context, capacity, spore_type.clone()); let tx = TransactionBuilder::default() @@ -229,9 +136,12 @@ fn test_spore_mint_with_lock_proxy_failure() { .out_point(cluster_out_point.clone()) .build(); let input_cell = build_normal_input(&mut context, cluster_capacity); - let cluster_type_id = build_script_args(&input_cell, 0); - let cluster_type = - build_spore_type_script(&mut context, &cluster_out_point, cluster_type_id.clone()); + let cluster_type_id = build_type_id(&input_cell, 0); + let cluster_type = build_spore_type_script( + &mut context, + &cluster_out_point, + cluster_type_id.to_vec().into(), + ); let cluster_cell = build_cluster_input(&mut context, cluster.clone(), cluster_type.clone()); // spore let spore_content: Vec = "Hello Spore!".as_bytes().to_vec(); @@ -253,8 +163,12 @@ fn test_spore_mint_with_lock_proxy_failure() { .build(); let input_cell = build_normal_input(&mut context, capacity); - let spore_type_id = build_script_args(&input_cell, 0); - let spore_type = build_spore_type_script(&mut context, &spore_out_point, spore_type_id.clone()); + let spore_type_id = build_type_id(&input_cell, 0); + let spore_type = build_spore_type_script( + &mut context, + &spore_out_point, + spore_type_id.to_vec().into(), + ); let spore_out_cell = build_output_cell_with_type_id(&mut context, capacity, spore_type.clone()); let tx = TransactionBuilder::default() @@ -294,9 +208,12 @@ fn test_spore_mint_with_cluster_proxy() { .out_point(cluster_out_point.clone()) .build(); let input_cell = build_normal_input(&mut context, cluster_capacity); - let cluster_type_id = build_script_args(&input_cell, 0); - let cluster_type = - build_spore_type_script(&mut context, &cluster_out_point, cluster_type_id.clone()); + let cluster_type_id = build_type_id(&input_cell, 0); + let cluster_type = build_spore_type_script( + &mut context, + &cluster_out_point, + cluster_type_id.to_vec().into(), + ); let cluster_dep = build_normal_cell_dep(&mut context, cluster.as_slice(), cluster_type); // proxy @@ -308,14 +225,18 @@ fn test_spore_mint_with_cluster_proxy() { .build(); let input_cell = build_normal_input(&mut context, capacity); - let proxy_type_id = build_script_args(&input_cell, 0); - let proxy_type = build_spore_type_script(&mut context, &proxy_out_point, proxy_type_id.clone()); + let proxy_type_id = build_type_id(&input_cell, 0); + let proxy_type = build_spore_type_script( + &mut context, + &proxy_out_point, + proxy_type_id.to_vec().into(), + ); let proxy_out_cell = build_output_cell_with_type_id(&mut context, capacity, proxy_type.clone()); let tx = TransactionBuilder::default() .inputs(vec![input_cell]) .outputs(vec![proxy_out_cell]) - .outputs_data(vec![cluster_type_id.pack()]) + .outputs_data(vec![cluster_type_id.to_vec().pack()]) .cell_deps(vec![cluster_script_dep, proxy_script_dep, cluster_dep]) .build(); let tx = context.complete_tx(tx); @@ -342,9 +263,12 @@ fn test_cluster_agent() { .out_point(cluster_out_point.clone()) .build(); let input_cell = build_normal_input(&mut context, cluster_capacity); - let cluster_type_id = build_script_args(&input_cell, 0); - let cluster_type = - build_spore_type_script(&mut context, &cluster_out_point, cluster_type_id.clone()); + let cluster_type_id = build_type_id(&input_cell, 0); + let cluster_type = build_spore_type_script( + &mut context, + &cluster_out_point, + cluster_type_id.to_vec().into(), + ); let cluster_dep = build_normal_cell_dep(&mut context, cluster.as_slice(), cluster_type); // proxy @@ -356,7 +280,7 @@ fn test_cluster_agent() { .build(); let input_cell = build_normal_input(&mut context, capacity); - let proxy_type_id = build_script_args(&input_cell, 0); + let proxy_type_id = build_type_id(&input_cell, 0); let mut proxy_type_arg = proxy_type_id.to_vec(); proxy_type_arg.push(1); println!("Proxy_type_arg len: {}", proxy_type_arg.len()); @@ -377,7 +301,11 @@ fn test_cluster_agent() { let input_cell = build_normal_input(&mut context, agent_capacity); - let agent_type = build_spore_type_script(&mut context, &agent_out_point, cluster_type_id); + let agent_type = build_spore_type_script( + &mut context, + &agent_out_point, + cluster_type_id.to_vec().into(), + ); let agent_out_cell = build_output_cell_with_type_id(&mut context, capacity, agent_type.clone()); let tx = TransactionBuilder::default() @@ -411,16 +339,15 @@ fn test_simple_spore_transfer() { // always success lock let (spore_out_point, spore_script_dep) = build_spore_materials(&mut context); - let spore_type_id = build_script_args(&build_normal_input(&mut context, capacity), 0); - let spore_type = build_spore_type_script(&mut context, &spore_out_point, spore_type_id.clone()); - let spore_input = build_spore_input( + let spore_type_id = build_type_id(&build_normal_input(&mut context, capacity), 0); + let spore_type = build_spore_type_script( &mut context, &spore_out_point, - serialized.clone(), - spore_type_id.clone(), + spore_type_id.to_vec().into(), ); + let spore_input = build_spore_input(&mut context, spore_type.clone(), serialized.clone()); - let spore_output = build_output_cell_with_type_id(&mut context, capacity, spore_type); + let spore_output = build_output_cell_with_type_id(&mut context, capacity, spore_type.clone()); let tx = build_simple_tx( vec![spore_input], vec![spore_output], @@ -428,6 +355,9 @@ fn test_simple_spore_transfer() { vec![serialized.as_slice().pack()], ); + let action = build_transfer_action(&mut context, spore_type_id); + let tx = complete_co_build_message_with_actions(tx, &[(spore_type, action)]); + let tx = context.complete_tx(tx); context @@ -436,7 +366,7 @@ fn test_simple_spore_transfer() { } #[test] -fn test_simple_spore_mint2() { +fn test_simple_spore_mint() { let (mut context, tx) = simple_build_context( "THIS IS A TEST NFT".as_bytes().to_vec(), "plain/text", @@ -446,11 +376,11 @@ fn test_simple_spore_mint2() { let tx = context.complete_tx(tx); context .verify_tx(&tx, MAX_CYCLES) - .expect("test simple spore mint 2"); + .expect("test simple spore mint"); } #[test] -fn test_simple_spore_mint3() { +fn test_simple_spore_mint_with_normal_cells() { // mint with normal output tx test let spore_content: Vec = "Hello Spore!".as_bytes().to_vec(); let spore_type = String::from("plain/text"); @@ -476,8 +406,12 @@ fn test_simple_spore_mint3() { let input_cell2 = build_normal_input(&mut context, capacity); let input_cell3 = build_normal_input(&mut context, capacity); - let spore_type_id = build_script_args(&input_cell1, 0); - let spore_type = build_spore_type_script(&mut context, &spore_out_point, spore_type_id.clone()); + let spore_type_id = build_type_id(&input_cell1, 0); + let spore_type = build_spore_type_script( + &mut context, + &spore_out_point, + spore_type_id.to_vec().into(), + ); let spore_out_cell = build_output_cell_with_type_id(&mut context, capacity, spore_type.clone()); let output_cell1 = build_normal_output(&mut context, capacity); @@ -489,6 +423,9 @@ fn test_simple_spore_mint3() { .cell_dep(spore_script_dep) .build(); + let action = build_mint_action(&mut context, spore_type_id, serialized.as_slice()); + let tx = complete_co_build_message_with_actions(tx, &[(spore_type, action)]); + let tx = context.complete_tx(tx); context @@ -497,7 +434,7 @@ fn test_simple_spore_mint3() { } #[test] -fn test_simple_spore_mint4() { +fn test_multi_spores_mint_with_normal_cells() { // multiple mint tx test let spore_content: Vec = "Hello Spore!".as_bytes().to_vec(); let spore_type = String::from("plain/text"); @@ -523,15 +460,22 @@ fn test_simple_spore_mint4() { let input_cell2 = build_normal_input(&mut context, capacity); let input_cell3 = build_normal_input(&mut context, capacity); - let spore_type_id = build_script_args(&input_cell1, 0); - let spore_type = build_spore_type_script(&mut context, &spore_out_point, spore_type_id.clone()); + let spore_type_id = build_type_id(&input_cell1, 0); + let spore_type = build_spore_type_script( + &mut context, + &spore_out_point, + spore_type_id.to_vec().into(), + ); let spore_out_cell = build_output_cell_with_type_id(&mut context, capacity, spore_type.clone()); let output_cell1 = build_normal_output(&mut context, capacity); - let spore_type_id2 = build_script_args(&input_cell1, 2); - let spore_type2 = - build_spore_type_script(&mut context, &spore_out_point, spore_type_id2.clone()); + let spore_type_id2 = build_type_id(&input_cell1, 2); + let spore_type2 = build_spore_type_script( + &mut context, + &spore_out_point, + spore_type_id2.to_vec().into(), + ); let spore_out_cell2 = build_output_cell_with_type_id(&mut context, capacity, spore_type2.clone()); @@ -547,6 +491,13 @@ fn test_simple_spore_mint4() { .cell_dep(spore_script_dep) .build(); + let action1 = build_mint_action(&mut context, spore_type_id, serialized.as_slice()); + let action2 = build_mint_action(&mut context, spore_type_id2, serialized.as_slice()); + let tx = complete_co_build_message_with_actions( + tx, + &[(spore_type, action1), (spore_type2, action2)], + ); + let tx = context.complete_tx(tx); context @@ -568,8 +519,9 @@ fn test_spore_multipart_mint() { .expect("test multipart mint"); } +#[should_panic] #[test] -fn test_spore_multipart_mint_failure01() { +fn test_spore_multipart_mint_failed_with_mixed() { let (mut context, tx) = simple_build_context("THIS IS A TEST MULTIPART NFT\n\n--SporeDefaultBoundary\nThis is an extra message I want to include".as_bytes().to_vec(), "multipart/mixed;", @@ -579,11 +531,12 @@ fn test_spore_multipart_mint_failure01() { let tx = context.complete_tx(tx); context .verify_tx(&tx, MAX_CYCLES) - .expect_err("test multipart failure 01"); + .expect("test multipart failure mixed"); } +#[should_panic] #[test] -fn test_spore_multipart_mint_failure02() { +fn test_spore_multipart_mint_failed_with_boundary() { let (mut context, tx) = simple_build_context( "THIS IS A TEST MULTIPART NFT\n\nThis is an extra message I want to include" .as_bytes() @@ -595,11 +548,11 @@ fn test_spore_multipart_mint_failure02() { let tx = context.complete_tx(tx); context .verify_tx(&tx, MAX_CYCLES) - .expect_err("test multipart failure 02"); + .expect("test multipart failure boundary"); } #[test] -fn test_simple_with_cluster() { +fn test_simple_spore_mint_with_cluster() { let (mut context, tx) = build_simple_create_context_with_cluster( "THIS IS A SIMPLE SPORE".to_string(), "plain/text".to_string(), @@ -654,20 +607,6 @@ fn test_read_base64() { context.verify_tx(&view, MAX_CYCLES).expect("Error tx"); } -#[test] -fn test_decode_hex() { - let hex_str = "42020000100000001e000000420200000a000000696d6167652f6a706567200200002f396a2f34414151536b5a4a5267414241514541534142494141442f3277424441416f484277674842676f494341674c43676f4c446867514467304e44683056466845594978386c4a4349664969456d4b7a63764a696b304b5345694d4545784e446b37506a342b4a53354553554d3853446339506a762f3277424441516f4c4377344e44687751454277374b43496f4f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a73374f7a762f774141524341414b41416f444153494141684542417845422f3851414651414241514141414141414141414141414141414141414241662f7841416645414144414149434177454241414141414141414141414241674d4542514152426945785152542f784141554151454141414141414141414141414141414141414141412f38514146424542414141414141414141414141414141414141414141502f61414177444151414345514d52414438415a486337584331766d327774733878386473375934556d6537482b4e305174416f53657071537a7036396c6a454166655550784f31636e773753337656363172723450536a73575a324d314a4a4a2b6b6e393470394e71715975526976724d4e73664b71625a456a42536c6e4a424c4d4f756d627341396e33364845786a4c47684f454a4a4b556c43546d69685652514f6741423841483577502f5a"; - let data = decode_hex(hex_str).unwrap(); - let nft = SporeData::from_slice(data.as_slice()).expect("error parse"); - - println!( - "content-type: {:?}, content: {:?}, cluster: {:?}", - nft.content_type(), - nft.content(), - nft.cluster_id() - ); -} - #[test] fn test_error_data() { let data = vec![0, 0, 0, 0, 0]; @@ -679,20 +618,20 @@ fn test_error_data() { } #[test] -fn test_destroy() { +fn test_simple_spore_destroy() { let serialized = build_serialized_spore("Hello Spore!", "plain/text"); let capacity = serialized.total_size() as u64; let mut context = Context::default(); // always success lock let (spore_out_point, spore_script_dep) = build_spore_materials(&mut context); - let spore_type_id = build_script_args(&build_normal_input(&mut context, capacity), 0); - let spore_input = build_spore_input( + let spore_type_id = build_type_id(&build_normal_input(&mut context, capacity), 0); + let type_ = build_spore_type_script( &mut context, &spore_out_point, - serialized.clone(), - spore_type_id.clone(), + spore_type_id.to_vec().into(), ); + let spore_input = build_spore_input(&mut context, type_.clone(), serialized.clone()); let output = build_normal_output(&mut context, capacity); let tx = build_simple_tx( @@ -702,6 +641,9 @@ fn test_destroy() { vec![serialized.as_slice().pack()], ); + let action = build_burn_action(&mut context, spore_type_id); + let tx = complete_co_build_message_with_actions(tx, &[(type_, action)]); + let tx = context.complete_tx(tx); context @@ -709,21 +651,22 @@ fn test_destroy() { .expect("try destroy immortal"); } +#[should_panic] #[test] -fn test_destroy_immortal() { +fn test_simple_spore_destroy_failed_with_immortal() { let serialized = build_serialized_spore("Hello Spore!", "plain/text;immortal=true"); let capacity = serialized.total_size() as u64; let mut context = Context::default(); // always success lock let (spore_out_point, spore_script_dep) = build_spore_materials(&mut context); - let spore_type_id = build_script_args(&build_normal_input(&mut context, capacity), 0); - let spore_input = build_spore_input( + let spore_type_id = build_type_id(&build_normal_input(&mut context, capacity), 0); + let spore_type = build_spore_type_script( &mut context, &spore_out_point, - serialized.clone(), - spore_type_id.clone(), + spore_type_id.to_vec().into(), ); + let spore_input = build_spore_input(&mut context, spore_type.clone(), serialized.clone()); let output = build_normal_output(&mut context, capacity); let tx = build_simple_tx( @@ -733,11 +676,14 @@ fn test_destroy_immortal() { vec![packed::Bytes::default()], ); + let action = build_burn_action(&mut context, spore_type_id); + let tx = complete_co_build_message_with_actions(tx, &[(spore_type, action)]); + let tx = context.complete_tx(tx); context .verify_tx(&tx, MAX_CYCLES) - .expect_err("try destroy immortal"); + .expect("try destroy immortal"); } #[test] @@ -757,8 +703,12 @@ fn test_destroy_cluster() { let cluster_script_dep = CellDep::new_builder() .out_point(cluster_out_point.clone()) .build(); - let cluster_type_id = build_script_args(&build_normal_input(&mut context, capacity), 0); - let type_ = build_spore_type_script(&mut context, &cluster_out_point, cluster_type_id.clone()); + let cluster_type_id = build_type_id(&build_normal_input(&mut context, capacity), 0); + let type_ = build_spore_type_script( + &mut context, + &cluster_out_point, + cluster_type_id.to_vec().into(), + ); let cluster_input = build_cluster_input(&mut context, cluster, type_.clone()); @@ -795,19 +745,16 @@ fn test_error_type() { } #[test] -fn test_extension_1() { +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 code_hash = CellOutput::calc_data_hash(&lua_lib_bin.clone()); let lua_lib_dep = CellDep::new_builder() .out_point(lua_lib_out_point.clone()) .build(); - println!("lua lib hash: {}", encode(code_hash.as_slice())); - let spore_extension_bin: Bytes = Loader::default().load_binary("spore_extension_lua"); println!( "extension hash: {}", @@ -829,11 +776,11 @@ fn test_extension_1() { input_cell.previous_output().tx_hash().unpack().to_string(), 0 ); - let spore_extension_type_id = build_script_args(&input_cell, 0); + let spore_extension_type_id = build_type_id(&input_cell, 0); let type_ = build_spore_type_script( &mut context, &spore_extension_out_point, - spore_extension_type_id.clone(), + spore_extension_type_id.to_vec().into(), ); let spore_out_cell = build_output_cell_with_type_id(&mut context, capacity, type_.clone()); diff --git a/tests/src/utils/co_build.rs b/tests/src/utils/co_build.rs new file mode 100644 index 0000000..535f74c --- /dev/null +++ b/tests/src/utils/co_build.rs @@ -0,0 +1,140 @@ +use ckb_testtool::ckb_hash::blake2b_256; +use ckb_testtool::ckb_types::core::TransactionView; +use ckb_testtool::ckb_types::packed; +use ckb_testtool::ckb_types::prelude::*; + +use ckb_testtool::context::Context; +use spore_types::generated::action::{ + Address, AddressUnion, Burn, Byte32, Bytes, ClusterCreate, ClusterTransfer, Mint, Script, + SporeAction, SporeActionUnion, Transfer, +}; +use spore_utils::co_build_types::{ + Action, ActionVec, Message, SighashAll, WitnessLayout, WitnessLayoutUnion, +}; + +use super::internal; + +fn h256_to_byte32(hash: [u8; 32]) -> Byte32 { + let hash = hash + .into_iter() + .map(packed::Byte::new) + .collect::>() + .try_into() + .unwrap(); + Byte32::new_builder().set(hash).build() +} + +fn script_to_address(script: packed::Script) -> Address { + let code_hash = script.code_hash().unpack(); + let hash_type = script.hash_type(); + let args = script.args().raw_data(); + + let code_hash = h256_to_byte32(code_hash.into()); + let args = Bytes::new_builder() + .set(args.into_iter().map(packed::Byte::new).collect()) + .build(); + + let script = Script::new_builder() + .code_hash(code_hash) + .hash_type(hash_type) + .args(args) + .build(); + + Address::new_builder() + .set(AddressUnion::Script(script)) + .build() +} + +pub fn complete_co_build_message_with_actions( + tx: TransactionView, + actions: &[(Option, SporeActionUnion)], +) -> TransactionView { + let action_value_vec = actions + .to_owned() + .into_iter() + .map(|(script_hash, action)| { + let script_hash = if let Some(script_hash) = script_hash { + script_hash.calc_script_hash() + } else { + packed::Byte32::default() + }; + let spore_action = SporeAction::new_builder().set(action).build(); + Action::new_builder() + .script_hash(script_hash) + .data(spore_action.as_slice().pack()) + .build() + }) + .collect(); + let action_vec = ActionVec::new_builder().set(action_value_vec).build(); + let message = Message::new_builder().actions(action_vec).build(); + let sighash_all = SighashAll::new_builder().message(message).build(); + let witness_layout = WitnessLayout::new_builder() + .set(WitnessLayoutUnion::SighashAll(sighash_all)) + .build(); + + tx.as_advanced_builder() + .witness(witness_layout.as_slice().pack()) + .build() +} + +pub fn build_mint_action( + context: &mut Context, + nft_id: [u8; 32], + content: &[u8], +) -> SporeActionUnion { + let to = internal::build_always_success_script(context); + let mint = Mint::new_builder() + .nft_id(h256_to_byte32(nft_id)) + .data_hash(h256_to_byte32(blake2b_256(content))) + .to(script_to_address(to)) + .build(); + SporeActionUnion::Mint(mint) +} + +pub fn build_transfer_action(context: &mut Context, nft_id: [u8; 32]) -> SporeActionUnion { + let script = internal::build_always_success_script(context); + let address = script_to_address(script); + let transfer = Transfer::new_builder() + .nft_id(h256_to_byte32(nft_id)) + .from(address.clone()) + .to(address) + .build(); + SporeActionUnion::Transfer(transfer) +} + +pub fn build_burn_action(context: &mut Context, nft_id: [u8; 32]) -> SporeActionUnion { + let from = internal::build_always_success_script(context); + let burn = Burn::new_builder() + .nft_id(h256_to_byte32(nft_id)) + .from(script_to_address(from)) + .build(); + SporeActionUnion::Burn(burn) +} + +pub fn build_cluster_create_action( + context: &mut Context, + cluster_id: [u8; 32], + content: &[u8], +) -> SporeActionUnion { + let to = internal::build_always_success_script(context); + let cluster_create = ClusterCreate::new_builder() + .cluster_id(h256_to_byte32(cluster_id)) + .data_hash(h256_to_byte32(blake2b_256(content))) + .to(script_to_address(to)) + .build(); + SporeActionUnion::ClusterCreate(cluster_create) +} + +pub fn build_cluster_transfer_action( + context: &mut Context, + cluster_id: [u8; 32], +) -> SporeActionUnion { + let script = internal::build_always_success_script(context); + let address = script_to_address(script); + let cluster_transfer = ClusterTransfer::new_builder() + .cluster_id(h256_to_byte32(cluster_id)) + .from(address.clone()) + .to(address) + .build(); + SporeActionUnion::ClusterTransfer(cluster_transfer) +} diff --git a/tests/src/utils/internal.rs b/tests/src/utils/internal.rs new file mode 100644 index 0000000..d288e71 --- /dev/null +++ b/tests/src/utils/internal.rs @@ -0,0 +1,48 @@ +use ckb_testtool::builtin::ALWAYS_SUCCESS; +use ckb_testtool::ckb_types::{bytes::Bytes, packed::*, prelude::*}; +use ckb_testtool::context::Context; + +pub fn build_always_success_script(context: &mut Context) -> Script { + let always_success_out_point = context.deploy_cell(ALWAYS_SUCCESS.clone()); + + // build lock script + context + .build_script(&always_success_out_point, Default::default()) + .expect("always success script") +} + +pub fn build_output( + context: &mut Context, + capacity: u64, + type_script: Option