-
Notifications
You must be signed in to change notification settings - Fork 140
/
ipld.rs
71 lines (63 loc) · 1.66 KB
/
ipld.rs
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
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use fvm_shared::sys;
use super::Context;
use crate::kernel::{IpldBlockOps, Result};
pub fn block_open(
context: Context<'_, impl IpldBlockOps>,
cid: u32,
) -> Result<sys::out::ipld::IpldOpen> {
let cid = context.memory.read_cid(cid)?;
let (id, stat) = context.kernel.block_open(&cid)?;
Ok(sys::out::ipld::IpldOpen {
id,
codec: stat.codec,
size: stat.size,
})
}
pub fn block_create(
context: Context<'_, impl IpldBlockOps>,
codec: u64,
data_off: u32,
data_len: u32,
) -> Result<u32> {
let data = context.memory.try_slice(data_off, data_len)?;
context.kernel.block_create(codec, data)
}
pub fn block_link(
context: Context<'_, impl IpldBlockOps>,
id: u32,
hash_fun: u64,
hash_len: u32,
cid_off: u32,
cid_len: u32,
) -> Result<u32> {
// Check arguments first.
context.memory.check_bounds(cid_off, cid_len)?;
// Link
let cid = context.kernel.block_link(id, hash_fun, hash_len)?;
// Return
context.memory.write_cid(&cid, cid_off, cid_len)
}
pub fn block_read(
context: Context<'_, impl IpldBlockOps>,
id: u32,
offset: u32,
obuf_off: u32,
obuf_len: u32,
) -> Result<i32> {
let data = context.memory.try_slice_mut(obuf_off, obuf_len)?;
context.kernel.block_read(id, offset, data)
}
pub fn block_stat(
context: Context<'_, impl IpldBlockOps>,
id: u32,
) -> Result<sys::out::ipld::IpldStat> {
context
.kernel
.block_stat(id)
.map(|stat| sys::out::ipld::IpldStat {
codec: stat.codec,
size: stat.size,
})
}