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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use super::*;

mod ipld {

    use cid::Cid;
    use fvm::kernel::IpldBlockOps;
    use fvm::machine::Machine;
    use fvm_ipld_blockstore::Blockstore;
    use fvm_ipld_encoding::DAG_CBOR;
    use multihash_codetable::MultihashDigest;
    use pretty_assertions::{assert_eq, assert_ne};

    use super::*;

    #[test]
    fn roundtrip() -> anyhow::Result<()> {
        let (mut kern, _) = build_inspecting_test()?;

        let block = "foo".as_bytes();
        let mut buf = [0u8; 3];
        // roundtrip
        let id = kern.block_create(DAG_CBOR, block)?;
        let cid = kern.block_link(id, Code::Blake2b256.into(), 32)?;
        let stat = kern.block_stat(id)?;
        let (opened_id, opened_stat) = kern.block_open(&cid)?;
        let remaining = kern.block_read(id, 0, &mut buf)?;

        let (call_manager, _) = kern.into_inner();
        let test_data = call_manager.test_data.borrow();

        // Create
        assert_eq!(id, 1, "block creation should be ID 1");

        // Link
        let expected_cid = Cid::new_v1(DAG_CBOR, Code::Blake2b256.digest(block));
        assert_eq!(
            cid, expected_cid,
            "CID that came from block_link does not match expected CID: Blake2b256 hash, 32 bytes long, DAG CBOR codec"
        );

        // Stat
        assert_eq!(stat.codec, opened_stat.codec);
        assert_eq!(stat.size, opened_stat.size);

        // Open
        assert_eq!(opened_id, 2, "block open should be ID 2");

        // Read
        assert_eq!(
            remaining, 0,
            "logical buffer should've been exactly the same size as the block"
        );
        assert_eq!(
            &buf, block,
            "data read after roundrip does not match inital data"
        );

        // assert gas charge calls
        assert_eq!(
            test_data.charge_gas_calls,
            // open 2 (load charge and per-byte charge)
            // link 1
            // stat 1
            // create 1
            // read 1
            6,
            "total number of operations that charge gas should be 6"
        );
        Ok(())
    }

    #[test]
    fn create() -> anyhow::Result<()> {
        let (mut kern, _) = build_inspecting_test()?;
        let (mut kern1, _) = build_inspecting_test()?;

        let block = "foo".as_bytes();
        let block_1 = "bar".as_bytes();
        let block_2 = "baz".as_bytes();

        // create blocks
        let id = kern.block_create(DAG_CBOR, block)?;
        let id1 = kern1.block_create(DAG_CBOR, block_1)?;

        assert_eq!(id, 1, "first block id should be 1");
        assert_eq!(
            id, id1,
            "two blocks of the different content but same order should have the same block id"
        );

        let id = kern1.block_create(DAG_CBOR, block_2)?;
        assert_eq!(id, 2, "second created block id should be 2");

        let (call_manager, _) = kern.into_inner();

        // assert gas
        {
            assert_eq!(
                call_manager.test_data.borrow().charge_gas_calls,
                1,
                "charge_gas should be called exactly once per block_create"
            );

            let expected_create_price = call_manager
                .machine
                .context()
                .price_list
                .on_block_create(block.len())
                .total();
            assert_eq!(
                call_manager.gas_tracker.gas_used(),
                expected_create_price,
                "gas use creating a block does not match price list"
            );
        }

        Ok(())
    }

    #[test]
    fn create_unexpected() -> anyhow::Result<()> {
        let (mut kern, _) = build_inspecting_test()?;

        let block = "foo".as_bytes();
        expect_syscall_err!(IllegalCodec, kern.block_create(0xFF, block));

        // valid for M1, shouldn't be for M2
        let _ = kern.block_create(DAG_CBOR, &[])?;

        // spec audit things arent (yet) tested
        Ok(())
    }

