Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 2e03106

Browse files
authored
stake-pool-cli: Sign messages before checking fee (#2759)
1 parent ba46fed commit 2e03106

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

stake-pool/cli/src/main.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use {
3030
solana_remote_wallet::remote_wallet::RemoteWalletManager,
3131
solana_sdk::{
3232
commitment_config::CommitmentConfig,
33+
hash::Hash,
3334
native_token::{self, Sol},
3435
signature::{Keypair, Signer},
3536
signers::Signers,
@@ -135,6 +136,12 @@ fn get_signer(
135136
})
136137
}
137138

139+
fn get_latest_blockhash(client: &RpcClient) -> Result<Hash, Error> {
140+
Ok(client
141+
.get_latest_blockhash_with_commitment(CommitmentConfig::confirmed())?
142+
.0)
143+
}
144+
138145
fn send_transaction_no_wait(
139146
config: &Config,
140147
transaction: Transaction,
@@ -170,7 +177,7 @@ fn checked_transaction_with_signers<T: Signers>(
170177
instructions: &[Instruction],
171178
signers: &T,
172179
) -> Result<Transaction, Error> {
173-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
180+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
174181
let transaction = Transaction::new_signed_with_payer(
175182
instructions,
176183
Some(&config.fee_payer.pubkey()),
@@ -365,27 +372,10 @@ fn command_create_pool(
365372
Some(&config.fee_payer.pubkey()),
366373
);
367374

368-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
369-
check_fee_payer_balance(
370-
config,
371-
total_rent_free_balances
372-
+ config
373-
.rpc_client
374-
.get_fee_for_message(setup_transaction.message())?
375-
+ config
376-
.rpc_client
377-
.get_fee_for_message(initialize_transaction.message())?,
378-
)?;
375+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
379376
let mut setup_signers = vec![config.fee_payer.as_ref(), &mint_keypair, &reserve_keypair];
380377
unique_signers!(setup_signers);
381378
setup_transaction.sign(&setup_signers, recent_blockhash);
382-
send_transaction(config, setup_transaction)?;
383-
384-
println!(
385-
"Creating stake pool {} with validator list {}",
386-
stake_pool_keypair.pubkey(),
387-
validator_list_keypair.pubkey()
388-
);
389379
let mut initialize_signers = vec![
390380
config.fee_payer.as_ref(),
391381
&stake_pool_keypair,
@@ -405,6 +395,23 @@ fn command_create_pool(
405395
unique_signers!(initialize_signers);
406396
initialize_transaction.sign(&initialize_signers, recent_blockhash);
407397
}
398+
check_fee_payer_balance(
399+
config,
400+
total_rent_free_balances
401+
+ config
402+
.rpc_client
403+
.get_fee_for_message(setup_transaction.message())?
404+
+ config
405+
.rpc_client
406+
.get_fee_for_message(initialize_transaction.message())?,
407+
)?;
408+
send_transaction(config, setup_transaction)?;
409+
410+
println!(
411+
"Creating stake pool {} with validator list {}",
412+
stake_pool_keypair.pubkey(),
413+
validator_list_keypair.pubkey()
414+
);
408415
send_transaction(config, initialize_transaction)?;
409416
Ok(())
410417
}
@@ -761,16 +768,16 @@ fn command_deposit_stake(
761768
let mut transaction =
762769
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));
763770

764-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
771+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
772+
unique_signers!(signers);
773+
transaction.sign(&signers, recent_blockhash);
765774
check_fee_payer_balance(
766775
config,
767776
total_rent_free_balances
768777
+ config
769778
.rpc_client
770779
.get_fee_for_message(transaction.message())?,
771780
)?;
772-
unique_signers!(signers);
773-
transaction.sign(&signers, recent_blockhash);
774781
send_transaction(config, transaction)?;
775782
Ok(())
776783
}
@@ -802,7 +809,7 @@ fn command_deposit_all_stake(
802809
&mut total_rent_free_balances,
803810
));
804811
if !create_token_account_instructions.is_empty() {
805-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
812+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
806813
let transaction = Transaction::new_signed_with_payer(
807814
&create_token_account_instructions,
808815
Some(&config.fee_payer.pubkey()),
@@ -903,7 +910,7 @@ fn command_deposit_all_stake(
903910
)
904911
};
905912

906-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
913+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
907914
let transaction = Transaction::new_signed_with_payer(
908915
&instructions,
909916
Some(&config.fee_payer.pubkey()),
@@ -1034,16 +1041,16 @@ fn command_deposit_sol(
10341041
let mut transaction =
10351042
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));
10361043

1037-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
1044+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
1045+
unique_signers!(signers);
1046+
transaction.sign(&signers, recent_blockhash);
10381047
check_fee_payer_balance(
10391048
config,
10401049
total_rent_free_balances
10411050
+ config
10421051
.rpc_client
10431052
.get_fee_for_message(transaction.message())?,
10441053
)?;
1045-
unique_signers!(signers);
1046-
transaction.sign(&signers, recent_blockhash);
10471054
send_transaction(config, transaction)?;
10481055
Ok(())
10491056
}
@@ -1495,19 +1502,19 @@ fn command_withdraw_stake(
14951502
let mut transaction =
14961503
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));
14971504

1498-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
1505+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
1506+
for new_stake_keypair in &new_stake_keypairs {
1507+
signers.push(new_stake_keypair);
1508+
}
1509+
unique_signers!(signers);
1510+
transaction.sign(&signers, recent_blockhash);
14991511
check_fee_payer_balance(
15001512
config,
15011513
total_rent_free_balances
15021514
+ config
15031515
.rpc_client
15041516
.get_fee_for_message(transaction.message())?,
15051517
)?;
1506-
for new_stake_keypair in &new_stake_keypairs {
1507-
signers.push(new_stake_keypair);
1508-
}
1509-
unique_signers!(signers);
1510-
transaction.sign(&signers, recent_blockhash);
15111518
send_transaction(config, transaction)?;
15121519
Ok(())
15131520
}
@@ -1620,15 +1627,15 @@ fn command_withdraw_sol(
16201627
let mut transaction =
16211628
Transaction::new_with_payer(&instructions, Some(&config.fee_payer.pubkey()));
16221629

1623-
let recent_blockhash = config.rpc_client.get_latest_blockhash()?;
1630+
let recent_blockhash = get_latest_blockhash(&config.rpc_client)?;
1631+
unique_signers!(signers);
1632+
transaction.sign(&signers, recent_blockhash);
16241633
check_fee_payer_balance(
16251634
config,
16261635
config
16271636
.rpc_client
16281637
.get_fee_for_message(transaction.message())?,
16291638
)?;
1630-
unique_signers!(signers);
1631-
transaction.sign(&signers, recent_blockhash);
16321639
send_transaction(config, transaction)?;
16331640
Ok(())
16341641
}

0 commit comments

Comments
 (0)