Skip to content

Commit 1c01982

Browse files
authored
Merge pull request #55 from ewasm/eth2-phase2
Eth 2 Phase 2 methods
2 parents 75837e9 + 9468730 commit 1c01982

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ default = ["std", "wee_alloc"]
1717
std = []
1818
debug = []
1919
experimental = []
20+
eth2 = []

circle.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ jobs:
4141
cargo build --release --no-default-features --features experimental
4242
cargo build --release --features experimental,debug
4343
cargo build --release --no-default-features --features experimental,debug
44+
cargo build --release --features eth2
45+
cargo build --release --no-default-features --features eth2

src/eth2.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pub mod debug;
5353
#[cfg(feature = "experimental")]
5454
pub mod bignum;
5555

56+
#[cfg(feature = "eth2")]
57+
pub mod eth2;
58+
5659
#[cfg(not(feature = "std"))]
5760
pub mod convert;
5861

0 commit comments

Comments
 (0)