    #[test]
    fn link() -> anyhow::Result<()> {
        let (mut kern, _) = build_inspecting_test()?;
        let (mut kern1, _) = build_inspecting_test()?;

        let block = "foo".as_bytes();
        let other_block = "baz".as_bytes();

        // link a block
        let id = kern.block_create(DAG_CBOR, block)?;
        let cid = kern.block_link(id, Code::Blake2b256.into(), 32)?;

        // link a block of the same data inside a different kernel
        let id1 = kern1.block_create(DAG_CBOR, block)?;
        let cid1 = kern1.block_link(id1, Code::Blake2b256.into(), 32)?;

        // link a block of different data into kern1
        let other_id = kern1.block_create(DAG_CBOR, other_block)?;
        let other_cid = kern1.block_link(other_id, Code::Blake2b256.into(), 32)?;

        let (call_manager, _) = kern.into_inner();

        // CIDs match CIDs generated manually from CID crate
        let expected_cid = Cid::new_v1(DAG_CBOR, Code::Blake2b256.digest(block));
        let expected_other_cid = Cid::new_v1(DAG_CBOR, Code::Blake2b256.digest(other_block));

        assert_eq!(
            cid,
            expected_cid,
            "CID that came from block_link and {} does not match expected CID: Blake2b256 hash, 32 bytes long, DAG CBOR codec",
            String::from_utf8_lossy(block)
        );
        assert_eq!(
            other_cid,
            expected_other_cid,
            "CID that came from block_link and {} does not match expected CID: Blake2b256 hash, 32 bytes long, DAG CBOR codec",
            String::from_utf8_lossy(other_block)
        );

        // Internal CIDs
        assert!(
            call_manager.machine.blockstore().has(&cid)?,
            "block_link was called but CID was not found in the blockstore"
        );
        assert_eq!(
            cid, cid1,
            "calling block_link in 2 different kernels of the same data and state should have the same CID"
        );
        assert_ne!(
            cid, other_cid,
            "calling block_link with different data should make different CIDs"
        );
        // assert gas
        {
            assert_eq!(
                call_manager.test_data.borrow().charge_gas_calls - 1,
                1,
                "charge_gas should only be called exactly once per block_link"
            );

            let expected_block = Block::new(cid.codec(), block);
            let expected_create_price = call_manager
                .machine
                .context()
                .price_list
                .on_block_create(block.len())
                .total();
            let expected_link_price = call_manager
                .machine
                .context()
                .price_list
                .on_block_link(expected_block.size() as usize)
                .total();

            assert_eq!(
                call_manager.gas_tracker.gas_used(),
                expected_create_price + expected_link_price,
                "gas use creating and linking a block does not match price list"
            )
        }

        Ok(())
    }

    #[test]
    fn link_unexpected() -> anyhow::Result<()> {
        let (mut kern, test_data) = build_inspecting_test()?;

        let block = "foo".as_bytes();

        let id = kern.block_create(DAG_CBOR, block)?;
        test_data.borrow_mut().charge_gas_calls = 0;

        // Invalid hash lengths
        expect_syscall_err!(IllegalCid, kern.block_link(id, Code::Blake2b256.into(), 0));
        expect_syscall_err!(
            IllegalCid,
            kern.block_link(id, Code::Blake2b256.into(), 128)
        );

        // Invalid hash function
        expect_syscall_err!(IllegalCid, kern.block_link(id, 0xFF, 32));
        expect_syscall_err!(IllegalCid, kern.block_link(id, 0xFF, 0));

        // Invalid BlockId
        expect_syscall_err!(
            InvalidHandle,
            kern.block_link(123456, Code::Blake2b256.into(), 32)
        );
        expect_syscall_err!(
            InvalidHandle,
            kern.block_link(0, Code::Blake2b256.into(), 32)
        );

        Ok(())
    }

