kythera-cli 0.2.1

Kythera is a Toolset for Filecoin Virtual Machine Native Actor development, testing and deployment. For more information, check out the official documentation at https://polyphene.github.io/kythera/ .
Documentation
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright 2023 Polyphene.
// SPDX-License-Identifier: Apache-2.0, MIT

use std::collections::BTreeMap;

use comfy_table::{
    modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, Color, Table,
};
use kythera_lib::{DeployedActor, ExecutionEvent, Method, Payload, TestResult, TestResultType};

/// Gas report for the tested contracts.
#[derive(Default, Debug)]
pub struct GasReport {
    reports: BTreeMap<DeployedActor, ActorInfo>,
}

#[derive(Debug, Default)]
/// Actor method calls information
/// TODO: calculate actor deployment gas consumption.
pub struct ActorInfo {
    methods: BTreeMap<Method, Vec<u64>>,
}

/// A Method and its gas cost.
struct MethodCost {
    gas_cost: u64,
    num: u64,
    at: u64,
}

impl GasReport {
    pub fn analyze_method(&mut self, actor: &DeployedActor, method: Method, cost: u64) {
        let (actor, mut info) = match self.reports.remove_entry(actor) {
            Some((actor, info)) => (actor, info),
            None => (actor.clone(), ActorInfo::default()),
        };
        let mut gas_info = match info.methods.remove(&method) {
            Some(gi) => gi,
            None => vec![],
        };
        gas_info.push(cost);
        info.methods.insert(method.clone(), gas_info);
        self.reports.insert(actor, info);
    }

    /// Analyze a set of [`TestResult`]s for a target Actor.
    pub fn analyze_results(&mut self, actor: &DeployedActor, test_results: &[TestResult]) {
        let (actor, mut info) = match self.reports.remove_entry(actor) {
            Some((actor, info)) => (actor, info),
            None => (actor.clone(), ActorInfo::default()),
        };

        let actor_id = match actor.address().payload() {
            Payload::ID(id) => id,
            _ => panic!("DeployedActor address payload should be an Id"),
        };

        for result in test_results {
            let apply_ret = match result.ret() {
                TestResultType::Passed(apply_ret) | TestResultType::Failed(apply_ret) => apply_ret,
                TestResultType::Erred(_) => {
                    continue;
                }
            };

            // Get the Gas consumption by each Call of the Target actor.
            let mut stack: Vec<MethodCost> = vec![];
            for trace in &apply_ret.exec_trace {
                match trace {
                    ExecutionEvent::GasCharge(gas_charge) => {
                        // Add this gas charge to the total gas charge of the current method.
                        // There is a `GasCharge` before any `Call` so we skip it.
                        let method = match stack.last_mut() {
                            Some(e) => e,
                            None => continue,
                        };
                        method.gas_cost += gas_charge.compute_gas.as_milligas();
                    }

                    ExecutionEvent::Call { method, to, .. } => {
                        let to_id = match to.payload() {
                            Payload::ID(id) => id,
                            _ => panic!("Call to address payload should be an Id"),
                        };
                        stack.push(MethodCost {
                            gas_cost: 0,
                            num: *method,
                            at: *to_id,
                        });
                    }
                    ExecutionEvent::CallReturn(_, _) | ExecutionEvent::CallError(_) => {
                        let method_return = stack.pop().expect("A CallReturn should match a Call");
                        // If stack is empty we reached the main Method call.
                        // If the stack is not empty we keep summing the gas totals.
                        if let Some(previous) = stack.last_mut() {
                            previous.gas_cost += method_return.gas_cost;
                        }

                        // If the method called was from the target actor
                        // we create a new call on `GasInfo` with the totals of gas charge.
                        let Some(method) = actor
                                .abi()
                                .methods()
                                .iter()
                                .find(|a| a.number() == method_return.num && method_return.at == *actor_id) else {
                            continue;
                        };

                        let mut gas_info = match info.methods.remove(method) {
                            Some(gi) => gi,
                            None => vec![],
                        };
                        gas_info.push(method_return.gas_cost);
                        info.methods.insert(method.clone(), gas_info);
                    }
                    _ => {}
                }
            }
        }
        self.reports.insert(actor, info);
    }

