Skip to content

Commit

Permalink
Delete deprecated UseBytes and RestoreBytes methods from verified reg…
Browse files Browse the repository at this point in the history
  • Loading branch information
anorth authored and shamb0 committed Jan 31, 2023
1 parent a3cdc7f commit 572ef21
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 227 deletions.
103 changes: 2 additions & 101 deletions actors/verifreg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub enum Method {
AddVerifier = 2,
RemoveVerifier = 3,
AddVerifiedClient = 4,
UseBytes = 5, // Deprecated
RestoreBytes = 6, // Deprecated
// UseBytes = 5, // Deprecated
// RestoreBytes = 6, // Deprecated
RemoveVerifiedClientDataCap = 7,
RemoveExpiredAllocations = 8,
ClaimAllocations = 9,
Expand Down Expand Up @@ -229,97 +229,6 @@ impl Actor {
Ok(())
}

/// Called by StorageMarketActor during PublishStorageDeals.
/// Do not allow partially verified deals (DealSize must be greater than equal to allowed cap).
/// Delete VerifiedClient if remaining DataCap is smaller than minimum VerifiedDealSize.
pub fn use_bytes<BS, RT>(rt: &mut RT, params: UseBytesParams) -> Result<(), ActorError>
where
BS: Blockstore,
RT: Runtime<BS>,
{
rt.validate_immediate_caller_is(std::iter::once(&*STORAGE_MARKET_ACTOR_ADDR))?;

let client = resolve_to_actor_id(rt, &params.address).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
format!("failed to resolve addr {} to ID", params.address),
)
})?;

let client = Address::new_id(client);

if params.deal_size < rt.policy().minimum_verified_allocation_size {
return Err(actor_error!(
illegal_argument,
"use bytes {} is below minimum {}",
params.deal_size,
rt.policy().minimum_verified_allocation_size
));
}

// Deduct from client's token allowance.
let remaining = destroy(rt, &client, &params.deal_size).context(format!(
"failed to deduct {} from allowance for {}",
&params.deal_size, &client
))?;

// Destroy any remaining balance below minimum verified deal size.
if remaining.is_positive() && remaining < rt.policy().minimum_verified_allocation_size {
destroy(rt, &client, &remaining).context(format!(
"failed to destroy remaining {} from allowance for {}",
&remaining, &client
))?;
}
Ok(())
}

/// Called by HandleInitTimeoutDeals from StorageMarketActor when a VerifiedDeal fails to init.
/// Restore allowable cap for the client, creating new entry if the client has been deleted.
pub fn restore_bytes<BS, RT>(rt: &mut RT, params: RestoreBytesParams) -> Result<(), ActorError>
where
BS: Blockstore,
RT: Runtime<BS>,
{
rt.validate_immediate_caller_is(std::iter::once(&*STORAGE_MARKET_ACTOR_ADDR))?;
if params.deal_size < rt.policy().minimum_verified_allocation_size {
return Err(actor_error!(
illegal_argument,
"Below minimum VerifiedDealSize requested in RestoreBytes: {}",
params.deal_size
));
}

let client = resolve_to_actor_id(rt, &params.address).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
format!("failed to resolve addr {} to ID", params.address),
)
})?;

let client = Address::new_id(client);

let st: State = rt.state()?;
// Disallow root as a client.
if client == st.root_key {
return Err(actor_error!(illegal_argument, "cannot restore allowance for root"));
}

// Disallow existing verifiers as clients.
if st.get_verifier_cap(rt.store(), &client)?.is_some() {
return Err(actor_error!(
illegal_argument,
"cannot restore allowance for verifier {}",
client
));
}

let operators = vec![*STORAGE_MARKET_ACTOR_ADDR];
mint(rt, &client, &params.deal_size, operators).context(format!(
"failed to restore {} to allowance for {}",
&params.deal_size, &client
))
}

