pub struct ElectrumServer<Blockchain: BlockchainInterface> {
chain: Arc<Blockchain>,
address_cache: Arc<AddressCache<KvDatabase>>,
clients: HashMap<u32, Arc<Client>>,
message_receiver: UnboundedReceiver<Message>,
message_transmitter: UnboundedSender<Message>,
client_addresses: HashMap<Hash, Arc<Client>>,
block_filters: Option<Arc<NetworkFilters<FlatFiltersStore>>>,
node_interface: NodeInterface,
addresses_to_scan: Vec<ScriptBuf>,
last_rebroadcast: Option<Instant>,
}Fields§
§chain: Arc<Blockchain>The blockchain backend we are using. This will be used to query blockchain information and broadcast transactions.
address_cache: Arc<AddressCache<KvDatabase>>The address cache is used to store addresses and transactions, like a watch-only wallet, but it is adapted to the electrum protocol.
clients: HashMap<u32, Arc<Client>>The clients are the clients connected to our server, we keep track of them using a unique id.
message_receiver: UnboundedReceiver<Message>The message_receiver receive messages and handles them.
message_transmitter: UnboundedSender<Message>The message_transmitter is used to send requests from clients or notifications like new or dropped clients
client_addresses: HashMap<Hash, Arc<Client>>The client_addresses is used to keep track of the addresses of each client. We keep the script_hash and which client has it, so we can notify the clients when a new transaction is received.
block_filters: Option<Arc<NetworkFilters<FlatFiltersStore>>>A Arc-ed copy of the block filters backend that we can use to check if a block contains a transaction that we are interested in.
node_interface: NodeInterfaceAn interface to a running node, used to broadcast transactions and request blocks.
addresses_to_scan: Vec<ScriptBuf>A list of addresses that we’ve just learned about and need to rescan for transactions.
We accumulate those addresses here and then periodically scan, since a wallet will often send multiple addresses, but in different requests.
last_rebroadcast: Option<Instant>Last time we’ve re-broadcasted our transactions. We want to do this every hour, to make sure our transactions don’t get stuck in the mempool if they are not getting confirmed for some reason. We keep track of this time to know when to re-broadcast them.
Implementations§
Source§impl<Blockchain: BlockchainInterface> ElectrumServer<Blockchain>
impl<Blockchain: BlockchainInterface> ElectrumServer<Blockchain>
pub fn new( address_cache: Arc<AddressCache<KvDatabase>>, chain: Arc<Blockchain>, block_filters: Option<Arc<NetworkFilters<FlatFiltersStore>>>, node_interface: NodeInterface, ) -> Result<ElectrumServer<Blockchain>, Box<dyn Error>>
Sourcepub fn get_notifier(&self) -> UnboundedSender<Message>
pub fn get_notifier(&self) -> UnboundedSender<Message>
Notifier to send messages to the main loop
Sourceasync fn handle_client_request(
&mut self,
client: Arc<Client>,
request: Request,
) -> Result<Value, Error>
async fn handle_client_request( &mut self, client: Arc<Client>, request: Request, ) -> Result<Value, Error>
Handle a request from a client. All methods are defined in the electrum protocol.
pub async fn rebroadcast_mempool_transactions(&self)
pub async fn main_loop(self) -> Result<(), Error>
Sourceasync fn rescan_for_addresses(
&mut self,
addresses: Vec<ScriptBuf>,
) -> Result<(), Error>
async fn rescan_for_addresses( &mut self, addresses: Vec<ScriptBuf>, ) -> Result<(), Error>
If a user adds a new address that we didn’t have cached, this method will look for historical transactions for it.
Usually, we’ll rely on compact block filters to speed things up. If we don’t have compact block filters, we may rescan using the older, more bandwidth-intensive method of actually downloading blocks.
Sourceasync fn rescan_with_block_filters(
&mut self,
cfilters: Arc<NetworkFilters<FlatFiltersStore>>,
start_height: Option<u32>,
stop_height: Option<u32>,
addresses: Vec<ScriptBuf>,
) -> Result<(), Error>
async fn rescan_with_block_filters( &mut self, cfilters: Arc<NetworkFilters<FlatFiltersStore>>, start_height: Option<u32>, stop_height: Option<u32>, addresses: Vec<ScriptBuf>, ) -> Result<(), Error>
If we have compact block filters enabled, this method will use them to find blocks of interest and download for our wallet to learn about new transactions, once a new address is added by subscription.
fn process_history(transactions: &[CachedTransaction]) -> Vec<Value>
fn handle_block(&self, block: Block, height: u32)
Sourceasync fn handle_message(&mut self, message: Message) -> Result<(), Error>
async fn handle_message(&mut self, message: Message) -> Result<(), Error>
Handles each kind of Message