-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
use std::net::SocketAddr; | ||
use std::{net::SocketAddr, sync::Arc}; | ||
|
||
use async_zmq::{subscribe, Context}; | ||
use bitcoin::{hashes::Hash, Txid}; | ||
use bitcoin_slices::{bsl, Parse}; | ||
use futures::StreamExt; | ||
|
||
pub async fn update_tx_zmq(socket: &SocketAddr) { | ||
use crate::{state::SharedState, Error}; | ||
|
||
pub async fn update_tx_zmq_infallible(socket: &SocketAddr, state: Arc<SharedState>) { | ||
if let Err(e) = update_tx_zmq(socket, state).await { | ||
log::error!("{:?}", e); | ||
} | ||
} | ||
|
||
async fn update_tx_zmq(socket: &SocketAddr, state: Arc<SharedState>) -> Result<(), Error> { | ||
log::info!("Start update_tx_zmq!"); | ||
|
||
let context = Context::new(); | ||
let url = format!("tcp://{socket}"); | ||
|
||
let mut sub = subscribe(&url) | ||
.unwrap() | ||
.with_context(&context) | ||
.connect() | ||
.unwrap(); | ||
sub.set_subscribe("rawtx").unwrap(); | ||
let mut sub = subscribe(&url).unwrap().with_context(&context).connect()?; | ||
sub.set_subscribe("rawtx")?; | ||
|
||
while let Some(msg) = sub.next().await { | ||
// Received message is a type of Result<MessageBuf> | ||
let msg = msg.unwrap(); | ||
// | "rawtx" | <serialized transaction> | <uint32 sequence number in Little Endian> | ||
if let Some(tx) = msg.get(1) { | ||
if let Ok(tx) = bsl::Transaction::parse(tx) { | ||
let txid = tx.parsed().txid_sha2(); | ||
let txid = Txid::from_byte_array(txid.into()); | ||
log::info!("inserting {}", txid); | ||
|
||
log::info!("{:?}", msg.iter()); | ||
let _ = state.txs.lock().await.insert(txid, tx.parsed()); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |