Skip to content

Commit

Permalink
feat: runtime: Add read_only getter
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Jan 29, 2023
1 parent 0ea75b1 commit 532a384
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions runtime/src/runtime/fvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ where
fn base_fee(&self) -> TokenAmount {
fvm::network::base_fee()
}

fn read_only(&self) -> bool {
fvm::vm::read_only()
}
}

impl<B> Primitives for FvmRuntime<B>
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ pub trait Runtime: Primitives + Verifier + RuntimePolicy {

/// Returns the gas base fee (cost per unit) for the current epoch.
fn base_fee(&self) -> TokenAmount;

/// Returns true if the system is in read-only mode.
fn read_only(&self) -> bool;
}

/// Message information available to the actor about executing message.
Expand Down
5 changes: 5 additions & 0 deletions runtime/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,11 @@ impl<BS: Blockstore> Runtime for MockRuntime<BS> {
fn base_fee(&self) -> TokenAmount {
self.base_fee.clone()
}

fn read_only(&self) -> bool {
// Unsupported for unit tests
false
}
}

impl<BS> Primitives for MockRuntime<BS> {
Expand Down
23 changes: 22 additions & 1 deletion test_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ impl<'bs> VM<'bs> {
policy: &Policy::default(),
subinvocations: RefCell::new(vec![]),
actor_exit: RefCell::new(None),
read_only: false,
};
let res = new_ctx.invoke_actor();

Expand Down Expand Up @@ -576,6 +577,7 @@ pub struct InvocationCtx<'invocation, 'bs> {
msg: InternalMessage,
allow_side_effects: bool,
caller_validated: bool,
read_only: bool,
policy: &'invocation Policy,
subinvocations: RefCell<Vec<InvocationTrace>>,
actor_exit: RefCell<Option<ActorExit>>,
Expand Down Expand Up @@ -613,6 +615,14 @@ impl<'invocation, 'bs> InvocationCtx<'invocation, 'bs> {
}
};

// But only if we're not in read-only mode.
if self.read_only() {
return Err(ActorError::unchecked(
ExitCode::USR_READ_ONLY,
format!("cannot create actor {target} in read-only mode"),
));
}

let mut st = self.v.get_state::<InitState>(INIT_ACTOR_ADDR).unwrap();
let (target_id, existing) = st.map_addresses_to_id(self.v.store, target, None).unwrap();
assert!(!existing, "should never have existing actor when no f4 address is specified");
Expand All @@ -638,6 +648,7 @@ impl<'invocation, 'bs> InvocationCtx<'invocation, 'bs> {
policy: self.policy,
subinvocations: RefCell::new(vec![]),
actor_exit: RefCell::new(None),
read_only: false,
};
if is_account {
new_ctx.create_actor(*ACCOUNT_ACTOR_CODE_ID, target_id, None).unwrap();
Expand Down Expand Up @@ -892,8 +903,13 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
params: Option<IpldBlock>,
value: TokenAmount,
_gas_limit: Option<u64>,
_send_flags: SendFlags,
mut send_flags: SendFlags,
) -> Result<Response, ErrorNumber> {
// replicate FVM by silently propagating read only flag to subcalls
if self.read_only() {
send_flags.set(SendFlags::READ_ONLY, true)
}

if !self.allow_side_effects {
return Ok(Response { exit_code: ExitCode::SYS_ASSERTION_FAILED, return_data: None });
}
Expand All @@ -908,6 +924,7 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
policy: self.policy,
subinvocations: RefCell::new(vec![]),
actor_exit: RefCell::new(None),
read_only: send_flags.read_only(),
};
let res = new_ctx.invoke_actor();
let invoc = new_ctx.gather_trace(res.clone());
Expand Down Expand Up @@ -1031,6 +1048,10 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
fn base_fee(&self) -> TokenAmount {
TokenAmount::zero()
}

fn read_only(&self) -> bool {
self.read_only
}
}

impl Primitives for VM<'_> {
Expand Down

0 comments on commit 532a384

Please sign in to comment.