/// Removes DataCap allocated to a verified client.
pub fn remove_verified_client_data_cap<BS, RT>(
rt: &mut RT,
Expand Down Expand Up @@ -1067,14 +976,6 @@ impl ActorCode for Actor {
Self::add_verified_client(rt, cbor::deserialize_params(params)?)?;
Ok(RawBytes::default())
}
Some(Method::UseBytes) => {
Self::use_bytes(rt, cbor::deserialize_params(params)?)?;
Ok(RawBytes::default())
}
Some(Method::RestoreBytes) => {
Self::restore_bytes(rt, cbor::deserialize_params(params)?)?;
Ok(RawBytes::default())
}
Some(Method::RemoveVerifiedClientDataCap) => {
let res =
Self::remove_verified_client_data_cap(rt, cbor::deserialize_params(params)?)?;
Expand Down
12 changes: 0 additions & 12 deletions actors/verifreg/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ pub type AddVerifierClientParams = VerifierParams;
/// We can introduce policy changes and replace this in the future.
pub type DataCap = StoragePower;

#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
pub struct BytesParams {
/// Address of verified client.
pub address: Address,
/// Number of bytes to use.
#[serde(with = "bigint_ser")]
pub deal_size: StoragePower,
}

pub type UseBytesParams = BytesParams;
pub type RestoreBytesParams = BytesParams;

pub const SIGNATURE_DOMAIN_SEPARATION_REMOVE_DATA_CAP: &[u8] = b"fil_removedatacap:";

impl Cbor for RemoveDataCapParams {}
Expand Down
37 changes: 1 addition & 36 deletions actors/verifreg/tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use fil_actor_verifreg::{
AllocationID, AllocationRequest, AllocationRequests, AllocationsResponse,
ClaimAllocationsParams, ClaimAllocationsReturn, ClaimID, DataCap, GetClaimsParams,
GetClaimsReturn, Method, RemoveExpiredAllocationsParams, RemoveExpiredAllocationsReturn,
RestoreBytesParams, SectorAllocationClaim, State,
SectorAllocationClaim, State,
};
use fil_actors_runtime::cbor::serialize;
use fil_actors_runtime::runtime::policy_constants::{
Expand Down Expand Up @@ -200,41 +200,6 @@ impl Harness {
Ok(())
}

pub fn restore_bytes(
&self,
rt: &mut MockRuntime,
client: &Address,
amount: &DataCap,
) -> Result<(), ActorError> {
rt.expect_validate_caller_addr(vec![*STORAGE_MARKET_ACTOR_ADDR]);
rt.set_caller(*MARKET_ACTOR_CODE_ID, *STORAGE_MARKET_ACTOR_ADDR);
let client_resolved = rt.get_id_address(client).unwrap_or(*client);

// Expect tokens to be minted.
let mint_params = ext::datacap::MintParams {
to: client_resolved,
amount: TokenAmount::from_whole(amount.to_i64().unwrap()),
operators: vec![*STORAGE_MARKET_ACTOR_ADDR],
};
rt.expect_send(
*DATACAP_TOKEN_ACTOR_ADDR,
ext::datacap::Method::Mint as MethodNum,
RawBytes::serialize(&mint_params).unwrap(),
TokenAmount::zero(),
RawBytes::default(),
ExitCode::OK,
);

let params = RestoreBytesParams { address: *client, deal_size: amount.clone() };
let ret = rt.call::<VerifregActor>(
Method::RestoreBytes as MethodNum,
&RawBytes::serialize(params).unwrap(),
)?;
assert_eq!(RawBytes::default(), ret);
rt.verify();
Ok(())
}

pub fn check_state(&self, rt: &MockRuntime) {
let (_, acc) = check_state_invariants(&rt.get_state(), rt.store());
acc.assert_empty();
Expand Down
80 changes: 2 additions & 78 deletions actors/verifreg/tests/verifreg_actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,20 +552,16 @@ mod datacap {
use fvm_shared::error::ExitCode;
use fvm_shared::{ActorID, MethodNum};

use fil_actor_verifreg::{Actor as VerifregActor, Method, RestoreBytesParams, State};
use fil_actor_verifreg::{Actor as VerifregActor, Method, State};
use fil_actors_runtime::cbor::serialize;
use fil_actors_runtime::runtime::policy_constants::{
MAXIMUM_VERIFIED_ALLOCATION_EXPIRATION, MAXIMUM_VERIFIED_ALLOCATION_TERM,
MINIMUM_VERIFIED_ALLOCATION_SIZE, MINIMUM_VERIFIED_ALLOCATION_TERM,
};
use fil_actors_runtime::runtime::Runtime;
use fil_actors_runtime::test_utils::*;
use fil_actors_runtime::{
DATACAP_TOKEN_ACTOR_ADDR, EPOCHS_IN_YEAR, STORAGE_MARKET_ACTOR_ADDR,
STORAGE_POWER_ACTOR_ADDR,
};
use fil_actors_runtime::{DATACAP_TOKEN_ACTOR_ADDR, EPOCHS_IN_YEAR, STORAGE_MARKET_ACTOR_ADDR};
use harness::*;
use util::*;

use crate::*;

Expand Down Expand Up @@ -790,76 +786,4 @@ mod datacap {
}
h.check_state(&rt);
}

#[test]
fn restore() {
let (h, mut rt) = new_harness();
let deal_size = &rt.policy.minimum_verified_allocation_size.clone();
h.restore_bytes(&mut rt, &CLIENT, deal_size).unwrap();
h.check_state(&rt);
}

#[test]
fn restore_resolves_client_address() {
let (h, mut rt) = new_harness();
let client_pubkey = Address::new_secp256k1(&[3u8; 65]).unwrap();
rt.id_addresses.insert(client_pubkey, *CLIENT);

// Restore to pubkey address.
let deal_size = rt.policy.minimum_verified_allocation_size.clone();
h.restore_bytes(&mut rt, &client_pubkey, &deal_size).unwrap();
h.check_state(&rt)
}

#[test]
fn restore_requires_market_actor_caller() {
let (h, mut rt) = new_harness();
rt.expect_validate_caller_addr(vec![*STORAGE_MARKET_ACTOR_ADDR]);
rt.set_caller(*POWER_ACTOR_CODE_ID, *STORAGE_POWER_ACTOR_ADDR);
let params = RestoreBytesParams {
address: *CLIENT,
deal_size: rt.policy.minimum_verified_allocation_size.clone(),
};
expect_abort(
ExitCode::USR_FORBIDDEN,
rt.call::<VerifregActor>(
Method::RestoreBytes as MethodNum,
&RawBytes::serialize(params).unwrap(),
),
);
h.check_state(&rt)
}

#[test]
fn restore_requires_minimum_deal_size() {
let (h, mut rt) = new_harness();

let deal_size = rt.policy.minimum_verified_allocation_size.clone() - 1;
expect_abort(ExitCode::USR_ILLEGAL_ARGUMENT, h.restore_bytes(&mut rt, &CLIENT, &deal_size));
h.check_state(&rt)
}

#[test]
fn restore_rejects_root() {
let (h, mut rt) = new_harness();
let deal_size = rt.policy.minimum_verified_allocation_size.clone();
expect_abort(
ExitCode::USR_ILLEGAL_ARGUMENT,
h.restore_bytes(&mut rt, &ROOT_ADDR, &deal_size),
);
h.check_state(&rt)
}

#[test]
fn restore_rejects_verifier() {
let (h, mut rt) = new_harness();
let allowance = verifier_allowance(&rt);
h.add_verifier(&mut rt, &VERIFIER, &allowance).unwrap();
let deal_size = rt.policy.minimum_verified_allocation_size.clone();
expect_abort(
ExitCode::USR_ILLEGAL_ARGUMENT,
h.restore_bytes(&mut rt, &VERIFIER, &deal_size),
);
h.check_state(&rt)
}
}

0 comments on commit 572ef21

Please sign in to comment.