hopper-runtime 0.2.0

Canonical low-level runtime surface for Hopper. Hopper Native is the primary backend; legacy Pinocchio and solana-program compatibility are explicit opt-ins.
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
439
440
441
442
443
444
445
446
use alloc::vec::Vec;
use core::cell::{Ref, RefMut};

use crate::account::AccountView;
use crate::address::{address_eq, Address};
use crate::error::ProgramError;
use crate::instruction::{InstructionView, Signer};
use crate::ProgramResult;

pub type BackendAddress = ::solana_program::pubkey::Pubkey;
pub type BackendProgramResult = ::solana_program::entrypoint::ProgramResult;
pub type BackendRef<'a, T> = Ref<'a, T>;
pub type BackendRefMut<'a, T> = RefMut<'a, T>;
pub const BACKEND_MAX_TX_ACCOUNTS: usize = 254;
pub const BACKEND_SUCCESS: u64 = ::solana_program::entrypoint::SUCCESS;

#[repr(transparent)]
#[derive(Clone)]
pub struct BackendAccountView {
    inner: ::solana_program::account_info::AccountInfo<'static>,
}

impl PartialEq for BackendAccountView {
    fn eq(&self, other: &Self) -> bool {
        self.inner.key == other.inner.key
            && self.inner.owner == other.inner.owner
            && self.inner.is_signer == other.inner.is_signer
            && self.inner.is_writable == other.inner.is_writable
            && self.inner.executable == other.inner.executable
    }
}

impl Eq for BackendAccountView {}

impl BackendAccountView {
    #[inline(always)]
    pub fn new(inner: ::solana_program::account_info::AccountInfo<'static>) -> Self {
        Self { inner }
    }

    #[inline(always)]
    pub fn is_signer(&self) -> bool {
        self.inner.is_signer
    }

    #[inline(always)]
    pub fn is_writable(&self) -> bool {
        self.inner.is_writable
    }

    #[inline(always)]
    pub fn executable(&self) -> bool {
        self.inner.executable
    }

    #[inline(always)]
    pub fn data_len(&self) -> usize {
        self.inner.data_len()
    }

    #[inline(always)]
    pub fn lamports(&self) -> u64 {
        self.inner.lamports()
    }

    #[inline(always)]
    pub fn set_lamports(&self, lamports: u64) {
        let mut current = self
            .inner
            .try_borrow_mut_lamports()
            .expect("lamports borrow conflict");
        **current = lamports;
    }

    #[inline(always)]
    pub fn try_borrow(
        &self,
    ) -> Result<BackendRef<'_, [u8]>, ::solana_program::program_error::ProgramError> {
        self.inner
            .try_borrow_data()
            .map(|data| Ref::map(data, |slice| &**slice))
    }

    #[inline(always)]
    pub fn try_borrow_mut(
        &self,
    ) -> Result<BackendRefMut<'_, [u8]>, ::solana_program::program_error::ProgramError> {
        self.inner
            .try_borrow_mut_data()
            .map(|data| RefMut::map(data, |slice| &mut **slice))
    }

    #[inline(always)]
    pub fn resize(
        &self,
        new_len: usize,
    ) -> Result<(), ::solana_program::program_error::ProgramError> {
        #[allow(deprecated)]
        self.inner.realloc(new_len, false)
    }

    #[inline(always)]
    pub fn check_borrow(&self) -> Result<(), ::solana_program::program_error::ProgramError> {
        self.inner.try_borrow_data().map(|_| ())
    }

    #[inline(always)]
    pub fn check_borrow_mut(&self) -> Result<(), ::solana_program::program_error::ProgramError> {
        self.inner.try_borrow_mut_data().map(|_| ())
    }

    #[inline(always)]
    ///
    /// # Safety
    ///
    /// Caller must uphold the invariants documented for this unsafe API before invoking it.
    pub unsafe fn borrow_unchecked(&self) -> &[u8] {
        let data_ptr = self.inner.data.as_ptr();
        // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
        unsafe { &**data_ptr }
    }

    #[inline(always)]
    ///
    /// # Safety
    ///
    /// Caller must uphold the invariants documented for this unsafe API before invoking it.
    pub unsafe fn borrow_unchecked_mut(&self) -> &mut [u8] {
        let data_ptr = self.inner.data.as_ptr();
        // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
        unsafe { &mut **data_ptr }
    }

    #[inline(always)]
    ///
    /// # Safety
    ///
    /// Caller must uphold the invariants documented for this unsafe API before invoking it.
    pub unsafe fn close_unchecked(&self) {
        self.set_lamports(0);
        // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
        let data = unsafe { self.borrow_unchecked_mut() };
        let mut i = 0;
        while i < data.len() {
            data[i] = 0;
            i += 1;
        }
    }

    #[inline(always)]
    pub(crate) fn as_account_info(&self) -> &::solana_program::account_info::AccountInfo<'static> {
        &self.inner
    }
}

