Repository navigation
Expand file tree
/
Copy pathinstance_document.rs
More file actions
397 lines (382 loc) · 21.3 KB
/
Copy pathinstance_document.rs
File metadata and controls
397 lines (382 loc) · 21.3 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//! A non-financial instance policy with real issuance, custody and retirement.
//! Only ordinary funding Cells are seeded. Document and observer Cells are
//! outputs of verified signed transactions; the local live set is not a node.
use super::{code_candidate, lock, package, source_policy_records};
use camino::Utf8Path;
use cellscript::package::frozen_interface::{
compile_module, freeze_code_catalog, freeze_instance_policy, freeze_package_sources, freeze_source_code_policy,
resolve_source_catalog, EntrySelection,
};
use cellscript::CompileOptions;
use cellscript_artifact_checker::{
code_origin::SuppliedDependencyCell,
instance_policy::{encode_selection, ArgsConstraint, ConsumerArgs, InstanceMode, InstancePolicy},
open_handle_policy::AuthorizationSet,
};
use ckb_sdk::{traits::SecpCkbRawKeySigner, types::ScriptGroup, unlock::ScriptSigner, unlock::SecpSighashScriptSigner, SECP256K1};
use ckb_testtool::{
ckb_types::{bytes::Bytes, core::Capacity, core::TransactionBuilder, core::TransactionView, packed, prelude::*},
context::Context,
};
use std::collections::HashMap;
const DOCUMENT: &str = include_str!("../fixtures/instance_policy/document.cell.in");
const OBSERVER: &str = include_str!("../fixtures/instance_policy/document_observer.cell.in");
const CAPACITY: u64 = 100_000_000_000;
const FEE: u64 = 100_000_000;
const CYCLES: u64 = 2_000_000;
fn witness(payload: &[u8]) -> packed::Bytes {
packed::WitnessArgs::new_builder().input_type(Some(Bytes::copy_from_slice(payload)).pack()).build().as_bytes().pack()
}
fn document_witness(expected: &[u8; 32]) -> packed::Bytes {
witness(&[b"CSARGv1\0".as_slice(), expected].concat())
}
pub(super) fn sign(tx: &TransactionView, script: &packed::Script, indices: Vec<usize>, key: u8) -> TransactionView {
let signer =
SecpSighashScriptSigner::new(Box::new(SecpCkbRawKeySigner::new_with_secret_keys(vec![secp256k1::SecretKey::from_slice(
&[key; 32],
)
.unwrap()])));
let mut group = ScriptGroup::from_lock_script(script);
group.input_indices = indices;
signer.sign_tx(tx, &group).unwrap()
}
pub(super) fn signer_lock(byte: u8) -> packed::Script {
let code = ckb_system_scripts_v0_6_0::BUNDLED_CELL.get("specs/cells/secp256k1_blake160_sighash_all").unwrap();
let key = secp256k1::SecretKey::from_slice(&[byte; 32]).unwrap();
let public = secp256k1::PublicKey::from_secret_key(&SECP256K1, &key);
let hash = cellscript_artifact_checker::ckb_blake2b256(&public.serialize());
packed::Script::new_builder()
.code_hash(packed::CellOutput::calc_data_hash(&code))
.hash_type(4u8)
.args(Bytes::copy_from_slice(&hash[..20]).pack())
.build()
}
pub(super) fn transaction_record(context: &Context, tx: &TransactionView, status: &str) -> serde_json::Value {
let hex = cellscript_artifact_checker::hex_encode;
serde_json::json!({
"status": status,
"transaction": hex(tx.data().as_slice()),
"transaction_hash": hex(tx.hash().as_slice()),
"inputs": tx.inputs().into_iter().map(|input| {
let (output, data) = context.get_cell(&input.previous_output()).unwrap();
serde_json::json!({"out_point": hex(input.previous_output().as_slice()), "output": hex(output.as_slice()), "data": hex(&data)})
}).collect::<Vec<_>>(),
})
}
fn reject(ledger: &mut Ledger, tx: &TransactionView, group: &str) {
let error = ledger.context.verify_tx(tx, CYCLES).unwrap_err();
assert!(format!("{error:?}").contains(group), "expected {group}, got {error:?}");
let mut record = transaction_record(&ledger.context, tx, "rejected");
record["expected_group"] = serde_json::json!(group);
record["error"] = serde_json::json!(format!("{error:?}"));
ledger.records.push(record);
}
struct Ledger {
context: Context,
live: HashMap<packed::OutPoint, (packed::CellOutput, Bytes)>,
maximum_cycles: u64,
maximum_transaction_bytes: usize,
records: Vec<serde_json::Value>,
}
impl Ledger {
fn seed(&mut self, script: &packed::Script) -> packed::OutPoint {
let output = packed::CellOutput::new_builder().capacity(10 * CAPACITY).lock(script.clone()).build();
// Context's deterministic option covers deploy_cell, not create_cell.
// Freeze funding points too so sighashes and measured cycles replay.
let point = packed::OutPoint::new_builder().tx_hash(script.calc_script_hash()).index(0x80u32).build();
self.context.create_cell_with_out_point(point.clone(), output.clone(), Bytes::new());
self.live.insert(point.clone(), (output, Bytes::new()));
point
}
fn commit(&mut self, tx: &TransactionView) -> Vec<packed::OutPoint> {
let mut capacity = 0;
for input in tx.inputs() {
capacity += u64::from(self.live.get(&input.previous_output()).expect("input must still be live").0.capacity());
}
let output_capacity: u64 = tx.outputs().into_iter().map(|cell| u64::from(cell.capacity())).sum();
assert_eq!(capacity - output_capacity, FEE);
let cycles = self.context.verify_tx(tx, CYCLES).unwrap();
self.maximum_cycles = self.maximum_cycles.max(cycles);
let mut record = transaction_record(&self.context, tx, "accepted");
record["cycles"] = serde_json::json!(cycles);
self.records.push(record);
self.maximum_transaction_bytes = self.maximum_transaction_bytes.max(tx.data().as_slice().len());
for input in tx.inputs() {
self.live.remove(&input.previous_output()).unwrap();
}
tx.outputs()
.into_iter()
.enumerate()
.map(|(index, output)| {
let data = tx.outputs_data().get(index).unwrap().raw_data();
assert!(
u64::from(output.capacity()) >= output.occupied_capacity(Capacity::bytes(data.len()).unwrap()).unwrap().as_u64()
);
let point = packed::OutPoint::new(tx.hash(), index as u32);
assert!(!self.live.contains_key(&point));
self.context.create_cell_with_out_point(point.clone(), output.clone(), data.clone());
self.live.insert(point.clone(), (output, data));
point
})
.collect()
}
}
#[test]
fn issued_document_instances_require_real_issuer_custody_and_peer_execution() {
let directory = tempfile::tempdir().unwrap();
let library = directory.path().join("library");
let consumer = directory.path().join("consumer");
let issuer = signer_lock(0x51);
let attacker = signer_lock(0x52);
let digest = cellscript_artifact_checker::ckb_blake2b256(b"CellScript constrained document: signed issuance and retirement.");
let mut wrong_digest = digest;
wrong_digest[31] ^= 1;
let issuer_literal = issuer.calc_script_hash().as_slice().iter().map(|byte| format!("\\x{byte:02x}")).collect::<String>();
package(&library, "library", &DOCUMENT.replace("@ISSUER_LOCK_HASH@", &issuer_literal), "");
lock(&library);
package(&consumer, "client", OBSERVER, "[dependencies]\nlibrary = { path = \"../library\" }\n");
lock(&consumer);
for diagnostics in [cellscript::CompilerDiagnostics::default(), cellscript::CompilerDiagnostics::without_optimizations()] {
let compile_document = || {
compile_module(
Utf8Path::from_path(&library).unwrap(),
"dev",
CompileOptions { diagnostics, ..Default::default() },
EntrySelection::Action("verify".into()),
)
.unwrap()
};
let candidate = code_candidate(compile_document(), vec![1]);
let catalog = freeze_code_catalog(compile_document(), vec![candidate], &Default::default()).unwrap();
let sources =
resolve_source_catalog(freeze_package_sources(Utf8Path::from_path(&consumer).unwrap(), "dev").unwrap(), catalog).unwrap();
let (header, members) = source_policy_records(&sources, false, 0);
let implementation = freeze_source_code_policy(sources, AuthorizationSet::new(header, &members).unwrap()).unwrap();
let implementation_selection = implementation.policy().selection(&members[0].receipt).unwrap();
let constraints = InstancePolicy {
implementation_root: implementation.policy().root(),
mode: InstanceMode::Constrained,
args_length: 32,
config_length: 16,
constraints: vec![
ArgsConstraint::Config { offset: 0, length: 16, config_offset: 0 },
ArgsConstraint::Unsigned { offset: 16, width: 8, minimum: 1, maximum: 5 },
ArgsConstraint::Unsigned { offset: 24, width: 8, minimum: 1, maximum: u64::MAX },
],
};
let policy = freeze_instance_policy(implementation, constraints).unwrap();
let provider = &policy.implementation().sources().catalog().candidates()[0];
let observer = compile_module(
Utf8Path::from_path(&consumer).unwrap(),
"dev",
CompileOptions { diagnostics, ..Default::default() },
EntrySelection::Lock("verify".into()),
)
.unwrap();
let mut ledger = Ledger {
context: Context::new_with_deterministic_rng(),
live: HashMap::new(),
maximum_cycles: 0,
maximum_transaction_bytes: 0,
records: Vec::new(),
};
let code_creation = packed::RawTransaction::from_slice(provider.raw_transaction()).unwrap();
let code_point =
packed::OutPoint::new_builder().tx_hash(code_creation.calc_tx_hash()).index(provider.origin().output_index()).build();
ledger.context.create_cell_with_out_point(
code_point.clone(),
code_creation.outputs().get(0).unwrap(),
Bytes::copy_from_slice(provider.module().bundle()[0]),
);
let observer_point = ledger.context.deploy_cell(Bytes::copy_from_slice(observer.bundle()[0]));
let lock_code = ckb_system_scripts_v0_6_0::BUNDLED_CELL.get("specs/cells/secp256k1_blake160_sighash_all").unwrap();
let secp_data = ckb_system_scripts_v0_6_0::BUNDLED_CELL.get("specs/cells/secp256k1_data").unwrap();
let lock_point = ledger.context.deploy_cell(Bytes::copy_from_slice(&lock_code));
let secp_point = ledger.context.deploy_cell(Bytes::copy_from_slice(&secp_data));
let mut funding = ledger.seed(&issuer);
let attacker_point = ledger.seed(&attacker);
let deps =
[code_point, observer_point, lock_point, secp_point].map(|point| packed::CellDep::new_builder().out_point(point).build());
let plain = |capacity| packed::CellOutput::new_builder().capacity(capacity).lock(issuer.clone()).build();
let mut instances = Vec::new();
for (serial, category) in [(1u64, 3u64), (2, 3), (3, 6)] {
let args = [[0x41; 16].as_slice(), &category.to_le_bytes(), &serial.to_le_bytes()].concat();
let script = packed::Script::from_slice(provider.selected_script())
.unwrap()
.as_builder()
.args(Bytes::from(args.clone()).pack())
.build();
assert_ne!(script.as_slice(), provider.selected_script(), "neither future instance was enumerated in the old member");
let committed = ConsumerArgs {
policy_root: policy.policy().root().unwrap(),
subject: script.calc_script_hash().unpack(),
config: vec![0x41; 16],
}
.encode()
.unwrap();
let selection = encode_selection(policy.policy(), &implementation_selection, &args).unwrap();
assert_eq!(policy.check_selection(&committed, &selection, &Default::default()).is_ok(), category <= 5);
let observer_script = packed::Script::new_builder()
.code_hash(packed::CellOutput::calc_data_hash(observer.bundle()[0]))
.hash_type(4u8)
.args(Bytes::copy_from_slice(&committed).pack())
.build();
let data = [args.as_slice(), &digest].concat();
let available = u64::from(ledger.live[&funding].0.capacity());
let unsigned = TransactionBuilder::default()
.cell_deps(deps.clone())
.input(packed::CellInput::new_builder().previous_output(funding.clone()).build())
.outputs([
plain(CAPACITY).as_builder().type_(Some(script.clone()).pack()).build(),
plain(CAPACITY).as_builder().lock(observer_script).build(),
plain(available - 2 * CAPACITY - FEE),
])
.outputs_data([Bytes::from(data.clone()).pack(), Bytes::new().pack(), Bytes::new().pack()])
.witness(document_witness(&digest))
.build();
if serial == 1 {
let forged = unsigned
.as_advanced_builder()
.set_inputs(vec![packed::CellInput::new_builder().previous_output(attacker_point.clone()).build()])
.build();
reject(&mut ledger, &sign(&forged, &attacker, vec![0], 0x52), "Outputs[0].Type");
let wrong_custody = unsigned
.as_advanced_builder()
.set_outputs(vec![
unsigned.outputs().get(0).unwrap().as_builder().lock(attacker.clone()).build(),
unsigned.outputs().get(1).unwrap(),
unsigned.outputs().get(2).unwrap(),
])
.build();
reject(&mut ledger, &sign(&wrong_custody, &issuer, vec![0], 0x51), "Outputs[0].Type");
let mut rebound_data = data.clone();
rebound_data[24] = 9;
let wrong_configuration = unsigned
.as_advanced_builder()
.set_outputs_data(vec![Bytes::from(rebound_data.clone()).pack(), Bytes::new().pack(), Bytes::new().pack()])
.build();
reject(&mut ledger, &sign(&wrong_configuration, &issuer, vec![0], 0x51), "Outputs[0].Type");
let malformed = unsigned
.as_advanced_builder()
.set_outputs_data(vec![Bytes::copy_from_slice(&data[..63]).pack(), Bytes::new().pack(), Bytes::new().pack()])
.build();
reject(&mut ledger, &sign(&malformed, &issuer, vec![0], 0x51), "Outputs[0].Type");
}
let points = ledger.commit(&sign(&unsigned, &issuer, vec![0], 0x51));
funding = points[2].clone();
instances.push((script, committed, selection, points));
}
assert_ne!(instances[0].0.calc_script_hash(), instances[1].0.calc_script_hash());
for index in 0..instances.len() {
let (_, committed, selection, points) = &instances[index];
let unsigned = TransactionBuilder::default()
.cell_deps(deps.clone())
.inputs(
[points[1].clone(), points[0].clone()]
.map(|point| packed::CellInput::new_builder().previous_output(point).build()),
)
.output(plain(2 * CAPACITY - FEE))
.output_data(Bytes::new().pack())
.witness(witness(&[b"CSARGv1\0".as_slice(), selection].concat()))
.witness(document_witness(&digest))
.build();
if index == 2 {
// Issuer authorization permits this real document, but the
// observer admits only categories 1..=5. Its concrete identity
// and correctly signed peer cannot override that policy.
assert!(policy.check_selection(committed, selection, &Default::default()).is_err());
reject(&mut ledger, &sign(&unsigned, &issuer, vec![1], 0x51), "Inputs[0].Lock");
assert!(ledger.live.contains_key(&points[0]));
continue;
}
let resolved = deps.iter().map(|dep| ledger.context.get_cell(&dep.out_point()).unwrap()).collect::<Vec<_>>();
let supplied = deps
.iter()
.zip(&resolved)
.map(|(dep, (output, data))| SuppliedDependencyCell {
out_point: dep.out_point().as_slice().try_into().unwrap(),
output: output.as_slice(),
data,
})
.collect::<Vec<_>>();
policy
.check_selection(committed, selection, &Default::default())
.unwrap()
.check_direct_dependency(unsigned.data().raw().as_slice(), &supplied, &Default::default())
.unwrap();
let bad_peer = unsigned
.as_advanced_builder()
.set_witnesses(vec![unsigned.witnesses().get(0).unwrap(), document_witness(&wrong_digest)])
.build();
reject(&mut ledger, &sign(&bad_peer, &issuer, vec![1], 0x51), "Inputs[1].Type");
let (document, data) = &ledger.live[&points[0]];
let unauthorized_successor = unsigned
.as_advanced_builder()
.set_outputs(vec![document.clone().as_builder().capacity(2 * CAPACITY - FEE).build()])
.set_outputs_data(vec![data.pack()])
.build();
reject(&mut ledger, &sign(&unauthorized_successor, &issuer, vec![1], 0x51), "Inputs[1].Type");
if index == 0 {
let (_, _, other_selection, other_points) = &instances[1];
let substitute = unsigned
.as_advanced_builder()
.set_inputs(vec![
unsigned.inputs().get(0).unwrap(),
packed::CellInput::new_builder().previous_output(other_points[0].clone()).build(),
])
.set_witnesses(vec![witness(&[b"CSARGv1\0".as_slice(), other_selection].concat()), document_witness(&digest)])
.build();
reject(&mut ledger, &sign(&substitute, &issuer, vec![1], 0x51), "Inputs[0].Lock");
let missing = unsigned
.as_advanced_builder()
.set_inputs(vec![unsigned.inputs().get(0).unwrap()])
.set_outputs(vec![plain(CAPACITY - FEE)])
.set_witnesses(vec![unsigned.witnesses().get(0).unwrap()])
.build();
reject(&mut ledger, &missing, "Inputs[0].Lock");
}
ledger.commit(&sign(&unsigned, &issuer, vec![1], 0x51));
assert!(!ledger.live.contains_key(&points[0]), "retired document cannot be spent again");
}
eprintln!(
"document instances {diagnostics}: maximum_cycles={} maximum_tx_bytes={}",
ledger.maximum_cycles, ledger.maximum_transaction_bytes
);
assert!(ledger.maximum_transaction_bytes <= 2_400);
let evidence = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("target/instance-policy-evidence/document")
.join(format!("{diagnostics}"));
std::fs::create_dir_all(&evidence).unwrap();
std::fs::write(evidence.join("document.cell"), std::fs::read(library.join("src/main.cell")).unwrap()).unwrap();
std::fs::write(evidence.join("observer.cell"), OBSERVER).unwrap();
for (name, module) in [("document", provider.module()), ("observer", &observer)] {
for (suffix, bytes) in ["elf", "metadata.json", "lowering.json", "sourcemap.json"].into_iter().zip(module.bundle()) {
std::fs::write(evidence.join(format!("{name}.{suffix}")), bytes).unwrap();
}
std::fs::write(evidence.join(format!("{name}.source-context.json")), module.context_bytes().unwrap()).unwrap();
}
std::fs::write(evidence.join("implementation-creation.molecule"), provider.raw_transaction()).unwrap();
std::fs::write(evidence.join("instance-policy.molecule"), policy.policy().encode().unwrap()).unwrap();
let dependencies = deps
.iter()
.enumerate()
.map(|(index, dep)| {
let (output, data) = ledger.context.get_cell(&dep.out_point()).unwrap();
let file = format!("dependency-{index}.bin");
std::fs::write(evidence.join(&file), &data).unwrap();
serde_json::json!({"out_point": cellscript_artifact_checker::hex_encode(dep.out_point().as_slice()),
"output": cellscript_artifact_checker::hex_encode(output.as_slice()), "data_file": file,
"data_hash": cellscript_artifact_checker::hex_encode(&cellscript_artifact_checker::ckb_blake2b256(&data))})
})
.collect::<Vec<_>>();
let report = serde_json::json!({
"schema": "cellscript-instance-document-evidence-v1", "diagnostics": diagnostics.to_string(),
"compiler_version": env!("CARGO_PKG_VERSION"), "scope": "local-all-group-ckb-vm",
"source_policy": policy.implementation().identity(), "instance_policy": policy.identity(),
"policy_record": serde_json::from_slice::<serde_json::Value>(&policy.canonical_bytes().unwrap()).unwrap(),
"maximum_cycles": ledger.maximum_cycles, "maximum_transaction_bytes": ledger.maximum_transaction_bytes,
"cases": ledger.records, "dependencies": dependencies,
});
std::fs::write(evidence.join("report.json"), serde_json::to_vec_pretty(&report).unwrap()).unwrap();
}
}