Skip to content

Commit

Permalink
MessageParams update and refactor (#175)
Browse files Browse the repository at this point in the history
* refactor message params, change serialization encoding format, update from changing spec

* remove now unneeded dev dependency
  • Loading branch information
austinabell authored Jan 21, 2020
1 parent 1d6dd98 commit 925d271
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 116 deletions.
9 changes: 4 additions & 5 deletions vm/actor/src/builtin/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use address::Address;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use runtime::{arg_end, ActorCode, Runtime};
use vm::{ExitCode, InvocOutput, MethodNum, MethodParams, SysCode, METHOD_CONSTRUCTOR};
use runtime::{ActorCode, Runtime};
use vm::{ExitCode, InvocOutput, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR};

/// AccountActorState includes the address for the actor
pub struct AccountActorState {
Expand Down Expand Up @@ -40,12 +40,11 @@ impl ActorCode for AccountActorCode {
&self,
rt: &RT,
method: MethodNum,
params: &MethodParams,
_params: &Serialized,
) -> InvocOutput {
match AccountMethod::from_method_num(method) {
Some(AccountMethod::Constructor) => {
// Assert no parameters passed
arg_end(params, rt);
// TODO unfinished spec
Self::constructor(rt)
}
_ => {
Expand Down
18 changes: 8 additions & 10 deletions vm/actor/src/builtin/cron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

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

use address::Address;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use runtime::{arg_end, ActorCode, Runtime};
use runtime::{ActorCode, Runtime};

/// CronActorState has no internal state
#[derive(Default)]
Expand Down Expand Up @@ -55,11 +55,11 @@ impl CronActorCode {
fn epoch_tick<RT: Runtime>(&self, rt: &RT) -> InvocOutput {
// self.entries is basically a static registry for now, loaded
// in the interpreter static registry.
for entry in self.entries.clone() {
for entry in &self.entries {
let res = rt.send_catching_errors(InvocInput {
to: entry.to_addr,
to: entry.to_addr.clone(),
method: entry.method_num,
params: MethodParams::default(),
params: Serialized::default(),
value: TokenAmount::new(0),
});
if let Err(e) = res {
Expand All @@ -76,17 +76,15 @@ impl ActorCode for CronActorCode {
&self,
rt: &RT,
method: MethodNum,
params: &MethodParams,
_params: &Serialized,
) -> InvocOutput {
match CronMethod::from_method_num(method) {
Some(CronMethod::Constructor) => {
// Assert no parameters passed
arg_end(params, rt);
// TODO unfinished spec
Self::constructor(rt)
}
Some(CronMethod::Cron) => {
// Assert no parameters passed
arg_end(params, rt);
// TODO unfinished spec
self.epoch_tick(rt)
}
_ => {
Expand Down
27 changes: 10 additions & 17 deletions vm/actor/src/builtin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

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

use address::Address;
use encoding::{from_slice, Cbor};
use encoding::Cbor;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use runtime::{arg_end, arg_pop, check_args, ActorCode, Runtime};
use runtime::{ActorCode, Runtime};
use std::collections::HashMap;

/// InitActorState is reponsible for creating
Expand Down Expand Up @@ -52,7 +52,7 @@ impl InitActorCode {

rt.success_return()
}
fn exec<RT: Runtime>(rt: &RT, _code: CodeID, _params: &MethodParams) -> InvocOutput {
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())
Expand All @@ -68,33 +68,26 @@ impl ActorCode for InitActorCode {
&self,
rt: &RT,
method: MethodNum,
params: &MethodParams,
params: &Serialized,
) -> InvocOutput {
// Create mutable copy of params for usage in functions
let params: &mut MethodParams = &mut params.clone();
let params: &mut Serialized = &mut params.clone();
match InitMethod::from_method_num(method) {
Some(InitMethod::Constructor) => {
// validate no arguments passed in
arg_end(params, rt);
// TODO unfinished spec

Self::constructor(rt)
}
Some(InitMethod::Exec) => {
// TODO deserialize CodeID on finished spec
let _ = arg_pop(params, rt);
check_args(params, rt, true);
Self::exec(rt, CodeID::Init, params)
}
Some(InitMethod::GetActorIDForAddress) => {
// Pop and unmarshall address parameter
let addr_res = from_slice(&arg_pop(params, rt).bytes());

// validate addr deserialization and parameters
check_args(params, rt, addr_res.is_ok());
arg_end(params, rt);
// Unmarshall address parameter
// TODO unfinished spec

// Errors checked, get actor by address
Self::get_actor_id_for_address(rt, addr_res.unwrap())
Self::get_actor_id_for_address(rt, Address::default())
}
_ => {
// Method number does not match available, abort in runtime
Expand Down
4 changes: 2 additions & 2 deletions vm/actor/src/builtin/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use num_traits::FromPrimitive;
use runtime::{ActorCode, Runtime};
use std::collections::HashMap;
use vm::{
ExitCode, InvocOutput, MethodNum, MethodParams, SysCode, TokenAmount, METHOD_CONSTRUCTOR,
ExitCode, InvocOutput, MethodNum, Serialized, SysCode, TokenAmount, METHOD_CONSTRUCTOR,
METHOD_PLACEHOLDER,
};

Expand Down Expand Up @@ -70,7 +70,7 @@ impl ActorCode for RewardActorCode {
&self,
rt: &RT,
method: MethodNum,
_params: &MethodParams,
_params: &Serialized,
) -> InvocOutput {
match RewardMethod::from_method_num(method) {
// TODO determine parameters for each method on finished spec
Expand Down
11 changes: 4 additions & 7 deletions vm/actor/src/builtin/storage_power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use num_bigint::BigUint;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use runtime::{arg_end, ActorCode, Runtime};
use vm::{ExitCode, InvocOutput, MethodNum, MethodParams, SysCode, METHOD_CONSTRUCTOR};
use runtime::{ActorCode, Runtime};
use vm::{ExitCode, InvocOutput, MethodNum, Serialized, SysCode, METHOD_CONSTRUCTOR};

/// State of storage power actor
pub struct StoragePowerActorState {
Expand Down Expand Up @@ -51,15 +51,12 @@ impl ActorCode for StoragePowerActorCode {
&self,
rt: &RT,
method: MethodNum,
params: &MethodParams,
_params: &Serialized,
) -> InvocOutput {
match StoragePowerMethod::from_method_num(method) {
// TODO determine parameters for each method on finished spec
Some(StoragePowerMethod::Constructor) => Self::constructor(rt),
Some(StoragePowerMethod::GetTotalStorage) => {
arg_end(params, rt);
Self::get_total_storage(rt)
}
Some(StoragePowerMethod::GetTotalStorage) => Self::get_total_storage(rt),
_ => {
rt.abort(
ExitCode::SystemErrorCode(SysCode::InvalidMethod),
Expand Down
6 changes: 3 additions & 3 deletions vm/message/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use unsigned_message::*;

use address::Address;
use num_bigint::BigUint;
use vm::{MethodNum, MethodParams, TokenAmount};
use vm::{MethodNum, Serialized, TokenAmount};

pub trait Message {
/// Returns the from address of the message
Expand All @@ -25,8 +25,8 @@ pub trait Message {
/// Returns the method number to be called
fn method_num(&self) -> &MethodNum;
/// Returns the encoded parameters for the method call
fn params(&self) -> &MethodParams;
/// Returns gas price for the message
fn params(&self) -> &Serialized;
/// gas_price returns gas price for the message
fn gas_price(&self) -> &BigUint;
/// Returns the gas limit for the message
fn gas_limit(&self) -> &BigUint;
Expand Down
4 changes: 2 additions & 2 deletions vm/message/src/signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crypto::{Error as CryptoError, Signature, Signer};
use encoding::Cbor;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use vm::{MethodNum, MethodParams, TokenAmount};
use vm::{MethodNum, Serialized, TokenAmount};

/// Represents a wrapped message with signature bytes
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Message for SignedMessage {
fn method_num(&self) -> &MethodNum {
self.message.method_num()
}
fn params(&self) -> &MethodParams {
fn params(&self) -> &Serialized {
self.message.params()
}
fn gas_price(&self) -> &BigUint {
Expand Down
10 changes: 5 additions & 5 deletions vm/message/src/unsigned_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::Message;
use crate::TokenAmount;
use crate::{MethodNum, MethodParams};
use crate::{MethodNum, Serialized};

use address::Address;
use derive_builder::Builder;
Expand All @@ -16,7 +16,7 @@ use serde::{Deserialize, Serialize};
/// Usage:
/// ```
/// use message::{UnsignedMessage, Message};
/// use vm::{TokenAmount, MethodParams, MethodNum};
/// use vm::{TokenAmount, Serialized, MethodNum};
/// use num_bigint::BigUint;
/// use address::Address;
///
Expand All @@ -27,7 +27,7 @@ use serde::{Deserialize, Serialize};
/// .sequence(0) // optional
/// .value(TokenAmount::new(0)) // optional
/// .method_num(MethodNum::default()) // optional
/// .params(MethodParams::default()) // optional
/// .params(Serialized::default()) // optional
/// .gas_limit(BigUint::default()) // optional
/// .gas_price(BigUint::default()) // optional
/// .build()
Expand All @@ -53,7 +53,7 @@ pub struct UnsignedMessage {
#[builder(default)]
method_num: MethodNum,
#[builder(default)]
params: MethodParams,
params: Serialized,
#[builder(default)]
gas_price: BigUint,
#[builder(default)]
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Message for UnsignedMessage {
fn method_num(&self) -> &MethodNum {
&self.method_num
}
fn params(&self) -> &MethodParams {
fn params(&self) -> &Serialized {
&self.params
}
fn gas_price(&self) -> &BigUint {
Expand Down
6 changes: 3 additions & 3 deletions vm/message/tests/builder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crypto::{Signature, Signer};
use message::{Message, SignedMessage, UnsignedMessage};
use num_bigint::BigUint;
use std::error::Error;
use vm::{MethodNum, MethodParams, TokenAmount};
use vm::{MethodNum, Serialized, TokenAmount};

const DUMMY_SIG: [u8; 1] = [0u8];

Expand All @@ -28,7 +28,7 @@ fn unsigned_message_builder() {
.sequence(0)
.value(TokenAmount::new(0))
.method_num(MethodNum::default())
.params(MethodParams::default())
.params(Serialized::default())
.gas_limit(BigUint::default())
.gas_price(BigUint::default())
.build()
Expand All @@ -37,7 +37,7 @@ fn unsigned_message_builder() {
assert_eq!(message.to(), &to_addr.clone());
assert_eq!(message.sequence(), 0);
assert_eq!(message.method_num(), &MethodNum::default());
assert_eq!(message.params(), &MethodParams::default());
assert_eq!(message.params(), &Serialized::default());
assert_eq!(message.value(), &TokenAmount::new(0));
assert_eq!(message.gas_price(), &BigUint::default());
assert_eq!(message.gas_limit(), &BigUint::default());
Expand Down
23 changes: 2 additions & 21 deletions vm/runtime/src/actor_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::Runtime;
use vm::{InvocOutput, MethodNum, MethodParams, Serialized};
use vm::{InvocOutput, MethodNum, Serialized};

/// Interface for invoking methods on an Actor
pub trait ActorCode {
Expand All @@ -12,25 +12,6 @@ pub trait ActorCode {
&self,
rt: &RT,
method: MethodNum,
params: &MethodParams,
params: &Serialized,
) -> InvocOutput;
}

/// This function will verify the parameters of a method invocation
pub fn check_args<RT: Runtime>(_params: &MethodParams, rt: &RT, cond: bool) {
if !cond {
rt.abort_arg();
}
// TODO assume there will be params validation on finished spec
}

/// Will return the next serialized parameter from the parameters and abort if empty
pub fn arg_pop<RT: Runtime>(params: &mut MethodParams, rt: &RT) -> Serialized {
check_args(params, rt, !params.is_empty());
params.remove(0)
}

/// Function will assert that there were no other parameters provided, and abort if so
pub fn arg_end<RT: Runtime>(params: &MethodParams, rt: &RT) {
check_args(params, rt, params.is_empty())
}
5 changes: 3 additions & 2 deletions vm/src/invoc.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

use crate::{ExitCode, MethodNum, MethodParams, TokenAmount};
use crate::{ExitCode, MethodNum, Serialized, TokenAmount};

use address::Address;

/// Input variables for actor method invocation.
pub struct InvocInput {
pub to: Address,
pub method: MethodNum,
pub params: MethodParams,
pub params: Serialized,
pub value: TokenAmount,
}

Expand Down
Loading

0 comments on commit 925d271

Please sign in to comment.