v1.1 backport custom error rename (#9826)

* Add program_error conversions (#9203)

* Rename CustomError to Custom (#9207)

* More custom error rename (#9227)

automerge

* Remove librapay conflicts

* Fix rebase

Co-authored-by: Jack May <jack@solana.com>
This commit is contained in:
Tyera Eulberg
2020-04-30 23:54:11 -06:00
committed by GitHub
parent 3d88b9ac22
commit 3aa52f95a2
23 changed files with 123 additions and 146 deletions

View File

@@ -24,7 +24,7 @@ pub enum MyError {
}
impl From<MyError> for ProgramError {
fn from(e: MyError) -> Self {
ProgramError::CustomError(e as u32)
ProgramError::Custom(e as u32)
}
}
impl<T> DecodeError<T> for MyError {

View File

@@ -249,14 +249,14 @@ mod bpf {
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::CustomError(0))
TransactionError::InstructionError(0, InstructionError::Custom(0))
);
let instruction = Instruction::new(program_id, &4u8, account_metas.clone());
let result = bank_client.send_instruction(&mint_keypair, instruction);
assert_eq!(
result.unwrap_err().unwrap(),
TransactionError::InstructionError(0, InstructionError::CustomError(42))
TransactionError::InstructionError(0, InstructionError::Custom(42))
);
let instruction = Instruction::new(program_id, &5u8, account_metas.clone());

View File

@@ -426,7 +426,7 @@ mod tests {
.unwrap(),
TransactionError::InstructionError(
0,
InstructionError::CustomError(BudgetError::DestinationMissing as u32)
InstructionError::Custom(BudgetError::DestinationMissing as u32)
)
);
assert_eq!(bank_client.get_balance(&alice_pubkey).unwrap(), 1);

View File

@@ -13,5 +13,5 @@ fn process_instruction(
_keyed_accounts: &[KeyedAccount],
_data: &[u8],
) -> Result<(), InstructionError> {
Err(InstructionError::CustomError(0))
Err(InstructionError::Custom(0))
}

View File

@@ -23,6 +23,6 @@ fn test_program_native_failure() {
.send_instruction(&alice_keypair, instruction)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::CustomError(0))
TransactionError::InstructionError(0, InstructionError::Custom(0))
);
}

View File

@@ -2537,16 +2537,9 @@ dependencies = [
"bincode 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
<<<<<<< HEAD
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-logger 1.1.0",
"solana-sdk 1.1.0",
=======
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-sdk 1.2.0",
>>>>>>> a4fe11fad... Remove old logging enabler artifacts (#9777)
]
[[package]]
@@ -2672,10 +2665,9 @@ dependencies = [
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
<<<<<<< HEAD
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-bpf-loader-program 1.1.0",
"solana-config-program 1.1.0",
"solana-logger 1.1.0",
"solana-measure 1.1.0",
"solana-metrics 1.1.0",
@@ -2684,19 +2676,6 @@ dependencies = [
"solana-stake-program 1.1.0",
"solana-storage-program 1.1.0",
"solana-vote-program 1.1.0",
=======
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-config-program 1.2.0",
"solana-logger 1.2.0",
"solana-measure 1.2.0",
"solana-metrics 1.2.0",
"solana-rayon-threadlimit 1.2.0",
"solana-sdk 1.2.0",
"solana-stake-program 1.2.0",
"solana-storage-program 1.2.0",
"solana-vote-program 1.2.0",
>>>>>>> efad19318... Make default programs static (#9717)
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -2752,7 +2731,6 @@ dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"num-derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
<<<<<<< HEAD
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-config-program 1.1.0",
@@ -2760,14 +2738,6 @@ dependencies = [
"solana-metrics 1.1.0",
"solana-sdk 1.1.0",
"solana-vote-program 1.1.0",
=======
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-config-program 1.2.0",
"solana-metrics 1.2.0",
"solana-sdk 1.2.0",
"solana-vote-program 1.2.0",
>>>>>>> a4fe11fad... Remove old logging enabler artifacts (#9777)
"thiserror 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -2794,18 +2764,10 @@ dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"num-derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
<<<<<<< HEAD
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-logger 1.1.0",
"solana-metrics 1.1.0",
"solana-sdk 1.1.0",
=======
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"solana-metrics 1.2.0",
"solana-sdk 1.2.0",
>>>>>>> a4fe11fad... Remove old logging enabler artifacts (#9777)
"thiserror 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]

View File

@@ -161,16 +161,8 @@ mod tests {
fn create_bank(lamports: u64) -> (Arc<Bank>, Keypair, Keypair, Pubkey, Pubkey) {
let (mut genesis_config, mint) = create_genesis_config(lamports);
genesis_config.rent.lamports_per_byte_year = 0;
<<<<<<< HEAD
let mut bank = Bank::new(&genesis_config);
bank.add_instruction_processor(
solana_sdk::move_loader::id(),
MoveProcessor::process_instruction,
);
=======
let bank = Bank::new(&genesis_config);
bank.add_native_program("solana_move_loader_program", &solana_sdk::move_loader::id());
>>>>>>> efad19318... Make default programs static (#9717)
let shared_bank = Arc::new(bank);
let bank_client = BankClient::new_shared(&shared_bank);
let genesis_pubkey = create_genesis(&mint, &bank_client, 1_000_000);

View File

@@ -38,10 +38,10 @@ pub fn map_failure_error(err: failure::Error) -> InstructionError {
}
pub fn map_err_vm_status(status: VMStatus) -> InstructionError {
// Attempt to map the StatusCode (repr(u64)) to a u32 for CustomError.
// Attempt to map the StatusCode (repr(u64)) to a u32 for Custom.
// The only defined StatusCode that fails is StatusCode::UNKNOWN_ERROR
match <StatusCode as Into<u64>>::into(status.major_status).try_into() {
Ok(u) => InstructionError::CustomError(u),
Ok(u) => InstructionError::Custom(u),
Err(_) => InstructionError::InvalidError,
}
}

View File

@@ -511,9 +511,7 @@ mod tests {
let code = "main() { return; }";
let sender_address = AccountAddress::default();
let script = LibraAccount::create_script(&sender_address, code, vec![]);
let keyed_accounts = vec![
KeyedAccount::new(&script.key, true, &script.account),
];
let keyed_accounts = vec![KeyedAccount::new(&script.key, true, &script.account)];
MoveProcessor::do_finalize(&keyed_accounts).unwrap();
let _ = MoveProcessor::deserialize_verified_script(&script.account.borrow().data).unwrap();
}
@@ -551,9 +549,7 @@ mod tests {
let script = LibraAccount::create_script(&sender_address, code, vec![]);
let genesis = LibraAccount::create_genesis(1_000_000_000);
let keyed_accounts = vec![
KeyedAccount::new(&script.key, true, &script.account),
];
let keyed_accounts = vec![KeyedAccount::new(&script.key, true, &script.account)];
MoveProcessor::do_finalize(&keyed_accounts).unwrap();
@@ -578,9 +574,7 @@ mod tests {
}
";
let module = LibraAccount::create_module(code, vec![]);
let keyed_accounts = vec![
KeyedAccount::new(&module.key, true, &module.account),
];
let keyed_accounts = vec![KeyedAccount::new(&module.key, true, &module.account)];
keyed_accounts[0]
.account
.borrow_mut()
@@ -604,9 +598,7 @@ mod tests {
let script = LibraAccount::create_script(&sender_address, code, vec![]);
let genesis = LibraAccount::create_genesis(1_000_000_000);
let keyed_accounts = vec![
KeyedAccount::new(&script.key, true, &script.account),
];
let keyed_accounts = vec![KeyedAccount::new(&script.key, true, &script.account)];
MoveProcessor::do_finalize(&keyed_accounts).unwrap();
@@ -622,7 +614,7 @@ mod tests {
"main".to_string(),
vec![],
),
Err(InstructionError::CustomError(4002))
Err(InstructionError::Custom(4002))
);
}
@@ -676,9 +668,7 @@ mod tests {
let script = LibraAccount::create_script(&genesis.address, code, vec![]);
let payee = LibraAccount::create_unallocated(BIG_ENOUGH);
let keyed_accounts = vec![
KeyedAccount::new(&script.key, true, &script.account),
];
let keyed_accounts = vec![KeyedAccount::new(&script.key, true, &script.account)];
MoveProcessor::do_finalize(&keyed_accounts).unwrap();
@@ -731,9 +721,7 @@ mod tests {
);
let module = LibraAccount::create_module(&code, vec![]);
let keyed_accounts = vec![
KeyedAccount::new(&module.key, true, &module.account),
];
let keyed_accounts = vec![KeyedAccount::new(&module.key, true, &module.account)];
keyed_accounts[0]
.account
.borrow_mut()
@@ -774,9 +762,7 @@ mod tests {
);
let payee = LibraAccount::create_unallocated(BIG_ENOUGH);
let keyed_accounts = vec![
KeyedAccount::new(&script.key, true, &script.account),
];
let keyed_accounts = vec![KeyedAccount::new(&script.key, true, &script.account)];
MoveProcessor::do_finalize(&keyed_accounts).unwrap();
@@ -821,9 +807,7 @@ mod tests {
let script = LibraAccount::create_script(&genesis.address.clone(), code, vec![]);
let payee = LibraAccount::create_unallocated(BIG_ENOUGH);
let keyed_accounts = vec![
KeyedAccount::new(&script.key, true, &script.account),
];
let keyed_accounts = vec![KeyedAccount::new(&script.key, true, &script.account)];
MoveProcessor::do_finalize(&keyed_accounts).unwrap();

View File

@@ -745,7 +745,7 @@ mod tests {
where
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
{
if let InstructionError::CustomError(code) = err {
if let InstructionError::Custom(code) = err {
let specific_error: T = T::decode_custom_error_to_enum(code).unwrap();
format!(
"{:?}: {}::{:?} - {}",
@@ -759,7 +759,7 @@ mod tests {
}
}
assert_eq!(
"CustomError(0): StakeError::NoCreditsToRedeem - not enough credits to redeem",
"Custom(0): StakeError::NoCreditsToRedeem - not enough credits to redeem",
pretty_err::<StakeError>(StakeError::NoCreditsToRedeem.into())
)
}

View File

@@ -194,7 +194,7 @@ impl<'a> StorageAccount<'a> {
if segment_index >= current_segment {
// attempt to submit proof for unconfirmed segment
return Err(InstructionError::CustomError(
return Err(InstructionError::Custom(
StorageError::InvalidSegment as u32,
));
}
@@ -207,7 +207,7 @@ impl<'a> StorageAccount<'a> {
// TODO check that this blockhash is valid and recent
// if !is_valid(&blockhash) {
// // proof isn't using a recent blockhash
// return Err(InstructionError::CustomError(InvalidBlockhash as u32));
// return Err(InstructionError::Custom(InvalidBlockhash as u32));
// }
let proof = Proof {
@@ -220,13 +220,13 @@ impl<'a> StorageAccount<'a> {
let segment_proofs = proofs.entry(current_segment).or_default();
if segment_proofs.contains(&proof) {
// do not accept duplicate proofs
return Err(InstructionError::CustomError(
return Err(InstructionError::Custom(
StorageError::DuplicateProof as u32,
));
}
if segment_proofs.len() >= MAX_PROOFS_PER_SEGMENT {
// do not accept more than MAX_PROOFS_PER_SEGMENT
return Err(InstructionError::CustomError(
return Err(InstructionError::Custom(
StorageError::ProofLimitReached as u32,
));
}
@@ -255,7 +255,7 @@ impl<'a> StorageAccount<'a> {
{
debug!("advertise new segment: {} orig: {}", segment, clock.segment);
if segment < *state_segment || segment > clock.segment {
return Err(InstructionError::CustomError(
return Err(InstructionError::Custom(
StorageError::InvalidSegment as u32,
));
}
@@ -290,7 +290,7 @@ impl<'a> StorageAccount<'a> {
} = &mut storage_contract
{
if segment_index > *state_segment {
return Err(InstructionError::CustomError(
return Err(InstructionError::Custom(
StorageError::InvalidSegment as u32,
));
}
@@ -329,7 +329,7 @@ impl<'a> StorageAccount<'a> {
if accounts.len() != proofs_per_account.len() {
// don't have all the accounts to validate the proofs_per_account against
return Err(InstructionError::CustomError(
return Err(InstructionError::Custom(
StorageError::InvalidProofMask as u32,
));
}
@@ -380,9 +380,7 @@ impl<'a> StorageAccount<'a> {
} = &mut storage_contract
{
if owner.id != *account_owner {
return Err(InstructionError::CustomError(
StorageError::InvalidOwner as u32,
));
return Err(InstructionError::Custom(StorageError::InvalidOwner as u32));
}
credits.update_epoch(clock.epoch);
@@ -397,9 +395,7 @@ impl<'a> StorageAccount<'a> {
} = &mut storage_contract
{
if owner.id != *account_owner {
return Err(InstructionError::CustomError(
StorageError::InvalidOwner as u32,
));
return Err(InstructionError::Custom(StorageError::InvalidOwner as u32));
}
credits.update_epoch(clock.epoch);
let (num_validations, _total_proofs) = count_valid_proofs(&validations);
@@ -422,7 +418,7 @@ fn check_redeemable(
) -> Result<(), InstructionError> {
let rewards = (credits.redeemable as f64 * storage_point_value) as u64;
if rewards_pool.lamports()? < rewards {
Err(InstructionError::CustomError(
Err(InstructionError::Custom(
StorageError::RewardPoolDepleted as u32,
))
} else {
@@ -629,7 +625,7 @@ mod tests {
keyed_pool_account.account.borrow_mut().lamports = 0;
assert_eq!(
check_redeemable(&mut credits, 1.0, &keyed_pool_account, &mut owner),
Err(InstructionError::CustomError(
Err(InstructionError::Custom(
StorageError::RewardPoolDepleted as u32,
))
);

View File

@@ -22,7 +22,7 @@ pub enum VestError {
impl From<VestError> for InstructionError {
fn from(e: VestError) -> Self {
InstructionError::CustomError(e as u32)
InstructionError::Custom(e as u32)
}
}

View File

@@ -372,7 +372,7 @@ mod tests {
where
T: 'static + std::error::Error + DecodeError<T> + FromPrimitive,
{
if let InstructionError::CustomError(code) = err {
if let InstructionError::Custom(code) = err {
let specific_error: T = T::decode_custom_error_to_enum(code).unwrap();
format!(
"{:?}: {}::{:?} - {}",
@@ -386,7 +386,7 @@ mod tests {
}
}
assert_eq!(
"CustomError(0): VoteError::VoteTooOld - vote already recorded or not in slot hashes history",
"Custom(0): VoteError::VoteTooOld - vote already recorded or not in slot hashes history",
pretty_err::<VoteError>(VoteError::VoteTooOld.into())
)
}