From e14ae33e86ef0be6b66da56251a0404fb9e5bdae Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Fri, 14 Jan 2022 10:03:29 -0800 Subject: [PATCH] Improve nonce-related error messages --- cli/src/nonce.rs | 35 ++++++++++++++++++++++++++++------- client/src/nonce_utils.rs | 9 +++++---- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/cli/src/nonce.rs b/cli/src/nonce.rs index 564e970847..e3092b0673 100644 --- a/cli/src/nonce.rs +++ b/cli/src/nonce.rs @@ -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); diff --git a/client/src/nonce_utils.rs b/client/src/nonce_utils.rs index de2cf41286..855e670cb5 100644 --- a/client/src/nonce_utils.rs +++ b/client/src/nonce_utils.rs @@ -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}")]