Skip to content

Commit

Permalink
Fix all the stuff I broke :(
Browse files Browse the repository at this point in the history
  • Loading branch information
Cashmaney committed Nov 1, 2023
1 parent cf60e62 commit 6168193
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 81 deletions.
12 changes: 6 additions & 6 deletions cosmwasm/enclaves/execute/Enclave.edl
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ enclave {
);


public sgx_status_t ecall_submit_store_roots(
[in, count=in_roots_len] const uint8_t* in_roots,
uintptr_t in_roots_len,
[in, count=in_compute_root_len] const uint8_t* in_compute_root,
uintptr_t in_compute_root_len
);
//public sgx_status_t ecall_submit_store_roots(
// [in, count=in_roots_len] const uint8_t* in_roots,
// uintptr_t in_roots_len,
// [in, count=in_compute_root_len] const uint8_t* in_compute_root,
// uintptr_t in_compute_root_len
//);

};

Expand Down
92 changes: 46 additions & 46 deletions cosmwasm/enclaves/execute/src/ecalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,52 +55,52 @@ pub unsafe extern "C" fn ecall_submit_block_signatures(
sgx_status_t::SGX_ERROR_ECALL_NOT_ALLOWED
}
}

#[no_mangle]
#[allow(unused_variables)]
pub unsafe extern "C" fn ecall_submit_store_roots(
in_roots: *const u8,
in_roots_len: u32,
in_compute_root: *const u8,
in_compute_root_len: u32,
) -> sgx_status_t {
let failed_call = || sgx_status_t::SGX_ERROR_INVALID_PARAMETER;

validate_input_length!(in_roots_len, "roots", MAX_ROOTS_LENGTH, failed_call());
validate_const_ptr!(
in_roots,
in_roots_len as usize,
sgx_status_t::SGX_ERROR_INVALID_PARAMETER
);
validate_input_length!(
in_compute_root_len,
"compute_root",
MAX_MERKLE_ROOT_LENGTH,
failed_call()
);
validate_const_ptr!(
in_compute_root,
in_compute_root_len as usize,
sgx_status_t::SGX_ERROR_INVALID_PARAMETER
);

let store_roots_slice = slice::from_raw_parts(in_roots, in_roots_len as usize);
let compute_root_slice = slice::from_raw_parts(in_compute_root, in_compute_root_len as usize);

let store_roots: Pairs = Pairs::decode(store_roots_slice).unwrap();
let mut store_roots_bytes = vec![];

// Encode all key-value pairs to bytes
for root in store_roots.pairs {
store_roots_bytes.push(pair_to_bytes(root));
}

let h = merkle::simple_hash_from_byte_vectors(store_roots_bytes);
debug!("received app_hash: {:?}", h);
debug!("received compute_root: {:?}", compute_root_slice);

sgx_status_t::SGX_SUCCESS
}
//
// #[no_mangle]
// #[allow(unused_variables)]
// pub unsafe extern "C" fn ecall_submit_store_roots(
// in_roots: *const u8,
// in_roots_len: u32,
// in_compute_root: *const u8,
// in_compute_root_len: u32,
// ) -> sgx_status_t {
// let failed_call = || sgx_status_t::SGX_ERROR_INVALID_PARAMETER;
//
// validate_input_length!(in_roots_len, "roots", MAX_ROOTS_LENGTH, failed_call());
// validate_const_ptr!(
// in_roots,
// in_roots_len as usize,
// sgx_status_t::SGX_ERROR_INVALID_PARAMETER
// );
// validate_input_length!(
// in_compute_root_len,
// "compute_root",
// MAX_MERKLE_ROOT_LENGTH,
// failed_call()
// );
// validate_const_ptr!(
// in_compute_root,
// in_compute_root_len as usize,
// sgx_status_t::SGX_ERROR_INVALID_PARAMETER
// );
//
// let store_roots_slice = slice::from_raw_parts(in_roots, in_roots_len as usize);
// let compute_root_slice = slice::from_raw_parts(in_compute_root, in_compute_root_len as usize);
//
// let store_roots: Pairs = Pairs::decode(store_roots_slice).unwrap();
// let mut store_roots_bytes = vec![];
//
// // Encode all key-value pairs to bytes
// for root in store_roots.pairs {
// store_roots_bytes.push(pair_to_bytes(root));
// }
//
// let h = merkle::simple_hash_from_byte_vectors(store_roots_bytes);
// debug!("received app_hash: {:?}", h);
// debug!("received compute_root: {:?}", compute_root_slice);
//
// sgx_status_t::SGX_SUCCESS
// }

