Struct gstd::Reservations
source · pub struct Reservations(/* private fields */);
Expand description
Reservation manager.
The manager is used to control multiple gas reservations
across executions. It can be used when you only care about
reserved amounts and not concrete ReservationId
s.
§Examples
Create gas reservations inside init
and use them inside handle
.
use gstd::{msg, prelude::*, Reservations};
static mut RESERVATIONS: Reservations = Reservations::new();
#[no_mangle]
extern "C" fn init() {
unsafe {
RESERVATIONS
.reserve(200_000, 50)
.expect("failed to reserve gas");
RESERVATIONS
.reserve(100_000, 100)
.expect("failed to reserve gas");
RESERVATIONS
.reserve(50_000, 30)
.expect("failed to reserve gas");
}
}
#[no_mangle]
extern "C" fn handle() {
let reservation = unsafe { RESERVATIONS.try_take_reservation(100_000) };
if let Some(reservation) = reservation {
msg::send_bytes_from_reservation(
reservation.id(),
msg::source(),
"send_bytes_from_reservation",
0,
)
.expect("Failed to send message from reservation");
} else {
msg::send_bytes(msg::source(), "send_bytes", 0).expect("Failed to send message");
}
}
§See also
ReservationId
is used to reserve and unreserve gas for program execution later.Reservation
stores some additional data along withReservationId
.
Implementations§
source§impl Reservations
impl Reservations
sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new Reservations
struct.
sourcepub fn reserve(&mut self, amount: u64, duration: u32) -> Result<()>
pub fn reserve(&mut self, amount: u64, duration: u32) -> Result<()>
Reserve the amount
of gas for further usage.
duration
is the block count within which the reservation must be used.
§Underlying logics
Executes for O(logN)..O(N), where N is a number of stored reservations.
All the reservations are kept sorted by amount in
ascending order(when amount is the same, they’re sorted by time when
they expire) when inserted, so the closer inserted element to the
beginning of the underlying Vec
the closer execution time will be
to O(N).
Also, when the underlying Vec
will allocate new memory
the attempt to clean expired reservations occurs to avoid memory
allocations.
sourcepub fn try_take_reservation(&mut self, amount: u64) -> Option<Reservation>
pub fn try_take_reservation(&mut self, amount: u64) -> Option<Reservation>
Find the appropriate reservation with reserved amount greater than or
equal to amount
.
If such a reservation is found, Reservation
is returned.
§Underlying logics
Executes for O(logN)..O(N), where N is amount of stored reservations. When there’s many expired reservations execution time is closer to O(N).
All the reservations are sorted by their amount and then by the time
when they’ll expire (both are in ascending order) in underlying Vec
,
so when one’s trying to take reservation, reservation with the least
possible amount is found and if it’s already expired, the search to the
end of underlying Vec
occurs. After that, all the expired
reservations that were found in process of search are cleaned out.
§See also
ReservationId
is used to reserve and unreserve gas amount for program execution later.Reservation
stores some additional data along withReservationId
.
sourcepub fn count_valid(&self) -> usize
pub fn count_valid(&self) -> usize
Returns an amount of the stored reservations that aren’t expired at this time.
Executes for O(N) where N is amount of stored reservations.
Trait Implementations§
source§impl Clone for Reservations
impl Clone for Reservations
source§fn clone(&self) -> Reservations
fn clone(&self) -> Reservations
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Reservations
impl Debug for Reservations
source§impl Decode for Reservations
impl Decode for Reservations
source§fn decode<__CodecInputEdqy: Input>(
__codec_input_edqy: &mut __CodecInputEdqy,
) -> Result<Self, Error>
fn decode<__CodecInputEdqy: Input>( __codec_input_edqy: &mut __CodecInputEdqy, ) -> Result<Self, Error>
§fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>,
) -> Result<DecodeFinished, Error>where
I: Input,
fn decode_into<I>(
input: &mut I,
dst: &mut MaybeUninit<Self>,
) -> Result<DecodeFinished, Error>where
I: Input,
§fn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
fn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
§fn encoded_fixed_size() -> Option<usize>
fn encoded_fixed_size() -> Option<usize>
source§impl Default for Reservations
impl Default for Reservations
source§fn default() -> Reservations
fn default() -> Reservations
source§impl Encode for Reservations
impl Encode for Reservations
source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
source§fn encode_to<__CodecOutputEdqy: Output + ?Sized>(
&self,
__codec_dest_edqy: &mut __CodecOutputEdqy,
)
fn encode_to<__CodecOutputEdqy: Output + ?Sized>( &self, __codec_dest_edqy: &mut __CodecOutputEdqy, )
source§fn using_encoded<__CodecOutputReturn, __CodecUsingEncodedCallback: FnOnce(&[u8]) -> __CodecOutputReturn>(
&self,
f: __CodecUsingEncodedCallback,
) -> __CodecOutputReturn
fn using_encoded<__CodecOutputReturn, __CodecUsingEncodedCallback: FnOnce(&[u8]) -> __CodecOutputReturn>( &self, f: __CodecUsingEncodedCallback, ) -> __CodecOutputReturn
§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
source§impl Hash for Reservations
impl Hash for Reservations
source§impl TypeInfo for Reservations
impl TypeInfo for Reservations
impl EncodeLike for Reservations
Auto Trait Implementations§
impl Freeze for Reservations
impl RefUnwindSafe for Reservations
impl Send for Reservations
impl Sync for Reservations
impl Unpin for Reservations
impl UnwindSafe for Reservations
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere
T: Decode,
§impl<T> KeyedVec for Twhere
T: Codec,
impl<T> KeyedVec for Twhere
T: Codec,
§fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8>
fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8>
Self
prepended by given slice.