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
1 change: 1 addition & 0 deletions src/commands/start/endorsement/endorsement_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl Step for EndorsementStep {

// Get all endorsed provider IDs in one call
let params = GetProviderIdsParams {
run_id: context.run_id().to_string(),
endorsements_contract_address: endorsements_address,
lotus_rpc_url,
};
Expand Down
53 changes: 24 additions & 29 deletions src/commands/start/endorsement/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::constants::{
use crate::commands::start::step::SetupContext;
use crate::constants::BUILDER_DOCKER_IMAGE;
use crate::docker::command_logger::run_and_log_command_strings;
use crate::docker::shell::ephemeral_run_args;
use std::error::Error;
use std::thread;
use std::time::Duration;
Expand Down Expand Up @@ -74,7 +75,13 @@ pub fn endorse_provider(
info!("Endorsement transaction: {}", tx_hash);
thread::sleep(Duration::from_secs(ENDORSEMENT_TX_WAIT_SECS));

verify_transaction_status(&params.lotus_rpc_url, &tx_hash, params.provider_id, context)?;
verify_transaction_status(
&params.run_id,
&params.lotus_rpc_url,
&tx_hash,
params.provider_id,
context,
)?;

info!("{} endorsed successfully", label);

Expand All @@ -96,28 +103,22 @@ fn extract_tx_hash(output: &str) -> Option<String> {

/// Verify that the endorsement transaction succeeded.
fn verify_transaction_status(
run_id: &str,
rpc_url: &str,
tx_hash: &str,
provider_id: u64,
context: &SetupContext,
) -> Result<(), Box<dyn Error>> {
let container_name = format!("foc-verify-endorse-{}", provider_id);
let container_name = format!("foc-verify-endorse-{}-{}", run_id, provider_id);

let cast_cmd = format!(r#"cast receipt {} --rpc-url {} --json"#, tx_hash, rpc_url);

let args: Vec<String> = vec![
"run".to_string(),
"--name".to_string(),
container_name.clone(),
"-u".to_string(),
"foc-user".to_string(),
"--network".to_string(),
"host".to_string(),
BUILDER_DOCKER_IMAGE.to_string(),
"bash".to_string(),
"-c".to_string(),
cast_cmd,
];
let mut args = ephemeral_run_args(&container_name, "foc-user");
args.extend(
[BUILDER_DOCKER_IMAGE, "bash", "-c", &cast_cmd]
.into_iter()
.map(str::to_string),
);

let key = format!("pdp_verify_endorse_tx_{}", provider_id);
let output = run_and_log_command_strings("docker", &args, context, &key)?;
Expand Down Expand Up @@ -146,6 +147,7 @@ fn verify_transaction_status(

/// Parameters for getting all provider IDs
pub struct GetProviderIdsParams {
pub run_id: String,
pub endorsements_contract_address: String,
pub lotus_rpc_url: String,
}
Expand All @@ -156,26 +158,19 @@ pub fn get_all_provider_ids(
params: GetProviderIdsParams,
context: &SetupContext,
) -> Result<Vec<u64>, Box<dyn Error>> {
let container_name = "foc-get-provider-ids";
let container_name = format!("foc-get-provider-ids-{}", params.run_id);

let cast_cmd = format!(
r#"cast call {} "getProviderIds()(uint256[])" --rpc-url {}"#,
params.endorsements_contract_address, params.lotus_rpc_url
);

let args: Vec<String> = vec![
"run".to_string(),
"--name".to_string(),
container_name.to_string(),
"-u".to_string(),
"foc-user".to_string(),
"--network".to_string(),
"host".to_string(),
BUILDER_DOCKER_IMAGE.to_string(),
"bash".to_string(),
"-c".to_string(),
cast_cmd,
];
let mut args = ephemeral_run_args(&container_name, "foc-user");
args.extend(
[BUILDER_DOCKER_IMAGE, "bash", "-c", &cast_cmd]
.into_iter()
.map(str::to_string),
);

let key = "pdp_get_all_provider_ids";
let output = run_and_log_command_strings("docker", &args, context, key)?;
Expand Down
75 changes: 33 additions & 42 deletions src/commands/start/pdp_service_provider/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::constants::*;
use crate::commands::start::step::SetupContext;
use crate::constants::BUILDER_DOCKER_IMAGE;
use crate::docker::command_logger::run_and_log_command_strings;
use crate::docker::shell::ephemeral_run_args;
use crate::utils::retry::{retry_with_fixed_delay, DEFAULT_MAX_RETRIES, DEFAULT_RETRY_DELAY_SECS};
use std::error::Error;

Expand Down Expand Up @@ -321,22 +322,20 @@ pub fn verify_provider_count(
|| {
let container_name = format!("foc-{}-pdp-verify-count", run_id);

let args: Vec<String> = vec![
"run".to_string(),
"--name".to_string(),
container_name,
"-u".to_string(),
"foc-user".to_string(),
"--network".to_string(),
"host".to_string(),
BUILDER_DOCKER_IMAGE.to_string(),
"cast".to_string(),
"call".to_string(),
registry_address.to_string(),
"getProviderCount()(uint256)".to_string(),
"--rpc-url".to_string(),
lotus_rpc_url.to_string(),
];
let mut args = ephemeral_run_args(&container_name, "foc-user");
args.extend(
[
BUILDER_DOCKER_IMAGE,
"cast",
"call",
registry_address,
"getProviderCount()(uint256)",
"--rpc-url",
lotus_rpc_url,
]
.into_iter()
.map(str::to_string),
);

let key = "pdp_verify_provider_count".to_string();
let output = run_and_log_command_strings("docker", &args, context, &key)?;
Expand Down Expand Up @@ -371,23 +370,21 @@ pub fn verify_provider_id_by_address(
|| {
let container_name = format!("foc-{}-pdp-verify-id-{}", run_id, provider_address);

let args: Vec<String> = vec![
"run".to_string(),
"--name".to_string(),
container_name,
"-u".to_string(),
"foc-user".to_string(),
"--network".to_string(),
"host".to_string(),
BUILDER_DOCKER_IMAGE.to_string(),
"cast".to_string(),
"call".to_string(),
registry_address.to_string(),
"getProviderIdByAddress(address)(uint256)".to_string(),
provider_address.to_string(),
"--rpc-url".to_string(),
lotus_rpc_url.to_string(),
];
let mut args = ephemeral_run_args(&container_name, "foc-user");
args.extend(
[
BUILDER_DOCKER_IMAGE,
"cast",
"call",
registry_address,
"getProviderIdByAddress(address)(uint256)",
provider_address,
"--rpc-url",
lotus_rpc_url,
]
.into_iter()
.map(str::to_string),
);

let key = format!("pdp_verify_provider_id_{}", provider_address);
let output = run_and_log_command_strings("docker", &args, context, &key)?;
Expand Down Expand Up @@ -424,14 +421,8 @@ pub fn verify_approved_provider(
// Use isProviderApproved function on StateView contract
let provider_id_str = provider_id.to_string();
let container_name = format!("foc-{}-pdp-verify-approved-{}", run_id, provider_id);
let args: Vec<String> = vec![
"run".to_string(),
"--name".to_string(),
container_name,
"-u".to_string(),
"foc-user".to_string(),
"--network".to_string(),
"host".to_string(),
let mut args = ephemeral_run_args(&container_name, "foc-user");
args.extend([
BUILDER_DOCKER_IMAGE.to_string(),
"cast".to_string(),
"call".to_string(),
Expand All @@ -440,7 +431,7 @@ pub fn verify_approved_provider(
provider_id_str,
"--rpc-url".to_string(),
lotus_rpc_url.to_string(),
];
]);

let key = format!("pdp_verify_approved_provider_{}", provider_id);
let output = run_and_log_command_strings("docker", &args, context, &key)?;
Expand Down
29 changes: 12 additions & 17 deletions src/commands/start/usdfc_deploy/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use crate::commands::start::step::SetupContext;
use crate::docker::bind_mount;
use crate::docker::command_logger::run_and_log_command;
use crate::docker::command_logger::run_and_log_command_strings;
use crate::docker::shell::ephemeral_run_args;
use crate::utils::retry::{retry_with_fixed_delay, DEFAULT_MAX_RETRIES, DEFAULT_RETRY_DELAY_SECS};
use std::error::Error;
use std::path::Path;
Expand Down Expand Up @@ -46,26 +47,20 @@ pub fn verify_mock_usdfc(

let key = format!("usdfc_verify_{}", run_id);
let container_name = format!("foc-{}-usdfc-verify", run_id);
let output = run_and_log_command(
"docker",
&[
"run",
"--name",
&container_name,
"-u",
"foc-user",
"--network",
"host",
"--mount",
&contract_mount,
let mut args = ephemeral_run_args(&container_name, "foc-user");
args.push("--mount".to_string());
args.push(contract_mount.clone());
args.extend(
[
crate::constants::BUILDER_DOCKER_IMAGE,
"bash",
"-c",
&verify_cmd,
],
context,
&key,
)?;
]
.into_iter()
.map(str::to_string),
);
let output = run_and_log_command_strings("docker", &args, context, &key)?;

let _stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
Expand Down
54 changes: 54 additions & 0 deletions src/docker/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ pub fn lotus_get_eth_address(f4_address: &str) -> Result<String, Box<dyn Error>>
Ok(eth_addr)
}

/// Arguments for a short-lived container that removes itself on exit.
///
/// Read-only checks that run inside retry loops must use this prefix: a failed
/// attempt would otherwise leave a stopped container behind, and the next
/// attempt with the same `--name` fails instantly with "container name is
/// already in use". Transaction containers deliberately keep theirs for log
/// inspection after a failure.
pub fn ephemeral_run_args(name: &str, user: &str) -> Vec<String> {
vec![
"run".to_string(),
"--rm".to_string(),
"--name".to_string(),
name.to_string(),
"-u".to_string(),
user.to_string(),
"--network".to_string(),
"host".to_string(),
]
}

/// Run a docker container with host networking.
/// Note: Does not use --rm flag to allow log inspection after container exits.
pub fn docker_run_host_network(image: &str, args: &[&str]) -> Result<Output, Box<dyn Error>> {
Expand Down Expand Up @@ -153,3 +173,37 @@ pub fn forge_script_deploy(
args.extend_from_slice(extra_args);
forge_command(&args)
}

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

#[test]
fn test_ephemeral_run_args_remove_container_on_exit() {
let args = ephemeral_run_args("foc-test-verify", "foc-user");

assert_eq!(args.first().map(String::as_str), Some("run"));
assert!(
args.iter().any(|arg| arg == "--rm"),
"ephemeral containers must remove themselves on exit"
);
assert_eq!(
args.iter()
.position(|arg| arg == "--name")
.map(|i| args[i + 1].as_str()),
Some("foc-test-verify")
);
assert_eq!(
args.iter()
.position(|arg| arg == "-u")
.map(|i| args[i + 1].as_str()),
Some("foc-user")
);
assert_eq!(
args.iter()
.position(|arg| arg == "--network")
.map(|i| args[i + 1].as_str()),
Some("host")
);
}
}