mirror of
https://github.com/pezkuwichain/revive-differential-tests.git
synced 2026-04-27 00:57:59 +00:00
init node interaction crate
Signed-off-by: xermicus <bigcyrill@hotmail.com>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
//! Implements helpers for node interactions using transactions.
|
||||
//!
|
||||
//! The alloy crate is convenient but requires running in a tokio runtime.
|
||||
//! We contain any async rust right here.
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use std::sync::Mutex;
|
||||
use std::thread;
|
||||
use tokio::runtime::Runtime;
|
||||
use tokio::sync::mpsc;
|
||||
use transaction::Transaction;
|
||||
|
||||
pub mod transaction;
|
||||
|
||||
pub(crate) static TO_TOKIO: Lazy<Mutex<TokioRuntime>> =
|
||||
Lazy::new(|| Mutex::new(TokioRuntime::spawn()));
|
||||
|
||||
pub struct TokioRuntime {
|
||||
pub transaction_sender: mpsc::Sender<Transaction>,
|
||||
}
|
||||
|
||||
impl TokioRuntime {
|
||||
pub fn spawn() -> Self {
|
||||
let rt = Runtime::new().expect("should be able to create the tokio runtime");
|
||||
let (transaction_sender, mut transaction_receiver) = mpsc::channel::<Transaction>(1024);
|
||||
|
||||
thread::spawn(move || {
|
||||
rt.block_on(async move {
|
||||
while let Some(transaction) = transaction_receiver.recv().await {
|
||||
tokio::task::spawn(async move {
|
||||
let sender = transaction.receipt_sender.clone();
|
||||
let result = transaction.execute().await;
|
||||
if let Err(error) = sender.send(result).await {
|
||||
log::error!("failed to send transaction receipt: {error}");
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("should alaways be able to spawn the tokio tasks");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Self { transaction_sender }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
use alloy::providers::{Provider, ProviderBuilder};
|
||||
use alloy::rpc::types::{TransactionReceipt, TransactionRequest};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::TO_TOKIO;
|
||||
|
||||
pub struct Transaction {
|
||||
pub transaction_request: TransactionRequest,
|
||||
pub receipt_sender: mpsc::Sender<anyhow::Result<TransactionReceipt>>,
|
||||
pub connection_string: String,
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
pub async fn execute(self) -> anyhow::Result<TransactionReceipt> {
|
||||
let provider = ProviderBuilder::new()
|
||||
.connect(&self.connection_string)
|
||||
.await?;
|
||||
Ok(provider
|
||||
.send_transaction(self.transaction_request)
|
||||
.await?
|
||||
.get_receipt()
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute_transaction(
|
||||
transaction_request: TransactionRequest,
|
||||
connection_string: String,
|
||||
) -> anyhow::Result<TransactionReceipt> {
|
||||
let request_sender = TO_TOKIO.lock().unwrap().transaction_sender.clone();
|
||||
let (receipt_sender, mut receipt_receiver) = mpsc::channel(1);
|
||||
|
||||
request_sender.blocking_send(Transaction {
|
||||
transaction_request,
|
||||
receipt_sender,
|
||||
connection_string,
|
||||
})?;
|
||||
|
||||
match receipt_receiver.blocking_recv() {
|
||||
Some(receipt) => receipt,
|
||||
None => anyhow::bail!("no receipt received"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user