From 50a6a84e4fa9161aa3536010dd18d0e4cdcb539e Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 30 Jan 2023 18:27:35 -0500 Subject: [PATCH] refactor: Runtime: Move create and state into default impl --- runtime/src/runtime/fvm.rs | 21 --------------------- runtime/src/runtime/mod.rs | 25 ++++++++++++++++++++++--- test_vm/src/lib.rs | 33 --------------------------------- 3 files changed, 22 insertions(+), 57 deletions(-) diff --git a/runtime/src/runtime/fvm.rs b/runtime/src/runtime/fvm.rs index 3fdcc43af8..7a1043d380 100644 --- a/runtime/src/runtime/fvm.rs +++ b/runtime/src/runtime/fvm.rs @@ -235,27 +235,6 @@ where }) } - fn create(&mut self, obj: &T) -> Result<(), ActorError> { - let root = fvm::sself::root()?; - if root != EMPTY_ARR_CID { - return Err( - actor_error!(illegal_state; "failed to create state; expected empty array CID, got: {}", root), - ); - } - let new_root = ActorBlockstore.put_cbor(obj, Code::Blake2b256) - .map_err(|e| actor_error!(illegal_argument; "failed to write actor state during creation: {}", e.to_string()))?; - fvm::sself::set_root(&new_root)?; - Ok(()) - } - - fn state(&self) -> Result { - let root = fvm::sself::root()?; - Ok(ActorBlockstore - .get_cbor(&root) - .map_err(|_| actor_error!(illegal_argument; "failed to get actor for Readonly state"))? - .expect("State does not exist for actor state root")) - } - fn get_state_root(&self) -> Result { Ok(fvm::sself::root()?) } diff --git a/runtime/src/runtime/mod.rs b/runtime/src/runtime/mod.rs index 8ee1af4bbd..c122f10afc 100644 --- a/runtime/src/runtime/mod.rs +++ b/runtime/src/runtime/mod.rs @@ -3,6 +3,7 @@ use cid::Cid; use fvm_ipld_blockstore::Blockstore; +use fvm_ipld_encoding::CborStore; use fvm_shared::address::Address; use fvm_shared::clock::ChainEpoch; use fvm_shared::consensus::ConsensusFault; @@ -23,7 +24,7 @@ pub use self::actor_code::*; pub use self::policy::*; pub use self::randomness::DomainSeparationTag; use crate::runtime::builtins::Type; -use crate::ActorError; +use crate::{actor_error, ActorError}; mod actor_code; pub mod builtins; @@ -43,6 +44,7 @@ pub use empty::EMPTY_ARR_CID; use fvm_ipld_encoding::ipld_block::IpldBlock; use fvm_shared::error::ErrorNumber; use fvm_shared::sys::SendFlags; +use multihash::Code; /// Runtime is the VM's internal runtime object. /// this is everything that is accessible to actors, beyond parameters. @@ -106,10 +108,27 @@ pub trait Runtime: Primitives + Verifier + RuntimePolicy { /// Initializes the state object. /// This is only valid when the state has not yet been initialized. /// NOTE: we should also limit this to being invoked during the constructor method - fn create(&mut self, obj: &T) -> Result<(), ActorError>; + fn create(&mut self, obj: &T) -> Result<(), ActorError> { + let root = self.get_state_root()?; + if root != EMPTY_ARR_CID { + return Err( + actor_error!(illegal_state; "failed to create state; expected empty array CID, got: {}", root), + ); + } + let new_root = self.store().put_cbor(obj, Code::Blake2b256) + .map_err(|e| actor_error!(illegal_argument; "failed to write actor state during creation: {}", e.to_string()))?; + self.set_state_root(&new_root)?; + Ok(()) + } /// Loads a readonly copy of the state of the receiver into the argument. - fn state(&self) -> Result; + fn state(&self) -> Result { + Ok(self + .store() + .get_cbor(&self.get_state_root()?) + .map_err(|_| actor_error!(illegal_argument; "failed to get actor for Readonly state"))? + .expect("State does not exist for actor state root")) + } /// Gets the state-root. fn get_state_root(&self) -> Result; diff --git a/test_vm/src/lib.rs b/test_vm/src/lib.rs index 35aaed1fa0..e1d12ad03b 100644 --- a/test_vm/src/lib.rs +++ b/test_vm/src/lib.rs @@ -941,35 +941,6 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> { Ok(TEST_VM_RAND_ARRAY) } - fn create(&mut self, obj: &T) -> Result<(), ActorError> { - if self.read_only { - return Err(ActorError::unchecked( - ExitCode::USR_READ_ONLY, - "cannot create state in read-only".to_string(), - )); - } - - let maybe_act = self.v.get_actor(self.to()); - match maybe_act { - None => Err(ActorError::unchecked( - ExitCode::SYS_ASSERTION_FAILED, - "failed to create state".to_string(), - )), - Some(mut act) => { - if act.head != EMPTY_ARR_CID { - Err(ActorError::unchecked( - ExitCode::SYS_ASSERTION_FAILED, - "failed to construct state: already initialized".to_string(), - )) - } else { - act.head = self.v.store.put_cbor(obj, Code::Blake2b256).unwrap(); - self.v.set_actor(self.to(), act); - Ok(()) - } - } - } - } - fn get_state_root(&self) -> Result { Ok(self.v.get_actor(self.to()).unwrap().head) } @@ -993,10 +964,6 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> { } } - fn state(&self) -> Result { - Ok(self.v.get_state::(self.to()).unwrap()) - } - fn transaction(&mut self, f: F) -> Result where S: Serialize + DeserializeOwned,