#[inline(always)]
///
/// # Safety
///
/// Caller must uphold the invariants documented for this unsafe API before invoking it.
pub unsafe fn wrap_account_slice(accounts: &[BackendAccountView]) -> &[AccountView] {
    // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
    unsafe { core::slice::from_raw_parts(accounts.as_ptr() as *const AccountView, accounts.len()) }
}

#[inline(always)]
unsafe fn wrap_deserialized_accounts<'a>(
    accounts: &'a [::solana_program::account_info::AccountInfo<'a>],
) -> &'a [BackendAccountView] {
    // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
    unsafe {
        core::slice::from_raw_parts(
            accounts.as_ptr() as *const BackendAccountView,
            accounts.len(),
        )
    }
}

#[inline(always)]
pub fn account_address(view: &BackendAccountView) -> &Address {
    // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
    unsafe { &*(view.inner.key as *const BackendAddress as *const Address) }
}

#[inline(always)]
///
/// # Safety
///
/// Caller must uphold the invariants documented for this unsafe API before invoking it.
pub unsafe fn account_owner(view: &BackendAccountView) -> &Address {
    // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
    unsafe { &*(view.inner.owner as *const BackendAddress as *const Address) }
}

#[inline(always)]
pub fn read_owner(view: &BackendAccountView) -> Address {
    Address::from(view.inner.owner.to_bytes())
}

#[inline(always)]
pub fn as_backend_address(address: &Address) -> BackendAddress {
    BackendAddress::new_from_array(address.to_bytes())
}

#[inline(always)]
pub fn owned_by(view: &BackendAccountView, program: &Address) -> bool {
    address_eq(&read_owner(view), program)
}

#[inline(always)]
pub fn disc(view: &BackendAccountView) -> u8 {
    if view.data_len() == 0 {
        0
    } else {
        // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
        unsafe { *view.borrow_unchecked().as_ptr() }
    }
}

#[inline(always)]
pub fn version(view: &BackendAccountView) -> u8 {
    if view.data_len() < 2 {
        0
    } else {
        // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
        unsafe { *view.borrow_unchecked().as_ptr().add(1) }
    }
}

#[inline(always)]
pub fn layout_id(view: &BackendAccountView) -> Option<&[u8; 8]> {
    if view.data_len() < 12 {
        None
    } else {
        // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
        Some(unsafe { &*(view.borrow_unchecked().as_ptr().add(4) as *const [u8; 8]) })
    }
}

#[inline(always)]
///
/// # Safety
///
/// Caller must uphold the invariants documented for this unsafe API before invoking it.
pub unsafe fn assign(view: &BackendAccountView, new_owner: &Address) {
    let owner = as_backend_address(new_owner);
    view.inner.assign(&owner);
}

#[inline(always)]
pub fn close(view: &BackendAccountView) -> ProgramResult {
    view.set_lamports(0);
    zero_data(view)
}

#[inline(always)]
pub fn zero_data(view: &BackendAccountView) -> ProgramResult {
    let mut data = view.try_borrow_mut().map_err(ProgramError::from)?;
    let mut i = 0;
    while i < data.len() {
        data[i] = 0;
        i += 1;
    }
    Ok(())
}

#[cfg(target_os = "solana")]
#[inline(always)]
pub fn find_program_address(seeds: &[&[u8]], program_id: &Address) -> (Address, u8) {
    let program_id = as_backend_address(program_id);
    let (address, bump) = BackendAddress::find_program_address(seeds, &program_id);
    (Address::from(address), bump)
}

#[inline(always)]
pub fn create_program_address(
    seeds: &[&[u8]],
    program_id: &Address,
) -> Result<Address, ProgramError> {
    let program_id = as_backend_address(program_id);
    BackendAddress::create_program_address(seeds, &program_id)
        .map(Address::from)
        .map_err(|_| ProgramError::InvalidSeeds)
}

#[inline(always)]
///
/// # Safety
///
/// Caller must uphold the invariants documented for this unsafe API before invoking it.
pub unsafe fn process_entrypoint<const MAX: usize>(
    input: *mut u8,
    process_instruction: fn(&BackendAddress, &[BackendAccountView], &[u8]) -> BackendProgramResult,
) -> u64 {
    // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
    let (program_id, accounts, data) = unsafe { ::solana_program::entrypoint::deserialize(input) };

    let count = accounts.len().min(MAX);
    // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
    let wrapped = unsafe { wrap_deserialized_accounts(&accounts[..count]) };

    let program_id: &'static BackendAddress = unsafe { core::mem::transmute(program_id) };

    match process_instruction(program_id, wrapped, data) {
        Ok(()) => ::solana_program::entrypoint::SUCCESS,
        Err(error) => error.into(),
    }
}

