Pass gbt mempool data directly without serialization

This commit is contained in:
Mononaut
2023-06-28 16:31:59 -04:00
parent 5065fa42d0
commit 0886e850f9
8 changed files with 70 additions and 131 deletions

View File

@@ -1,6 +1,5 @@
use crate::{
thread_transaction::ThreadTransaction,
u32_hasher_types::{u32hashset_new, U32HasherState},
u32_hasher_types::{u32hashset_new, U32HasherState}, ThreadTransaction,
};
use std::{
cmp::Ordering,
@@ -16,7 +15,6 @@ pub struct AuditTransaction {
pub weight: u32,
pub sigop_adjusted_vsize: u32,
pub sigops: u32,
pub fee_per_vsize: f64,
pub effective_fee_per_vsize: f64,
pub dependency_rate: f64,
pub inputs: Vec<u32>,
@@ -81,18 +79,17 @@ impl AuditTransaction {
let sigop_adjusted_vsize = ((tx.weight + 3) / 4).max(tx.sigops * 5);
Self {
uid: tx.uid,
fee: tx.fee,
fee: tx.fee as u64,
weight: tx.weight,
sigop_adjusted_vsize,
sigops: tx.sigops,
fee_per_vsize: tx.fee_per_vsize,
effective_fee_per_vsize: tx.effective_fee_per_vsize,
dependency_rate: f64::INFINITY,
inputs: tx.inputs.clone(),
relatives_set_flag: false,
ancestors: u32hashset_new(),
children: u32hashset_new(),
ancestor_fee: tx.fee,
ancestor_fee: tx.fee as u64,
ancestor_weight: tx.weight,
ancestor_sigop_adjusted_vsize: sigop_adjusted_vsize,
ancestor_sigops: tx.sigops,

View File

@@ -6,8 +6,9 @@
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::float_cmp)]
use napi::bindgen_prelude::{Result, Uint8Array};
use napi::bindgen_prelude::{Result};
use napi_derive::napi;
use thread_transaction::ThreadTransaction;
use tracing::{debug, info, trace};
use tracing_log::LogTracer;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
@@ -19,9 +20,7 @@ mod audit_transaction;
mod gbt;
mod thread_transaction;
mod u32_hasher_types;
mod utils;
use thread_transaction::ThreadTransaction;
use u32_hasher_types::{u32hashmap_with_capacity, U32HasherState};
/// This is the starting capacity for HashMap/Vec/etc. that deal with transactions.
@@ -78,10 +77,10 @@ impl GbtGenerator {
///
/// Rejects if the thread panics or if the Mutex is poisoned.
#[napi]
pub async fn make(&self, mempool_buffer: Uint8Array) -> Result<GbtResult> {
pub async fn make(&self, mempool: Vec<ThreadTransaction>) -> Result<GbtResult> {
trace!("make: Current State {:#?}", self.thread_transactions);
run_task(Arc::clone(&self.thread_transactions), move |map| {
for tx in ThreadTransaction::batch_from_buffer(&mempool_buffer) {
for tx in mempool {
map.insert(tx.uid, tx);
}
})
@@ -92,13 +91,13 @@ impl GbtGenerator {
///
/// Rejects if the thread panics or if the Mutex is poisoned.
#[napi]
pub async fn update(&self, new_txs: Uint8Array, remove_txs: Uint8Array) -> Result<GbtResult> {
pub async fn update(&self, new_txs: Vec<ThreadTransaction>, remove_txs: Vec<u32>) -> Result<GbtResult> {
trace!("update: Current State {:#?}", self.thread_transactions);
run_task(Arc::clone(&self.thread_transactions), move |map| {
for tx in ThreadTransaction::batch_from_buffer(&new_txs) {
for tx in new_txs {
map.insert(tx.uid, tx);
}
for txid in &utils::txids_from_buffer(&remove_txs) {
for txid in &remove_txs {
map.remove(txid);
}
})

View File

@@ -1,45 +1,12 @@
use bytes::buf::Buf;
use std::io::Cursor;
use napi_derive::napi;
#[derive(Debug)]
#[napi(object)]
pub struct ThreadTransaction {
pub uid: u32,
pub fee: u64,
pub fee: f64,
pub weight: u32,
pub sigops: u32,
pub fee_per_vsize: f64,
pub effective_fee_per_vsize: f64,
pub inputs: Vec<u32>,
}
impl ThreadTransaction {
pub fn batch_from_buffer(buffer: &[u8]) -> Vec<Self> {
let mut transactions: Vec<Self> = Vec::new();
let mut cursor = Cursor::new(buffer);
let size = cursor.get_u32();
for _ in 0..size {
let uid = cursor.get_u32();
let fee = cursor.get_f64().round() as u64;
let weight = cursor.get_u32();
let sigops = cursor.get_u32();
let fee_per_vsize = cursor.get_f64();
let effective_fee_per_vsize = cursor.get_f64();
let input_count = cursor.get_u32();
let mut inputs: Vec<u32> = Vec::new();
for _ in 0..input_count {
inputs.push(cursor.get_u32());
}
transactions.push(Self {
uid,
fee,
weight,
sigops,
fee_per_vsize,
effective_fee_per_vsize,
inputs,
});
}
transactions
}
}
}

View File

@@ -1,13 +0,0 @@
use bytes::buf::Buf;
use std::io::Cursor;
pub fn txids_from_buffer(buffer: &[u8]) -> Vec<u32> {
let mut txids: Vec<u32> = Vec::new();
let mut cursor: Cursor<&[u8]> = Cursor::new(buffer);
let size: u32 = cursor.get_u32();
for _ in 0..size {
txids.push(cursor.get_u32());
}
txids
}