Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions src/commands/start/usdfc_deploy/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ use std::error::Error;
use std::path::PathBuf;
use tracing::{error, info};

/// Build the forge deploy command.
///
/// `INITIAL_SUPPLY` is passed explicitly from the Rust constant so the deployed
/// token always matches the supply that funding allocates against. Relying on
/// the Solidity script's fallback default would let the two values drift apart.
fn mock_usdfc_deploy_command(lotus_rpc_url: &str, private_key: &str) -> String {
format!(
"cd /workspace && \
INITIAL_SUPPLY={} \
forge script script/Deploy.s.sol:DeployMockUSDFC \
--rpc-url {} \
--private-key {} \
--broadcast \
--slow \
--gas-estimate-multiplier 10000 \
-vv",
crate::constants::MOCK_USDFC_INITIAL_SUPPLY,
lotus_rpc_url,
private_key
)
}

/// Deploy MockUSDFC using the Foundry project
pub fn deploy_mock_usdfc_foundry(
context: &SetupContext,
Expand All @@ -31,17 +53,7 @@ pub fn deploy_mock_usdfc_foundry(
// Deploy using forge script with explicit gas limit for FEVM
info!("Executing deployment script...");

let deploy_cmd = format!(
"cd /workspace && \
forge script script/Deploy.s.sol:DeployMockUSDFC \
--rpc-url {} \
--private-key {} \
--broadcast \
--slow \
--gas-estimate-multiplier 10000 \
-vv",
lotus_rpc_url, private_key
);
let deploy_cmd = mock_usdfc_deploy_command(lotus_rpc_url, private_key);

let key = format!("usdfc_deploy_{}", run_id);
let contract_mount = bind_mount(&contract_dir, "/workspace")?;
Expand Down Expand Up @@ -143,10 +155,33 @@ pub fn perform_token_deployment(
info!("✓ MockUSDFC token deployed successfully!");
info!("Token Address: {}", mock_usdfc_address);
info!(
"Initial Supply: {} tokens",
super::usdfc_deploy_step::MOCK_USDFC_INITIAL_SUPPLY
"Initial Supply: {} tokens (base units: {})",
crate::constants::MOCK_USDFC_INITIAL_SUPPLY_TOKENS,
crate::constants::MOCK_USDFC_INITIAL_SUPPLY
);
info!("Decimals: 18");

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn deploy_command_sets_initial_supply_from_shared_constant() {
let cmd = mock_usdfc_deploy_command("http://localhost:1234/rpc/v1", "0xabc");

assert!(
cmd.contains(&format!(
"INITIAL_SUPPLY={}",
crate::constants::MOCK_USDFC_INITIAL_SUPPLY
)),
"command must pass the shared supply constant: {}",
cmd
);
assert!(cmd.contains("script/Deploy.s.sol:DeployMockUSDFC"));
assert!(cmd.contains("--rpc-url http://localhost:1234/rpc/v1"));
assert!(cmd.contains("--private-key 0xabc"));
}
}
3 changes: 0 additions & 3 deletions src/commands/start/usdfc_deploy/usdfc_deploy_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use std::error::Error;
use std::path::PathBuf;
use tracing::info;

// Token configuration
pub const MOCK_USDFC_INITIAL_SUPPLY: &str = "1000000000000000000000000"; // 1 million tokens (18 decimals)

/// Step for deploying MockUSDFC token
pub struct USDFCDeployStep {
volumes_dir: PathBuf,
Expand Down
72 changes: 71 additions & 1 deletion src/commands/start/usdfc_funding/constants.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,83 @@
//! Constants for MockUSDFC token distribution.
//! Constants and allocation planning for MockUSDFC token distribution.
//!
//! This module defines constants used in the MockUSDFC funding process.

use crate::constants::{
mock_usdfc_sp_allocation_tokens, MOCK_USDFC_USER_ALLOCATION_TOKENS, USER_ACCOUNT_COUNT,
};
use ethers_core::types::U256;

/// Convert token amount to wei (multiply by 10^18)
pub fn token_amount_to_wei(tokens: u64) -> U256 {
U256::from(tokens) * U256::exp10(18)
}

/// Build the startup allocation plan as `(account name, whole tokens)` pairs.
///
/// Balance checks, transfers, and post-transfer verification all derive from
/// this plan so they cannot drift apart. The plan never exceeds the MockUSDFC
/// initial supply; see [`mock_usdfc_sp_allocation_tokens`].
pub fn distribution_plan(active_pdp_sp_count: usize) -> Vec<(String, u64)> {
let mut plan = Vec::with_capacity(USER_ACCOUNT_COUNT + active_pdp_sp_count);

for user_num in 1..=USER_ACCOUNT_COUNT {
plan.push((
format!("USER_{}", user_num),
MOCK_USDFC_USER_ALLOCATION_TOKENS,
));
}

let sp_amount = mock_usdfc_sp_allocation_tokens(active_pdp_sp_count);
for sp_num in 1..=active_pdp_sp_count {
plan.push((format!("PDP_SP_{}", sp_num), sp_amount));
}

plan
}

/// Transaction confirmation wait time in seconds
pub const TRANSACTION_CONFIRMATION_WAIT_SECS: u64 = 5;

#[cfg(test)]
mod tests {
use super::*;
use crate::constants::{
mock_usdfc_distribution_total_tokens, MAX_PDP_SP_COUNT, MOCK_USDFC_INITIAL_SUPPLY_TOKENS,
MOCK_USDFC_SP_ALLOCATION_TOKENS,
};

#[test]
fn plan_lists_every_user_and_active_sp() {
let plan = distribution_plan(2);
let names: Vec<&str> = plan.iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(
names,
["USER_1", "USER_2", "USER_3", "PDP_SP_1", "PDP_SP_2"]
);
}

#[test]
fn plan_ignores_inactive_sps() {
let plan = distribution_plan(0);
assert_eq!(plan.len(), USER_ACCOUNT_COUNT);
assert!(plan.iter().all(|(name, _)| name.starts_with("USER_")));
}

#[test]
fn plan_keeps_default_allocations() {
let plan = distribution_plan(2);
assert_eq!(plan[0].1, 100_000);
assert_eq!(plan[3].1, MOCK_USDFC_SP_ALLOCATION_TOKENS);
}

#[test]
fn plan_fits_within_supply_at_max_sp_count() {
let plan = distribution_plan(MAX_PDP_SP_COUNT);
let total: u64 = plan.iter().map(|(_, tokens)| tokens).sum();
assert!(total <= MOCK_USDFC_INITIAL_SUPPLY_TOKENS);
assert_eq!(
total,
mock_usdfc_distribution_total_tokens(MAX_PDP_SP_COUNT)
);
}
}
51 changes: 9 additions & 42 deletions src/commands/start/usdfc_funding/usdfc_funding_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
//! This module contains the main Step implementation for distributing MockUSDFC tokens
//! to user and service provider addresses.

use super::constants::{token_amount_to_wei, TRANSACTION_CONFIRMATION_WAIT_SECS};
use super::constants::{
distribution_plan, token_amount_to_wei, TRANSACTION_CONFIRMATION_WAIT_SECS,
};
use super::funding_operations::{self, check_mock_usdfc_balance, transfer_mock_usdfc};
use super::key_operations::get_user_private_key;
use crate::commands::start::step::{SetupContext, Step};
use crate::commands::start::usdfc_funding::key_operations::get_user_eth_address;
use crate::constants::USER_ACCOUNT_COUNT;
use crate::docker::containers::lotus_container_name;
use crate::docker::core::container_is_running;
use std::error::Error;
Expand Down Expand Up @@ -38,20 +39,8 @@ impl USDFCFundingStep {
mockusdfc_address: &str,
lotus_rpc_url: &str,
) -> Result<bool, Box<dyn Error>> {
// Build list of accounts to check (same as what will be transferred)
let mut accounts_to_check = Vec::new();

// Add user accounts (base-1 numbering)
for user_num in 1..=USER_ACCOUNT_COUNT {
let account_name = format!("USER_{}", user_num);
accounts_to_check.push((account_name, 100_000u64));
}

// Add only active PDP SPs (base-1 numbering)
for sp_num in 1..=self.active_pdp_sp_count {
let account_name = format!("PDP_SP_{}", sp_num);
accounts_to_check.push((account_name, 200_000u64));
}
// Build list of accounts to check (same plan as what will be transferred)
let accounts_to_check = distribution_plan(self.active_pdp_sp_count);

// Check balances for all accounts
for (account_name, amount_tokens) in accounts_to_check.iter() {
Expand Down Expand Up @@ -247,23 +236,11 @@ impl Step for USDFCFundingStep {
// Get deployer private key
let deployer_private_key = get_user_private_key("DEPLOYER_MOCKUSDFC")?;

// Build list of transfers
// Build list of transfers from the shared allocation plan
let mut token_transfers = Vec::new();

// Add user accounts (base-1 numbering)
for user_num in 1..=USER_ACCOUNT_COUNT {
let account_name = format!("USER_{}", user_num);
for (account_name, amount_tokens) in distribution_plan(self.active_pdp_sp_count) {
let eth_address = get_user_eth_address(&account_name)?;
let amount_tokens = 100_000u64;
let amount_wei = token_amount_to_wei(amount_tokens);
token_transfers.push((account_name, eth_address, amount_wei, amount_tokens));
}

// Add only active PDP SPs (base-1 numbering)
for sp_num in 1..=self.active_pdp_sp_count {
let account_name = format!("PDP_SP_{}", sp_num);
let eth_address = get_user_eth_address(&account_name)?;
let amount_tokens = 200_000u64;
let amount_wei = token_amount_to_wei(amount_tokens);
token_transfers.push((account_name, eth_address, amount_wei, amount_tokens));
}
Expand All @@ -289,18 +266,8 @@ impl Step for USDFCFundingStep {
// Get Lotus RPC URL
let lotus_rpc_url = get_lotus_rpc_url(context)?;

// Build list of accounts to verify
let mut accounts_to_verify = Vec::new();

for user_num in 1..=USER_ACCOUNT_COUNT {
let account_name = format!("USER_{}", user_num);
accounts_to_verify.push((account_name, 100_000u64));
}

for sp_num in 1..=self.active_pdp_sp_count {
let account_name = format!("PDP_SP_{}", sp_num);
accounts_to_verify.push((account_name, 200_000u64));
}
// Build list of accounts to verify (same plan as the transfers)
let accounts_to_verify = distribution_plan(self.active_pdp_sp_count);

for (account_name, amount_tokens) in accounts_to_verify.iter() {
let eth_address = get_user_eth_address(account_name)?;
Expand Down
58 changes: 58 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ impl Config {
///
/// Ensures that:
/// - ENDORSED_PDP_SP_COUNT <= APPROVED_PDP_SP_COUNT <= ACTIVE_PDP_SP_COUNT <= MAX_PDP_SP_COUNT
/// - the MockUSDFC startup distribution fits within the initial supply
pub fn validate(&self) -> Result<(), String> {
const MAX_PDP_SP_COUNT: usize = crate::constants::MAX_PDP_SP_COUNT;

Expand All @@ -335,6 +336,30 @@ impl Config {
));
}

// Funding allocates a fixed amount per user and splits what remains
// between the active PDP SPs. Reject topologies where either group can
// no longer be funded from the initial supply.
let user_allocation_total = crate::constants::USER_ACCOUNT_COUNT as u64
* crate::constants::MOCK_USDFC_USER_ALLOCATION_TOKENS;
if user_allocation_total > crate::constants::MOCK_USDFC_INITIAL_SUPPLY_TOKENS {
return Err(format!(
"USER_ACCOUNT_COUNT ({}) * MockUSDFC user allocation ({} tokens) exceeds the initial supply ({} tokens)",
crate::constants::USER_ACCOUNT_COUNT,
crate::constants::MOCK_USDFC_USER_ALLOCATION_TOKENS,
crate::constants::MOCK_USDFC_INITIAL_SUPPLY_TOKENS
));
}

if self.active_pdp_sp_count > 0
&& crate::constants::mock_usdfc_sp_allocation_tokens(self.active_pdp_sp_count) == 0
{
return Err(format!(
"active_pdp_sp_count ({}) leaves no MockUSDFC for PDP SPs after user allocations from a {} token supply",
self.active_pdp_sp_count,
crate::constants::MOCK_USDFC_INITIAL_SUPPLY_TOKENS
));
}

Ok(())
}
}
Expand Down Expand Up @@ -492,4 +517,37 @@ mod tests {
assert!(serialized.contains("pdp"));
assert!(serialized.contains("https://github.com/FilOzone/pdp.git"));
}

fn config_with_active_sps(active: usize) -> Config {
Config {
active_pdp_sp_count: active,
approved_pdp_sp_count: active,
endorsed_pdp_sp_count: active.min(1),
..Default::default()
}
}

#[test]
fn validate_accepts_every_supported_sp_count() {
for active in 0..=crate::constants::MAX_PDP_SP_COUNT {
let config = config_with_active_sps(active);
assert!(
config.validate().is_ok(),
"{} active PDP SPs should be fundable: {:?}",
active,
config.validate()
);
}
}

#[test]
fn validate_rejects_more_sps_than_supported() {
let config = config_with_active_sps(crate::constants::MAX_PDP_SP_COUNT + 1);
let err = config.validate().unwrap_err();
assert!(
err.contains("MAX_PDP_SP_COUNT"),
"unexpected error: {}",
err
);
}
}
Loading