// This is a copy of a cosmos-sdk function: https://github.com/scrtlabs/cosmos-sdk/blob/1b9278476b3ac897d8ebb90241008476850bf212/store/internal/maps/maps.go#LL152C1-L152C1
// Returns key || value, with both the key and value length prefixed.
Expand Down
46 changes: 18 additions & 28 deletions cosmwasm/packages/sgx-vm/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use log::{debug, error, warn};

use crate::enclave::ENCLAVE_DOORBELL;

const RETRY_LIMIT: i32 = 3;

extern "C" {
pub fn ecall_submit_store_roots(
pub fn ecall_app_begin_blocker(
eid: sgx_enclave_id_t,
retval: *mut sgx_status_t,
retval: *mut SdkBeginBlockerResult,
in_roots: *const u8,
in_roots_len: u32,
in_compute_root: *const u8,
Expand All @@ -19,41 +21,29 @@ extern "C" {
pub fn untrusted_submit_store_roots(roots: &[u8], compute_root: &[u8]) -> SgxResult<()> {
debug!("Hello from just before - untrusted_submit_store_roots");

const RETRY_LIMIT: i32 = 3;

let mut retry_count = 0;

// this is here so we can
loop {
for _ in 0..RETRY_LIMIT {
let (retval, status) = submit_store_roots_impl(roots, compute_root)?;

if status != sgx_status_t::SGX_SUCCESS {
return Err(status);
} else if retval != sgx_status_t::SGX_SUCCESS {
if retval == sgx_status_t::SGX_ERROR_FILE_RECOVERY_NEEDED {
warn!(
"Validator set read by enclave was mismatched with current height.. retrying"
);
// retry with
std::thread::sleep(std::time::Duration::from_millis(500));
retry_count += 1;

if retry_count == RETRY_LIMIT {
error!("Validator timed out while waiting for correct validator set");
return Err(retval);
}
} else {
return Err(retval);
}
} else {
} else if retval == SdkBeginBlockerResult::Success {
return Ok(());
} else if retval == SdkBeginBlockerResult::Failure {
warn!("Validator set read by enclave was mismatched with current height.. retrying");
std::thread::sleep(std::time::Duration::from_millis(500));
} else {
return Err(sgx_status_t::SGX_ERROR_UNEXPECTED);
}
}

error!("Validator timed out while waiting for correct validator set");
Err(sgx_status_t::SGX_ERROR_UNEXPECTED) // or any appropriate error
}

fn submit_store_roots_impl(
roots: &[u8],
compute_root: &[u8],
) -> SgxResult<(sgx_status_t, sgx_status_t)> {
) -> SgxResult<(SdkBeginBlockerResult, sgx_status_t)> {
// Bind the token to a local variable to ensure its
// destructor runs in the end of the function
let enclave_access_token = ENCLAVE_DOORBELL
Expand All @@ -62,11 +52,11 @@ fn submit_store_roots_impl(
let enclave = (*enclave_access_token)?;

let eid = enclave.geteid();
let mut retval = sgx_status_t::SGX_SUCCESS;
let mut retval = SdkBeginBlockerResult::Success;

// let status = unsafe { ecall_get_encrypted_seed(eid, &mut retval, cert, cert_len, & mut seed) };
let status = unsafe {
ecall_submit_store_roots(
ecall_app_begin_blocker(
eid,
&mut retval,
roots.as_ptr(),
Expand Down
2 changes: 1 addition & 1 deletion go-cosmwasm/api/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func cGetNoProof(ptr *C.db_t, gasMeter *C.gas_meter_t, usedGas *u64, key C.Buffe
}

//export cGet
func cGet(ptr *C.db_t, gasMeter *C.gas_meter_t, usedGas *u64, block_height u64, key C.Buffer, val *C.Buffer, merkle_p *C.Buffer, mp_key *C.Buffer, errOut *C.Buffer) (ret C.GoResult) {
func cGet(ptr *C.db_t, gasMeter *C.gas_meter_t, usedGas *u64, block_height u64, key C.Buffer, val *C.Buffer, merkle_p *C.Buffer, mp_key *C.Buffer, _ *C.Buffer) (ret C.GoResult) {
defer recoverPanic(&ret)
if ptr == nil || gasMeter == nil || usedGas == nil || val == nil {
// we received an invalid pointer
Expand Down

0 comments on commit 6168193

Please sign in to comment.