bokken_runtime/
debug_env.rs

1use std::collections::HashMap;
2
3use borsh::{BorshSerialize, BorshDeserialize};
4use solana_program::{pubkey::Pubkey, instruction::AccountMeta, program_error::ProgramError};
5
6/// The structure used to store a Solana account's information
7#[derive(PartialEq, Eq, Debug, Clone, BorshSerialize, BorshDeserialize, Default)]
8pub struct BokkenAccountData {
9	// pub pubkey: Pubkey,
10	pub lamports: u64,
11	pub data: Vec<u8>,
12	pub owner: Pubkey,
13	pub executable: bool,
14	pub rent_epoch: u64
15}
16impl BokkenAccountData {
17	pub fn move_lamports<'a>(&'a mut self, to: &'a mut Self, amount: u64) -> Result<(), ProgramError> {
18		if self.lamports < amount {
19			return Err(ProgramError::InsufficientFunds)
20		}
21		self.lamports -= amount;
22		to.lamports += amount;
23		Ok(())
24	}
25}
26
27/// Same as Solana's own `AccountMeta`, except this implements `BorshSerialize` and `BorshDeserialize`
28#[derive(Debug, Default, PartialEq, Clone, BorshSerialize, BorshDeserialize)]
29pub struct BorshAccountMeta {
30    /// An account's public key.
31    pub pubkey: Pubkey,
32    /// True if an `Instruction` requires a `Transaction` signature matching `pubkey`.
33    pub is_signer: bool,
34    /// True if the account data or metadata may be mutated during program execution.
35    pub is_writable: bool,
36}
37impl From<&AccountMeta> for BorshAccountMeta {
38	fn from(meta: &AccountMeta) -> Self {
39		Self {
40			pubkey: meta.pubkey,
41			is_signer: meta.is_signer,
42			is_writable: meta.is_writable
43		}
44	}
45}
46impl From<AccountMeta> for BorshAccountMeta {
47	fn from(meta: AccountMeta) -> Self {
48		Self {
49			pubkey: meta.pubkey,
50			is_signer: meta.is_signer,
51			is_writable: meta.is_writable
52		}
53	}
54}
55impl From<BorshAccountMeta> for AccountMeta {
56	fn from(meta: BorshAccountMeta) -> Self {
57		Self {
58			pubkey: meta.pubkey,
59			is_signer: meta.is_signer,
60			is_writable: meta.is_writable
61		}
62	}
63}
64
65
66/// IPC message sent from a debuggable program to the main Bokken process.
67#[derive(Debug, BorshSerialize, BorshDeserialize)]
68pub enum BokkenRuntimeMessage {
69	Log {
70		nonce: u64,
71		message: String
72	},
73	Executed {
74		nonce: u64,
75		return_code: u64,
76		account_datas: HashMap<Pubkey, BokkenAccountData>
77	},
78	CrossProgramInvoke {
79		nonce: u64,
80		program_id: Pubkey,
81		instruction: Vec<u8>,
82		account_metas: Vec<BorshAccountMeta>,
83		account_datas: HashMap<Pubkey, BokkenAccountData>,
84		call_depth: u8
85	}
86}
87
88/// IPC message send from the main Bokken process to a debuggable program
89#[derive(Debug, BorshSerialize, BorshDeserialize)]
90pub enum BokkenValidatorMessage {
91	Invoke {
92		nonce: u64,
93		program_id: Pubkey,
94		instruction: Vec<u8>,
95		account_metas: Vec<BorshAccountMeta>,
96		account_datas: HashMap<Pubkey, BokkenAccountData>,
97		call_depth: u8
98	},
99	CrossProgramInvokeResult {
100		nonce: u64,
101		return_code: u64,
102		account_datas: HashMap<Pubkey, BokkenAccountData>
103	}
104}