Skip to content

Commit

Permalink
feat: switch to AsyncTask instead of tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Oct 7, 2024
1 parent 4e07346 commit 59d9d31
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
export const SHUFFLE_ROUNDS_MINIMAL: number
export const SHUFFLE_ROUNDS_MAINNET: number
export declare function shuffleList(activeIndices: Uint32Array, seed: Uint8Array, rounds: number): Uint32Array
export declare function asyncShuffleList(activeIndices: Uint32Array, seed: Uint8Array, rounds: number): Promise<Uint32Array>
export declare function unshuffleList(activeIndices: Uint32Array, seed: Uint8Array, rounds: number): Uint32Array
export declare function asyncShuffleList(activeIndices: Uint32Array, seed: Uint8Array, rounds: number): Promise<Uint32Array>
export declare function asyncUnshuffleList(activeIndices: Uint32Array, seed: Uint8Array, rounds: number): Promise<Uint32Array>
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { SHUFFLE_ROUNDS_MINIMAL, SHUFFLE_ROUNDS_MAINNET, shuffleList, asyncShuffleList, unshuffleList, asyncUnshuffleList } = nativeBinding
const { SHUFFLE_ROUNDS_MINIMAL, SHUFFLE_ROUNDS_MAINNET, shuffleList, unshuffleList, asyncShuffleList, asyncUnshuffleList } = nativeBinding

module.exports.SHUFFLE_ROUNDS_MINIMAL = SHUFFLE_ROUNDS_MINIMAL
module.exports.SHUFFLE_ROUNDS_MAINNET = SHUFFLE_ROUNDS_MAINNET
module.exports.shuffleList = shuffleList
module.exports.asyncShuffleList = asyncShuffleList
module.exports.unshuffleList = unshuffleList
module.exports.asyncShuffleList = asyncShuffleList
module.exports.asyncUnshuffleList = asyncUnshuffleList
72 changes: 51 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![deny(clippy::all)]

use std::{mem, u8};
use std::mem;

use napi::bindgen_prelude::{Result, Uint32Array, Uint8Array};
use napi::{
bindgen_prelude::{AsyncTask, Result, Uint32Array, Uint8Array},
Env, Task,
};
use napi_derive::napi;

use ethereum_hashing::hash_fixed;
Expand Down Expand Up @@ -112,11 +115,12 @@ impl ShufflingManager {
/// - `list_size > 2**24`
/// - `list_size > usize::MAX / 2`
fn inner_shuffle_list(
mut input: Vec<u32>,
input: &Uint32Array,
seed: &[u8],
rounds: i32,
forwards: bool,
) -> Result<Vec<u32>> {
let mut input = input.to_vec();
if rounds == 0 {
// no shuffling rounds
return Ok(input);
Expand Down Expand Up @@ -229,51 +233,77 @@ pub fn shuffle_list(
rounds: i32,
) -> Result<Uint32Array> {
Ok(Uint32Array::new(inner_shuffle_list(
active_indices.to_vec(),
&active_indices,
&seed,
rounds,
true,
)?))
}

#[napi]
pub async fn async_shuffle_list(
pub fn unshuffle_list(
active_indices: Uint32Array,
seed: Uint8Array,
rounds: i32,
) -> Result<Uint32Array> {
Ok(Uint32Array::new(inner_shuffle_list(
active_indices.to_vec(),
&active_indices,
&seed,
rounds,
true,
false,
)?))
}

pub struct AsyncInnerShuffle {
input: Uint32Array,
seed: Vec<u8>,
rounds: i32,
forwards: bool,
}

#[napi]
pub fn unshuffle_list(
impl Task for AsyncInnerShuffle {
type Output = Vec<u32>;
type JsValue = Uint32Array;

fn compute(&mut self) -> Result<Self::Output> {
Ok(inner_shuffle_list(
&self.input,
&self.seed,
self.rounds,
self.forwards,
)?)
}

fn resolve(&mut self, _: Env, output: Self::Output) -> Result<Self::JsValue> {
Ok(Uint32Array::new(output))
}
}

#[napi]
pub fn async_shuffle_list(
active_indices: Uint32Array,
seed: Uint8Array,
rounds: i32,
) -> Result<Uint32Array> {
Ok(Uint32Array::new(inner_shuffle_list(
active_indices.to_vec(),
&seed,
) -> AsyncTask<AsyncInnerShuffle> {
AsyncTask::new(AsyncInnerShuffle {
input: active_indices,
seed: seed.to_vec(),
rounds,
false,
)?))
forwards: true,
})
}

#[napi]
pub async fn async_unshuffle_list(
pub fn async_unshuffle_list(
active_indices: Uint32Array,
seed: Uint8Array,
rounds: i32,
) -> Result<Uint32Array> {
Ok(Uint32Array::new(inner_shuffle_list(
active_indices.to_vec(),
&seed,
) -> AsyncTask<AsyncInnerShuffle> {
AsyncTask::new(AsyncInnerShuffle {
input: active_indices,
seed: seed.to_vec(),
rounds,
false,
)?))
forwards: false,
})
}

0 comments on commit 59d9d31

Please sign in to comment.