solana-program-stubs 0.1.1

Stub implementations for Solana program interface
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/// This macro exports a global alternative container to ProgramTest's SYSCALL_STUBS
/// necessary at loader.
#[macro_export]
macro_rules! declare_sol_loader_stubs {
    () => {
        $crate::common_stub_types!();

        pub use lazy_static;

        lazy_static::lazy_static! {
            pub static ref SYSCALL_STUBS: Arc<RwLock<Box<dyn SyscallStubs>>> =
                Arc::new(RwLock::new(Box::new(UnimplementedSyscallStubs {})));
        }

        pub struct UnimplementedSyscallStubs {}
        impl SyscallStubs for UnimplementedSyscallStubs {
            fn sol_get_clock_sysvar(&self, _var_addr: *mut u8) -> u64 {
                unimplemented!()
            }
            fn sol_get_epoch_rewards_sysvar(&self, _var_addr: *mut u8) -> u64 {
                unimplemented!()
            }
            fn sol_get_epoch_schedule_sysvar(&self, _var_addr: *mut u8) -> u64 {
                unimplemented!()
            }
            fn sol_get_fees_sysvar(&self, _var_addr: *mut u8) -> u64 {
                unimplemented!()
            }
            fn sol_get_last_restart_slot(&self, _var_addr: *mut u8) -> u64 {
                unimplemented!()
            }
            fn sol_get_processed_sibling_instruction(&self, _index: usize) -> Option<Instruction> {
                unimplemented!()
            }
            fn sol_get_rent_sysvar(&self, _var_addr: *mut u8) -> u64 {
                unimplemented!()
            }
            fn sol_get_return_data(&self) -> Option<(Pubkey, Vec<u8>)> {
                unimplemented!()
            }
            fn sol_get_stack_height(&self) -> u64 {
                unimplemented!()
            }
            fn sol_invoke_signed(
                &self,
                _instruction: &Instruction,
                _account_infos: &[AccountInfo],
                _signers_seeds: &[&[&[u8]]],
            ) -> ProgramResult {
                unimplemented!()
            }
            fn sol_log(&self, _message: &str) {
                unimplemented!()
            }
            fn sol_log_compute_units(&self) {
                unimplemented!()
            }
            fn sol_log_data(&self, _fields: &[&[u8]]) {
                unimplemented!()
            }
            unsafe fn sol_memcmp(
                &self,
                _s1: *const u8,
                _s2: *const u8,
                _n: usize,
                _result: *mut i32,
            ) {
                unimplemented!()
            }
            unsafe fn sol_memcpy(&self, _dst: *mut u8, _src: *const u8, _n: usize) {
                unimplemented!()
            }
            unsafe fn sol_memmove(&self, _dst: *mut u8, _src: *const u8, _n: usize) {
                unimplemented!()
            }
            unsafe fn sol_memset(&self, _s: *mut u8, _c: u8, _n: usize) {
                unimplemented!()
            }
            fn sol_remaining_compute_units(&self) -> u64 {
                unimplemented!()
            }
            fn sol_set_return_data(&self, _data: &[u8]) {
                unimplemented!()
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_log_(msg: *const u8, len: u64) {
            let message = unsafe { std::slice::from_raw_parts(msg, len as _) };
            let m = String::from_utf8_lossy(message);
            SYSCALL_STUBS.read().unwrap().sol_log(&m);
        }

        #[no_mangle]
        pub extern "C" fn sol_log_compute_units_() {
            SYSCALL_STUBS.read().unwrap().sol_log_compute_units();
        }

        #[no_mangle]
        pub extern "C" fn sol_remaining_compute_units() -> u64 {
            SYSCALL_STUBS.read().unwrap().sol_remaining_compute_units()
        }

        #[no_mangle]
        pub extern "C" fn sol_memcpy_(dst: *mut u8, src: *const u8, n: u64) {
            unsafe {
                SYSCALL_STUBS.read().unwrap().sol_memcpy(dst, src, n as _);
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_memmove_(dst: *mut u8, src: *const u8, n: u64) {
            unsafe {
                SYSCALL_STUBS.read().unwrap().sol_memmove(dst, src, n as _);
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_memcmp_(s1: *const u8, s2: *const u8, n: u64, result: *mut i32) {
            unsafe {
                SYSCALL_STUBS
                    .read()
                    .unwrap()
                    .sol_memcmp(s1, s2, n as _, result);
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_memset_(s: *mut u8, c: u8, n: u64) {
            unsafe {
                SYSCALL_STUBS.read().unwrap().sol_memset(s, c, n as _);
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_get_stack_height() -> u64 {
            SYSCALL_STUBS.read().unwrap().sol_get_stack_height()
        }

        #[no_mangle]
        pub extern "C" fn sol_get_clock_sysvar(addr: *mut u8) -> u64 {
            SYSCALL_STUBS.read().unwrap().sol_get_clock_sysvar(addr)
        }

        #[no_mangle]
        pub extern "C" fn sol_get_epoch_schedule_sysvar(addr: *mut u8) -> u64 {
            SYSCALL_STUBS
                .read()
                .unwrap()
                .sol_get_epoch_schedule_sysvar(addr)
        }

        #[no_mangle]
        pub extern "C" fn sol_get_fees_sysvar(addr: *mut u8) -> u64 {
            SYSCALL_STUBS.read().unwrap().sol_get_fees_sysvar(addr)
        }

        #[no_mangle]
        pub extern "C" fn sol_get_rent_sysvar(addr: *mut u8) -> u64 {
            SYSCALL_STUBS.read().unwrap().sol_get_rent_sysvar(addr)
        }

        #[no_mangle]
        pub extern "C" fn sol_get_epoch_rewards_sysvar(addr: *mut u8) -> u64 {
            SYSCALL_STUBS
                .read()
                .unwrap()
                .sol_get_epoch_rewards_sysvar(addr)
        }

        #[no_mangle]
        pub extern "C" fn sol_get_last_restart_slot(addr: *mut u8) -> u64 {
            SYSCALL_STUBS
                .read()
                .unwrap()
                .sol_get_last_restart_slot(addr)
        }

        #[no_mangle]
        pub extern "C" fn sol_get_epoch_stake(vote_address: *const u8) -> u64 {
            SYSCALL_STUBS
                .read()
                .unwrap()
                .sol_get_epoch_stake(vote_address)
        }

        #[no_mangle]
        pub extern "C" fn sol_get_sysvar(
            sysvar_id_addr: *const u8,
            result: *mut u8,
            offset: u64,
            length: u64,
        ) -> u64 {
            SYSCALL_STUBS
                .read()
                .unwrap()
                .sol_get_sysvar(sysvar_id_addr, result, offset, length)
        }

        #[no_mangle]
        pub extern "C" fn sol_set_return_data(data: *const u8, length: u64) {
            let slice = unsafe { std::slice::from_raw_parts(data, length as _) };
            SYSCALL_STUBS.read().unwrap().sol_set_return_data(slice);
        }

        #[no_mangle]
        pub extern "C" fn sol_get_return_data(
            data: *mut u8,
            length: u64,
            program_id: *mut CPubkey,
        ) -> u64 {
            let ret_data = SYSCALL_STUBS.read().unwrap().sol_get_return_data();

            match ret_data {
                None => 0,
                Some((key, src)) => {
                    // Caller is wondering how many to allocate.
                    if length == 0 {
                        unsafe { *program_id = key.as_array().into() };
                        return src.len() as _;
                    }

                    // Caller is ready with the allocation - we're expected to copy the data.
                    // Let's check if there's enough space.
                    let src_len = src.len() as _;
                    if src_len > length || unsafe { *(*program_id).as_array() } != *key.as_array() {
                        return 0;
                    }
                    unsafe {
                        std::ptr::copy_nonoverlapping(src.as_ptr(), data, length as _);
                    };
                    src_len
                }
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_log_data(data: *const u8, data_len: u64) {
            // reinterpret the buffer as a fat pointer to (*const u8, usize) pairs
            let fat_ptrs = data as *const (*const u8, u64);
            let mut v: Vec<&[u8]> = Vec::with_capacity(data_len as _);
            for i in 0..data_len {
                let (data_ptr, len) = unsafe { *fat_ptrs.add(i as _) };
                let slice = unsafe { std::slice::from_raw_parts(data_ptr, len as _) };
                v.push(slice);
            }
            SYSCALL_STUBS.read().unwrap().sol_log_data(&v[..]);
        }

        #[no_mangle]
        pub extern "C" fn sol_get_processed_sibling_instruction(
            index: u64,
            meta: *mut CProcessedSiblingInstruction,
            program_id: *mut CPubkey,
            data: *mut u8,
            accounts: *mut CAccountMeta,
        ) -> u64 {
            let instruction = SYSCALL_STUBS
                .read()
                .unwrap()
                .sol_get_processed_sibling_instruction(index as _);
            match instruction {
                None => 0, // 0 - No processed sibling instruction.
                Some(instr) => {
                    let data_len = instr.data.len();
                    let accounts_len = instr.accounts.len();
                    unsafe {
                        if (*meta).accounts_len == 0 && (*meta).data_len == 0 {
                            // Caller is wondering how many to allocate.
                            // https://github.com/anza-xyz/solana-sdk/blob/master/instruction/src/syscalls.rs#L32
                            (*meta).data_len = data_len as _;
                            (*meta).accounts_len = accounts_len as _;
                            *program_id = instr.program_id.as_array().into();

                            // 1 - Return the allocation details so that caller can prepare.
                            return 1;
                        }
                    }

                    // Caller is ready with the allocation.
                    // But first - a little sanity check.
                    unsafe {
                        if (*meta).data_len != data_len as u64
                            || (*meta).accounts_len != accounts_len as u64
                            || *(*program_id).as_array() != *instr.program_id.as_array()
                        {
                            return 0;
                        }

                        // Now just copy the data and the account metas.
                        std::ptr::copy_nonoverlapping(instr.data.as_ptr(), data, data_len);
                        // Now copy the account metas taking into consideration that pubkey is a *const u8.
                        // https://github.com/anza-xyz/pinocchio/blob/main/sdk/pinocchio/src/instruction.rs#L116
                        for i in 0..instr.accounts.len() {
                            let account_meta = accounts.add(i);
                            (*account_meta).is_signer = instr.accounts[i].is_signer;
                            (*account_meta).is_writable = instr.accounts[i].is_writable;
                            (*account_meta).pubkey = Box::leak(Box::new(instr.accounts[i].pubkey))
                                as *const _
                                as *const CPubkey;
                        }
                    }
                    2 // 2 - All good.
                }
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_invoke_signed_c(
            instruction_addr: *const u8,
            account_infos_addr: *const u8,
            account_infos_len: u64,
            signers_seeds_addr: *const u8,
            signers_seeds_len: u64,
        ) -> u64 {
            // instruction
            let cinstr = instruction_addr as *const CInstruction;
            let instruction = unsafe {
                Instruction {
                    program_id: Pubkey::new_from_array(*(*(*cinstr).program_id).as_array()),
                    accounts: {
                        (0..(*cinstr).accounts_len)
                            .map(|i| {
                                let cam = (*cinstr).accounts.add(i as _);
                                AccountMeta {
                                    pubkey: Pubkey::new_from_array(*(*(*cam).pubkey).as_array()),
                                    is_signer: (*cam).is_signer,
                                    is_writable: (*cam).is_writable,
                                }
                            })
                            .collect()
                    },
                    data: {
                        let slice =
                            std::slice::from_raw_parts((*cinstr).data, (*cinstr).data_len as _);
                        slice.to_vec()
                    },
                }
            };

            // account_infos
            let ai_ptr = account_infos_addr as *const CAccountInfo;
            let mut account_infos: Vec<AccountInfo<'_>> = vec![];
            for i in 0..account_infos_len {
                let cai = unsafe { &*ai_ptr.add(i as _) };
                let ai = unsafe {
                    AccountInfo {
                        key: &*((*cai).key as *const Pubkey),
                        lamports: std::rc::Rc::new(std::cell::RefCell::new(
                            &mut *((*cai).lamports as *mut _),
                        )),
                        data: {
                            let slice = std::slice::from_raw_parts_mut(
                                (*cai).data as _,
                                (*cai).data_len as _,
                            );
                            std::rc::Rc::new(std::cell::RefCell::new(slice))
                        },
                        owner: &*((*cai).owner as *const Pubkey),
                        rent_epoch: (*cai).rent_epoch,
                        is_signer: (*cai).is_signer,
                        is_writable: (*cai).is_writable,
                        executable: (*cai).executable,
                    }
                };
                account_infos.push(ai);
            }

            // signers_seeds
            let q_fat_ptr = signers_seeds_addr as *const (*const u8, u64);
            let mut qv: Vec<Vec<&[u8]>> = vec![];
            for q in 0..signers_seeds_len {
                let (q_data_ptr, q_data_len) = unsafe { *q_fat_ptr.add(q as _) };
                let mut pv: Vec<&[u8]> = vec![];
                for p in 0..q_data_len {
                    let p_fat_ptr = q_data_ptr as *const (*const u8, u64);
                    let (p_data_ptr, p_data_len) = unsafe { *p_fat_ptr.add(p as _) };
                    let slice =
                        unsafe { std::slice::from_raw_parts(p_data_ptr, p_data_len as usize) };
                    pv.push(slice);
                }
                qv.push(pv);
            }

            let signers_seeds: Vec<_> = qv.iter().map(|e| &e[..]).collect();
            match SYSCALL_STUBS.read().unwrap().sol_invoke_signed(
                &instruction,
                &account_infos[..],
                &signers_seeds[..],
            ) {
                Ok(_) => {
                    let ai_ptr = account_infos_addr as *mut CAccountInfo;
                    for (i, acc) in account_infos.iter().enumerate() {
                        // Update data lens so that caller has them.
                        let cai = unsafe { &mut *ai_ptr.add(i as _) };
                        cai.data_len = acc.data_len() as _;
                    }
                    0
                }
                Err(e) => e.into(),
            }
        }

        #[no_mangle]
        pub extern "C" fn sol_log_pubkey(pubkey: *const u8) {
            let pubkey = unsafe { &*(pubkey as *const Pubkey) };
            SYSCALL_STUBS.read().unwrap().sol_log(&pubkey.to_string());
        }

        impl SyscallStubsApi {
            pub fn new() -> Self {
                Self {
                    sol_get_clock_sysvar: sol_get_clock_sysvar,
                    sol_get_epoch_rewards_sysvar: sol_get_epoch_rewards_sysvar,
                    sol_get_epoch_schedule_sysvar: sol_get_epoch_schedule_sysvar,
                    sol_get_fees_sysvar: sol_get_fees_sysvar,
                    sol_get_last_restart_slot: sol_get_last_restart_slot,
                    sol_get_rent_sysvar: sol_get_rent_sysvar,
                    sol_get_stack_height: sol_get_stack_height,
                    sol_log_: sol_log_,
                    sol_log_compute_units_: sol_log_compute_units_,
                    sol_memcmp_: sol_memcmp_,
                    sol_memcpy_: sol_memcpy_,
                    sol_memmove_: sol_memmove_,
                    sol_memset_: sol_memset_,
                    sol_remaining_compute_units: sol_remaining_compute_units,
                    sol_get_return_data: sol_get_return_data,
                    sol_set_return_data: sol_set_return_data,
                    sol_log_data: sol_log_data,
                    sol_invoke_signed_c: sol_invoke_signed_c,
                    sol_get_processed_sibling_instruction: sol_get_processed_sibling_instruction,
                    sol_get_epoch_stake: sol_get_epoch_stake,
                    sol_get_sysvar: sol_get_sysvar,
                }
            }
        }
    };
}