    /// Finalize the Report and convert into a printable Table.
    pub fn finalize(self) -> Vec<Table> {
        let mut tables = vec![];

        for (actor, contract_info) in self.reports {
            let mut table = Table::new();
            table.load_preset(UTF8_FULL);
            table.apply_modifier(UTF8_ROUND_CORNERS);
            table.set_header(vec![Cell::new(format!("{} contract", actor.name()))
                .add_attribute(Attribute::Bold)
                .fg(Color::Green)]);
            table.add_row(vec![
                Cell::new("Function Name")
                    .add_attribute(Attribute::Bold)
                    .fg(Color::Magenta),
                Cell::new("min")
                    .add_attribute(Attribute::Bold)
                    .fg(Color::Green),
                Cell::new("max")
                    .add_attribute(Attribute::Bold)
                    .fg(Color::Red),
                Cell::new("avg")
                    .add_attribute(Attribute::Bold)
                    .fg(Color::Yellow),
                Cell::new("median")
                    .add_attribute(Attribute::Bold)
                    .fg(Color::Yellow),
                Cell::new("# calls").add_attribute(Attribute::Bold),
            ]);
            for (method, mut calls) in contract_info.methods {
                calls.sort_unstable();
                let min = calls.first().copied().unwrap_or_default();
                let max = calls.last().copied().unwrap_or_default();

                let mean = {
                    if calls.is_empty() {
                        0f64
                    } else {
                        calls.iter().copied().sum::<u64>() as f64 / calls.len() as f64
                    }
                };

                let median = {
                    if calls.is_empty() {
                        0u64
                    } else {
                        let len = calls.len();
                        let mid = len / 2;
                        if len % 2 == 0 {
                            (calls[mid - 1] + calls[mid]) / 2u64
                        } else {
                            calls[mid]
                        }
                    }
                };
                table.add_row(vec![
                    Cell::new(method.name()).add_attribute(Attribute::Bold),
                    Cell::new(min.to_string()).fg(Color::Green),
                    Cell::new(mean.to_string()).fg(Color::Yellow),
                    Cell::new(median.to_string()).fg(Color::Yellow),
                    Cell::new(max.to_string()).fg(Color::Red),
                    Cell::new(calls.len().to_string()),
                ]);
            }
            tables.push(table);
        }
        tables
    }
}

#[cfg(test)]
mod tests {
    use kythera_lib::{
        Address, ApplyRet, ErrorNumber, ExitCode, Gas, GasCharge, RawBytes, Receipt, TokenAmount,
        WasmActor, Zero,
    };

    use super::*;

    const TARGET_ACTOR_ADDRESS: u64 = 44;

    #[test]
    fn analyzes_gas_consumption() {
        let constructor = Method::new_from_name("Constructor").unwrap();
        let c_number = constructor.number();
        let m1 = Method::new_from_name("Method1").unwrap();
        let m1_number = m1.number();
        let m2 = Method::new_from_name("Method2").unwrap();
        let m2_number = m2.number();
        let target = WasmActor::new(
            "Target".into(),
            vec![],
            kythera_lib::Abi {
                // We can define constructor here as None,
                // as target actor is not deployed.
                constructor: None,
                set_up: None,
                methods: vec![m1, m2],
            },
        )
        .deploy(Address::new_id(44));
        let mut gr = GasReport::default();
        gr.analyze_method(&target, constructor, 40);
        let result = TestResult::new(
            Method::new_from_name("TestMethod").unwrap(),
            TestResultType::Passed(ApplyRet {
                msg_receipt: Receipt {
                    exit_code: ExitCode::new(0),
                    return_data: RawBytes::default(),
                    gas_used: 0,
                    events_root: None,
                },
                penalty: TokenAmount::zero(),
                miner_tip: TokenAmount::zero(),
                base_fee_burn: TokenAmount::zero(),
                over_estimation_burn: TokenAmount::zero(),
                refund: TokenAmount::zero(),
                gas_refund: 0,
                gas_burned: 0,
                failure_info: None,
                exec_trace: vec![
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m1_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m2_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(10),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallError(kythera_lib::SyscallError(
                        "error".into(),
                        ErrorNumber::Forbidden,
                    )),
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(20),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallReturn(ExitCode::new(0), None),
                ],
                events: vec![],
            }),
        );
        gr.analyze_results(&target, &[result]);
        let report = gr.reports.get(&target).unwrap();
        assert_eq!(report.methods.len(), 3);
        let cm = report.methods.get(&c_number).unwrap();
        assert_eq!(cm.len(), 1);
        assert_eq!(cm[0], 40);
        let m1m = report.methods.get(&m1_number).unwrap();
        assert_eq!(m1m.len(), 1);
        assert_eq!(m1m[0], 30);
        let m2m = report.methods.get(&m2_number).unwrap();
        assert_eq!(m2m.len(), 1);
        assert_eq!(m2m[0], 10);
    }

