Skip to content

Commit

Permalink
fix(sdk+runtime): identity exposure (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx authored Oct 10, 2024
1 parent 3a47246 commit 78ed9d0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
13 changes: 8 additions & 5 deletions crates/runtime/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ impl<'a> VMLogic<'a> {
}
.build()
}

pub fn get_executor_identity(&mut self, register_id: u64) -> VMLogicResult<()> {
self.registers
.set(self.limits, register_id, self.context.executor_public_key)
}
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -251,6 +246,14 @@ impl VMHostFunctions<'_> {
Ok(1)
}

pub fn executor_id(&mut self, register_id: u64) -> VMLogicResult<()> {
self.with_logic_mut(|logic| {
logic
.registers
.set(logic.limits, register_id, logic.context.executor_public_key)
})
}

pub fn input(&mut self, register_id: u64) -> VMLogicResult<()> {
self.with_logic_mut(|logic| {
logic
Expand Down
2 changes: 2 additions & 0 deletions crates/runtime/src/logic/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ impl VMLogic<'_> {
fn register_len(register_id: u64) -> u64;
fn read_register(register_id: u64, ptr: u64, len: u64) -> u32;

fn executor_id(register_id: u64);

fn input(register_id: u64);
fn value_return(tag: u64, value_ptr: u64, value_len: u64);
fn log_utf8(ptr: u64, len: u64);
Expand Down
18 changes: 10 additions & 8 deletions crates/sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ fn expected_boolean<T>(e: u32) -> T {
panic_str(&format!("Expected 0|1. Got {e}"));
}

#[must_use]
pub fn get_executor_identity() -> [u8; 32] {
unsafe { sys::get_executor_identity(DATA_REGISTER) }
read_register_sized(DATA_REGISTER).expect("Must have executor identity.")
}

pub fn setup_panic_hook() {
set_hook(Box::new(|info| {
#[expect(clippy::option_if_let_else, reason = "Clearer this way")]
Expand Down Expand Up @@ -106,9 +100,11 @@ pub fn read_register(register_id: RegisterId) -> Option<Vec<u8>> {
#[inline]
fn read_register_sized<const N: usize>(register_id: RegisterId) -> Option<[u8; N]> {
let len = register_len(register_id)?;
let buffer = [0; N];

let mut buffer = [0; N];

let succeed: bool = unsafe {
sys::read_register(register_id, BufferMut::new(buffer))
sys::read_register(register_id, BufferMut::new(&mut buffer))
.try_into()
.unwrap_or_else(expected_boolean)
};
Expand All @@ -122,6 +118,12 @@ fn read_register_sized<const N: usize>(register_id: RegisterId) -> Option<[u8; N
Some(buffer)
}

#[must_use]
pub fn executor_id() -> [u8; 32] {
unsafe { sys::executor_id(DATA_REGISTER) }
read_register_sized(DATA_REGISTER).expect("Must have executor identity.")
}

#[inline]
#[must_use]
pub fn input() -> Option<Vec<u8>> {
Expand Down
3 changes: 2 additions & 1 deletion crates/sdk/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ wasm_imports! {
fn register_len(register_id: RegisterId) -> PtrSizedInt;
fn read_register(register_id: RegisterId, buf: BufferMut<'_>) -> Bool;
// --
fn get_executor_identity(register_id: RegisterId);
fn executor_id(register_id: RegisterId);
// --
fn input(register_id: RegisterId);
fn value_return(value: ValueReturn<'_>);
fn log_utf8(msg: Buffer<'_>);
Expand Down

0 comments on commit 78ed9d0

Please sign in to comment.