1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

Use parking_lot::{Mutex, RwLock} instead of std::sync

This commit is contained in:
Roman Zeyde 2021-06-14 13:19:37 +03:00
parent 188aba50f1
commit 45833c3b61
4 changed files with 10 additions and 8 deletions

1
Cargo.lock generated
View File

@ -321,6 +321,7 @@ dependencies = [
"env_logger",
"hyper",
"log 0.4.14",
"parking_lot",
"prometheus",
"rayon",
"rocksdb",

View File

@ -29,6 +29,7 @@ dirs-next = "2.0"
env_logger = "0.7"
hyper = { version = "0.10", optional = true }
log = "0.4"
parking_lot = "0.11"
prometheus = { version = "0.12", features = ["process"], optional = true }
rayon = "1.5"
serde = "1.0"

View File

@ -1,7 +1,8 @@
use bitcoin::{BlockHash, Transaction, Txid};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::sync::Arc;
use crate::merkle::Proof;
@ -13,14 +14,14 @@ pub struct Cache {
impl Cache {
pub(crate) fn add_tx(&self, txid: Txid, f: impl FnOnce() -> Transaction) {
self.txs.write().unwrap().entry(txid).or_insert_with(f);
self.txs.write().entry(txid).or_insert_with(f);
}
pub(crate) fn get_tx<F, T>(&self, txid: &Txid, f: F) -> Option<T>
where
F: FnOnce(&Transaction) -> T,
{
self.txs.read().unwrap().get(txid).map(f)
self.txs.read().get(txid).map(f)
}
pub(crate) fn add_proof<F>(&self, blockhash: BlockHash, txid: Txid, f: F)
@ -29,7 +30,6 @@ impl Cache {
{
self.proofs
.write()
.unwrap()
.entry((blockhash, txid))
.or_insert_with(f);
}
@ -38,6 +38,6 @@ impl Cache {
where
F: FnOnce(&Proof) -> T,
{
self.proofs.read().unwrap().get(&(blockhash, txid)).map(f)
self.proofs.read().get(&(blockhash, txid)).map(f)
}
}

View File

@ -3,7 +3,6 @@ use anyhow::{Context, Result};
use std::io::Write;
use std::iter::FromIterator;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
use std::sync::Mutex;
use std::time::{SystemTime, UNIX_EPOCH};
use bitcoin::consensus::encode;
@ -18,6 +17,7 @@ use bitcoin::secp256k1;
use bitcoin::secp256k1::rand::Rng;
use bitcoin::{Amount, Block, BlockHash, Network, Transaction, Txid};
use bitcoincore_rpc::{self, json, RpcApi};
use parking_lot::Mutex;
use crate::{
chain::{Chain, NewHeader},
@ -239,7 +239,7 @@ impl Daemon {
}
pub(crate) fn get_new_headers(&self, chain: &Chain) -> Result<Vec<NewHeader>> {
let mut conn = self.p2p.lock().unwrap();
let mut conn = self.p2p.lock();
let msg = GetHeadersMessage::new(chain.locator(), BlockHash::default());
conn.send(NetworkMessage::GetHeaders(msg))?;
@ -278,7 +278,7 @@ impl Daemon {
.map(|h| Inventory::WitnessBlock(*h))
.collect();
debug!("loading {} blocks", blockhashes.len());
let mut conn = self.p2p.lock().unwrap();
let mut conn = self.p2p.lock();
conn.send(NetworkMessage::GetData(inv))?;
for hash in blockhashes {
match conn