|
| 1 | +//! This is the work in progress interface for Eth 2 Phase 2. |
| 2 | +//! |
| 3 | +//! Please check Ewasm [Scout](https://github.com/ewasm/scout) for more details. |
| 4 | +
|
| 5 | +use super::*; |
| 6 | + |
| 7 | +mod native { |
| 8 | + extern "C" { |
| 9 | + pub fn eth2_loadPreStateRoot(offset: *const u32); |
| 10 | + pub fn eth2_blockDataSize() -> u32; |
| 11 | + pub fn eth2_blockDataCopy(outputOfset: *const u32, offset: u32, length: u32); |
| 12 | + pub fn eth2_savePostStateRoot(offset: *const u32); |
| 13 | + pub fn eth2_pushNewDeposit(offset: *const u32, length: u32); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +/// Load current state root. |
| 18 | +pub fn load_pre_state_root() -> Bytes32 { |
| 19 | + let mut ret = Bytes32::default(); |
| 20 | + |
| 21 | + unsafe { native::eth2_loadPreStateRoot(ret.bytes.as_mut_ptr() as *const u32) } |
| 22 | + |
| 23 | + ret |
| 24 | +} |
| 25 | + |
| 26 | +/// Returns the length of the "block data" supplied with the current block. |
| 27 | +pub fn block_data_size() -> usize { |
| 28 | + unsafe { native::eth2_blockDataSize() as usize } |
| 29 | +} |
| 30 | + |
| 31 | +/// Copies a slices from the "block data", but does not check for overflow. |
| 32 | +pub fn unsafe_block_data_copy(from: usize, length: usize, ret: &mut [u8]) { |
| 33 | + unsafe { |
| 34 | + native::eth2_blockDataCopy(ret.as_mut_ptr() as *const u32, from as u32, length as u32); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(feature = "std")] |
| 39 | +/// Returns a vector containing the entire "block data". |
| 40 | +pub fn acquire_block_data() -> Vec<u8> { |
| 41 | + let length = block_data_size(); |
| 42 | + |
| 43 | + let mut ret: Vec<u8> = unsafe_alloc_buffer(length); |
| 44 | + unsafe_block_data_copy(0, length, &mut ret); |
| 45 | + ret |
| 46 | +} |
| 47 | + |
| 48 | +/// Returns the segment of "block data" beginning at `from`, and continuing for `length` bytes. |
| 49 | +pub fn block_data_copy(from: usize, length: usize, ret: &mut [u8]) -> Result<(), Error> { |
| 50 | + let size = block_data_size(); |
| 51 | + |
| 52 | + if (size < from) || ((size - from) < length) || (ret.len() < length) { |
| 53 | + Err(Error::OutOfBoundsCopy) |
| 54 | + } else { |
| 55 | + unsafe_block_data_copy(from, length, ret); |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Push new deposit receipt. |
| 61 | +pub fn push_new_deposit(deposit: &[u8]) { |
| 62 | + unsafe { native::eth2_pushNewDeposit(deposit.as_ptr() as *const u32, deposit.len() as u32) } |
| 63 | +} |
| 64 | + |
| 65 | +/// Save new state root. |
| 66 | +pub fn save_post_state_root(state: Bytes32) { |
| 67 | + unsafe { native::eth2_savePostStateRoot(state.bytes.as_ptr() as *const u32) } |
| 68 | +} |
0 commit comments