-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7acf6fc
commit e46f95f
Showing
5 changed files
with
119 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use multiversx_sc::codec::{DefaultErrorHandler, NestedEncodeOutput}; | ||
|
||
use crate::custom_buffer::AlwaysTopEncodedBuffer; | ||
|
||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
#[derive(TypeAbi, Clone, ManagedVecItem, Default)] | ||
pub struct CallData<M: ManagedTypeApi> { | ||
pub endpoint: ManagedBuffer<M>, | ||
pub gas_limit: u64, | ||
pub args: ManagedVec<M, ManagedBuffer<M>>, | ||
} | ||
|
||
impl<M: ManagedTypeApi> TopEncode for CallData<M> { | ||
fn top_encode<O>(&self, output: O) -> Result<(), codec::EncodeError> | ||
where | ||
O: codec::TopEncodeOutput, | ||
{ | ||
let mut nested_encode_output = output.start_nested_encode(); | ||
self.dep_encode(&mut nested_encode_output) | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi> NestedEncode for CallData<M> { | ||
fn dep_encode<O: NestedEncodeOutput>(&self, dest: &mut O) -> Result<(), codec::EncodeError> { | ||
let endpoint_len_u8 = self.endpoint.len() as u8; | ||
dest.push_byte(endpoint_len_u8); | ||
|
||
let endpoint_buffer = AlwaysTopEncodedBuffer::new(self.endpoint.clone()); | ||
endpoint_buffer.dep_encode(dest)?; | ||
|
||
let gas_limit_u32 = self.gas_limit as u32; | ||
gas_limit_u32.dep_encode(dest)?; | ||
|
||
let args_len_u8 = self.args.len() as u8; | ||
dest.push_byte(args_len_u8); | ||
|
||
for arg in &self.args { | ||
arg.dep_encode(dest)?; | ||
} | ||
|
||
Result::Ok(()) | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi> NestedDecode for CallData<M> { | ||
fn dep_decode<I: codec::NestedDecodeInput>(input: &mut I) -> Result<Self, DecodeError> { | ||
let mut temp_buffer = [0u8; u8::MAX as usize + 1]; | ||
let endpoint_len_u8 = u8::dep_decode(input)?; | ||
input.read_into( | ||
&mut temp_buffer[0..endpoint_len_u8 as usize], | ||
DefaultErrorHandler, | ||
)?; | ||
let endpoint_name = | ||
ManagedBuffer::new_from_bytes(&temp_buffer[0..endpoint_len_u8 as usize]); | ||
|
||
let gas_limit_u32 = u32::dep_decode(input)?; | ||
let gas_limit = gas_limit_u32 as u64; | ||
|
||
let args_len_u8 = u8::dep_decode(input)?; | ||
let mut args = ManagedVec::new(); | ||
for _ in 0..args_len_u8 { | ||
let arg = ManagedBuffer::dep_decode(input)?; | ||
args.push(arg); | ||
} | ||
|
||
core::result::Result::Ok(Self { | ||
endpoint: endpoint_name, | ||
gas_limit, | ||
args, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
pub struct AlwaysTopEncodedBuffer<M: ManagedTypeApi> { | ||
buffer: ManagedBuffer<M>, | ||
} | ||
|
||
impl<M: ManagedTypeApi> AlwaysTopEncodedBuffer<M> { | ||
#[inline(always)] | ||
pub fn new(buffer: ManagedBuffer<M>) -> Self { | ||
Self { buffer } | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi> TopEncode for AlwaysTopEncodedBuffer<M> { | ||
fn top_encode<O>(&self, output: O) -> Result<(), codec::EncodeError> | ||
where | ||
O: codec::TopEncodeOutput, | ||
{ | ||
self.buffer.top_encode(output) | ||
} | ||
} | ||
|
||
impl<M: ManagedTypeApi> NestedEncode for AlwaysTopEncodedBuffer<M> { | ||
fn dep_encode<O: codec::NestedEncodeOutput>( | ||
&self, | ||
dest: &mut O, | ||
) -> Result<(), codec::EncodeError> { | ||
let mut output_buffer = ManagedBuffer::<M>::new(); | ||
self.buffer.top_encode(&mut output_buffer)?; | ||
|
||
let mut output_buffer_bytes = [0u8; u8::MAX as usize + 1]; | ||
let slice_ref = output_buffer.load_to_byte_array(&mut output_buffer_bytes); | ||
dest.write(slice_ref); | ||
|
||
Result::Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters