Skip to content

Commit

Permalink
Update runtime to new spec/ impl (#256)
Browse files Browse the repository at this point in the history
* Initial pass to update to new spec/ impl

* Small updates
  • Loading branch information
austinabell authored Mar 11, 2020
1 parent 0c45139 commit 17a447e
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 194 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"vm/runtime",
"vm/state_tree",
"vm/interpreter",
"vm/abi",
"node",
"node/clock",
"crypto",
Expand Down
10 changes: 6 additions & 4 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: Apache-2.0, MIT

mod errors;
mod randomness;
mod signature;
mod signer;
mod vrf;

pub use errors::*;
pub use signature::*;
pub use signer::*;
pub use vrf::*;
pub use self::errors::Error;
pub use self::randomness::DomainSeparationTag;
pub use self::signature::*;
pub use self::signer::*;
pub use self::vrf::*;
22 changes: 22 additions & 0 deletions crypto/src/randomness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

/// Specifies a domain for randomness generation.
#[derive(PartialEq, Eq, Copy, Clone, FromPrimitive, Debug, Hash)]
pub enum DomainSeparationTag {
TicketProduction = 1,
ElectionPoStChallengeSeed = 2,
WindowedPoStChallengeSeed = 3,
SealRandomness = 4,
InteractiveSealChallengeSeed = 5,
}

impl DomainSeparationTag {
/// from_byte allows generating DST from encoded byte
pub fn from_byte(b: u8) -> Option<Self> {
FromPrimitive::from_u8(b)
}
}
10 changes: 10 additions & 0 deletions vm/abi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "forest_abi"
version = "0.1.0"
authors = ["ChainSafe Systems <info@chainsafe.io>"]
edition = "2018"

[dependencies]
cid = { package = "forest_cid", path = "../../ipld/cid" }
num-traits = "0.2"
num-derive = "0.2"
8 changes: 8 additions & 0 deletions vm/abi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

mod piece;
mod sector;

pub use self::piece::*;
pub use self::sector::*;
15 changes: 15 additions & 0 deletions vm/abi/src/piece.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use cid::Cid;

type _UnpaddedPieceSize = u64;
type PaddedPieceSize = u64;

// TODO implement
pub struct PieceInfo {
/// Size in nodes. For BLS12-381 (capacity 254 bits), must be >= 16. (16 * 8 = 128)
pub size: PaddedPieceSize,
/// Content identifier for piece
pub cid: Cid,
}
32 changes: 32 additions & 0 deletions vm/abi/src/sector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

// This ordering, defines mappings to UInt in a way which MUST never change.
#[derive(PartialEq, Eq, Copy, Clone, FromPrimitive, Debug, Hash)]
pub enum RegisteredProof {
StackedDRG32GiBSeal = 1,
StackedDRG32GiBPoSt = 2,
StackedDRG2KiBSeal = 3,
StackedDRG2KiBPoSt = 4,
StackedDRG8MiBSeal = 5,
StackedDRG8MiBPoSt = 6,
StackedDRG512MiBSeal = 7,
StackedDRG512MiBPoSt = 8,
}

impl RegisteredProof {
pub fn from_byte(b: u8) -> Option<Self> {
FromPrimitive::from_u8(b)
}
}

pub struct SealVerifyInfo {
// TODO implement SealVerifyInfo
}

pub struct PoStVerifyInfo {
// TODO implement PoStVerifyInfo
}
14 changes: 4 additions & 10 deletions vm/actor/src/builtin/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use address::Address;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use runtime::{ActorCode, Runtime};
use vm::{ExitCode, InvocOutput, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR};
use vm::{ExitCode, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR};

/// AccountActorState includes the address for the actor
pub struct AccountActorState {
Expand All @@ -29,19 +29,13 @@ pub struct AccountActorCode;

impl AccountActorCode {
/// Constructor for Account actor
fn constructor<RT: Runtime>(rt: &RT) -> InvocOutput {
fn constructor<RT: Runtime>(_rt: &RT) {
// Intentionally left blank
rt.success_return()
}
}

impl ActorCode for AccountActorCode {
fn invoke_method<RT: Runtime>(
&self,
rt: &RT,
method: MethodNum,
_params: &Serialized,
) -> InvocOutput {
fn invoke_method<RT: Runtime>(&self, rt: &RT, method: MethodNum, _params: &Serialized) {
match AccountMethod::from_method_num(method) {
Some(AccountMethod::Constructor) => {
// TODO unfinished spec
Expand All @@ -50,7 +44,7 @@ impl ActorCode for AccountActorCode {
_ => {
rt.abort(
ExitCode::SystemErrorCode(SysCode::InvalidMethod),
"Invalid method",
"Invalid method".to_owned(),
);
unreachable!();
}
Expand Down
45 changes: 18 additions & 27 deletions vm/actor/src/builtin/cron.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use vm::{
ExitCode, InvocInput, InvocOutput, MethodNum, Serialized, SysCode, TokenAmount,
METHOD_CONSTRUCTOR, METHOD_CRON,
};
use vm::{ExitCode, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR, METHOD_CRON};

use address::Address;
use num_derive::FromPrimitive;
Expand Down Expand Up @@ -45,39 +42,33 @@ pub struct CronActorCode {

impl CronActorCode {
/// Constructor for Cron actor
fn constructor<RT: Runtime>(rt: &RT) -> InvocOutput {
fn constructor<RT: Runtime>(_rt: &RT) {
// Intentionally left blank
rt.success_return()
}
/// epoch_tick executes built-in periodic actions, run at every Epoch.
/// epoch_tick(r) is called after all other messages in the epoch have been applied.
/// This can be seen as an implicit last message.
fn epoch_tick<RT: Runtime>(&self, rt: &RT) -> InvocOutput {
fn epoch_tick<RT: Runtime>(&self, _rt: &RT) {
// self.entries is basically a static registry for now, loaded
// in the interpreter static registry.
for entry in &self.entries {
let res = rt.send_catching_errors(InvocInput {
to: entry.to_addr.clone(),
method: entry.method_num,
params: Serialized::default(),
value: TokenAmount::new(0),
});
if let Err(e) = res {
return e.into();
}
}

rt.success_return()
// TODO update to new spec
todo!()
// for entry in &self.entries {
// let res = rt.send_catching_errors(InvocInput {
// to: entry.to_addr.clone(),
// method: entry.method_num,
// params: Serialized::default(),
// value: TokenAmount::new(0),
// });
// if let Err(e) = res {
// return e.into();
// }
// }
}
}

impl ActorCode for CronActorCode {
fn invoke_method<RT: Runtime>(
&self,
rt: &RT,
method: MethodNum,
_params: &Serialized,
) -> InvocOutput {
fn invoke_method<RT: Runtime>(&self, rt: &RT, method: MethodNum, _params: &Serialized) {
match CronMethod::from_method_num(method) {
Some(CronMethod::Constructor) => {
// TODO unfinished spec
Expand All @@ -90,7 +81,7 @@ impl ActorCode for CronActorCode {
_ => {
rt.abort(
ExitCode::SystemErrorCode(SysCode::InvalidMethod),
"Invalid method",
"Invalid method".to_owned(),
);
unreachable!();
}
Expand Down
28 changes: 8 additions & 20 deletions vm/actor/src/builtin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::{ActorID, CodeID};
use vm::{
ExitCode, InvocOutput, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR, METHOD_PLACEHOLDER,
};
use vm::{ExitCode, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR, METHOD_PLACEHOLDER};

use address::Address;
use encoding::Cbor;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use runtime::{ActorCode, Runtime};
Expand Down Expand Up @@ -46,30 +43,21 @@ impl InitMethod {

pub struct InitActorCode;
impl InitActorCode {
fn constructor<RT: Runtime>(rt: &RT) -> InvocOutput {
fn constructor<RT: Runtime>(_rt: &RT) {
// Acquire state
// Update actor substate

rt.success_return()
}
fn exec<RT: Runtime>(rt: &RT, _code: CodeID, _params: &Serialized) -> InvocOutput {
// TODO
let addr = Address::new_id(0).unwrap();
rt.value_return(addr.marshal_cbor().unwrap())
fn exec<RT: Runtime>(_rt: &RT, _code: CodeID, _params: &Serialized) {
todo!()
}
fn get_actor_id_for_address<RT: Runtime>(rt: &RT, _address: Address) -> InvocOutput {
fn get_actor_id_for_address<RT: Runtime>(_rt: &RT, _address: Address) {
// TODO
rt.value_return(ActorID(0).marshal_cbor().unwrap())
todo!()
}
}

impl ActorCode for InitActorCode {
fn invoke_method<RT: Runtime>(
&self,
rt: &RT,
method: MethodNum,
params: &Serialized,
) -> InvocOutput {
fn invoke_method<RT: Runtime>(&self, rt: &RT, method: MethodNum, params: &Serialized) {
// Create mutable copy of params for usage in functions
let params: &mut Serialized = &mut params.clone();
match InitMethod::from_method_num(method) {
Expand All @@ -93,7 +81,7 @@ impl ActorCode for InitActorCode {
// Method number does not match available, abort in runtime
rt.abort(
ExitCode::SystemErrorCode(SysCode::InvalidMethod),
"Invalid method",
"Invalid method".to_owned(),
);
unreachable!();
}
Expand Down
18 changes: 6 additions & 12 deletions vm/actor/src/builtin/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use num_traits::FromPrimitive;
use runtime::{ActorCode, Runtime};
use std::collections::HashMap;
use vm::{
ExitCode, InvocOutput, MethodNum, Serialized, SysCode, TokenAmount, METHOD_CONSTRUCTOR,
METHOD_PLACEHOLDER,
ExitCode, MethodNum, Serialized, SysCode, TokenAmount, METHOD_CONSTRUCTOR, METHOD_PLACEHOLDER,
};

pub struct Reward {
Expand Down Expand Up @@ -49,29 +48,24 @@ impl RewardMethod {
pub struct RewardActorCode;
impl RewardActorCode {
/// Constructor for Reward actor
fn constructor<RT: Runtime>(_rt: &RT) -> InvocOutput {
fn constructor<RT: Runtime>(_rt: &RT) {
// TODO
unimplemented!();
}
/// Mints a reward and puts into state reward map
fn mint_reward<RT: Runtime>(_rt: &RT) -> InvocOutput {
fn mint_reward<RT: Runtime>(_rt: &RT) {
// TODO
unimplemented!();
}
/// Withdraw available funds from reward map
fn withdraw_reward<RT: Runtime>(_rt: &RT) -> InvocOutput {
fn withdraw_reward<RT: Runtime>(_rt: &RT) {
// TODO
unimplemented!();
}
}

impl ActorCode for RewardActorCode {
fn invoke_method<RT: Runtime>(
&self,
rt: &RT,
method: MethodNum,
_params: &Serialized,
) -> InvocOutput {
fn invoke_method<RT: Runtime>(&self, rt: &RT, method: MethodNum, _params: &Serialized) {
match RewardMethod::from_method_num(method) {
// TODO determine parameters for each method on finished spec
Some(RewardMethod::Constructor) => Self::constructor(rt),
Expand All @@ -80,7 +74,7 @@ impl ActorCode for RewardActorCode {
_ => {
rt.abort(
ExitCode::SystemErrorCode(SysCode::InvalidMethod),
"Invalid method",
"Invalid method".to_owned(),
);
unreachable!();
}
Expand Down
22 changes: 8 additions & 14 deletions vm/actor/src/builtin/storage_power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use num_bigint::BigUint;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use runtime::{ActorCode, Runtime};
use vm::{ExitCode, InvocOutput, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR};
use vm::{ExitCode, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR};

/// State of storage power actor
pub struct StoragePowerActorState {
Expand Down Expand Up @@ -34,33 +34,27 @@ impl StoragePowerMethod {
pub struct StoragePowerActorCode;
impl StoragePowerActorCode {
/// Constructor for StoragePower actor
fn constructor<RT: Runtime>(_rt: &RT) -> InvocOutput {
fn constructor<RT: Runtime>(_rt: &RT) {
// TODO
unimplemented!();
todo!();
}
/// Withdraw available funds from StoragePower map
fn get_total_storage<RT: Runtime>(rt: &RT) -> InvocOutput {
// TODO get actor state from storage and use as output
let result = BigUint::from(0 as u32).to_bytes_be();
rt.value_return(result)
fn get_total_storage<RT: Runtime>(_rt: &RT) {
// TODO
todo!()
}
}

impl ActorCode for StoragePowerActorCode {
fn invoke_method<RT: Runtime>(
&self,
rt: &RT,
method: MethodNum,
_params: &Serialized,
) -> InvocOutput {
fn invoke_method<RT: Runtime>(&self, rt: &RT, method: MethodNum, _params: &Serialized) {
match StoragePowerMethod::from_method_num(method) {
// TODO determine parameters for each method on finished spec
Some(StoragePowerMethod::Constructor) => Self::constructor(rt),
Some(StoragePowerMethod::GetTotalStorage) => Self::get_total_storage(rt),
_ => {
rt.abort(
ExitCode::SystemErrorCode(SysCode::InvalidMethod),
"Invalid method",
"Invalid method".to_owned(),
);
unreachable!();
}
Expand Down
Loading

0 comments on commit 17a447e

Please sign in to comment.