@@ -9,8 +9,8 @@ use solana_rbpf::{memory_region::MemoryRegion, EbpfVm};
9
9
use solana_sdk:: {
10
10
account:: KeyedAccount ,
11
11
entrypoint:: SUCCESS ,
12
- instruction:: InstructionError ,
13
12
loader_instruction:: LoaderInstruction ,
13
+ program_error:: ProgramError ,
14
14
program_utils:: { is_executable, limited_deserialize, next_keyed_account} ,
15
15
pubkey:: Pubkey ,
16
16
sysvar:: rent,
@@ -58,7 +58,7 @@ pub fn serialize_parameters(
58
58
program_id : & Pubkey ,
59
59
keyed_accounts : & [ KeyedAccount ] ,
60
60
data : & [ u8 ] ,
61
- ) -> Result < Vec < u8 > , InstructionError > {
61
+ ) -> Result < Vec < u8 > , ProgramError > {
62
62
assert_eq ! ( 32 , mem:: size_of:: <Pubkey >( ) ) ;
63
63
64
64
let mut v: Vec < u8 > = Vec :: new ( ) ;
@@ -91,7 +91,7 @@ pub fn serialize_parameters(
91
91
pub fn deserialize_parameters (
92
92
keyed_accounts : & [ KeyedAccount ] ,
93
93
buffer : & [ u8 ] ,
94
- ) -> Result < ( ) , InstructionError > {
94
+ ) -> Result < ( ) , ProgramError > {
95
95
assert_eq ! ( 32 , mem:: size_of:: <Pubkey >( ) ) ;
96
96
97
97
let mut start = mem:: size_of :: < u64 > ( ) ; // number of accounts
@@ -122,12 +122,12 @@ pub fn process_instruction(
122
122
program_id : & Pubkey ,
123
123
keyed_accounts : & [ KeyedAccount ] ,
124
124
instruction_data : & [ u8 ] ,
125
- ) -> Result < ( ) , InstructionError > {
125
+ ) -> Result < ( ) , ProgramError > {
126
126
solana_logger:: setup_with_default ( "solana=info" ) ;
127
127
128
128
if keyed_accounts. is_empty ( ) {
129
129
warn ! ( "No account keys" ) ;
130
- return Err ( InstructionError :: NotEnoughAccountKeys ) ;
130
+ return Err ( ProgramError :: NotEnoughAccountKeys ) ;
131
131
}
132
132
133
133
if is_executable ( keyed_accounts) ? {
@@ -138,7 +138,8 @@ pub fn process_instruction(
138
138
Ok ( info) => info,
139
139
Err ( e) => {
140
140
warn ! ( "Failed to create BPF VM: {}" , e) ;
141
- return Err ( InstructionError :: GenericError ) ;
141
+ // TODO make these errors bpf enums
142
+ return Err ( ProgramError :: CustomError ( 0 ) ) ;
142
143
}
143
144
} ;
144
145
let parameter_accounts = keyed_accounts_iter. as_slice ( ) ;
@@ -149,14 +150,15 @@ pub fn process_instruction(
149
150
match vm. execute_program ( parameter_bytes. as_slice ( ) , & [ ] , & [ heap_region] ) {
150
151
Ok ( status) => {
151
152
if status != SUCCESS {
152
- let error: InstructionError = status. into ( ) ;
153
+ let error: ProgramError = status. into ( ) ;
153
154
warn ! ( "BPF program failed: {:?}" , error) ;
154
155
return Err ( error) ;
155
156
}
156
157
}
157
158
Err ( e) => {
158
159
warn ! ( "BPF VM failed to run program: {}" , e) ;
159
- return Err ( InstructionError :: GenericError ) ;
160
+ // TODO make these errors bpf enums
161
+ return Err ( ProgramError :: CustomError ( 1 ) ) ;
160
162
}
161
163
}
162
164
deserialize_parameters ( parameter_accounts, & parameter_bytes) ?;
@@ -168,14 +170,14 @@ pub fn process_instruction(
168
170
let program = next_keyed_account ( & mut keyed_accounts_iter) ?;
169
171
if program. signer_key ( ) . is_none ( ) {
170
172
warn ! ( "key[0] did not sign the transaction" ) ;
171
- return Err ( InstructionError :: MissingRequiredSignature ) ;
173
+ return Err ( ProgramError :: MissingRequiredSignature ) ;
172
174
}
173
175
let offset = offset as usize ;
174
176
let len = bytes. len ( ) ;
175
177
trace ! ( "Write: offset={} length={}" , offset, len) ;
176
178
if program. data_len ( ) ? < offset + len {
177
179
warn ! ( "Write overflow: {} < {}" , program. data_len( ) ?, offset + len) ;
178
- return Err ( InstructionError :: AccountDataTooSmall ) ;
180
+ return Err ( ProgramError :: AccountDataTooSmall ) ;
179
181
}
180
182
program. try_account_ref_mut ( ) ?. data [ offset..offset + len] . copy_from_slice ( & bytes) ;
181
183
}
@@ -186,12 +188,12 @@ pub fn process_instruction(
186
188
187
189
if program. signer_key ( ) . is_none ( ) {
188
190
warn ! ( "key[0] did not sign the transaction" ) ;
189
- return Err ( InstructionError :: MissingRequiredSignature ) ;
191
+ return Err ( ProgramError :: MissingRequiredSignature ) ;
190
192
}
191
193
192
194
if let Err ( e) = check_elf ( & program. try_account_ref ( ) ?. data ) {
193
195
warn ! ( "Invalid ELF: {}" , e) ;
194
- return Err ( InstructionError :: InvalidAccountData ) ;
196
+ return Err ( ProgramError :: InvalidAccountData ) ;
195
197
}
196
198
197
199
rent:: verify_rent_exemption ( & program, & rent) ?;
@@ -242,13 +244,13 @@ mod tests {
242
244
243
245
// Case: Empty keyed accounts
244
246
assert_eq ! (
245
- Err ( InstructionError :: NotEnoughAccountKeys ) ,
247
+ Err ( ProgramError :: NotEnoughAccountKeys ) ,
246
248
process_instruction( & program_id, & vec![ ] , & instruction_data)
247
249
) ;
248
250
249
251
// Case: Not signed
250
252
assert_eq ! (
251
- Err ( InstructionError :: MissingRequiredSignature ) ,
253
+ Err ( ProgramError :: MissingRequiredSignature ) ,
252
254
process_instruction( & program_id, & keyed_accounts, & instruction_data)
253
255
) ;
254
256
@@ -268,7 +270,7 @@ mod tests {
268
270
let mut keyed_accounts = vec ! [ KeyedAccount :: new( & program_key, true , & program_account) ] ;
269
271
keyed_accounts[ 0 ] . account . borrow_mut ( ) . data = vec ! [ 0 ; 5 ] ;
270
272
assert_eq ! (
271
- Err ( InstructionError :: AccountDataTooSmall ) ,
273
+ Err ( ProgramError :: AccountDataTooSmall ) ,
272
274
process_instruction( & program_id, & keyed_accounts, & instruction_data)
273
275
) ;
274
276
}
@@ -289,7 +291,7 @@ mod tests {
289
291
290
292
// Case: Empty keyed accounts
291
293
assert_eq ! (
292
- Err ( InstructionError :: NotEnoughAccountKeys ) ,
294
+ Err ( ProgramError :: NotEnoughAccountKeys ) ,
293
295
process_instruction( & program_id, & vec![ ] , & instruction_data)
294
296
) ;
295
297
@@ -298,7 +300,7 @@ mod tests {
298
300
299
301
// Case: Not signed
300
302
assert_eq ! (
301
- Err ( InstructionError :: MissingRequiredSignature ) ,
303
+ Err ( ProgramError :: MissingRequiredSignature ) ,
302
304
process_instruction( & program_id, & keyed_accounts, & instruction_data)
303
305
) ;
304
306
@@ -322,7 +324,7 @@ mod tests {
322
324
KeyedAccount :: new( & rent_key, false , & rent_account) ,
323
325
] ;
324
326
assert_eq ! (
325
- Err ( InstructionError :: InvalidAccountData ) ,
327
+ Err ( ProgramError :: InvalidAccountData ) ,
326
328
process_instruction( & program_id, & keyed_accounts, & instruction_data)
327
329
) ;
328
330
}
@@ -346,7 +348,7 @@ mod tests {
346
348
347
349
// Case: Empty keyed accounts
348
350
assert_eq ! (
349
- Err ( InstructionError :: NotEnoughAccountKeys ) ,
351
+ Err ( ProgramError :: NotEnoughAccountKeys ) ,
350
352
process_instruction( & program_id, & vec![ ] , & vec![ ] )
351
353
) ;
352
354
@@ -359,7 +361,7 @@ mod tests {
359
361
// Case: Account not executable
360
362
keyed_accounts[ 0 ] . account . borrow_mut ( ) . executable = false ;
361
363
assert_eq ! (
362
- Err ( InstructionError :: InvalidInstructionData ) ,
364
+ Err ( ProgramError :: InvalidInstructionData ) ,
363
365
process_instruction( & program_id, & keyed_accounts, & vec![ ] )
364
366
) ;
365
367
keyed_accounts[ 0 ] . account . borrow_mut ( ) . executable = true ;
0 commit comments