Improve nonce-related error messages

This commit is contained in:
Michael Vines 2022-01-14 10:03:29 -08:00
parent 871fd291f3
commit e14ae33e86
2 changed files with 33 additions and 11 deletions

View File

@ -334,9 +334,17 @@ pub fn check_nonce_account(
match state_from_account(nonce_account)? { match state_from_account(nonce_account)? {
State::Initialized(ref data) => { State::Initialized(ref data) => {
if &data.blockhash != nonce_hash { if &data.blockhash != nonce_hash {
Err(Error::InvalidHash.into()) Err(Error::InvalidHash {
provided: *nonce_hash,
expected: data.blockhash,
}
.into())
} else if nonce_authority != &data.authority { } else if nonce_authority != &data.authority {
Err(Error::InvalidAuthority.into()) Err(Error::InvalidAuthority {
provided: *nonce_authority,
expected: data.authority,
}
.into())
} else { } else {
Ok(()) Ok(())
} }
@ -946,15 +954,22 @@ mod tests {
hash(b"invalid"), hash(b"invalid"),
0, 0,
))); )));
let invalid_hash = Account::new_data(1, &data, &system_program::ID); let invalid_hash = Account::new_data(1, &data, &system_program::ID).unwrap();
if let CliError::InvalidNonce(err) = if let CliError::InvalidNonce(err) =
check_nonce_account(&invalid_hash.unwrap(), &nonce_pubkey, &blockhash).unwrap_err() check_nonce_account(&invalid_hash, &nonce_pubkey, &blockhash).unwrap_err()
{ {
assert_eq!(err, Error::InvalidHash,); assert_eq!(
err,
Error::InvalidHash {
provided: blockhash,
expected: hash(b"invalid"),
}
);
} }
let new_nonce_authority = solana_sdk::pubkey::new_rand();
let data = Versions::new_current(State::Initialized(nonce::state::Data::new( let data = Versions::new_current(State::Initialized(nonce::state::Data::new(
solana_sdk::pubkey::new_rand(), new_nonce_authority,
blockhash, blockhash,
0, 0,
))); )));
@ -962,7 +977,13 @@ mod tests {
if let CliError::InvalidNonce(err) = if let CliError::InvalidNonce(err) =
check_nonce_account(&invalid_authority.unwrap(), &nonce_pubkey, &blockhash).unwrap_err() check_nonce_account(&invalid_authority.unwrap(), &nonce_pubkey, &blockhash).unwrap_err()
{ {
assert_eq!(err, Error::InvalidAuthority,); assert_eq!(
err,
Error::InvalidAuthority {
provided: nonce_pubkey,
expected: new_nonce_authority,
}
);
} }
let data = Versions::new_current(State::Uninitialized); let data = Versions::new_current(State::Uninitialized);

View File

@ -4,6 +4,7 @@ use {
account::{Account, ReadableAccount}, account::{Account, ReadableAccount},
account_utils::StateMut, account_utils::StateMut,
commitment_config::CommitmentConfig, commitment_config::CommitmentConfig,
hash::Hash,
nonce::{ nonce::{
state::{Data, Versions}, state::{Data, Versions},
State, State,
@ -21,10 +22,10 @@ pub enum Error {
InvalidAccountData, InvalidAccountData,
#[error("unexpected account data size")] #[error("unexpected account data size")]
UnexpectedDataSize, UnexpectedDataSize,
#[error("query hash does not match stored hash")] #[error("provided hash ({provided}) does not match nonce hash ({expected})")]
InvalidHash, InvalidHash { provided: Hash, expected: Hash },
#[error("query authority does not match account authority")] #[error("provided authority ({provided}) does not match nonce authority ({expected})")]
InvalidAuthority, InvalidAuthority { provided: Pubkey, expected: Pubkey },
#[error("invalid state for requested operation")] #[error("invalid state for requested operation")]
InvalidStateForOperation, InvalidStateForOperation,
#[error("client error: {0}")] #[error("client error: {0}")]