Skip to content

Commit

Permalink
adds size to the object struct
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
  • Loading branch information
brunocalza committed Jul 4, 2024
1 parent 2a2427b commit 24d3dae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions fendermint/actors/objectstore/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Actor {
rt.store(),
BytesKey(params.key),
params.cid,
params.size,
params.metadata,
params.overwrite,
)
Expand Down
2 changes: 2 additions & 0 deletions fendermint/actors/objectstore/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct AddParams {
pub key: Vec<u8>,
/// Object value.
pub cid: Cid,
/// Object size.
pub size: usize,
/// Object metadata.
pub metadata: HashMap<String, String>,
/// Whether to overwrite a key if it already exists.
Expand Down
28 changes: 25 additions & 3 deletions fendermint/actors/objectstore/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ impl MachineState for State {
pub struct Object {
/// The object content identifier.
pub cid: ByteBuf,
/// The size of the content.
pub size: usize,
/// Whether the object has been resolved.
pub resolved: bool,
/// User-defined object metadata (e.g., size, last modified timestamp, etc.).
Expand Down Expand Up @@ -87,12 +89,14 @@ impl State {
store: &BS,
key: BytesKey,
cid: Cid,
size: usize,
metadata: HashMap<String, String>,
overwrite: bool,
) -> anyhow::Result<Cid> {
let mut hamt = Hamt::<_, Object>::load_with_bit_width(&self.root, store, BIT_WIDTH)?;
let object = Object {
cid: ByteBuf(cid.to_bytes()),
size,
resolved: false,
metadata,
};
Expand Down Expand Up @@ -219,6 +223,7 @@ mod tests {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
Object {
cid: ByteBuf(ArbCid::<64>::arbitrary(g).0.to_bytes()),
size: usize::arbitrary(g),
metadata: HashMap::arbitrary(g),
resolved: false,
}
Expand All @@ -228,6 +233,7 @@ mod tests {
fn default_object() -> Object {
Object {
cid: ByteBuf(Cid::default().to_bytes()),
size: 0,
metadata: HashMap::<String, String>::new(),
resolved: false,
}
Expand All @@ -243,12 +249,13 @@ mod tests {
metadata.insert("_modified".to_string(), String::from("1718464345"));
Object {
cid: ByteBuf(cid.to_bytes()),
size: 5,
metadata,
resolved: false,
}
}

const GOLDEN_CID: &str = "bafy2bzacebi2bfkcucl3r2oih4du4hd7jrlvm7cqgggofnkzzz3m4asycsbmq";
const GOLDEN_CID: &str = "bafy2bzacebmog6w3ept45xctbw3lrt76i3rdbeaib6bikuhcddu5y5bqspozu";

#[test]
fn test_constructor() {
Expand All @@ -272,6 +279,7 @@ mod tests {
&store,
BytesKey(vec![1, 2, 3]),
Cid::from_bytes(&object.cid.0).unwrap(),
object.size,
object.metadata,
true
)
Expand All @@ -287,7 +295,9 @@ mod tests {
let key = BytesKey(vec![1, 2, 3]);
let cid = Cid::from_bytes(&object.cid.0).unwrap();
let md = object.metadata.clone();
state.add(&store, key.clone(), cid, md, true).unwrap();
state
.add(&store, key.clone(), cid, object.size, md, true)
.unwrap();
assert!(state.resolve(&store, key.clone(), cid).is_ok());

object.resolved = true;
Expand All @@ -306,6 +316,7 @@ mod tests {
&store,
key.clone(),
Cid::from_bytes(&object.cid.0).unwrap(),
object.size,
object.metadata,
true,
)
Expand All @@ -324,7 +335,9 @@ mod tests {
let key = BytesKey(vec![1, 2, 3]);
let cid = Cid::from_bytes(&object.cid.0).unwrap();
let md = object.metadata.clone();
state.add(&store, key.clone(), cid, md, true).unwrap();
state
.add(&store, key.clone(), cid, object.size, md, true)
.unwrap();
let result = state.get(&store, &key);

assert!(result.is_ok());
Expand All @@ -340,6 +353,7 @@ mod tests {
store,
jpeg_key.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)?;
Expand All @@ -348,6 +362,7 @@ mod tests {
store,
bar_key.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)?;
Expand All @@ -356,6 +371,7 @@ mod tests {
store,
baz_key.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)?;
Expand All @@ -366,6 +382,7 @@ mod tests {
&store,
other_key.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)?;
Expand Down Expand Up @@ -438,6 +455,7 @@ mod tests {
&store,
jpeg_key.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)
Expand All @@ -448,6 +466,7 @@ mod tests {
&store,
bar_key.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)
Expand All @@ -458,6 +477,7 @@ mod tests {
&store,
baz_key.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)
Expand Down Expand Up @@ -503,6 +523,7 @@ mod tests {
&store,
one.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)
Expand All @@ -513,6 +534,7 @@ mod tests {
&store,
two.clone(),
Cid::default(),
0,
HashMap::<String, String>::new(),
false,
)
Expand Down
3 changes: 2 additions & 1 deletion fendermint/app/src/cmd/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ mod tests {
"info": "",
"index": "0",
"key": "",
"value": "mJoSGEcYoxhjGGMYaRhkGFgYJAEYcBIYIBiRGMEYgBjyGIEYohiGGB8YKRgdGDoYSRj4GOIYSgIYzhh/GNgY/RiPGFwYcRi5GNYY1BEYZhhFGFoCGMwYaBhyGGUYcxhvGGwYdhhlGGQY9RhoGG0YZRh0GGEYZBhhGHQYYRihGGUYXxhzGGkYehhlGGEYNhgwGNAYwRhtGDoYSwoHGG0YZRhzGHMYYRhnGGUSDQoEGGYYchhvGG0SAxh0GDAYMBgYARIYMQoCGHQYbxIYKRh0GDIYcBhjGDQYahhpGGIYbxhiGDMYdhhzGHQYMxh2GHAYchg1GDYYaBhuGDIYaRhpGGYYZhhlGGwYdhhsGHkYbRhoGHYYZBh5GGMYeRhoGGEYGAE=",
"value": "mKASGE0YpBhjGGMYaRhkGFgYJAEYcBIYIBilGGgY5AQY2BjqGNoY7BiSGFoYwxi8GBsYNhglGJwPGG0YcAANGHIYnBjZGBgYxBhqGIMYbxhJGHYYVxhkGHMYaRh6GGUGGGgYchhlGHMYbxhsGHYYZRhkGPUYaBhtGGUYdBhhGGQYYRh0GGEYoRhlGF8YcxhpGHoYZRhhGDYYMBiiGNgYbhg6GEsKBxhtGGUYcxhzGGEYZxhlEg0KBBhmGHIYbxhtEgMYdBgwGDAYGAESGDEKAhh0GG8SGCkYdBgyGHcYdRhoGHEYNxh0GGEYMxgzGGUYdxgzGGMYMhhuGGQYbRhnGGIYbBg3GDcYNxh6GDUYeBg0GGQYbBh5GGYYbhhtGHkYbBhqGGkYaBhxGBgB",
"proof": null,
"height": "6017",
"codespace": ""
Expand Down Expand Up @@ -737,6 +737,7 @@ mod tests {
let params = AddParams {
key: key.to_vec(),
cid: object_cid,
size: 11,
metadata: HashMap::new(),
overwrite: true,
};
Expand Down

0 comments on commit 24d3dae

Please sign in to comment.