Skip to content

Commit

Permalink
FIP-0050: API between user-programmed actors and built-in actors (#1004)
Browse files Browse the repository at this point in the history
* Proof of concept exported API for Account actor (#797)

* Export stable methods for public access (#807)

* Export Datacap Actor methods

* Export Init Actor methods

* Export Market Actor methods

* Export Miner Actor methods

* Export Multisig Actor methods

* Export Verifreg Actor methods

* Address review

* Restrict internal APIs of all actors (#809)

* Exported API method for market actor escrow/locked balance (#812)

* Power actor: Add exported getters for raw power (#810)

* Power actor: Add exported getters for raw power

* FRC-XXXX is FRC-0042

* Power actor: network_raw_power: Return this_epoch_raw_byte_power

* Power actor: miner_raw_power: Return whether above consensus min power

* Power actor: types: serialize one-element structs transparently

* Address review

* Miner actor: Add exported getters for info and monies (#811)

* Miner actor: Add exported getters for info and monies

* Tweak comment

* Miner actor: Replace GetWorker and GetControls with IsControllingAddress

* Miner actor: Add exported GetAvailableBalance

* Miner actor: Add exported GetVestingFunds

* Miner actor: Remove exported monies getters

* Miner actor: types: serialize one-element structs transparently

* Address review

* Address review

* Built-in market API for deal proposal metadata (#818)

* Call exported authenticate method from PSD (#829)


Co-authored-by: zenground0 <[email protected]>

* Drop CALLER_TYPES_SIGNABLE and signable caller validation (#821)

* Market actor: Minor improvements to two exported getters (#826)

* Market actor: GetDealTermExported: Return (start_epoch, duration)

* Market actor: Export getter for deal total price

* Exported API for market deal activation state (#819)

* Paych actor: Drop account req, use AuthenticateMessage to verify sigs (#824)

* Paych actor: Drop account req, use AuthenticateMessage to verify sigs

* Address review

* Address review

* Account actor: Deprecate AuthenticateMessage (#856)

* Market actor: Export PublishStorageDeals (#857)

* Miner: Export several more methods (#863)

* Miner: Export ChangeWorkerAddress

* Miner: Export ChangePeerID

* Miner: Export WithdrawBalance

* Miner: Export ChangeMultiaddrs

* Miner: Export ConfirmUpdateWorkerKey

* Miner: Export RepayDebt

* Miner: Export ChangeOwnerAddress

* Miner: Add exported getters for PeerID & multiaddrs

* Miner: Refactor: Rename ConfirmUpdateWorkerKey to ConfirmChangeWorkerAddress

* Power actor: Export methods to CreateMiner and get miner counts (#868)

* Power: Export CreateMiner

* Power Actor: Export MinerCount and MinerConsensusCount

* Update actors/power/src/lib.rs

Co-authored-by: Alex <[email protected]>

Co-authored-by: Alex <[email protected]>

* Verifreg: Export AddVerifiedClient and GetClaims (#873)

* Verifreg: Rename AddVerifierClientParams to AddVerifiedClientParams

* Verifreg: Export AddVerifiedClient and GetClaims

* Datacap actor: Modify exported methods (#909)

* Datacap: Export Mint and Destroy

* Datacap actor: Deprecate all internal methods

* Datacap actor: Rename BalanceOf to Balance

* Datacap actor: Add Granularity method

* fix: comments on newly exported methods (#910)

* Miner: Export method to GetPendingOwner

* MarketNotifyDeal (#944)


Co-authored-by: zenground0 <[email protected]>

* Verifreg: Call AuthenticateMessage instead of validating siggys

* Multisig: Do not export any functionality (#967)

* use const for EX_DEAL_EXPIRED (#954)

* Address review

Co-authored-by: Alex <[email protected]>
Co-authored-by: ZenGround0 <[email protected]>
Co-authored-by: zenground0 <[email protected]>
Co-authored-by: Shashank <[email protected]>
  • Loading branch information
5 people authored Jan 12, 2023
1 parent ad437f1 commit b84d38c
Show file tree
Hide file tree
Showing 74 changed files with 2,779 additions and 895 deletions.
13 changes: 9 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions actors/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use num_traits::FromPrimitive;

use fil_actors_runtime::builtin::singletons::SYSTEM_ACTOR_ADDR;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{actor_dispatch, ActorDowncast};
use fil_actors_runtime::{actor_dispatch, restrict_internal_api, ActorDowncast};
use fil_actors_runtime::{actor_error, ActorError};

use crate::types::AuthenticateMessageParams;
Expand All @@ -32,7 +32,9 @@ fil_actors_runtime::wasm_trampoline!(Actor);
pub enum Method {
Constructor = METHOD_CONSTRUCTOR,
PubkeyAddress = 2,
AuthenticateMessage = 3,
// Deprecated in v10
// AuthenticateMessage = 3,
AuthenticateMessageExported = frc42_dispatch::method_hash!("AuthenticateMessage"),
UniversalReceiverHook = frc42_dispatch::method_hash!("Receive"),
}

Expand Down Expand Up @@ -105,7 +107,7 @@ impl ActorCode for Actor {
actor_dispatch! {
Constructor => constructor,
PubkeyAddress => pubkey_address,
AuthenticateMessage => authenticate_message,
AuthenticateMessageExported => authenticate_message,
UniversalReceiverHook => universal_receiver_hook,
}
}
60 changes: 30 additions & 30 deletions actors/account/tests/account_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ use fil_actors_runtime::test_utils::*;
#[test]
fn construction() {
fn construct(addr: Address, exit_code: ExitCode) {
let mut rt = MockRuntime {
receiver: Address::new_id(100),
caller: SYSTEM_ACTOR_ADDR,
caller_type: *SYSTEM_ACTOR_CODE_ID,
..Default::default()
};
let mut rt = MockRuntime { receiver: Address::new_id(100), ..Default::default() };
rt.set_caller(*SYSTEM_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR);
rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]);

if exit_code.is_success() {
Expand Down Expand Up @@ -65,12 +61,8 @@ fn construction() {

#[test]
fn token_receiver() {
let mut rt = MockRuntime {
receiver: Address::new_id(100),
caller: SYSTEM_ACTOR_ADDR,
caller_type: *SYSTEM_ACTOR_CODE_ID,
..Default::default()
};
let mut rt = MockRuntime { receiver: Address::new_id(100), ..Default::default() };
rt.set_caller(*SYSTEM_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR);
rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]);

let param = Address::new_secp256k1(&[2; fvm_shared::address::SECP_PUB_LEN]).unwrap();
Expand All @@ -80,6 +72,7 @@ fn token_receiver() {
)
.unwrap();

rt.set_caller(make_identity_cid(b"1234"), Address::new_id(1000));
rt.expect_validate_caller_any();
let ret = rt
.call::<AccountActor>(
Expand All @@ -94,25 +87,18 @@ fn token_receiver() {
assert!(ret.is_none());
}

fn check_state(rt: &MockRuntime) {
let test_address = Address::new_id(1000);
let (_, acc) = check_state_invariants(&rt.get_state(), &test_address);
acc.assert_empty();
}

#[test]
fn authenticate_message() {
let mut rt = MockRuntime {
receiver: Address::new_id(100),
caller: SYSTEM_ACTOR_ADDR,
caller_type: *SYSTEM_ACTOR_CODE_ID,
..Default::default()
};
let mut rt = MockRuntime { receiver: Address::new_id(100), ..Default::default() };
rt.set_caller(*SYSTEM_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR);

let addr = Address::new_secp256k1(&[2; fvm_shared::address::SECP_PUB_LEN]).unwrap();
rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]);

rt.call::<AccountActor>(1, IpldBlock::serialize_cbor(&addr).unwrap()).unwrap();
rt.call::<AccountActor>(
Method::Constructor as MethodNum,
IpldBlock::serialize_cbor(&addr).unwrap(),
)
.unwrap();

let state: State = rt.get_state();
assert_eq!(state.address, addr);
Expand All @@ -123,26 +109,40 @@ fn authenticate_message() {
})
.unwrap();

// Valid signature
rt.expect_validate_caller_any();
rt.expect_verify_signature(ExpectedVerifySig {
sig: Signature::new_secp256k1(vec![]),
signer: addr,
plaintext: vec![],
result: Ok(()),
});
assert!(rt.call::<AccountActor>(3, params.clone()).unwrap().is_none());

assert!(rt
.call::<AccountActor>(Method::AuthenticateMessageExported as MethodNum, params.clone())
.unwrap()
.is_none());

rt.verify();

// Invalid signature
rt.expect_validate_caller_any();
rt.expect_verify_signature(ExpectedVerifySig {
sig: Signature::new_secp256k1(vec![]),
signer: addr,
plaintext: vec![],
result: Err(anyhow!("bad signature")),
});
assert_eq!(
expect_abort_contains_message(
ExitCode::USR_ILLEGAL_ARGUMENT,
rt.call::<AccountActor>(3, params).unwrap_err().exit_code()
"bad signature",
rt.call::<AccountActor>(Method::AuthenticateMessageExported as MethodNum, params),
);

rt.verify();
}

fn check_state(rt: &MockRuntime) {
let test_address = Address::new_id(1000);
let (_, acc) = check_state_invariants(&rt.get_state(), &test_address);
acc.assert_empty();
}
4 changes: 3 additions & 1 deletion actors/cron/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{actor_dispatch, actor_error, ActorError, SYSTEM_ACTOR_ADDR};
use fil_actors_runtime::{
actor_dispatch, actor_error, restrict_internal_api, ActorError, SYSTEM_ACTOR_ADDR,
};

use fvm_ipld_encoding::tuple::*;
use fvm_shared::econ::TokenAmount;
Expand Down
1 change: 1 addition & 0 deletions actors/cron/tests/cron_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn epoch_tick_with_entries() {
}

fn construct_and_verify(rt: &mut MockRuntime, params: &ConstructorParams) {
rt.set_caller(*SYSTEM_ACTOR_CODE_ID, SYSTEM_ACTOR_ADDR);
rt.expect_validate_caller_addr(vec![SYSTEM_ACTOR_ADDR]);
let ret = rt.call::<CronActor>(1, IpldBlock::serialize_cbor(&params).unwrap()).unwrap();
assert!(ret.is_none());
Expand Down
91 changes: 56 additions & 35 deletions actors/datacap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use num_traits::{FromPrimitive, Zero};

use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, ActorContext, ActorError, AsActorError, SYSTEM_ACTOR_ADDR,
actor_dispatch, actor_error, restrict_internal_api, ActorContext, ActorError, AsActorError,
SYSTEM_ACTOR_ADDR,
};
use fvm_ipld_encoding::ipld_block::IpldBlock;

Expand All @@ -42,29 +43,43 @@ lazy_static! {
* BigInt::from(1_000_000_000_000_000_000_000_i128)
);
}
/// Static method numbers for builtin-actor private dispatch.
/// The methods are also expected to be exposed via FRC-XXXX standard calling convention,
/// with numbers determined by name.

/// Datacap actor methods available
#[derive(FromPrimitive)]
#[repr(u64)]
pub enum Method {
Constructor = METHOD_CONSTRUCTOR,
// Non-standard.
Mint = 2,
Destroy = 3,
// Static method numbers for token standard methods, for private use.
Name = 10,
Symbol = 11,
TotalSupply = 12,
BalanceOf = 13,
Transfer = 14,
TransferFrom = 15,
IncreaseAllowance = 16,
DecreaseAllowance = 17,
RevokeAllowance = 18,
Burn = 19,
BurnFrom = 20,
Allowance = 21,
// Deprecated in v10
// Mint = 2,
// Destroy = 3,
// Name = 10,
// Symbol = 11,
// TotalSupply = 12,
// BalanceOf = 13,
// Transfer = 14,
// TransferFrom = 15,
// IncreaseAllowance = 16,
// DecreaseAllowance = 17,
// RevokeAllowance = 18,
// Burn = 19,
// BurnFrom = 20,
// Allowance = 21,
// Method numbers derived from FRC-0042 standards
MintExported = frc42_dispatch::method_hash!("Mint"),
DestroyExported = frc42_dispatch::method_hash!("Destroy"),
NameExported = frc42_dispatch::method_hash!("Name"),
SymbolExported = frc42_dispatch::method_hash!("Symbol"),
GranularityExported = frc42_dispatch::method_hash!("GranularityExported"),
TotalSupplyExported = frc42_dispatch::method_hash!("TotalSupply"),
BalanceExported = frc42_dispatch::method_hash!("Balance"),
TransferExported = frc42_dispatch::method_hash!("Transfer"),
TransferFromExported = frc42_dispatch::method_hash!("TransferFrom"),
IncreaseAllowanceExported = frc42_dispatch::method_hash!("IncreaseAllowance"),
DecreaseAllowanceExported = frc42_dispatch::method_hash!("DecreaseAllowance"),
RevokeAllowanceExported = frc42_dispatch::method_hash!("RevokeAllowance"),
BurnExported = frc42_dispatch::method_hash!("Burn"),
BurnFromExported = frc42_dispatch::method_hash!("BurnFrom"),
AllowanceExported = frc42_dispatch::method_hash!("Allowance"),
}

pub struct Actor;
Expand Down Expand Up @@ -93,6 +108,11 @@ impl Actor {
Ok("DCAP".to_string())
}

pub fn granularity(rt: &mut impl Runtime) -> Result<GranularityReturn, ActorError> {
rt.validate_immediate_caller_accept_any()?;
Ok(GranularityReturn { granularity: DATACAP_GRANULARITY })
}

pub fn total_supply(rt: &mut impl Runtime) -> Result<TokenAmount, ActorError> {
rt.validate_immediate_caller_accept_any()?;
let mut st: State = rt.state()?;
Expand All @@ -101,7 +121,7 @@ impl Actor {
Ok(token.total_supply())
}

pub fn balance_of(rt: &mut impl Runtime, params: Address) -> Result<TokenAmount, ActorError> {
pub fn balance(rt: &mut impl Runtime, params: Address) -> Result<TokenAmount, ActorError> {
// NOTE: mutability and method caller here are awkward for a read-only call
rt.validate_immediate_caller_accept_any()?;
let mut st: State = rt.state()?;
Expand Down Expand Up @@ -442,19 +462,20 @@ impl ActorCode for Actor {
type Methods = Method;
actor_dispatch! {
Constructor => constructor,
Mint => mint,
Destroy => destroy,
Name => name,
Symbol => symbol,
TotalSupply => total_supply,
BalanceOf => balance_of,
Transfer => transfer,
TransferFrom => transfer_from,
IncreaseAllowance => increase_allowance,
DecreaseAllowance => decrease_allowance,
RevokeAllowance => revoke_allowance,
Burn => burn,
BurnFrom => burn_from,
Allowance => allowance,
MintExported => mint,
DestroyExported => destroy,
NameExported => name,
SymbolExported => symbol,
GranularityExported => granularity,
TotalSupplyExported => total_supply,
BalanceExported => balance,
TransferExported => transfer,
TransferFromExported => transfer_from,
IncreaseAllowanceExported => increase_allowance,
DecreaseAllowanceExported => decrease_allowance,
RevokeAllowanceExported => revoke_allowance,
BurnExported => burn,
BurnFromExported => burn_from,
AllowanceExported => allowance,
}
}
6 changes: 6 additions & 0 deletions actors/datacap/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ pub struct DestroyParams {
pub owner: Address,
pub amount: TokenAmount,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
#[serde(transparent)]
pub struct GranularityReturn {
pub granularity: u64,
}
Loading

0 comments on commit b84d38c

Please sign in to comment.