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

Fix ci 20241226 #25

Merged
merged 8 commits into from
Dec 28, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ jest.config.js
typescript/**/*.js
docs
Cargo.lock
.aider*
.env
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ optional = true
[target.'cfg(any(unix, windows))'.dev-dependencies]
arbitrary = { version = "1", features = ["derive"] }
bincode = "1"
wasmer = { version = "4.3", features = [ "sys-default"] }
wasmer = { version = "5.0.4", features = [ "sys-default"] }
rand = { version = "0.8", features = ["small_rng"] }

[features]
Expand Down
30 changes: 15 additions & 15 deletions rust/src/contract_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ impl RelatedContracts<'static> {
}

impl<'a> RelatedContracts<'a> {
pub fn update<'b>(
&'b mut self,
) -> impl Iterator<Item = (&ContractInstanceId, &mut Option<State<'a>>)> + 'b {
pub fn update(
&mut self,
) -> impl Iterator<Item = (&ContractInstanceId, &mut Option<State<'a>>)> + '_ {
self.map.iter_mut()
}

Expand Down Expand Up @@ -590,7 +590,7 @@ impl State<'_> {
}
}

impl<'a> From<Vec<u8>> for State<'a> {
impl From<Vec<u8>> for State<'_> {
fn from(state: Vec<u8>) -> Self {
State(Cow::from(state))
}
Expand All @@ -602,7 +602,7 @@ impl<'a> From<&'a [u8]> for State<'a> {
}
}

