Skip to content

Commit

Permalink
Merge pull request #3 from ChainSafe/mkeil/switch-to-async-task
Browse files Browse the repository at this point in the history
feat: switch to AsyncTask instead of tokio
  • Loading branch information
matthewkeil authored Oct 7, 2024
2 parents 4e07346 + 8e37969 commit f5a80cb
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 33 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
2 changes: 1 addition & 1 deletion npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-darwin-arm64",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-darwin-x64",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"darwin"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-linux-arm64-gnu",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-arm64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-linux-arm64-musl",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-linux-x64-gnu",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-musl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-linux-x64-musl",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"linux"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-arm64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-win32-arm64-msvc",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle-win32-x64-msvc",
"version": "0.0.1",
"version": "0.0.2",
"os": [
"win32"
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/swap-or-not-shuffle",
"version": "0.0.1",
"version": "0.0.2",
"scripts": {
"artifacts": "napi artifacts",
"build": "napi build --platform --release",
Expand Down
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 f5a80cb

Please sign in to comment.