    #[test]
    fn read() -> anyhow::Result<()> {
        let (mut kern, _) = build_inspecting_test()?;
        let (mut kern1, _) = build_inspecting_test()?;

        let block = "foo".as_bytes();
        let other_block = "baz".as_bytes();
        let long_block = "hello world!".as_bytes();

        // add block
        let id = kern.block_create(DAG_CBOR, block)?;

        // add a block of the same data inside a different kernel
        let id1 = kern1.block_create(DAG_CBOR, block)?;

        // add a block of different data inside a different kernel
        let other_id = kern1.block_create(DAG_CBOR, other_block)?;
        let long_id = kern1.block_create(DAG_CBOR, long_block)?;

        // setup buffers
        let mut block_buf = [0u8; 32];
        let mut block1_buf = [0u8; 32];
        let mut other_block_buf = [0u8; 32];
        let mut long_block_buf = [0u8; 6];
        let mut remaining_buf = [0u8; 6];

        // read data
        let buf_i = kern.block_read(id, 0, &mut block_buf)?;
        let buf1_i = kern1.block_read(id1, 0, &mut block1_buf)?;

        assert_eq!(
            buf_i,
            3 - block_buf.len() as i32,
            "input buffer is larger than bytes to be read, expected value here must be exactly block.len()-buf.len()"
        );
        assert_eq!(
            buf_i, buf1_i,
            "remaining offsets from 2 different kernels of same data are mismatched"
        );
        assert_eq!(
            block_buf, block1_buf,
            "bytes read from 2 different kernels of same data are not equal"
        );

        // read 'other' data
        kern1.block_read(other_id, 0, &mut other_block_buf)?;

        assert_eq!(
            &block_buf[..block.len()],
            block,
            "bytes read from block does not match bytes put in"
        );
        assert_ne!(
            block_buf, other_block_buf,
            "bytes read from 2 different kernels of different ids are equal when they shouldn't be"
        );
        assert_ne!(
            block1_buf, other_block_buf,
            "bytes read from the same kernel of different ids are equal when they shouldn't be"
        );

        // partial read
        let partial_offset = kern1.block_read(long_id, 0, &mut long_block_buf)?;

        // remaining
        assert_eq!(
            partial_offset, 6,
            "6 bytes should be following after reading 6 bytes from a block 12 bytes long"
        );

        // read remaining
        let remaining_offset = kern1.block_read(
            long_id,
            long_block.len() as u32 - partial_offset as u32,
            &mut remaining_buf,
        )?;
        assert_eq!(
            &remaining_buf,
            "world!".as_bytes(),
            "bytes read from block with offset is unexpected"
        );
        assert_eq!(
            remaining_offset, 0,
            "number of bytes read here should be exactly the same as the number of bytes in the block"
        );

        // read with non-empty buffer
        {
            let mut garbage_buf = [0xFFu8; 32];
            kern1.block_read(long_id, 0, &mut garbage_buf)?;
            assert_eq!(
                &garbage_buf[..long_block.len()],
                long_block,
                "non-empty input buffer affects bytes read"
            )
        }

        let (call_manager, _) = kern.into_inner();

        // assert gas
        {
            let price_list = call_manager.machine.context().price_list;
            let expected_create_price = price_list.on_block_create(block.len()).total();
            let expected_read_price = price_list.on_block_read(block.len()).total();

            assert_eq!(
                call_manager.test_data.borrow().charge_gas_calls - 1,
                1,
                "charge_gas should be called exactly once in block_read"
            );
            assert_eq!(
                call_manager.gas_tracker.gas_used(),
                expected_create_price + expected_read_price,
                "gas use of creating and reading a block does not match price list"
            )
        }

        Ok(())
    }

    #[test]
    fn read_unexpected() -> anyhow::Result<()> {
        let (mut kern, test_data) = build_inspecting_test()?;

        let block = "foo".as_bytes();
        let buf = &mut [0u8; 3];

        // read before creation
        expect_syscall_err!(InvalidHandle, kern.block_read(1, 0, buf));

        // create block
        let id = kern.block_create(DAG_CBOR, block)?;
        test_data.borrow_mut().charge_gas_calls = 0;

        // ID
        expect_syscall_err!(InvalidHandle, kern.block_read(0, 0, buf));
        expect_syscall_err!(InvalidHandle, kern.block_read(0xFF, 0, buf));

        // Offset
        let buf = &mut [0u8; 258];

        // offset is larger than total bytes in block
        let diff = kern.block_read(id, 255, &mut buf[255..])?;
        assert_eq!(diff, -255);
        let end = (buf.len() as i32 + diff) as usize;
        let _ = &buf[..end];

        Ok(())
    }

    #[test]
    fn stat() -> anyhow::Result<()> {
        let (mut kern, _) = build_inspecting_test()?;

        let block = "foo".as_bytes();

        let id = kern.block_create(DAG_CBOR, block)?;

        let stat = kern.block_stat(id)?;

        assert_eq!(stat.codec, DAG_CBOR);
        assert_eq!(stat.size, 3);

        let (call_manager, _) = kern.into_inner();

        // assert gas
        {
            let price_list = call_manager.machine.context().price_list;
            let expected_create_price = price_list.on_block_create(block.len()).total();
            let expected_stat_price = price_list.on_block_stat().total();

            assert_eq!(
                call_manager.test_data.borrow().charge_gas_calls - 1,
                1,
                "charge_gas should be called exactly once in block_stat"
            );
            assert_eq!(
                call_manager.gas_tracker.gas_used(),
                expected_create_price + expected_stat_price,
                "gas use of creating and 'stat'ing a block does not match price list"
            )
        }
        Ok(())
    }

