Skip to content

Commit

Permalink
piecrust: pre-loading metadata on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Jun 3, 2024
1 parent 9434ccc commit 76e0917
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
20 changes: 8 additions & 12 deletions piecrust/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,10 @@ fn get_metadata(env: &mut Env, mod_id_ofs: usize) -> Option<&ContractMetadata> {
// The null pointer is always zero, so we can use this to check if the
// caller wants their own ID.
if mod_id_ofs == 0 {
let self_id = env.self_contract_id();
let self_id = env.self_contract_id().to_owned();

let contract_metadata = env
.contract_metadata(self_id)
.contract_metadata(&self_id)
.expect("contract metadata should exist");

Some(contract_metadata)
Expand All @@ -459,10 +459,6 @@ fn get_metadata(env: &mut Env, mod_id_ofs: usize) -> Option<&ContractMetadata> {
mod_id
});

if env.instance(&mod_id).is_none() {
let _ = env.create_instance(mod_id);
}

env.contract_metadata(&mod_id)
}
}
Expand Down Expand Up @@ -544,14 +540,14 @@ fn free_price_hint(
}
}

fn self_id(fenv: Caller<Env>) {
let env = fenv.data();
let self_id = env.self_contract_id();
fn self_id(mut fenv: Caller<Env>) {
let env = fenv.data_mut();
let self_id = env.self_contract_id().to_owned();
let contract_metadata = env
.contract_metadata(self_id)
.contract_metadata(&self_id)
.expect("contract metadata should exist");
let slice = contract_metadata.contract_id.as_bytes();
let slice = contract_metadata.contract_id.to_bytes();
let len = slice.len();
env.self_instance()
.with_arg_buf_mut(|arg| arg[..len].copy_from_slice(slice));
.with_arg_buf_mut(|arg| arg[..len].copy_from_slice(&slice));
}
2 changes: 1 addition & 1 deletion piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ impl Session {
}

pub fn contract_metadata(
&self,
&mut self,
contract_id: &ContractId,
) -> Option<&ContractMetadata> {
self.inner.contract_session.contract_metadata(contract_id)
Expand Down
3 changes: 2 additions & 1 deletion piecrust/src/store/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,10 @@ impl ContractSession {

/// Provides metadata of the contract with a given `contract_id`.
pub fn contract_metadata(
&self,
&mut self,
contract_id: &ContractId,
) -> Option<&ContractMetadata> {
let _ = self.contract(*contract_id);
self.contracts
.get(contract_id)
.map(|store_data| store_data.metadata.data())
Expand Down

0 comments on commit 76e0917

Please sign in to comment.