Changeset View
Changeset View
Standalone View
Standalone View
chronik/chronik-http/src/electrum.rs
| Show First 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | pub enum ChronikElectrumServerError { | ||||
| )] | )] | ||||
| PrivateKeyFileNotFound(String, String), | PrivateKeyFileNotFound(String, String), | ||||
| } | } | ||||
| fn electrum_notification_encoder(nt: NewNotification) -> message::Notification { | fn electrum_notification_encoder(nt: NewNotification) -> message::Notification { | ||||
| message::Notification { | message::Notification { | ||||
| jsonrpc: message::JSONRPC_VERSION.to_string(), | jsonrpc: message::JSONRPC_VERSION.to_string(), | ||||
| method: nt.method, | method: nt.method, | ||||
| params: Some(Value::Array(vec![nt.result])), | params: Some(nt.result), | ||||
| } | } | ||||
| } | } | ||||
| impl ChronikElectrumServer { | impl ChronikElectrumServer { | ||||
| /// Binds the Chronik server on the given hosts | /// Binds the Chronik server on the given hosts | ||||
| pub fn setup(params: ChronikElectrumServerParams) -> Result<Self> { | pub fn setup(params: ChronikElectrumServerParams) -> Result<Self> { | ||||
| Ok(ChronikElectrumServer { | Ok(ChronikElectrumServer { | ||||
| hosts: params.hosts, | hosts: params.hosts, | ||||
| ▲ Show 20 Lines • Show All 384 Lines • ▼ Show 20 Lines | for inp in tx.inputs.iter() { | ||||
| fee += inp.sats; | fee += inp.sats; | ||||
| } | } | ||||
| for outp in tx.outputs.iter() { | for outp in tx.outputs.iter() { | ||||
| fee -= outp.sats; | fee -= outp.sats; | ||||
| } | } | ||||
| fee | fee | ||||
| } | } | ||||
| async fn header_json_from_height( | async fn header_hex_from_height( | ||||
| blocks: &QueryBlocks<'_>, | blocks: &QueryBlocks<'_>, | ||||
| height: i32, | height: i32, | ||||
| ) -> Result<Value, RPCError> { | ) -> Result<String, RPCError> { | ||||
| let header = blocks.header(height.to_string(), 0).await.map_err(|_| { | let header = blocks.header(height.to_string(), 0).await.map_err(|_| { | ||||
| RPCError::CustomError( | RPCError::CustomError( | ||||
| 1, | 1, | ||||
| format!("Unable to retrieve the header at height {height}"), | format!("Unable to retrieve the header at height {height}"), | ||||
| ) | ) | ||||
| })?; | })?; | ||||
| Ok(json!({ | Ok(hex::encode(header.raw_header)) | ||||
| "height": height, | |||||
| "hex": hex::encode(header.raw_header), | |||||
| })) | |||||
| } | } | ||||
| #[rpc_pubsub_impl(name = "blockchain")] | #[rpc_pubsub_impl(name = "blockchain")] | ||||
| impl ChronikElectrumRPCBlockchainEndpoint { | impl ChronikElectrumRPCBlockchainEndpoint { | ||||
| #[rpc_method(name = "headers.subscribe")] | #[rpc_method(name = "headers.subscribe")] | ||||
| async fn headers_subscribe( | async fn headers_subscribe( | ||||
| &self, | &self, | ||||
| chan: Arc<Channel>, | chan: Arc<Channel>, | ||||
| Show All 30 Lines | ) -> Result<Value, RPCError> { | ||||
| // from Fulcrum. | // from Fulcrum. | ||||
| continue; | continue; | ||||
| } | } | ||||
| let indexer = indexer_clone.read().await; | let indexer = indexer_clone.read().await; | ||||
| let blocks: chronik_indexer::query::QueryBlocks<'_> = | let blocks: chronik_indexer::query::QueryBlocks<'_> = | ||||
| indexer.blocks(&node_clone); | indexer.blocks(&node_clone); | ||||
| match header_json_from_height(&blocks, block_msg.height) | match header_hex_from_height(&blocks, block_msg.height) | ||||
| .await | .await | ||||
| { | { | ||||
| Err(err) => { | Err(err) => { | ||||
| log_chronik!("{err}\n"); | log_chronik!("{err}\n"); | ||||
| break; | break; | ||||
| } | } | ||||
| Ok(header_json) => { | Ok(header_hex) => { | ||||
| if sub.notify(header_json).await.is_err() { | if sub | ||||
| .notify(json!([{ | |||||
| "height": block_msg.height, | |||||
| "hex": header_hex, | |||||
| }])) | |||||
| .await | |||||
| .is_err() | |||||
| { | |||||
| // Don't log, it's likely a client | // Don't log, it's likely a client | ||||
| // unsubscription or disconnection | // unsubscription or disconnection | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| }; | }; | ||||
| } | } | ||||
| log_chronik!("Unsubscription from electrum headers\n"); | log_chronik!("Unsubscription from electrum headers\n"); | ||||
| }); | }); | ||||
| } | } | ||||
| let tip_height = blocks | let tip_height = blocks | ||||
| .blockchain_info() | .blockchain_info() | ||||
| .map_err(|_| RPCError::InternalError)? | .map_err(|_| RPCError::InternalError)? | ||||
| .tip_height; | .tip_height; | ||||
| header_json_from_height(&blocks, tip_height).await | let header_hex = header_hex_from_height(&blocks, tip_height).await?; | ||||
| Ok(json!({ | |||||
| "height": tip_height, | |||||
| "hex": header_hex, | |||||
| })) | |||||
| } | } | ||||
| #[rpc_method(name = "headers.unsubscribe")] | #[rpc_method(name = "headers.unsubscribe")] | ||||
| async fn headers_unsubscribe( | async fn headers_unsubscribe( | ||||
| &self, | &self, | ||||
| chan: Arc<Channel>, | chan: Arc<Channel>, | ||||
| _method: String, | _method: String, | ||||
| _params: Value, | _params: Value, | ||||
| ▲ Show 20 Lines • Show All 499 Lines • Show Last 20 Lines | |||||