Skip to content

Commit

Permalink
Merge pull request #103 from amazingdatamachine/sander/object-state-r…
Browse files Browse the repository at this point in the history
…efactor

Refactor objectstore state
  • Loading branch information
brunocalza authored Jul 5, 2024
2 parents 6874efd + 24d3dae commit 56f00d8
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 253 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fendermint/actors/objectstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ fvm_ipld_hamt = { workspace = true }
fendermint_actor_machine = { path = "../machine" }

[dev-dependencies]
fendermint_testing = { path = "../../testing", features = ["arb"] }
fil_actors_runtime = { workspace = true, features = [
"test_utils",
"fil-actor",
] }
quickcheck = { workspace = true }
quickcheck_macros = { workspace = true }

[features]
default = []
Expand Down
25 changes: 12 additions & 13 deletions fendermint/actors/objectstore/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use fvm_ipld_hamt::BytesKey;
use fvm_shared::{error::ExitCode, MethodNum};

use crate::{
DeleteParams, GetParams, ListParams, Method, Object, ObjectList, PutParams,
ResolveExternalParams, State, OBJECTSTORE_ACTOR_NAME,
AddParams, DeleteParams, GetParams, ListParams, Method, Object, ObjectList, ResolveParams,
State, OBJECTSTORE_ACTOR_NAME,
};

#[cfg(feature = "fil-actor")]
Expand All @@ -36,29 +36,28 @@ impl Actor {
rt.create(&state)
}

fn put_object(rt: &impl Runtime, params: PutParams) -> Result<Cid, ActorError> {
fn add_object(rt: &impl Runtime, params: AddParams) -> Result<Cid, ActorError> {
Self::ensure_write_allowed(rt)?;

let root = rt.transaction(|st: &mut State, rt| {
st.put(
st.add(
rt.store(),
BytesKey(params.key),
params.kind,
params.cid,
params.size,
params.metadata,
params.overwrite,
)
.map_err(|e| e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to put object"))
.map_err(|e| e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to add object"))
})?;
Ok(root)
}

fn resolve_external_object(
rt: &impl Runtime,
params: ResolveExternalParams,
) -> Result<(), ActorError> {
fn resolve_object(rt: &impl Runtime, params: ResolveParams) -> Result<(), ActorError> {
rt.validate_immediate_caller_is(std::iter::once(&SYSTEM_ACTOR_ADDR))?;

rt.transaction(|st: &mut State, rt| {
st.resolve_external(rt.store(), BytesKey(params.key), params.value)
st.resolve(rt.store(), BytesKey(params.key), params.value)
.map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to resolve object")
})
Expand Down Expand Up @@ -135,8 +134,8 @@ impl ActorCode for Actor {
actor_dispatch! {
Constructor => constructor,
GetMetadata => get_metadata,
PutObject => put_object,
ResolveExternalObject => resolve_external_object,
AddObject => add_object,
ResolveObject => resolve_object,
DeleteObject => delete_object,
GetObject => get_object,
ListObjects => list_objects,
Expand Down
23 changes: 14 additions & 9 deletions fendermint/actors/objectstore/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,35 @@ use fendermint_actor_machine::GET_METADATA_METHOD;
use fvm_ipld_encoding::{strict_bytes, tuple::*};
use fvm_shared::METHOD_CONSTRUCTOR;
use num_derive::FromPrimitive;
use std::collections::HashMap;

pub use crate::state::{Object, ObjectKind, ObjectList, ObjectListItem, State};
pub use crate::state::{Object, ObjectList, State};

pub const OBJECTSTORE_ACTOR_NAME: &str = "objectstore";

/// Params for putting an object.
#[derive(Clone, Debug, Serialize_tuple, Deserialize_tuple)]
pub struct PutParams {
pub struct AddParams {
/// Object key.
#[serde(with = "strict_bytes")]
pub key: Vec<u8>,
/// Kind of object.
pub kind: ObjectKind,
/// 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.
pub overwrite: bool,
}

/// Params for resolving an external object.
/// Params for resolving an object.
#[derive(Clone, Debug, Serialize_tuple, Deserialize_tuple)]
pub struct ResolveExternalParams {
pub struct ResolveParams {
/// Object key.
#[serde(with = "strict_bytes")]
pub key: Vec<u8>,
/// External object value.
/// Object value.
pub value: Cid,
}

Expand Down Expand Up @@ -70,8 +75,8 @@ pub struct ListParams {
pub enum Method {
Constructor = METHOD_CONSTRUCTOR,
GetMetadata = GET_METADATA_METHOD,
PutObject = frc42_dispatch::method_hash!("PutObject"),
ResolveExternalObject = frc42_dispatch::method_hash!("ResolveExternalObject"),
AddObject = frc42_dispatch::method_hash!("AddObject"),
ResolveObject = frc42_dispatch::method_hash!("ResolveObject"),
DeleteObject = frc42_dispatch::method_hash!("DeleteObject"),
GetObject = frc42_dispatch::method_hash!("GetObject"),
ListObjects = frc42_dispatch::method_hash!("ListObjects"),
Expand Down
Loading

0 comments on commit 56f00d8

Please sign in to comment.