Skip to content

Commit 1ef7650

Browse files
authored
serialize system actor state as tuple (#183)
* serialize system actor state as tuple * construct proper state with empty vector for system actor constructor
1 parent 32f2acd commit 1ef7650

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

actors/system/src/lib.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright 2019-2022 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
3-
use cid::Cid;
3+
use cid::{multihash, Cid};
44
use fvm_shared::blockstore::{Blockstore, CborStore};
5+
use fvm_shared::encoding::tuple::*;
56
use fvm_shared::encoding::{Cbor, RawBytes};
7+
use fvm_shared::error::ExitCode;
68
use fvm_shared::{MethodNum, METHOD_CONSTRUCTOR};
79
use num_derive::FromPrimitive;
810
use num_traits::FromPrimitive;
9-
use serde::{Deserialize, Serialize};
1011

1112
use fil_actors_runtime::runtime::{ActorCode, Runtime};
12-
use fil_actors_runtime::{actor_error, ActorError, SYSTEM_ACTOR_ADDR};
13+
use fil_actors_runtime::{actor_error, ActorDowncast, ActorError, SYSTEM_ACTOR_ADDR};
1314

1415
#[cfg(feature = "fil-actor")]
1516
fil_actors_runtime::wasm_trampoline!(Actor);
@@ -24,11 +25,10 @@ pub enum Method {
2425
}
2526

2627
/// System actor state.
27-
#[derive(Default, Deserialize, Serialize)]
28-
#[serde(transparent)]
28+
#[derive(Default, Deserialize_tuple, Serialize_tuple)]
2929
pub struct State {
3030
// builtin actor registry: Vec<(String, Cid)>
31-
builtin_actors: Cid,
31+
pub builtin_actors: Cid,
3232
}
3333
impl Cbor for State {}
3434

@@ -56,7 +56,14 @@ impl Actor {
5656
{
5757
rt.validate_immediate_caller_is(std::iter::once(&*SYSTEM_ACTOR_ADDR))?;
5858

59-
rt.create(&State::default())?;
59+
let c = rt
60+
.store()
61+
.put_cbor(&Vec::<(String, Cid)>::new(), multihash::Code::Blake2b256)
62+
.map_err(|e| {
63+
e.downcast_default(ExitCode::ErrIllegalState, "failed to construct state")
64+
})?;
65+
66+
rt.create(&State { builtin_actors: c })?;
6067
Ok(())
6168
}
6269
}
@@ -89,7 +96,7 @@ mod tests {
8996
use fil_actors_runtime::test_utils::{MockRuntime, SYSTEM_ACTOR_CODE_ID};
9097
use fil_actors_runtime::SYSTEM_ACTOR_ADDR;
9198

92-
use crate::{Actor, Cid, Method, State};
99+
use crate::{Actor, Method, State};
93100

94101
pub fn new_runtime() -> MockRuntime {
95102
MockRuntime {
@@ -108,6 +115,7 @@ mod tests {
108115
rt.call::<Actor>(Method::Constructor as MethodNum, &RawBytes::default()).unwrap();
109116

110117
let state: State = rt.get_state().unwrap();
111-
assert_eq!(state.builtin_actors, Cid::default());
118+
let builtin_actors = state.get_builtin_actors(&rt.store).unwrap();
119+
assert!(builtin_actors.is_empty());
112120
}
113121
}

0 commit comments

Comments
 (0)