    #[test]
    fn stat_unexpected() -> anyhow::Result<()> {
        let (mut kern, test_data) = build_inspecting_test()?;

        let block = "foo".as_bytes();

        expect_syscall_err!(InvalidHandle, kern.block_stat(1));

        kern.block_create(DAG_CBOR, block)?;
        // reset gas calls
        test_data.borrow_mut().charge_gas_calls = 0;

        expect_syscall_err!(InvalidHandle, kern.block_stat(0));
        expect_syscall_err!(InvalidHandle, kern.block_stat(0xFF));

        Ok(())
    }
}

mod gas {
    use fvm::gas::*;
    use fvm::kernel::GasOps;
    use fvm_shared::version::NetworkVersion;
    use pretty_assertions::{assert_eq, assert_ne};

    use super::*;

    #[test]
    fn test() -> anyhow::Result<()> {
        let avaliable = Gas::new(10);
        let gas_tracker = GasTracker::new(avaliable, Gas::new(0));

        let (mut kern, _) = build_inspecting_gas_test(gas_tracker)?;

        assert_eq!(kern.gas_available(), avaliable);
        assert_eq!(kern.gas_used(), Gas::new(0));

        kern.charge_gas("charge 6 gas", Gas::new(6))?;

        assert_eq!(kern.gas_available(), Gas::new(4));
        assert_eq!(kern.gas_used(), Gas::new(6));

        kern.charge_gas("refund 6 gas", Gas::new(-6))?;

        assert_eq!(kern.gas_available(), avaliable);
        assert_eq!(kern.gas_used(), Gas::new(0));
        Ok(())
    }

    #[test]
    fn used() -> anyhow::Result<()> {
        let used = Gas::new(123456);
        let gas_tracker = GasTracker::new(Gas::new(i64::MAX), used);

        let (kern, _) = build_inspecting_gas_test(gas_tracker)?;

        assert_eq!(kern.gas_used(), used);

        Ok(())
    }

    #[test]
    fn available() -> anyhow::Result<()> {
        let avaliable = Gas::new(123456);
        let gas_tracker = GasTracker::new(avaliable, Gas::new(0));

        let (kern, _) = build_inspecting_gas_test(gas_tracker)?;

        assert_eq!(kern.gas_available(), avaliable);

        Ok(())
    }

    #[test]
    fn charge() -> anyhow::Result<()> {
        let test_gas = Gas::new(123456);
        let neg_test_gas = Gas::new(-123456);
        let gas_tracker = GasTracker::new(test_gas, Gas::new(0));

        let (mut kern, _) = build_inspecting_gas_test(gas_tracker)?;

        // charge exactly as much as avaliable
        kern.charge_gas("test test 123", test_gas)?;
        assert_eq!(kern.gas_used(), test_gas);

        // charge over by 1
        expect_out_of_gas!(kern.charge_gas("spend more!", Gas::new(1)));

        assert_eq!(
            kern.gas_used(),
            test_gas,
            "charging gas over what is avaliable and failing should not affect gas used"
        );

        // charge negative (refund) gas
        kern.charge_gas("refund~", neg_test_gas)?;
        assert_eq!(kern.gas_used(), Gas::new(0));
        kern.charge_gas("free gas!", neg_test_gas)?;

        assert_eq!(
            kern.gas_used(),
            neg_test_gas,
            "gas avaliable should be negative"
        );
        assert_eq!(
            kern.gas_available() + kern.gas_used(),
            test_gas,
            "gas avaliable + gas used should be equal to the gas limit"
        );

        // kernel with 0 avaliable gas
        let gas_tracker = GasTracker::new(Gas::new(0), Gas::new(0));
        let (mut kern, _) = build_inspecting_gas_test(gas_tracker)?;
        expect_out_of_gas!(kern.charge_gas("spend more!", test_gas));

        Ok(())
    }

    #[test]
    fn price_list() -> anyhow::Result<()> {
        let (kern, _) = build_inspecting_test()?;

        let expected_list = price_list_by_network_version(STUB_NETWORK_VER);
        assert_eq!(
            kern.price_list(),
            expected_list,
            "price list should be the same as the one used in the kernel {}",
            STUB_NETWORK_VER
        );

        let unexpected_list = price_list_by_network_version(NetworkVersion::V16);
        assert_ne!(kern.price_list(), unexpected_list);

        Ok(())
    }
}