impl<'a> AsRef<[u8]> for State<'a> {
impl AsRef<[u8]> for State<'_> {
fn as_ref(&self) -> &[u8] {
match &self.0 {
Cow::Borrowed(arr) => arr,
Expand All @@ -619,13 +619,13 @@ impl<'a> Deref for State<'a> {
}
}

impl<'a> DerefMut for State<'a> {
impl DerefMut for State<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<'a> std::io::Read for State<'a> {
impl std::io::Read for State<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.as_ref().read(buf)
}
Expand All @@ -647,7 +647,7 @@ pub struct StateDelta<'a>(
Cow<'a, [u8]>,
);

impl<'a> StateDelta<'a> {
impl StateDelta<'_> {
/// Gets the number of bytes of data stored in the `StateDelta`.
pub fn size(&self) -> usize {
self.0.len()
Expand All @@ -663,7 +663,7 @@ impl<'a> StateDelta<'a> {
}
}

impl<'a> From<Vec<u8>> for StateDelta<'a> {
impl From<Vec<u8>> for StateDelta<'_> {
fn from(delta: Vec<u8>) -> Self {
StateDelta(Cow::from(delta))
}
Expand All @@ -675,7 +675,7 @@ impl<'a> From<&'a [u8]> for StateDelta<'a> {
}
}

impl<'a> AsRef<[u8]> for StateDelta<'a> {
impl AsRef<[u8]> for StateDelta<'_> {
fn as_ref(&self) -> &[u8] {
match &self.0 {
Cow::Borrowed(arr) => arr,
Expand All @@ -692,7 +692,7 @@ impl<'a> Deref for StateDelta<'a> {
}
}

impl<'a> DerefMut for StateDelta<'a> {
impl DerefMut for StateDelta<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
Expand Down Expand Up @@ -737,7 +737,7 @@ impl StateSummary<'_> {
}
}

impl<'a> From<Vec<u8>> for StateSummary<'a> {
impl From<Vec<u8>> for StateSummary<'_> {
fn from(state: Vec<u8>) -> Self {
StateSummary(Cow::from(state))
}
Expand All @@ -749,7 +749,7 @@ impl<'a> From<&'a [u8]> for StateSummary<'a> {
}
}

impl<'a> AsRef<[u8]> for StateSummary<'a> {
impl AsRef<[u8]> for StateSummary<'_> {
fn as_ref(&self) -> &[u8] {
match &self.0 {
Cow::Borrowed(arr) => arr,
Expand All @@ -766,7 +766,7 @@ impl<'a> Deref for StateSummary<'a> {
}
}

impl<'a> DerefMut for StateSummary<'a> {
impl DerefMut for StateSummary<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
Expand Down Expand Up @@ -865,7 +865,7 @@ impl From<Vec<u8>> for ContractCode<'static> {
}

impl<'a> From<&'a [u8]> for ContractCode<'a> {
fn from(data: &'a [u8]) -> ContractCode {
fn from(data: &'a [u8]) -> ContractCode<'a> {
let hash = ContractCode::gen_hash(data);
ContractCode {
data: Cow::from(data),
Expand Down
1 change: 0 additions & 1 deletion rust/src/delegate_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ impl<'a> TryFromFbs<&FbsSecretsId<'a>> for SecretsId {
/// the delegate to sign messages, it will ask the user for permission
/// * A delegate monitors an inbox contract and downloads new messages when
/// they arrive

pub trait DelegateInterface {
/// Process inbound message, producing zero or more outbound messages in response.
/// All state for the delegate must be stored using the secret mechanism.
Expand Down
6 changes: 3 additions & 3 deletions rust/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Parameters<'a>(
Cow<'a, [u8]>,
);

impl<'a> Parameters<'a> {
impl Parameters<'_> {
/// Gets the number of bytes of data stored in the `Parameters`.
pub fn size(&self) -> usize {
self.0.len()
Expand All @@ -43,7 +43,7 @@ impl<'a> Parameters<'a> {
}
}

impl<'a> From<Vec<u8>> for Parameters<'a> {
impl From<Vec<u8>> for Parameters<'_> {
fn from(data: Vec<u8>) -> Self {
Parameters(Cow::from(data))
}
Expand All @@ -55,7 +55,7 @@ impl<'a> From<&'a [u8]> for Parameters<'a> {
}
}

impl<'a> AsRef<[u8]> for Parameters<'a> {
impl AsRef<[u8]> for Parameters<'_> {
fn as_ref(&self) -> &[u8] {
match &self.0 {
Cow::Borrowed(arr) => arr,
Expand Down
51 changes: 25 additions & 26 deletions rust/src/rand.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
//! Random number generation.
use std::cell::RefCell;

thread_local! {
static SMALL_BUF: RefCell<[u8; 512]> = const { RefCell::new([0u8; 512]) };
static LARGE_BUF: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
}

/// Get the specified number of random bytes.
pub fn rand_bytes<'a>(number: u32) -> &'a [u8] {
// usually when some random bytes may be needed
// no more than a 512 cryptographic key
pub fn rand_bytes(number: u32) -> Vec<u8> {
const MAX_KEY_SIZE: u32 = 512;

if number <= MAX_KEY_SIZE {
static mut BUF: [u8; MAX_KEY_SIZE as usize] = [0u8; MAX_KEY_SIZE as usize];
// Safety: this should be fine to do as long as is called within a single threaded context,
// which should be the case since the WASM instance runs in a single thread.
unsafe {
__frnt__rand__rand_bytes(crate::global::INSTANCE_ID, BUF.as_mut_ptr() as _, number);
BUF.as_slice()
}
SMALL_BUF.with(|buf| {
let mut buf = buf.borrow_mut();
unsafe {
__frnt__rand__rand_bytes(crate::global::INSTANCE_ID, buf.as_mut_ptr() as _, number);
}
buf[..number as usize].to_vec()
})
} else {
let len = number as usize;
static mut BUF: std::cell::OnceCell<Vec<u8>> = std::cell::OnceCell::new();
// Safety: this should be fine to do as long as is called within a single threaded context,
// which should be the case since the WASM instance runs in a single thread.
unsafe {
BUF.get_or_init(|| Vec::with_capacity(len));
let buf_borrow = BUF.get_mut().unwrap();
if buf_borrow.len() < len {
buf_borrow.extend(std::iter::repeat(0).take(len - buf_borrow.len()));
LARGE_BUF.with(|buf| {
let mut buf = buf.borrow_mut();
let len = number as usize;
if buf.len() < len {
buf.resize(len, 0);
}
unsafe {
__frnt__rand__rand_bytes(crate::global::INSTANCE_ID, buf.as_mut_ptr() as _, number);
}
__frnt__rand__rand_bytes(
crate::global::INSTANCE_ID,
buf_borrow.as_mut_ptr() as _,
number,
);
BUF.get().unwrap().as_slice()
}
buf[..len].to_vec()
})
}
}

Expand Down
Loading