Skip to content

Commit

Permalink
Merge pull request #4 from amazingdatamachine/bcalza/machine-metadata
Browse files Browse the repository at this point in the history
adds support for user-defined machine metadata
  • Loading branch information
brunocalza authored Aug 1, 2024
2 parents e1f7b12 + 7d3e72e commit 0d270cc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
34 changes: 26 additions & 8 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 actors/adm/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ pub mod account {
pub mod machine {
use super::*;
use fvm_shared::address::Address;
use std::collections::HashMap;

#[derive(Debug, Serialize_tuple, Deserialize_tuple)]
pub struct ConstructorParams {
/// The machine creator robust address.
pub creator: Address,
/// Write access dictates who can write to the machine.
pub write_access: WriteAccess,
/// User-defined metadata.
pub metadata: HashMap<String, String>,
}

/// The different types of machine write access.
Expand Down
23 changes: 18 additions & 5 deletions actors/adm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct ConstructorParams {
pub struct CreateExternalParams {
pub kind: Kind,
pub write_access: WriteAccess,
pub metadata: HashMap<String, String>,
}

#[derive(Serialize_tuple, Deserialize_tuple, Debug, PartialEq, Eq)]
Expand All @@ -67,9 +68,10 @@ fn create_machine(
creator: Address,
write_access: WriteAccess,
code_cid: Cid,
metadata: HashMap<String, String>,
) -> Result<CreateExternalReturn, ActorError> {
let constructor_params =
RawBytes::serialize(ext::machine::ConstructorParams { creator, write_access })?;
RawBytes::serialize(ext::machine::ConstructorParams { creator, write_access, metadata })?;
let value = rt.message().value_received();

let init_params = ExecParams { code_cid, constructor_params };
Expand Down Expand Up @@ -223,14 +225,25 @@ impl AdmActor {

let creator = resolve_caller_external(rt)?;
let machine_code = get_machine_code(rt, &params.kind)?;
let ret = create_machine(rt, creator, params.write_access, machine_code)?;
let ret = create_machine(
rt,
creator,
params.write_access,
machine_code,
params.metadata.clone(),
)?;

// Save machine metadata.
let address = ret.robust_address.expect("rubust address");
rt.transaction(|st: &mut State, rt| {
st.set_metadata(rt.store(), creator, address, params.kind).map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_ARGUMENT, "failed to set machine metadata")
})
st.set_metadata(rt.store(), creator, address, params.kind, params.metadata).map_err(
|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_ARGUMENT,
"failed to set machine metadata",
)
},
)
})?;

Ok(ret)
Expand Down
9 changes: 6 additions & 3 deletions actors/adm/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub struct Metadata {
pub kind: Kind,
/// Machine robust address.
pub address: Address,
/// User-defined data.
pub metadata: HashMap<String, String>,
}

/// ADM actor state representation.
Expand Down Expand Up @@ -205,12 +207,13 @@ impl State {
owner: Address,
address: Address,
kind: Kind,
metadata: HashMap<String, String>,
) -> anyhow::Result<()> {
let mut owner_map = OwnerMap::load(store, &self.owners, DEFAULT_HAMT_CONFIG, "owners")?;
let mut metadata =
let mut machine_metadata =
owner_map.get(&owner)?.map(|machines| machines.to_owned()).unwrap_or_default();
metadata.push(Metadata { kind, address });
owner_map.set(&owner, metadata)?;
machine_metadata.push(Metadata { kind, address, metadata });
owner_map.set(&owner, machine_metadata)?;
self.owners = owner_map.flush()?;
Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ short-precommit = []
min-power-2k = []
# Lower the minimum power requirement to 2g
min-power-2g = []
# Lower the minimum power requirement to 32g
min-power-32g = []

# no collateral for deals (for testing)
no-provider-deal-collateral = []
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "stable"
channel = "1.77.2"
components = ["clippy", "llvm-tools-preview", "rustfmt"]
targets = ["wasm32-unknown-unknown"]

0 comments on commit 0d270cc

Please sign in to comment.