#[inline(always)]
pub fn bridge_to_runtime(
    program_id: &BackendAddress,
    accounts: &[BackendAccountView],
    data: &[u8],
    process_instruction: fn(&Address, &[AccountView], &[u8]) -> ProgramResult,
) -> BackendProgramResult {
    let hopper_id = Address::from(*program_id);
    // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
    let hopper_accounts = unsafe { wrap_account_slice(accounts) };
    match process_instruction(&hopper_id, hopper_accounts, data) {
        Ok(()) => Ok(()),
        Err(error) => Err(error.into()),
    }
}

#[inline(always)]
fn build_instruction(instruction: &InstructionView) -> ::solana_program::instruction::Instruction {
    let mut accounts = Vec::with_capacity(instruction.accounts.len());
    let mut i = 0;
    while i < instruction.accounts.len() {
        let account = &instruction.accounts[i];
        let pubkey = as_backend_address(account.address);
        let meta = match (account.is_writable, account.is_signer) {
            (true, true) => ::solana_program::instruction::AccountMeta::new(pubkey, true),
            (true, false) => ::solana_program::instruction::AccountMeta::new(pubkey, false),
            (false, true) => ::solana_program::instruction::AccountMeta::new_readonly(pubkey, true),
            (false, false) => {
                ::solana_program::instruction::AccountMeta::new_readonly(pubkey, false)
            }
        };
        accounts.push(meta);
        i += 1;
    }

    ::solana_program::instruction::Instruction {
        program_id: as_backend_address(instruction.program_id),
        accounts,
        data: instruction.data.to_vec(),
    }
}

#[inline(always)]
fn clone_account_infos<const ACCOUNTS: usize>(
    account_views: &[&AccountView; ACCOUNTS],
) -> Vec<::solana_program::account_info::AccountInfo<'static>> {
    let mut infos = Vec::with_capacity(ACCOUNTS);
    let mut i = 0;
    while i < ACCOUNTS {
        infos.push(account_views[i].as_backend().as_account_info().clone());
        i += 1;
    }
    infos
}

#[inline(always)]
fn signer_seed_groups<'a, 'b>(signers: &[Signer<'a, 'b>]) -> Vec<Vec<&'a [u8]>> {
    let mut groups = Vec::with_capacity(signers.len());
    let mut i = 0;
    while i < signers.len() {
        let signer = &signers[i];
        // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
        let seeds = unsafe { core::slice::from_raw_parts(signer.seeds, signer.len as usize) };
        let mut group = Vec::with_capacity(seeds.len());

        let mut j = 0;
        while j < seeds.len() {
            // SAFETY: This block is part of Hopper's audited zero-copy/backend boundary; surrounding checks and caller contracts uphold the required raw-pointer, layout, and aliasing invariants.
            group
                .push(unsafe { core::slice::from_raw_parts(seeds[j].seed, seeds[j].len as usize) });
            j += 1;
        }

        groups.push(group);
        i += 1;
    }

    groups
}

#[inline(always)]
pub fn invoke_signed<const ACCOUNTS: usize>(
    instruction: &InstructionView,
    account_views: &[&AccountView; ACCOUNTS],
    signers_seeds: &[Signer],
) -> ProgramResult {
    let backend_instruction = build_instruction(instruction);
    let backend_accounts = clone_account_infos(account_views);
    let signer_groups = signer_seed_groups(signers_seeds);
    let mut signer_refs = Vec::with_capacity(signer_groups.len());

    let mut i = 0;
    while i < signer_groups.len() {
        signer_refs.push(signer_groups[i].as_slice());
        i += 1;
    }

    ::solana_program::program::invoke_signed(
        &backend_instruction,
        &backend_accounts,
        signer_refs.as_slice(),
    )
    .map_err(ProgramError::from)
}

#[inline(always)]
pub fn set_return_data(data: &[u8]) {
    ::solana_program::program::set_return_data(data)
}

impl From<BackendAddress> for Address {
    #[inline(always)]
    fn from(address: BackendAddress) -> Self {
        Self(address.to_bytes())
    }
}

impl From<Address> for BackendAddress {
    #[inline(always)]
    fn from(address: Address) -> Self {
        BackendAddress::new_from_array(address.to_bytes())
    }
}

impl From<::solana_program::program_error::ProgramError> for ProgramError {
    #[inline(always)]
    fn from(error: ::solana_program::program_error::ProgramError) -> Self {
        ProgramError::from(u64::from(error))
    }
}

impl From<ProgramError> for ::solana_program::program_error::ProgramError {
    #[inline(always)]
    fn from(error: ProgramError) -> Self {
        ::solana_program::program_error::ProgramError::from(u64::from(error))
    }
}