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

Commit 197a82f

Browse files
committed
Rreturn ProgramError from programs
1 parent 71f77a8 commit 197a82f

21 files changed

+357
-383
lines changed

programs/bpf_loader/src/lib.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use solana_rbpf::{memory_region::MemoryRegion, EbpfVm};
1010
use solana_sdk::{
1111
account::KeyedAccount,
1212
entrypoint::SUCCESS,
13-
instruction::InstructionError,
1413
loader_instruction::LoaderInstruction,
1514
program_utils::DecodeError,
15+
program_error::ProgramError,
1616
program_utils::{is_executable, limited_deserialize, next_keyed_account},
1717
pubkey::Pubkey,
1818
sysvar::rent,
@@ -75,7 +75,7 @@ pub fn serialize_parameters(
7575
program_id: &Pubkey,
7676
keyed_accounts: &[KeyedAccount],
7777
data: &[u8],
78-
) -> Result<Vec<u8>, InstructionError> {
78+
) -> Result<Vec<u8>, ProgramError> {
7979
assert_eq!(32, mem::size_of::<Pubkey>());
8080

8181
let mut v: Vec<u8> = Vec::new();
@@ -108,7 +108,7 @@ pub fn serialize_parameters(
108108
pub fn deserialize_parameters(
109109
keyed_accounts: &[KeyedAccount],
110110
buffer: &[u8],
111-
) -> Result<(), InstructionError> {
111+
) -> Result<(), ProgramError> {
112112
assert_eq!(32, mem::size_of::<Pubkey>());
113113

114114
let mut start = mem::size_of::<u64>(); // number of accounts
@@ -139,12 +139,12 @@ pub fn process_instruction(
139139
program_id: &Pubkey,
140140
keyed_accounts: &[KeyedAccount],
141141
instruction_data: &[u8],
142-
) -> Result<(), InstructionError> {
142+
) -> Result<(), ProgramError> {
143143
solana_logger::setup_with_default("solana=info");
144144

145145
if keyed_accounts.is_empty() {
146146
warn!("No account keys");
147-
return Err(InstructionError::NotEnoughAccountKeys);
147+
return Err(ProgramError::NotEnoughAccountKeys);
148148
}
149149

150150
if is_executable(keyed_accounts)? {
@@ -166,7 +166,7 @@ pub fn process_instruction(
166166
match vm.execute_program(parameter_bytes.as_slice(), &[], &[heap_region]) {
167167
Ok(status) => {
168168
if status != SUCCESS {
169-
let error: InstructionError = status.into();
169+
let error: ProgramError = status.into();
170170
warn!("BPF program failed: {:?}", error);
171171
return Err(error);
172172
}
@@ -185,14 +185,14 @@ pub fn process_instruction(
185185
let program = next_keyed_account(&mut keyed_accounts_iter)?;
186186
if program.signer_key().is_none() {
187187
warn!("key[0] did not sign the transaction");
188-
return Err(InstructionError::MissingRequiredSignature);
188+
return Err(ProgramError::MissingRequiredSignature);
189189
}
190190
let offset = offset as usize;
191191
let len = bytes.len();
192192
trace!("Write: offset={} length={}", offset, len);
193193
if program.data_len()? < offset + len {
194194
warn!("Write overflow: {} < {}", program.data_len()?, offset + len);
195-
return Err(InstructionError::AccountDataTooSmall);
195+
return Err(ProgramError::AccountDataTooSmall);
196196
}
197197
program.try_account_ref_mut()?.data[offset..offset + len].copy_from_slice(&bytes);
198198
}
@@ -203,12 +203,12 @@ pub fn process_instruction(
203203

204204
if program.signer_key().is_none() {
205205
warn!("key[0] did not sign the transaction");
206-
return Err(InstructionError::MissingRequiredSignature);
206+
return Err(ProgramError::MissingRequiredSignature);
207207
}
208208

209209
if let Err(e) = check_elf(&program.try_account_ref()?.data) {
210210
warn!("Invalid ELF: {}", e);
211-
return Err(InstructionError::InvalidAccountData);
211+
return Err(ProgramError::InvalidAccountData);
212212
}
213213

214214
rent::verify_rent_exemption(&program, &rent)?;
@@ -259,13 +259,13 @@ mod tests {
259259

260260
// Case: Empty keyed accounts
261261
assert_eq!(
262-
Err(InstructionError::NotEnoughAccountKeys),
262+
Err(ProgramError::NotEnoughAccountKeys),
263263
process_instruction(&program_id, &vec![], &instruction_data)
264264
);
265265

266266
// Case: Not signed
267267
assert_eq!(
268-
Err(InstructionError::MissingRequiredSignature),
268+
Err(ProgramError::MissingRequiredSignature),
269269
process_instruction(&program_id, &keyed_accounts, &instruction_data)
270270
);
271271

@@ -285,7 +285,7 @@ mod tests {
285285
let mut keyed_accounts = vec![KeyedAccount::new(&program_key, true, &program_account)];
286286
keyed_accounts[0].account.borrow_mut().data = vec![0; 5];
287287
assert_eq!(
288-
Err(InstructionError::AccountDataTooSmall),
288+
Err(ProgramError::AccountDataTooSmall),
289289
process_instruction(&program_id, &keyed_accounts, &instruction_data)
290290
);
291291
}
@@ -306,7 +306,7 @@ mod tests {
306306

307307
// Case: Empty keyed accounts
308308
assert_eq!(
309-
Err(InstructionError::NotEnoughAccountKeys),
309+
Err(ProgramError::NotEnoughAccountKeys),
310310
process_instruction(&program_id, &vec![], &instruction_data)
311311
);
312312

@@ -315,7 +315,7 @@ mod tests {
315315

316316
// Case: Not signed
317317
assert_eq!(
318-
Err(InstructionError::MissingRequiredSignature),
318+
Err(ProgramError::MissingRequiredSignature),
319319
process_instruction(&program_id, &keyed_accounts, &instruction_data)
320320
);
321321

@@ -339,7 +339,7 @@ mod tests {
339339
KeyedAccount::new(&rent_key, false, &rent_account),
340340
];
341341
assert_eq!(
342-
Err(InstructionError::InvalidAccountData),
342+
Err(ProgramError::InvalidAccountData),
343343
process_instruction(&program_id, &keyed_accounts, &instruction_data)
344344
);
345345
}
@@ -363,7 +363,7 @@ mod tests {
363363

364364
// Case: Empty keyed accounts
365365
assert_eq!(
366-
Err(InstructionError::NotEnoughAccountKeys),
366+
Err(ProgramError::NotEnoughAccountKeys),
367367
process_instruction(&program_id, &vec![], &vec![])
368368
);
369369

@@ -376,7 +376,7 @@ mod tests {
376376
// Case: Account not executable
377377
keyed_accounts[0].account.borrow_mut().executable = false;
378378
assert_eq!(
379-
Err(InstructionError::InvalidInstructionData),
379+
Err(ProgramError::InvalidInstructionData),
380380
process_instruction(&program_id, &keyed_accounts, &vec![])
381381
);
382382
keyed_accounts[0].account.borrow_mut().executable = true;

programs/config/src/config_processor.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use crate::ConfigKeys;
44
use bincode::deserialize;
55
use log::*;
66
use solana_sdk::account::KeyedAccount;
7-
use solana_sdk::instruction::InstructionError;
7+
use solana_sdk::program_error::ProgramError;
88
use solana_sdk::program_utils::{limited_deserialize, next_keyed_account};
99
use solana_sdk::pubkey::Pubkey;
1010

1111
pub fn process_instruction(
1212
_program_id: &Pubkey,
1313
keyed_accounts: &[KeyedAccount],
1414
data: &[u8],
15-
) -> Result<(), InstructionError> {
15+
) -> Result<(), ProgramError> {
1616
let key_list: ConfigKeys = limited_deserialize(data)?;
1717
let keyed_accounts_iter = &mut keyed_accounts.iter();
1818
let config_keyed_account = &mut next_keyed_account(keyed_accounts_iter)?;
@@ -25,7 +25,7 @@ pub fn process_instruction(
2525
config_account.data.len(),
2626
err
2727
);
28-
InstructionError::InvalidAccountData
28+
ProgramError::InvalidAccountData
2929
})?
3030
};
3131
let current_signer_keys: Vec<Pubkey> = current_data
@@ -40,7 +40,7 @@ pub fn process_instruction(
4040
// or when no signers specified in Config data
4141
if config_keyed_account.signer_key().is_none() {
4242
error!("account[0].signer_key().is_none()");
43-
return Err(InstructionError::MissingRequiredSignature);
43+
return Err(ProgramError::MissingRequiredSignature);
4444
}
4545
}
4646

@@ -51,19 +51,19 @@ pub fn process_instruction(
5151
let signer_account = keyed_accounts_iter.next();
5252
if signer_account.is_none() {
5353
error!("account {:?} is not in account list", signer);
54-
return Err(InstructionError::MissingRequiredSignature);
54+
return Err(ProgramError::MissingRequiredSignature);
5555
}
5656
let signer_key = signer_account.unwrap().signer_key();
5757
if signer_key.is_none() {
5858
error!("account {:?} signer_key().is_none()", signer);
59-
return Err(InstructionError::MissingRequiredSignature);
59+
return Err(ProgramError::MissingRequiredSignature);
6060
}
6161
if signer_key.unwrap() != signer {
6262
error!(
6363
"account[{:?}].signer_key() does not match Config data)",
6464
counter + 1
6565
);
66-
return Err(InstructionError::MissingRequiredSignature);
66+
return Err(ProgramError::MissingRequiredSignature);
6767
}
6868
// If Config account is already initialized, update signatures must match Config data
6969
if !current_data.keys.is_empty()
@@ -73,11 +73,11 @@ pub fn process_instruction(
7373
.is_none()
7474
{
7575
error!("account {:?} is not in stored signer list", signer);
76-
return Err(InstructionError::MissingRequiredSignature);
76+
return Err(ProgramError::MissingRequiredSignature);
7777
}
7878
} else if config_keyed_account.signer_key().is_none() {
7979
error!("account[0].signer_key().is_none()");
80-
return Err(InstructionError::MissingRequiredSignature);
80+
return Err(ProgramError::MissingRequiredSignature);
8181
}
8282
}
8383

@@ -88,12 +88,12 @@ pub fn process_instruction(
8888
counter,
8989
current_signer_keys.len()
9090
);
91-
return Err(InstructionError::MissingRequiredSignature);
91+
return Err(ProgramError::MissingRequiredSignature);
9292
}
9393

9494
if config_keyed_account.data_len()? < data.len() {
9595
error!("instruction data too large");
96-
return Err(InstructionError::InvalidInstructionData);
96+
return Err(ProgramError::InvalidInstructionData);
9797
}
9898

9999
config_keyed_account.try_account_ref_mut()?.data[..data.len()].copy_from_slice(&data);
@@ -215,7 +215,7 @@ mod tests {
215215
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
216216
assert_eq!(
217217
process_instruction(&id(), &keyed_accounts, &instruction.data),
218-
Err(InstructionError::InvalidInstructionData)
218+
Err(ProgramError::InvalidInstructionData)
219219
);
220220
}
221221

@@ -233,7 +233,7 @@ mod tests {
233233
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
234234
assert_eq!(
235235
process_instruction(&id(), &keyed_accounts, &instruction.data),
236-
Err(InstructionError::MissingRequiredSignature)
236+
Err(ProgramError::MissingRequiredSignature)
237237
);
238238
}
239239

@@ -290,7 +290,7 @@ mod tests {
290290
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
291291
assert_eq!(
292292
process_instruction(&id(), &keyed_accounts, &instruction.data),
293-
Err(InstructionError::InvalidAccountData)
293+
Err(ProgramError::InvalidAccountData)
294294
);
295295
}
296296

@@ -316,7 +316,7 @@ mod tests {
316316
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
317317
assert_eq!(
318318
process_instruction(&id(), &keyed_accounts, &instruction.data),
319-
Err(InstructionError::MissingRequiredSignature)
319+
Err(ProgramError::MissingRequiredSignature)
320320
);
321321

322322
// Config-data pubkey not a signer
@@ -327,7 +327,7 @@ mod tests {
327327
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
328328
assert_eq!(
329329
process_instruction(&id(), &keyed_accounts, &instruction.data),
330-
Err(InstructionError::MissingRequiredSignature)
330+
Err(ProgramError::MissingRequiredSignature)
331331
);
332332
}
333333

@@ -395,7 +395,7 @@ mod tests {
395395
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
396396
assert_eq!(
397397
process_instruction(&id(), &keyed_accounts, &instruction.data),
398-
Err(InstructionError::MissingRequiredSignature)
398+
Err(ProgramError::MissingRequiredSignature)
399399
);
400400

401401
// Attempt update with incorrect signatures
@@ -414,7 +414,7 @@ mod tests {
414414
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
415415
assert_eq!(
416416
process_instruction(&id(), &keyed_accounts, &instruction.data),
417-
Err(InstructionError::MissingRequiredSignature)
417+
Err(ProgramError::MissingRequiredSignature)
418418
);
419419
}
420420

@@ -477,7 +477,7 @@ mod tests {
477477
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
478478
assert_eq!(
479479
process_instruction(&id(), &keyed_accounts, &instruction.data),
480-
Err(InstructionError::MissingRequiredSignature)
480+
Err(ProgramError::MissingRequiredSignature)
481481
);
482482
}
483483

@@ -491,7 +491,7 @@ mod tests {
491491
let keyed_accounts = create_keyed_is_signer_accounts(&accounts);
492492
assert_eq!(
493493
process_instruction(&id(), &keyed_accounts, &instructions[1].data),
494-
Err(InstructionError::NotEnoughAccountKeys)
494+
Err(ProgramError::NotEnoughAccountKeys)
495495
);
496496
}
497497
}

programs/stake/src/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use solana_config_program::{create_config_account, get_config_data, ConfigState}
66
use solana_sdk::{
77
account::{Account, KeyedAccount},
88
genesis_config::GenesisConfig,
9-
instruction::InstructionError,
9+
program_error::ProgramError,
1010
};
1111

1212
// stake config ID
@@ -63,11 +63,11 @@ pub fn create_account(lamports: u64, config: &Config) -> Account {
6363
create_config_account(vec![], config, lamports)
6464
}
6565

66-
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Config, InstructionError> {
66+
pub fn from_keyed_account(account: &KeyedAccount) -> Result<Config, ProgramError> {
6767
if !check_id(account.unsigned_key()) {
68-
return Err(InstructionError::InvalidArgument);
68+
return Err(ProgramError::InvalidArgument);
6969
}
70-
Config::from(&*account.try_account_ref()?).ok_or(InstructionError::InvalidArgument)
70+
Config::from(&*account.try_account_ref()?).ok_or(ProgramError::InvalidArgument)
7171
}
7272

7373
#[cfg(test)]
@@ -82,7 +82,7 @@ mod tests {
8282
assert_eq!(Config::from(&account.borrow()), Some(Config::default()));
8383
assert_eq!(
8484
from_keyed_account(&KeyedAccount::new(&Pubkey::default(), false, &mut account)),
85-
Err(InstructionError::InvalidArgument)
85+
Err(ProgramError::InvalidArgument)
8686
);
8787
}
8888
}

0 commit comments

Comments
 (0)