Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use slices instead of Vec #1158

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion service/src/ocall_bridge/bridge_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub trait MetricsBridge {
/// Trait for all the OCalls related to sidechain operations
#[cfg_attr(test, automock)]
pub trait SidechainBridge {
fn propose_sidechain_blocks(&self, signed_blocks_encoded: Vec<u8>) -> OCallBridgeResult<()>;
fn propose_sidechain_blocks(&self, signed_blocks_encoded: &[u8]) -> OCallBridgeResult<()>;

fn store_sidechain_blocks(&self, signed_blocks_encoded: Vec<u8>) -> OCallBridgeResult<()>;

Expand Down
4 changes: 2 additions & 2 deletions service/src/ocall_bridge/ffi/propose_sidechain_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn propose_sidechain_blocks(
signed_blocks_size: u32,
sidechain_api: Arc<dyn SidechainBridge>,
) -> sgx_status_t {
let signed_blocks_vec: Vec<u8> =
unsafe { Vec::from(slice::from_raw_parts(signed_blocks_ptr, signed_blocks_size as usize)) };
let signed_blocks_vec =
unsafe { slice::from_raw_parts(signed_blocks_ptr, signed_blocks_size as usize) };

match sidechain_api.propose_sidechain_blocks(signed_blocks_vec) {
Ok(_) => sgx_status_t::SGX_SUCCESS,
Expand Down
23 changes: 12 additions & 11 deletions service/src/ocall_bridge/sidechain_ocall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,22 @@ where
PeerBlockFetcher: FetchBlocksFromPeer<SignedBlockType = SignedSidechainBlock>,
TokioHandle: GetTokioHandle,
{
fn propose_sidechain_blocks(&self, signed_blocks_encoded: Vec<u8>) -> OCallBridgeResult<()> {
fn propose_sidechain_blocks(&self, signed_blocks_encoded: &[u8]) -> OCallBridgeResult<()> {
// TODO: improve error handling, using a mut status is not good design?
let mut status: OCallBridgeResult<()> = Ok(());

let mut slice = signed_blocks_encoded;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the "trick": Create a mutable copy of the slice (as decode() needs it). Maybe this could be written nicer?

// handle blocks
let signed_blocks: Vec<SignedSidechainBlock> =
match Decode::decode(&mut signed_blocks_encoded.as_slice()) {
Ok(blocks) => blocks,
Err(_) => {
status = Err(OCallBridgeError::ProposeSidechainBlock(
"Could not decode signed blocks".to_string(),
));
vec![]
},
};

let signed_blocks: Vec<SignedSidechainBlock> = match Decode::decode(&mut slice) {
Ok(blocks) => blocks,
Err(_) => {
status = Err(OCallBridgeError::ProposeSidechainBlock(
"Could not decode signed blocks".to_string(),
));
vec![]
},
};

if !signed_blocks.is_empty() {
info!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl SidechainBridgeMock {
}

impl SidechainBridge for SidechainBridgeMock {
fn propose_sidechain_blocks(&self, _signed_blocks_encoded: Vec<u8>) -> OCallBridgeResult<()> {
fn propose_sidechain_blocks(&self, _signed_blocks_encoded: &[u8]) -> OCallBridgeResult<()> {
Ok(())
}

Expand Down