Skip to content

Commit

Permalink
Fixing topics
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Sep 6, 2024
1 parent bc41883 commit 696c461
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ use uapi::{HostFn, HostFnImpl as api};
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {
let buffer = [1u8, 2, 3, 4];
api::deposit_event(&[0u8; 0], &buffer);
let topics = [[0u8; 32]; 0];
api::deposit_event(&topics, &buffer);
api::return_value(uapi::ReturnFlags::empty(), &buffer);
}

Expand Down
3 changes: 2 additions & 1 deletion substrate/frame/revive/fixtures/contracts/event_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub extern "C" fn call() {
input!(len: u32,);

let data = &BUFFER[..len as usize];
let topics = [[0u8; 32]; 0];

api::deposit_event(&[0u8; 0], data);
api::deposit_event(&topics, data);
}
5 changes: 5 additions & 0 deletions substrate/frame/revive/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ where
MomentOf<T>: Into<U256>,
T::Hash: IsType<H256>,
{
/// The address of the contract.
fn address(&self) -> H160 {
T::AddressMapper::to_address(&self.account_id)
}

/// Create new contract and use a default account id as instantiator.
fn new(module: WasmModule, data: Vec<u8>) -> Result<Contract<T>, &'static str> {
Self::with_index(0, module, data)
Expand Down
29 changes: 16 additions & 13 deletions substrate/frame/revive/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,32 +1533,35 @@ pub mod env {
&mut self,
memory: &mut M,
topics_ptr: u32,
topics_len: u32,
num_topic: u32,
data_ptr: u32,
data_len: u32,
) -> Result<(), TrapReason> {
let num_topic = topics_len
.checked_div(core::mem::size_of::<H256>() as u32)
.ok_or("Zero sized topics are not allowed")?;
self.charge_gas(RuntimeCosts::DepositEvent { num_topic, len: data_len })?;

if num_topic > limits::NUM_EVENT_TOPICS {
return Err(Error::<E::T>::TooManyTopics.into());
}

if data_len > self.ext.max_value_size() {
return Err(Error::<E::T>::ValueTooLarge.into());
}

let topics: Vec<H256> = match topics_len {
let topics: Vec<H256> = match num_topic {
0 => Vec::new(),
_ => memory.read_as_unbounded(topics_ptr, topics_len)?,
_ => {
let mut v = Vec::with_capacity(num_topic as usize);
let topics_len = num_topic * core::mem::size_of::<H256>() as u32;
let buf = memory.read(topics_ptr, topics_len)?;
for chunk in buf.chunks_exact(core::mem::size_of::<H256>()) {
v.push(H256::from_slice(chunk));
}
v
},
};

// If there are more than `event_topics`, then trap.
if topics.len() as u32 > limits::NUM_EVENT_TOPICS {
return Err(Error::<E::T>::TooManyTopics.into());
}

let event_data = memory.read(data_ptr, data_len)?;

self.ext.deposit_event(topics, event_data);

Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/revive/uapi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ pub trait HostFn: private::Sealed {
///
/// # Parameters
///
/// - `topics`: The topics list encoded as `Vec<T::Hash>`. It can't contain duplicates.
fn deposit_event(topics: &[u8], data: &[u8]);
/// - `topics`: The topics list. It can't contain duplicates.
fn deposit_event(topics: &[[u8; 32]], data: &[u8]);

/// Recovers the ECDSA public key from the given message hash and signature.
///
Expand Down
6 changes: 3 additions & 3 deletions substrate/frame/revive/uapi/src/host/riscv32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ mod sys {
pub fn now(out_ptr: *mut u8);
pub fn minimum_balance(out_ptr: *mut u8);
pub fn deposit_event(
topics_ptr: *const u8,
topics_len: u32,
topics_ptr: *const [u8; 32],
num_topic: u32,
data_ptr: *const u8,
data_len: u32,
);
Expand Down Expand Up @@ -326,7 +326,7 @@ impl HostFn for HostFnImpl {
ret_code.into()
}

fn deposit_event(topics: &[u8], data: &[u8]) {
fn deposit_event(topics: &[[u8; 32]], data: &[u8]) {
unsafe {
sys::deposit_event(
topics.as_ptr(),
Expand Down

0 comments on commit 696c461

Please sign in to comment.