2023-06-24 12:21:33 -07:00
|
|
|
use napi::{
|
|
|
|
bindgen_prelude::*,
|
|
|
|
threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
|
|
|
|
};
|
2023-06-24 11:21:58 -07:00
|
|
|
use napi_derive::napi;
|
|
|
|
|
2023-06-23 20:41:39 -04:00
|
|
|
use once_cell::sync::Lazy;
|
2023-06-23 18:37:03 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Mutex;
|
2023-06-23 16:42:58 -04:00
|
|
|
|
2023-06-23 20:41:39 -04:00
|
|
|
mod audit_transaction;
|
2023-06-23 16:42:58 -04:00
|
|
|
mod gbt;
|
|
|
|
mod thread_transaction;
|
2023-06-23 18:37:03 -04:00
|
|
|
mod utils;
|
|
|
|
use thread_transaction::ThreadTransaction;
|
2023-06-23 16:42:58 -04:00
|
|
|
|
2023-06-24 12:21:33 -07:00
|
|
|
/// Used for ThreadsafeFunction's queue size parameter
|
|
|
|
const UNBOUNDED_QUEUE: usize = 0;
|
|
|
|
|
2023-06-23 20:41:39 -04:00
|
|
|
static THREAD_TRANSACTIONS: Lazy<Mutex<HashMap<u32, ThreadTransaction>>> =
|
|
|
|
Lazy::new(|| Mutex::new(HashMap::new()));
|
2023-06-23 18:37:03 -04:00
|
|
|
|
2023-06-24 12:21:33 -07:00
|
|
|
#[napi(ts_args_type = "mempoolBuffer: Uint8Array, callback: (result: GbtResult) => void")]
|
|
|
|
pub fn make(mempool_buffer: Uint8Array, callback: JsFunction) -> Result<()> {
|
2023-06-23 20:41:39 -04:00
|
|
|
let mut map = HashMap::new();
|
2023-06-24 11:21:58 -07:00
|
|
|
for tx in ThreadTransaction::batch_from_buffer(&mempool_buffer) {
|
2023-06-23 20:41:39 -04:00
|
|
|
map.insert(tx.uid, tx);
|
|
|
|
}
|
2023-06-23 16:42:58 -04:00
|
|
|
|
2023-06-23 20:41:39 -04:00
|
|
|
let mut global_map = THREAD_TRANSACTIONS.lock().unwrap();
|
|
|
|
*global_map = map;
|
2023-06-23 18:37:03 -04:00
|
|
|
|
2023-06-24 11:21:58 -07:00
|
|
|
run_in_thread(callback)
|
2023-06-23 18:37:03 -04:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:21:33 -07:00
|
|
|
#[napi(
|
|
|
|
ts_args_type = "newTxs: Uint8Array, removeTxs: Uint8Array, callback: (result: GbtResult) => void"
|
|
|
|
)]
|
|
|
|
pub fn update(new_txs: Uint8Array, remove_txs: Uint8Array, callback: JsFunction) -> Result<()> {
|
2023-06-23 18:37:03 -04:00
|
|
|
let mut map = THREAD_TRANSACTIONS.lock().unwrap();
|
2023-06-24 11:21:58 -07:00
|
|
|
for tx in ThreadTransaction::batch_from_buffer(&new_txs) {
|
2023-06-23 20:41:39 -04:00
|
|
|
map.insert(tx.uid, tx);
|
|
|
|
}
|
2023-06-24 11:21:58 -07:00
|
|
|
for txid in &utils::txids_from_buffer(&remove_txs) {
|
2023-06-23 20:41:39 -04:00
|
|
|
map.remove(txid);
|
|
|
|
}
|
2023-06-23 18:37:03 -04:00
|
|
|
drop(map);
|
2023-06-23 16:42:58 -04:00
|
|
|
|
2023-06-24 11:21:58 -07:00
|
|
|
run_in_thread(callback)
|
2023-06-23 20:41:39 -04:00
|
|
|
}
|
|
|
|
|
2023-06-24 11:21:58 -07:00
|
|
|
/// The result from calling the gbt function.
|
|
|
|
///
|
|
|
|
/// This tuple contains the following:
|
|
|
|
/// blocks: A 2D Vector of transaction IDs (u32), the inner Vecs each represent a block.
|
|
|
|
/// clusters: A 2D Vector of transaction IDs representing clusters of dependent mempool transactions
|
|
|
|
/// rates: A Vector of tuples containing transaction IDs (u32) and effective fee per vsize (f64)
|
|
|
|
#[napi(constructor)]
|
|
|
|
pub struct GbtResult {
|
|
|
|
pub blocks: Vec<Vec<u32>>,
|
|
|
|
pub clusters: Vec<Vec<u32>>,
|
|
|
|
pub rates: Vec<Vec<f64>>, // Tuples not supported. u32 fits inside f64
|
|
|
|
}
|
2023-06-23 20:41:39 -04:00
|
|
|
|
2023-06-24 12:21:33 -07:00
|
|
|
fn run_in_thread(callback: JsFunction) -> Result<()> {
|
|
|
|
let thread_safe_callback: ThreadsafeFunction<GbtResult, ErrorStrategy::Fatal> =
|
|
|
|
callback.create_threadsafe_function(UNBOUNDED_QUEUE, |ctx| Ok(vec![ctx.value]))?;
|
|
|
|
|
|
|
|
let handle = std::thread::spawn(move || {
|
2023-06-24 11:21:58 -07:00
|
|
|
let mut map = THREAD_TRANSACTIONS
|
|
|
|
.lock()
|
|
|
|
.map_err(|_| napi::Error::from_reason("THREAD_TRANSACTIONS Mutex poisoned"))?;
|
2023-06-24 12:21:33 -07:00
|
|
|
let result = gbt::gbt(&mut map).ok_or_else(|| napi::Error::from_reason("gbt failed"))?;
|
|
|
|
|
|
|
|
// Note: A call mode of Blocking does not mean it will block, but rather it tells
|
|
|
|
// the N-API what to do in the event of a full queue.
|
|
|
|
// The queue will never be full, so Blocking is fine.
|
|
|
|
thread_safe_callback.call(result, ThreadsafeFunctionCallMode::Blocking);
|
|
|
|
Ok(())
|
2023-06-23 16:42:58 -04:00
|
|
|
});
|
|
|
|
|
2023-06-24 12:21:33 -07:00
|
|
|
handle
|
|
|
|
.join()
|
|
|
|
.map_err(|_| napi::Error::from_reason("thread panicked"))?
|
2023-06-23 16:42:58 -04:00
|
|
|
}
|