Repository navigation
Expand file tree
/
Copy pathresources.rs
More file actions
341 lines (332 loc) · 15.2 KB
/
Copy pathresources.rs
File metadata and controls
341 lines (332 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! Exact-transaction resource observations. Never add separately replayed
//! rejected groups into a purported executed transaction cycle total.
use super::*;
use ckb_testtool::{
ckb_script::ScriptGroupType,
ckb_types::{
core::{Capacity, TransactionView},
prelude::*,
},
context::Context,
};
use sha2::{Digest, Sha256};
pub(super) fn measure(
key: &str,
context: &Context,
input: &ProtocolBundleInput,
compiled: &[CompileResult],
tx: &TransactionView,
exits: &[i8],
) {
let row = observe(key, context, input, compiled, tx, exits);
check_frozen(key, &row);
}
/// Library extraction may change source/artifact identities, but it must not
/// silently enlarge the original program's measured resource allowance.
pub(super) fn measure_package(
key: &str,
context: &Context,
input: &ProtocolBundleInput,
compiled: &[CompileResult],
tx: &TransactionView,
exits: &[i8],
) {
let row = observe(&format!("{key}-package"), context, input, compiled, tx, exits);
let profile = key.split('-').take(2).collect::<Vec<_>>().join("-");
check_ceiling(key, &row, &frozen()["ceilings"][&profile]);
}
pub(super) fn observe(
key: &str,
context: &Context,
input: &ProtocolBundleInput,
compiled: &[CompileResult],
tx: &TransactionView,
exits: &[i8],
) -> Value {
// Derive required groups from actual resolved inputs and packed outputs,
// independently of the participant list we are about to measure.
let mut groups = BTreeMap::new();
for cell in tx.inputs() {
let (output, _) = context.get_cell(&cell.previous_output()).unwrap();
groups.insert((0, output.lock().as_slice().to_vec()), output.lock());
if let Some(script) = output.type_().to_opt() {
groups.insert((1, script.as_slice().to_vec()), script);
}
}
for output in tx.outputs() {
if let Some(script) = output.type_().to_opt() {
groups.insert((1, script.as_slice().to_vec()), script);
}
}
assert_eq!(groups.len(), input.artifacts.len());
assert_eq!(compiled.len(), exits.len());
let valid = exits.iter().all(|exit| *exit == 0);
let outcome = context.verify_tx(tx, 20_000_000);
if valid {
assert!(outcome.is_ok(), "{key}: {outcome:?}");
} else {
assert!(outcome.is_err(), "{key}: invalid transaction accepted");
}
let mut measured = Vec::new();
let mut group_sum = 0;
for ((artifact, result), exit) in input.artifacts.iter().zip(compiled).zip(exits) {
let lock = artifact.script_role == cellscript::protocol_bundle::ProtocolScriptRole::Lock;
let script = super::support::packed_script(&artifact.deployment.script);
assert_eq!(groups.remove(&(if lock { 0 } else { 1 }, script.as_slice().to_vec())), Some(script.clone()));
let trace = super::cost_trace::measure(
context,
tx,
&script,
if lock { ScriptGroupType::Lock } else { ScriptGroupType::Type },
20_000_000,
);
assert_eq!(trace.status, "measured", "{key}/{}: {:?}", artifact.id, trace.unavailable_reason);
assert_eq!(trace.exit_code, Some(*exit), "{key}/{}", artifact.id);
assert_eq!(trace.authoritative_cycles, trace.stepped_cycles);
// These complete programs have no EXEC/SPAWN. Do not silently drop a
// newly introduced process or ELF generation from the resource scope.
assert_eq!(trace.vms.len(), 1);
let vm = &trace.vms[0];
assert_eq!(vm.program_sha256, hex::encode(Sha256::digest(&result.artifact_bytes)));
assert!(vm.observed_stack_bytes > 0);
assert!(vm.touched_guest_page_bytes > 0);
group_sum += trace.authoritative_cycles.unwrap();
let frames = result
.verified_lowering_record
.as_ref()
.unwrap()
.entries
.iter()
.map(|entry| (entry.id.clone(), entry.frame_size_bytes))
.collect::<BTreeMap<_, _>>();
measured.push(json!({
"artifact":artifact.id,"role":if lock {"lock"} else {"type"},
"script_hash":hex::encode(script.calc_script_hash().as_slice()),
"elf_sha256":vm.program_sha256,"elf_bytes":result.artifact_bytes.len(),
"exit_code":exit,"cycles":trace.authoritative_cycles,
"scheduler_and_loading_cycles":trace.scheduler_and_loading_cycles,
"instruction_cycles":vm.pcs.values().map(|pc|pc.instruction_cycles).sum::<u64>(),
"synchronous_syscall_cycles":vm.pcs.values().map(|pc|pc.synchronous_syscall_cycles).sum::<u64>(),
"declared_frames":frames,"vm_version":vm.vm_version,"isa":vm.isa,
"observed_stack_bytes":vm.observed_stack_bytes,
"touched_guest_page_bytes":vm.touched_guest_page_bytes,
"observed_heap_bytes":null
}));
}
assert!(groups.is_empty());
let transaction_cycles = outcome.ok();
if valid {
assert_eq!(transaction_cycles, Some(group_sum));
}
let occupied: u64 = tx
.outputs()
.into_iter()
.zip(tx.outputs_data())
.map(|(cell, data)| cell.occupied_capacity(Capacity::bytes(data.raw_data().len()).unwrap()).unwrap().as_u64())
.sum();
let row = json!({
"key":key,"transaction_sha256":hex::encode(Sha256::digest(tx.data().as_slice())),
"transaction_bytes":tx.data().as_slice().len(),
"raw_witness_args_bytes":tx.witnesses().into_iter().map(|w|w.raw_data().len()).sum::<usize>(),
"occupied_output_capacity_shannons":occupied,"transaction_cycles":transaction_cycles,
"groups":measured
});
eprintln!("selection-resource {row}");
row
}
fn frozen() -> Value {
serde_json::from_str(include_str!("../fixtures/selection_business_resources.json")).unwrap()
}
fn check_frozen(key: &str, row: &Value) {
let fixture = frozen();
assert_eq!(fixture["schema"], "cellscript-selection-business-resources-v1");
let profile = key.split('-').take(2).collect::<Vec<_>>().join("-");
let ceiling = &fixture["ceilings"][&profile];
check_ceiling(key, row, ceiling);
// Numeric ceilings are separate from exact observations: an intentional
// artifact/fixture refresh must not silently raise previously frozen limits.
// The full transaction SHA includes witnesses, unlike the raw tx hash.
if refresh_observations("selection_business_resources.json", &json!({key: row})) {
return;
}
assert_eq!(row, &fixture["observations"][key], "{key}: resource/identity drift requires review");
}
/// Explicit measurement refresh only. Callers must check the original ceilings
/// first; this replaces existing observations, never limits or case inventory.
pub(super) fn refresh_observations(file: &str, rows: &Value) -> bool {
if std::env::var("CELLSCRIPT_UPDATE_SELECTION_RESOURCES").as_deref() != Ok("1") {
return false;
}
static WRITE: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _guard = WRITE.lock().unwrap();
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures").join(file);
let mut fixture: Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
for (key, row) in rows.as_object().unwrap() {
*fixture["observations"].get_mut(key).expect("refresh requires an existing measured case") = row.clone();
}
let mut bytes = serde_json::to_vec_pretty(&fixture).unwrap();
bytes.push(b'\n');
std::fs::write(path, bytes).unwrap();
true
}
pub(super) fn check_ceiling(key: &str, row: &Value, ceiling: &Value) {
for metric in ["transaction_cycles", "transaction_bytes", "raw_witness_args_bytes", "occupied_output_capacity_shannons"] {
if metric == "transaction_cycles" && row[metric].is_null() {
continue;
}
assert!(row[metric].as_u64().unwrap() <= ceiling[metric].as_u64().unwrap(), "{key}: exceeded {metric}");
}
for group in row["groups"].as_array().unwrap() {
let id = group["artifact"].as_str().unwrap();
for metric in ["cycles", "observed_stack_bytes", "touched_guest_page_bytes", "elf_bytes"] {
assert!(
group[metric].as_u64().unwrap() <= ceiling["groups"][id][metric].as_u64().unwrap(),
"{key}/{id}: exceeded {metric}"
);
}
}
}
/// Fill the declared transaction scan domain with unrelated fee/change Cells.
/// Maximum selected membership alone does not exercise the maximum global scan.
pub(super) fn scan_limits(
name: &str,
count: usize,
base: &ProtocolBundleInput,
compiled: &[CompileResult],
root: &Path,
late: &ProtocolBundleInput,
late_exits: &[i8],
) {
// The late candidate changes output business data only. Keep its input
// resolution, witnesses and identities identical before adding scan work.
let mut restored = late.clone();
restored.transaction.outputs = base.transaction.outputs.clone();
assert_eq!(serde_json::to_value(&restored).unwrap(), serde_json::to_value(base).unwrap());
assert_eq!(late.transaction.outputs.len(), base.transaction.outputs.len());
assert_eq!(late_exits.len(), compiled.len());
assert_eq!(late_exits[0], 5);
let mut full = base.clone();
let untyped_input = base.transaction.inputs.iter().find(|cell| cell.r#type.is_none()).unwrap().clone();
let untyped_output = base.transaction.outputs.iter().find(|cell| cell.r#type.is_none()).unwrap().clone();
let add_input = |input: &mut ProtocolBundleInput| {
let mut cell = untyped_input.clone();
cell.out_point.as_mut().unwrap().index = input.transaction.inputs.len() as u32;
input.transaction.inputs.push(cell);
input.transaction.witnesses.push(serde_json::from_value(json!({})).unwrap());
};
while full.transaction.inputs.len() < 32 {
add_input(&mut full);
}
while full.transaction.outputs.len() < 32 {
full.transaction.outputs.push(untyped_output.clone());
}
let report = check_protocol_bundle(&full, root).unwrap();
let original = check_protocol_bundle(base, root).unwrap();
assert_eq!(
serde_json::to_value(&report.evidence.exact_role_selections).unwrap(),
serde_json::to_value(&original.evidence.exact_role_selections).unwrap()
);
let (tx, _) = cellscript_ckb_adapter::materialize_protocol_bundle_report(&serde_json::to_vec(&report).unwrap()).unwrap();
assert_eq!(tx.data().as_slice(), super::support::reference(&full));
let tx = ckb_testtool::ckb_types::packed::Transaction::from_slice(tx.data().as_slice()).unwrap().into_view();
measure(
&format!("{name}Scan-{count}-valid"),
&super::support::context(&full, compiled, None),
&full,
compiled,
&tx,
&vec![0; compiled.len()],
);
for output in [false, true] {
let mut over = full.clone();
if output {
over.transaction.outputs.push(untyped_output.clone());
// Keep the unrelated capacity balanced so that the Script scan
// bound, rather than funding, is the rejection under test.
over.transaction.inputs[0].capacity += untyped_output.capacity;
} else {
add_input(&mut over);
}
let error = check_protocol_bundle(&over, root).unwrap_err();
assert!(error.to_string().contains("scan bound"), "{error}");
// The rejected candidate has no adapter report. Independently construct
// its exact bytes to test the on-chain end probe as well as host bounds.
let tx = ckb_testtool::ckb_types::packed::Transaction::from_slice(&super::support::reference(&over)).unwrap().into_view();
let mut exits = vec![0; compiled.len()];
exits[0] = 21;
measure(
&format!("{name}Scan-{count}-{}33", if output { "output" } else { "input" }),
&super::support::context(&over, compiled, None),
&over,
compiled,
&tx,
&exits,
);
}
// Compose the maximum scan with a canonical but semantically invalid last
// transfer/note. A fast bounds failure cannot stand in for this late path.
let mut bad = full.clone();
bad.transaction.outputs[..late.transaction.outputs.len()].clone_from_slice(&late.transaction.outputs);
let bad_report = check_protocol_bundle(&bad, root).unwrap();
assert_eq!(
serde_json::to_value(&bad_report.evidence.exact_role_selections).unwrap(),
serde_json::to_value(&report.evidence.exact_role_selections).unwrap()
);
let (bad_tx, _) = cellscript_ckb_adapter::materialize_protocol_bundle_report(&serde_json::to_vec(&bad_report).unwrap()).unwrap();
assert_eq!(bad_tx.data().as_slice(), super::support::reference(&bad));
let bad_tx = ckb_testtool::ckb_types::packed::Transaction::from_slice(bad_tx.data().as_slice()).unwrap().into_view();
measure(
&format!("{name}Scan-{count}-late-business"),
&super::support::context(&bad, compiled, None),
&bad,
compiled,
&bad_tx,
late_exits,
);
}
#[test]
fn frozen_business_resources_bind_sources_and_complete_case_inventory() {
let fixture = frozen();
let sources = [
(
"examples/language/batches/mixed_settlement.cell",
include_bytes!("../../examples/language/batches/mixed_settlement.cell").as_slice(),
),
(
"examples/language/batches/nested_migration.cell",
include_bytes!("../../examples/language/batches/nested_migration.cell").as_slice(),
),
(
"examples/language/batches/record_migration.cell",
include_bytes!("../../examples/language/batches/record_migration.cell").as_slice(),
),
("tests/fixtures/mixed_settlement_asset.cell", include_bytes!("../fixtures/mixed_settlement_asset.cell").as_slice()),
("tests/fixtures/nested_migration_registry.cell", include_bytes!("../fixtures/nested_migration_registry.cell").as_slice()),
];
assert_eq!(fixture["sources"].as_object().unwrap().len(), sources.len());
for (path, bytes) in sources {
assert_eq!(fixture["sources"][path], hex::encode(Sha256::digest(bytes)), "{path}");
}
let mut required = std::collections::BTreeSet::new();
let mut profiles = std::collections::BTreeSet::new();
for (name, counts, maximum, late) in [
("Settlement", vec![1, 3, 6], 6, "late-amount"),
("Bytes", vec![1, 3, 8], 8, "late-note"),
("Records", vec![1, 3, 7, 8], 8, "late-note"),
] {
for count in counts {
profiles.insert(format!("{name}-{count}"));
for reverse in [false, true] {
required.insert(format!("{name}-{count}-{reverse}-valid"));
}
}
required.insert(format!("{name}-{maximum}-false-{late}"));
required.insert(format!("{name}Scan-{maximum}-late-business"));
profiles.insert(format!("{name}Scan-{maximum}"));
for outcome in ["valid", "input33", "output33"] {
required.insert(format!("{name}Scan-{maximum}-{outcome}"));
}
}
assert_eq!(fixture["observations"].as_object().unwrap().keys().cloned().collect::<std::collections::BTreeSet<_>>(), required);
assert_eq!(fixture["ceilings"].as_object().unwrap().keys().cloned().collect::<std::collections::BTreeSet<_>>(), profiles);
}