|
1 | 1 | mod c_oracle_header;
|
2 |
| -mod time_machine_types; |
3 | 2 | mod error;
|
4 | 3 | mod log;
|
| 4 | +mod processor; |
| 5 | +mod rust_oracle; |
| 6 | +mod time_machine_types; |
5 | 7 |
|
| 8 | +use crate::c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE; |
| 9 | +use crate::error::{OracleError, OracleResult}; |
6 | 10 | use crate::log::{post_log, pre_log};
|
7 |
| -use solana_program::entrypoint::deserialize; |
| 11 | +use processor::process_instruction; |
| 12 | +use solana_program::program_error::ProgramError; |
| 13 | +use solana_program::{custom_heap_default, custom_panic_default, entrypoint::deserialize}; |
8 | 14 |
|
9 | 15 | //Below is a high lever description of the rust/c setup.
|
10 | 16 |
|
@@ -36,29 +42,43 @@ pub extern "C" fn c_entrypoint(input: *mut u8) -> u64 {
|
36 | 42 | 0 //SUCCESS value
|
37 | 43 | }
|
38 | 44 |
|
| 45 | +pub fn c_entrypoint_wrapper(input: *mut u8) -> OracleResult { |
| 46 | + //Throwing an exception from C into Rust is undefined behavior |
| 47 | + //This seems to be the best we can do |
| 48 | + match unsafe { c_entrypoint(input) } { |
| 49 | + 0 => Ok(0), // Success |
| 50 | + SUCCESSFULLY_UPDATED_AGGREGATE => Ok(SUCCESSFULLY_UPDATED_AGGREGATE), |
| 51 | + 2 => Err(ProgramError::InvalidArgument), //2 is ERROR_INVALID_ARGUMENT in solana_sdk.h |
| 52 | + _ => Err(OracleError::UnknownCError.into()), |
| 53 | + } |
| 54 | +} |
| 55 | + |
39 | 56 | #[no_mangle]
|
40 | 57 | pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
|
41 |
| - let (_program_id, accounts, instruction_data) = unsafe { deserialize(input) }; |
| 58 | + let (program_id, accounts, instruction_data) = unsafe { deserialize(input) }; |
42 | 59 |
|
43 |
| - match pre_log(&accounts,instruction_data) { |
| 60 | + match pre_log(&accounts, instruction_data) { |
44 | 61 | Err(error) => return error.into(),
|
45 | 62 | _ => {}
|
46 | 63 | }
|
47 | 64 |
|
48 |
| - let c_ret_val = unsafe { c_entrypoint(input) }; |
| 65 | + let c_ret_val = match process_instruction(program_id, &accounts, instruction_data, input) { |
| 66 | + Err(error) => error.into(), |
| 67 | + Ok(success_status) => success_status, |
| 68 | + }; |
49 | 69 |
|
50 | 70 | match post_log(c_ret_val, &accounts) {
|
51 | 71 | Err(error) => return error.into(),
|
52 | 72 | _ => {}
|
53 | 73 | }
|
54 | 74 |
|
55 |
| - if c_ret_val == c_oracle_header::SUCCESSFULLY_UPDATED_AGGREGATE { |
| 75 | + if c_ret_val == SUCCESSFULLY_UPDATED_AGGREGATE { |
56 | 76 | //0 is the SUCCESS value for solana
|
57 | 77 | return 0;
|
58 | 78 | } else {
|
59 | 79 | return c_ret_val;
|
60 | 80 | }
|
61 | 81 | }
|
62 | 82 |
|
63 |
| -solana_program::custom_heap_default!(); |
64 |
| -solana_program::custom_panic_default!(); |
| 83 | +custom_heap_default!(); |
| 84 | +custom_panic_default!(); |
0 commit comments