Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge ulm with ulm_hooks #32

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/ulm/erc20/rust/src/assertions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

use crate::ulm_hooks;
use crate::ulm;

pub fn fail(msg: &str) -> ! {
ulm_hooks::failWrapper(msg);
ulm::failWrapper(msg);
}

#[macro_export]
Expand Down
1 change: 0 additions & 1 deletion tests/ulm/erc20/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod predicate;
mod storage;
mod unsigned;
mod ulm;
mod ulm_hooks;

mod encoding_tests;
mod storage_tests;
Expand Down
11 changes: 4 additions & 7 deletions tests/ulm/erc20/rust/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use crate::encoder::Encodable;
use crate::encoder::Encoder;
use crate::unsigned::U256;
use crate::ulm;
use crate::ulm_hooks;

pub struct SingleChunkStorage<'a, ValueType>
where
Expand Down Expand Up @@ -74,25 +73,23 @@ pub struct SingleChunkStorageBuilder<'a, ValueType>
{
phantom_value: PhantomData<&'a ValueType>,
api: Rc<RefCell<dyn ulm::Ulm>>,
hooks_api: Rc<RefCell<dyn ulm_hooks::UlmHooks>>,
encoder: Encoder,
}

impl<'a, ValueType> SingleChunkStorageBuilder<'a, ValueType>
where
ValueType: Into<U256> + TryFrom<U256>,
{
pub fn new(api: Rc<RefCell<dyn ulm::Ulm>>, hooks_api: Rc<RefCell<dyn ulm_hooks::UlmHooks>>, name: &String) -> Self {
pub fn new(api: Rc<RefCell<dyn ulm::Ulm>>, name: &String) -> Self {
let mut encoder = Encoder::new();
encoder.add(name);
Self::from_encoder(api, hooks_api, encoder)
Self::from_encoder(api, encoder)
}

fn from_encoder(api: Rc<RefCell<dyn ulm::Ulm>>, hooks_api: Rc<RefCell<dyn ulm_hooks::UlmHooks>>, encoder: Encoder) -> Self {
fn from_encoder(api: Rc<RefCell<dyn ulm::Ulm>>, encoder: Encoder) -> Self {
SingleChunkStorageBuilder::<ValueType> {
phantom_value: PhantomData,
api,
hooks_api,
encoder,
}
}
Expand All @@ -103,7 +100,7 @@ impl<'a, ValueType> SingleChunkStorageBuilder<'a, ValueType>

pub fn build(&mut self) -> SingleChunkStorage<ValueType> {
let bytes = self.encoder.encode();
let fingerprint = ulm_hooks::keccak_hash_int(&*self.hooks_api.borrow(), &bytes);
let fingerprint = ulm::keccak_hash_int(&*self.api.borrow(), &bytes);
SingleChunkStorage::new(self.api.clone(), fingerprint)
}
}
24 changes: 9 additions & 15 deletions tests/ulm/erc20/rust/src/storage_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
mod encoding_tests {
use crate::storage::*;
use crate::ulm;
use crate::ulm_hooks;
use crate::unsigned::*;

#[test]
fn read_value_not_set() {
let api = ulm::mock::UlmMock::new();
let hooks_api = ulm_hooks::mock::UlmHooksMock::new();
let mut builder = SingleChunkStorageBuilder::<U256>::new(api, hooks_api, &("my_storage".to_string()));
let mut builder = SingleChunkStorageBuilder::<U256>::new(api, &("my_storage".to_string()));

let storage = builder.build();
let value: U256 = storage.get();
Expand All @@ -20,8 +18,7 @@ mod encoding_tests {
#[test]
fn write_read_u256() {
let api = ulm::mock::UlmMock::new();
let hooks_api = ulm_hooks::mock::UlmHooksMock::new();
let mut builder = SingleChunkStorageBuilder::<U256>::new(api, hooks_api, &("my_storage".to_string()));
let mut builder = SingleChunkStorageBuilder::<U256>::new(api, &("my_storage".to_string()));

let mut storage = builder.build();
storage.set(U256::from_u64(123456789));
Expand All @@ -33,8 +30,7 @@ mod encoding_tests {
#[test]
fn write_read_u8() {
let api = ulm::mock::UlmMock::new();
let hooks_api = ulm_hooks::mock::UlmHooksMock::new();
let mut builder = SingleChunkStorageBuilder::<Unsigned::<1>>::new(api, hooks_api, &("my_storage".to_string()));
let mut builder = SingleChunkStorageBuilder::<Unsigned::<1>>::new(api, &("my_storage".to_string()));

let mut storage = builder.build();
storage.set(Unsigned::<1>::from_u64(123));
Expand All @@ -46,9 +42,8 @@ mod encoding_tests {
#[test]
fn write_read_args() {
let api = ulm::mock::UlmMock::new();
let hooks_api = ulm_hooks::mock::UlmHooksMock::new();

let mut builder = SingleChunkStorageBuilder::<U256>::new(api, hooks_api, &("my_storage".to_string()));
let mut builder = SingleChunkStorageBuilder::<U256>::new(api, &("my_storage".to_string()));

builder.add_arg(&U256::from_u64(5));

Expand All @@ -62,13 +57,12 @@ mod encoding_tests {
#[test]
fn no_confusion() {
let api = ulm::mock::UlmMock::new();
let hooks_api = ulm_hooks::mock::UlmHooksMock::new();

let mut builder1 = SingleChunkStorageBuilder::<U256>::new(api.clone(), hooks_api.clone(), &("my_storage".to_string()));
let mut builder2 = SingleChunkStorageBuilder::<U256>::new(api.clone(), hooks_api.clone(), &("my_storage1".to_string()));
let mut builder3 = SingleChunkStorageBuilder::<U256>::new(api.clone(), hooks_api.clone(), &("my_storage".to_string()));
let mut builder4 = SingleChunkStorageBuilder::<U256>::new(api.clone(), hooks_api.clone(), &("my_storage".to_string()));
let mut builder5 = SingleChunkStorageBuilder::<U256>::new(api, hooks_api, &("my_storage".to_string()));
let mut builder1 = SingleChunkStorageBuilder::<U256>::new(api.clone(), &("my_storage".to_string()));
let mut builder2 = SingleChunkStorageBuilder::<U256>::new(api.clone(), &("my_storage1".to_string()));
let mut builder3 = SingleChunkStorageBuilder::<U256>::new(api.clone(), &("my_storage".to_string()));
let mut builder4 = SingleChunkStorageBuilder::<U256>::new(api.clone(), &("my_storage".to_string()));
let mut builder5 = SingleChunkStorageBuilder::<U256>::new(api, &("my_storage".to_string()));

builder3.add_arg(&U256::from_u64(3));
builder4.add_arg(&U256::from_u64(4));
Expand Down
56 changes: 56 additions & 0 deletions tests/ulm/erc20/rust/src/ulm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bytes::{Bytes, Buf};

use crate::unsigned::U256;

#[cfg(not(test))]
Expand All @@ -9,11 +11,41 @@ extern "C" {
// key and value must have a length of exactly 32.
#[allow(non_snake_case)]
pub fn SetAccountStorage(key: *const u8, value: *const u8);

#[allow(dead_code)]
pub fn fail(msg: *const u8, msg_len: usize) -> !;

// result must have a length of exactly 32.
pub fn keccakHash(msg: *const u8, msg_len: usize, result: *mut u8);

}

#[cfg(test)]
pub mod overrides {
#[no_mangle]
pub extern "C" fn fail(_msg: *const u8, _msg_len: usize) -> ! {
panic!("fail called");
}
}

#[cfg(test)]
#[allow(non_snake_case)]
pub fn failWrapper(msg: &str) -> ! {
panic!("{}", msg);
}

#[cfg(not(test))]
#[allow(non_snake_case)]
pub fn failWrapper(msg: &str) -> ! {
let msg_bytes = msg.as_bytes();
unsafe { fail(msg_bytes.as_ptr(), msg_bytes.len()); }
}

pub trait Ulm {
fn get_account_storage(&self, key: &[u8; 32], value: &mut [u8; 32]);
fn set_account_storage(&mut self, key: &[u8; 32], value: &[u8; 32]);

fn keccak_hash(&self, value: &[u8], result: &mut [u8; 32]);
}

#[cfg(not(test))]
Expand All @@ -28,6 +60,10 @@ impl Ulm for UlmImpl {
fn set_account_storage(&mut self, key: &[u8; 32], value: &[u8; 32]) {
unsafe { SetAccountStorage(key.as_ptr(), value.as_ptr()); }
}

fn keccak_hash(&self, value: &[u8], result: &mut [u8; 32]) {
unsafe { keccakHash(value.as_ptr(), value.len(), result.as_mut_ptr()); }
}
}

#[cfg(test)]
Expand Down Expand Up @@ -73,6 +109,15 @@ pub mod mock {
let bytes_value = Bytes::copy_from_slice(value);
self.storage.insert(bytes_key, bytes_value);
}

fn keccak_hash(&self, value: &[u8], result: &mut [u8; 32]) {
for i in 1 .. result.len() {
result[i] = 0;
}
for i in 1 .. value.len() {
result[i % 32] ^= value[i];
}
}
}
}

Expand All @@ -95,3 +140,14 @@ pub fn set_account_storage(api: &mut dyn Ulm, key: &U256, value: &U256) {

api.set_account_storage(&key_bytes, &value_bytes);
}

pub fn keccak_hash(api: &dyn Ulm, value: &Bytes) -> [u8; 32] {
let mut fingerprint = [0_u8; 32];
api.keccak_hash(value.chunk(), &mut fingerprint);
fingerprint
}

pub fn keccak_hash_int(api: &dyn Ulm, value: &Bytes) -> U256 {
let fingerprint = keccak_hash(api, value);
U256::from_array_le(fingerprint)
}
92 changes: 0 additions & 92 deletions tests/ulm/erc20/rust/src/ulm_hooks.rs

This file was deleted.

Loading