Skip to content

Commit

Permalink
pcli(integration): don't match an exact amount of delegation tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Jan 24, 2024
1 parent a36992c commit 083d02b
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions crates/bin/pcli/tests/network_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ fn transaction_sweep() {
#[ignore]
#[test]
fn delegate_and_undelegate() {
tracing_subscriber::fmt::try_init().ok();
tracing::info!("delegate_and_undelegate");
let tmpdir = load_wallet_into_tmpdir();

// Get a validator from the testnet.
Expand All @@ -246,6 +248,7 @@ fn delegate_and_undelegate() {

let mut num_attempts = 0;
loop {
tracing::info!(attempt_number = num_attempts, "attempting delegation");
// Delegate a tiny bit of penumbra to the validator.
let mut delegate_cmd = Command::cargo_bin("pcli").unwrap();
delegate_cmd
Expand All @@ -260,57 +263,89 @@ fn delegate_and_undelegate() {
])
.timeout(std::time::Duration::from_secs(TIMEOUT_COMMAND_SECONDS));
let delegation_result = delegate_cmd.assert().try_success();
tracing::info!(?delegation_result, "delegation result");

// If the undelegation command succeeded, we can exit this loop.
if delegation_result.is_ok() {
tracing::info!("delegation succeeded");
break;
} else {
tracing::info!("delegation failed");
num_attempts += 1;
if num_attempts >= max_attempts {
panic!("Exceeded max attempts for fallible command");
}
}
}

tracing::info!("check that we have some of the delegation token");
// Check we have some of the delegation token for that validator now.
let mut balance_cmd = Command::cargo_bin("pcli").unwrap();
balance_cmd
.args(["--home", tmpdir.path().to_str().unwrap(), "view", "balance"])
.timeout(std::time::Duration::from_secs(TIMEOUT_COMMAND_SECONDS));

balance_cmd
.assert()
.stdout(predicate::str::is_match(validator.as_str()).unwrap());

let balance_output = balance_cmd.output().unwrap().stdout;
let balance_output_string = String::from_utf8_lossy(&balance_output);

tracing::debug!(?balance_output_string, "balance output string");

// We successfully delegated. But since the validator exchange rates are dynamic, we
// need to pull the amount of delegation tokens we obtained so that we can later
// try to execute an undelegation (`tx undelegate <AMOUNT><DELEGATION_TOKEN_DENOM>`).
// To do this, we use a regex to extract the amount of delegation tokens we obtained:
let delegation_token_pattern = Regex::new(r"(\d+\.?\d+[a-z]?delegation_[a-zA-Z0-9]*)").unwrap();
let (delegation_token_str, [_match]) = delegation_token_pattern
.captures(&balance_output_string)
.expect("can find delegation token in balance output")
.extract();

tracing::info!("check passed, now undelegate");

// Now undelegate. We attempt `max_attempts` times in case an epoch boundary passes
// while we prepare the delegation. See issues #1522, #2047.
let mut num_attempts = 0;
loop {
let amount_to_undelegate = format!("0.99delegation_{}", validator.as_str());
tracing::info!(attempt_number = num_attempts, "attempting undelegation");
let mut undelegate_cmd = Command::cargo_bin("pcli").unwrap();
undelegate_cmd
.args([
"--home",
tmpdir.path().to_str().unwrap(),
"tx",
"undelegate",
amount_to_undelegate.as_str(),
delegation_token_str,
])
.timeout(std::time::Duration::from_secs(TIMEOUT_COMMAND_SECONDS));
let undelegation_result = undelegate_cmd.assert().try_success();

tracing::debug!(?undelegation_result, "undelegation done - output");
// If the undelegation command succeeded, we can exit this loop.
if undelegation_result.is_ok() {
break;
} else {
num_attempts += 1;
tracing::info!(
?undelegation_result,
num_attempts,
max_attempts,
"undelegation failed"
);
if num_attempts >= max_attempts {
panic!("Exceeded max attempts for fallible command");
}
}
}

tracing::info!("undelegation succeeded, wait an epoch before claiming.");

// Wait for the epoch duration.
thread::sleep(*UNBONDING_DURATION);
tracing::info!("epoch passed, claiming now");
let mut undelegate_claim_cmd = Command::cargo_bin("pcli").unwrap();
undelegate_claim_cmd
.args([
Expand All @@ -320,7 +355,9 @@ fn delegate_and_undelegate() {
"undelegate-claim",
])
.timeout(std::time::Duration::from_secs(TIMEOUT_COMMAND_SECONDS));
tracing::info!(?undelegate_claim_cmd, "claiming");
undelegate_claim_cmd.assert().success();
tracing::info!("success!");
sync(&tmpdir);
}

Expand Down

0 comments on commit 083d02b

Please sign in to comment.