{"id":24511063,"url":"https://github.com/libraries/spawn-cycles-argue","last_synced_at":"2025-03-15T09:43:15.716Z","repository":{"id":168133809,"uuid":"643716199","full_name":"libraries/spawn-cycles-argue","owner":"libraries","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-10T09:20:35.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-22T00:38:25.887Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/libraries.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-22T02:19:14.000Z","updated_at":"2023-05-22T05:36:49.000Z","dependencies_parsed_at":"2024-04-10T10:32:07.364Z","dependency_job_id":"2f813336-dc85-4e09-bb08-7dd4b552d035","html_url":"https://github.com/libraries/spawn-cycles-argue","commit_stats":null,"previous_names":["libraries/spawn-cycles-argue"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libraries%2Fspawn-cycles-argue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libraries%2Fspawn-cycles-argue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libraries%2Fspawn-cycles-argue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libraries%2Fspawn-cycles-argue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/libraries","download_url":"https://codeload.github.com/libraries/spawn-cycles-argue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243713394,"owners_count":20335566,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-22T00:36:13.339Z","updated_at":"2025-03-15T09:43:15.688Z","avatar_url":"https://github.com/libraries.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spawn Cyclse Argure\n\n本项目提供了三个关于 spawn syscall 的测试用例, 用于评估 spawn syscall 的额外 cycles 消耗.\n\n```sh\n$ make build\n```\n\n```sh\n$ tree bin\nbin\n├── prime           # 该脚本循环执行 1024 次 secp256k1 ecdsa verify\n├── spawn_callee    # 该脚本从命令行接受参数并执行一次 secp256k1 ecdsa verify\n├── spawn_caller_2m # 该脚本循环调用 1024 次 spawn_callee, 子虚拟机设置 2m 内存\n└── spawn_caller_4m # 该脚本循环调用 1024 次 spawn_callee, 子虚拟机设置 4m 内存\n```\n\n之后将如下三个测试用例添加到[此](https://github.com/nervosnetwork/ckb/blob/ckb2023/script/src/verify/tests/ckb_latest/features_since_v2023.rs):\n\n```rs\n#[test]\nfn check_spawn_secp256k1_2m() {\n    let script_version = SCRIPT_VERSION;\n\n    let (spawn_caller_cell, spawn_caller_data_hash) =\n        load_cell_from_path(\"/tmp/spawn-cycles-argue/bin/spawn_caller_2m\");\n    let (spawn_callee_cell, _spawn_callee_data_hash) =\n        load_cell_from_path(\"/tmp/spawn-cycles-argue/bin/spawn_callee\");\n\n    let spawn_caller_script = Script::new_builder()\n        .hash_type(script_version.data_hash_type().into())\n        .code_hash(spawn_caller_data_hash)\n        .build();\n    let output = CellOutputBuilder::default()\n        .capacity(capacity_bytes!(100).pack())\n        .lock(spawn_caller_script)\n        .build();\n    let input = CellInput::new(OutPoint::null(), 0);\n\n    let transaction = TransactionBuilder::default().input(input).build();\n    let dummy_cell = create_dummy_cell(output);\n\n    let rtx = ResolvedTransaction {\n        transaction,\n        resolved_cell_deps: vec![spawn_caller_cell, spawn_callee_cell],\n        resolved_inputs: vec![dummy_cell],\n        resolved_dep_groups: vec![],\n    };\n    let verifier = TransactionScriptsVerifierWithEnv::new();\n    let tic = std::time::SystemTime::now();\n    let result = verifier.verify_without_limit(script_version, \u0026rtx);\n    let toc = std::time::SystemTime::elapsed(\u0026tic).unwrap();\n    println!(\"{:?}\", toc.as_millis());\n    assert_eq!(result.is_ok(), script_version \u003e= ScriptVersion::V2);\n}\n\n#[test]\nfn check_spawn_secp256k1_4m() {\n    let script_version = SCRIPT_VERSION;\n\n    let (spawn_caller_cell, spawn_caller_data_hash) =\n        load_cell_from_path(\"/tmp/spawn-cycles-argue/bin/spawn_caller_4m\");\n    let (spawn_callee_cell, _spawn_callee_data_hash) =\n        load_cell_from_path(\"/tmp/spawn-cycles-argue/bin/spawn_callee\");\n\n    let spawn_caller_script = Script::new_builder()\n        .hash_type(script_version.data_hash_type().into())\n        .code_hash(spawn_caller_data_hash)\n        .build();\n    let output = CellOutputBuilder::default()\n        .capacity(capacity_bytes!(100).pack())\n        .lock(spawn_caller_script)\n        .build();\n    let input = CellInput::new(OutPoint::null(), 0);\n\n    let transaction = TransactionBuilder::default().input(input).build();\n    let dummy_cell = create_dummy_cell(output);\n\n    let rtx = ResolvedTransaction {\n        transaction,\n        resolved_cell_deps: vec![spawn_caller_cell, spawn_callee_cell],\n        resolved_inputs: vec![dummy_cell],\n        resolved_dep_groups: vec![],\n    };\n    let verifier = TransactionScriptsVerifierWithEnv::new();\n    let tic = std::time::SystemTime::now();\n    let result = verifier.verify_without_limit(script_version, \u0026rtx);\n    let toc = std::time::SystemTime::elapsed(\u0026tic).unwrap();\n    println!(\"{:?}\", toc.as_millis());\n    assert_eq!(result.is_ok(), script_version \u003e= ScriptVersion::V2);\n}\n\n#[test]\nfn check_prime_secp256k1() {\n    let script_version = SCRIPT_VERSION;\n\n    let (spawn_caller_cell, spawn_caller_data_hash) =\n        load_cell_from_path(\"/tmp/spawn-cycles-argue/bin/prime\");\n\n    let spawn_caller_script = Script::new_builder()\n        .hash_type(script_version.data_hash_type().into())\n        .code_hash(spawn_caller_data_hash)\n        .build();\n    let output = CellOutputBuilder::default()\n        .capacity(capacity_bytes!(100).pack())\n        .lock(spawn_caller_script)\n        .build();\n    let input = CellInput::new(OutPoint::null(), 0);\n\n    let transaction = TransactionBuilder::default().input(input).build();\n    let dummy_cell = create_dummy_cell(output);\n\n    let rtx = ResolvedTransaction {\n        transaction,\n        resolved_cell_deps: vec![spawn_caller_cell],\n        resolved_inputs: vec![dummy_cell],\n        resolved_dep_groups: vec![],\n    };\n    let verifier = TransactionScriptsVerifierWithEnv::new();\n    let tic = std::time::SystemTime::now();\n    let result = verifier.verify_without_limit(script_version, \u0026rtx);\n    let toc = std::time::SystemTime::elapsed(\u0026tic).unwrap();\n    println!(\"{:?}\", toc.as_millis());\n    assert_eq!(result.is_ok(), script_version \u003e= ScriptVersion::V2);\n}\n```\n\n并添加如下代码到[虚拟机运行结束时](https://github.com/nervosnetwork/ckb/blob/ckb2023/script/src/verify.rs#L1018)\n\n```rs\nprintln!(\"cycles={:?}\", machine.machine.cycles());\n```\n\n执行测试\n\n```sh\n$ cargo test --release -- --nocapture verify::tests::ckb_2023::features_since_v2023::check_spawn_secp256k1_2m\n$ cargo test --release -- --nocapture verify::tests::ckb_2023::features_since_v2023::check_spawn_secp256k1_4m\n$ cargo test --release -- --nocapture verify::tests::ckb_2023::features_since_v2023::check_prime_secp256k1\n```\n\n获得测试结果\n\n```text\ncheck_spawn_secp256k1_2m: cycles=1410654032 mils=4705\ncheck_spawn_secp256k1_4m: cycles=1410654032 mils=4693\ncheck_prime_secp256k1:    cycles=1127690716 mils=3432\n```\n\n可以发现:\n\n1. 子虚拟机内存设置为 2m 还是 4m, 对整体运行时间几乎没有影响.\n2. 对于 secp256k1 来说, spawn syscall 如果额外消耗 `(1127690716 / 3432 * 4693 - 1410654032) / 1024 = 128298`, 那么单位时间内两者消耗的 cycles 将基本相同.\n\n我建议将 spawn syscall 的额外 syscall 消耗分为两个部分, 分别是固定消耗和内存消耗, 公式为\n\n```\nspawn_cycles = 100000 + memory_size_in_bytes / 64\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibraries%2Fspawn-cycles-argue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flibraries%2Fspawn-cycles-argue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibraries%2Fspawn-cycles-argue/lists"}