    #[test]
    fn calculates_totals() {
        let m1 = Method::new_from_name("Method1").unwrap();
        let m1_number = m1.number();
        let m2 = Method::new_from_name("Method2").unwrap();
        let m2_number = m2.number();
        let target = WasmActor::new(
            "Target.wasm".into(),
            vec![],
            kythera_lib::Abi {
                constructor: None,
                set_up: None,
                methods: vec![m1, m2],
            },
        )
        .deploy(Address::new_id(44));
        let mut gr = GasReport::default();
        let result1 = TestResult::new(
            Method::new_from_name("TestMethod1").unwrap(),
            TestResultType::Passed(ApplyRet {
                msg_receipt: Receipt {
                    exit_code: ExitCode::new(0),
                    return_data: RawBytes::default(),
                    gas_used: 0,
                    events_root: None,
                },
                penalty: TokenAmount::zero(),
                miner_tip: TokenAmount::zero(),
                base_fee_burn: TokenAmount::zero(),
                over_estimation_burn: TokenAmount::zero(),
                refund: TokenAmount::zero(),
                gas_refund: 0,
                gas_burned: 0,
                failure_info: None,
                exec_trace: vec![
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m1_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m2_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(10),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallError(kythera_lib::SyscallError(
                        "error".into(),
                        ErrorNumber::Forbidden,
                    )),
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(20),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallReturn(ExitCode::new(0), None),
                ],
                events: vec![],
            }),
        );
        let result2 = TestResult::new(
            Method::new_from_name("TestMethod2").unwrap(),
            TestResultType::Passed(ApplyRet {
                msg_receipt: Receipt {
                    exit_code: ExitCode::new(0),
                    return_data: RawBytes::default(),
                    gas_used: 0,
                    events_root: None,
                },
                penalty: TokenAmount::zero(),
                miner_tip: TokenAmount::zero(),
                base_fee_burn: TokenAmount::zero(),
                over_estimation_burn: TokenAmount::zero(),
                refund: TokenAmount::zero(),
                gas_refund: 0,
                gas_burned: 0,
                failure_info: None,
                exec_trace: vec![
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m1_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m2_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(20),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallError(kythera_lib::SyscallError(
                        "error".into(),
                        ErrorNumber::Forbidden,
                    )),
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(10),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallReturn(ExitCode::new(0), None),
                ],
                events: vec![],
            }),
        );
        let result3 = TestResult::new(
            Method::new_from_name("TestMethod3").unwrap(),
            TestResultType::Passed(ApplyRet {
                msg_receipt: Receipt {
                    exit_code: ExitCode::new(0),
                    return_data: RawBytes::default(),
                    gas_used: 0,
                    events_root: None,
                },
                penalty: TokenAmount::zero(),
                miner_tip: TokenAmount::zero(),
                base_fee_burn: TokenAmount::zero(),
                over_estimation_burn: TokenAmount::zero(),
                refund: TokenAmount::zero(),
                gas_refund: 0,
                gas_burned: 0,
                failure_info: None,
                exec_trace: vec![
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m1_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::Call {
                        from: 0,
                        to: Address::new_id(TARGET_ACTOR_ADDRESS),
                        method: m2_number,
                        params: None,
                        value: TokenAmount::zero(),
                    },
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(0),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallError(kythera_lib::SyscallError(
                        "error".into(),
                        ErrorNumber::Forbidden,
                    )),
                    ExecutionEvent::GasCharge(GasCharge::new(
                        "",
                        Gas::from_milligas(30),
                        Gas::from_milligas(0),
                    )),
                    ExecutionEvent::CallReturn(ExitCode::new(0), None),
                ],
                events: vec![],
            }),
        );

        gr.analyze_results(&target, &[result1, result2, result3]);
        let table = gr.finalize().pop().unwrap();
        let header = table.header().unwrap();
        assert_eq!(
            header.cell_iter().next().unwrap().content(),
            "Target.wasm contract"
        );
        let mut names = table.row(0).unwrap().cell_iter();
        assert_eq!(names.next().unwrap().content(), "Function Name");
        assert_eq!(names.next().unwrap().content(), "min");
        assert_eq!(names.next().unwrap().content(), "max");
        assert_eq!(names.next().unwrap().content(), "avg");
        assert_eq!(names.next().unwrap().content(), "median");
        assert_eq!(names.next().unwrap().content(), "# calls");

        let mut method2 = table.row(1).unwrap().cell_iter();
        assert_eq!(method2.next().unwrap().content(), "Method2");
        assert_eq!(method2.next().unwrap().content(), "0");
        assert_eq!(method2.next().unwrap().content(), "10");
        assert_eq!(method2.next().unwrap().content(), "10");
        assert_eq!(method2.next().unwrap().content(), "20");
        assert_eq!(method2.next().unwrap().content(), "3");

        let mut method1 = table.row(2).unwrap().cell_iter();
        assert_eq!(method1.next().unwrap().content(), "Method1");
        assert_eq!(method1.next().unwrap().content(), "30");
        assert_eq!(method1.next().unwrap().content(), "30");
        assert_eq!(method1.next().unwrap().content(), "30");
        assert_eq!(method1.next().unwrap().content(), "30");
        assert_eq!(method1.next().unwrap().content(), "3");
    }
}