light_compressible/rent/
config.rs1use aligned_sized::aligned_sized;
2use bytemuck::{Pod, Zeroable};
3use light_zero_copy::{ZeroCopy, ZeroCopyMut};
4
5use crate::{AnchorDeserialize, AnchorSerialize};
6pub const COMPRESSION_COST: u16 = 10_000;
7pub const COMPRESSION_INCENTIVE: u16 = 1000;
8
9pub const BASE_RENT: u16 = 128; pub const RENT_PER_BYTE: u8 = 1; pub const SLOTS_PER_EPOCH: u64 = 13500; pub trait RentConfigTrait {
20 fn base_rent(&self) -> u64;
22
23 fn compression_cost(&self) -> u64;
25
26 fn lamports_per_byte_per_epoch(&self) -> u64;
28
29 fn max_funded_epochs(&self) -> u64;
31
32 fn max_top_up(&self) -> u64;
34
35 #[inline(always)]
37 fn rent_curve_per_epoch(&self, num_bytes: u64) -> u64 {
38 self.base_rent() + num_bytes * self.lamports_per_byte_per_epoch()
39 }
40
41 #[inline(always)]
43 fn get_rent(&self, num_bytes: u64, epochs: u64) -> u64 {
44 self.rent_curve_per_epoch(num_bytes) * epochs
45 }
46
47 #[inline(always)]
49 fn get_rent_with_compression_cost(&self, num_bytes: u64, epochs: u64) -> u64 {
50 self.get_rent(num_bytes, epochs) + self.compression_cost()
51 }
52}
53
54#[derive(
57 Debug,
58 Clone,
59 Hash,
60 Copy,
61 PartialEq,
62 Eq,
63 AnchorSerialize,
64 AnchorDeserialize,
65 ZeroCopy,
66 ZeroCopyMut,
67 Pod,
68 Zeroable,
69)]
70#[repr(C)]
71#[aligned_sized]
72pub struct RentConfig {
73 pub base_rent: u16,
75 pub compression_cost: u16,
76 pub lamports_per_byte_per_epoch: u8,
77 pub max_funded_epochs: u8, pub max_top_up: u16,
81}
82
83impl Default for RentConfig {
84 fn default() -> Self {
85 Self {
86 base_rent: BASE_RENT,
87 compression_cost: COMPRESSION_COST + COMPRESSION_INCENTIVE,
88 lamports_per_byte_per_epoch: RENT_PER_BYTE,
89 max_funded_epochs: 2, max_top_up: 12416, }
92 }
93}
94
95impl RentConfigTrait for RentConfig {
96 #[inline(always)]
97 fn base_rent(&self) -> u64 {
98 self.base_rent as u64
99 }
100
101 #[inline(always)]
102 fn compression_cost(&self) -> u64 {
103 self.compression_cost as u64
104 }
105
106 #[inline(always)]
107 fn lamports_per_byte_per_epoch(&self) -> u64 {
108 self.lamports_per_byte_per_epoch as u64
109 }
110
111 #[inline(always)]
112 fn max_funded_epochs(&self) -> u64 {
113 self.max_funded_epochs as u64
114 }
115
116 #[inline(always)]
117 fn max_top_up(&self) -> u64 {
118 self.max_top_up as u64
119 }
120}
121
122impl RentConfig {
123 pub fn rent_curve_per_epoch(&self, num_bytes: u64) -> u64 {
124 RentConfigTrait::rent_curve_per_epoch(self, num_bytes)
125 }
126 pub fn get_rent(&self, num_bytes: u64, epochs: u64) -> u64 {
127 RentConfigTrait::get_rent(self, num_bytes, epochs)
128 }
129 pub fn get_rent_with_compression_cost(&self, num_bytes: u64, epochs: u64) -> u64 {
130 RentConfigTrait::get_rent_with_compression_cost(self, num_bytes, epochs)
131 }
132}
133
134impl RentConfigTrait for ZRentConfig<'_> {
136 #[inline(always)]
137 fn base_rent(&self) -> u64 {
138 self.base_rent.into()
139 }
140
141 #[inline(always)]
142 fn compression_cost(&self) -> u64 {
143 self.compression_cost.into()
144 }
145
146 #[inline(always)]
147 fn lamports_per_byte_per_epoch(&self) -> u64 {
148 self.lamports_per_byte_per_epoch as u64
149 }
150
151 #[inline(always)]
152 fn max_funded_epochs(&self) -> u64 {
153 self.max_funded_epochs as u64
154 }
155
156 #[inline(always)]
157 fn max_top_up(&self) -> u64 {
158 self.max_top_up.into()
159 }
160}
161
162impl RentConfigTrait for ZRentConfigMut<'_> {
164 #[inline(always)]
165 fn base_rent(&self) -> u64 {
166 self.base_rent.into()
167 }
168
169 #[inline(always)]
170 fn compression_cost(&self) -> u64 {
171 self.compression_cost.into()
172 }
173
174 #[inline(always)]
175 fn lamports_per_byte_per_epoch(&self) -> u64 {
176 self.lamports_per_byte_per_epoch as u64
177 }
178
179 #[inline(always)]
180 fn max_funded_epochs(&self) -> u64 {
181 self.max_funded_epochs as u64
182 }
183
184 #[inline(always)]
185 fn max_top_up(&self) -> u64 {
186 self.max_top_up.into()
187 }
188}
189
190impl ZRentConfigMut<'_> {
191 pub fn set(&mut self, config: &RentConfig) {
193 self.base_rent = config.base_rent.into();
194 self.compression_cost = config.compression_cost.into();
195 self.lamports_per_byte_per_epoch = config.lamports_per_byte_per_epoch;
196 self.max_funded_epochs = config.max_funded_epochs;
197 self.max_top_up = config.max_top_up.into();
198 }
199}