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)? {
State::Initialized(ref data) => {
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 {
Err(Error::InvalidAuthority.into())
Err(Error::InvalidAuthority {
provided: *nonce_authority,
expected: data.authority,
}
.into())
} else {
Ok(())
}
@ -946,15 +954,22 @@ mod tests {
hash(b"invalid"),
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) =
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(
solana_sdk::pubkey::new_rand(),
new_nonce_authority,
blockhash,
0,
)));
@ -962,7 +977,13 @@ mod tests {
if let CliError::InvalidNonce(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);

View File

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