Skip to content

Commit

Permalink
Merge #115: Revert "errors if expecing headers notification but not s…
Browse files Browse the repository at this point in the history
…ubscribed"

2d44350 Revert "errors if expecing headers notification but not subscribed" (Riccardo Casatta)

Pull request description:

  This reverts commit b86f2bb.

  Some errors started to happen in downstream tests after this commit

  #114

ACKs for top commit:
  danielabrozzoni:
    utACK 2d44350

Tree-SHA512: d31174055f4245cc9d99f336b166a44271067b8daecaf2bb55d507ecfa4eb557b9802d576742fba59fd4dfda3f45fc76f02d7896af31353f19fc3a38698ac5a2
  • Loading branch information
danielabrozzoni committed Aug 3, 2023
2 parents 84b1435 + 2d44350 commit ed9bb09
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ macro_rules! impl_inner_call {
drop(read_client);
match res {
Ok(val) => return Ok(val),
Err(Error::Protocol(_) | Error::AlreadySubscribed(_) | Error::NotSubscribed(_) | Error::NotSubscribedToHeaders) => {
Err(Error::Protocol(_) | Error::AlreadySubscribed(_)) => {
return res;
},
Err(e) => {
Expand Down
41 changes: 8 additions & 33 deletions src/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
last_id: AtomicUsize,
waiting_map: Mutex<HashMap<usize, Sender<ChannelMessage>>>,

headers: Mutex<Option<VecDeque<RawHeaderNotification>>>,
headers: Mutex<VecDeque<RawHeaderNotification>>,
script_notifications: Mutex<HashMap<ScriptHash, VecDeque<ScriptStatus>>>,

#[cfg(feature = "debug-calls")]
Expand All @@ -154,7 +154,7 @@ where
last_id: AtomicUsize::new(0),
waiting_map: Mutex::new(HashMap::new()),

headers: Mutex::new(None),
headers: Mutex::new(VecDeque::new()),
script_notifications: Mutex::new(HashMap::new()),

#[cfg(feature = "debug-calls")]
Expand Down Expand Up @@ -648,17 +648,11 @@ impl<S: Read + Write> RawClient<S> {

fn handle_notification(&self, method: &str, result: serde_json::Value) -> Result<(), Error> {
match method {
"blockchain.headers.subscribe" => {
let mut queue = self.headers.lock()?;
match queue.as_mut() {
None => return Err(Error::NotSubscribedToHeaders),
Some(queue) => queue.append(
&mut serde_json::from_value::<Vec<RawHeaderNotification>>(result)?
.into_iter()
.collect(),
),
}
}
"blockchain.headers.subscribe" => self.headers.lock()?.append(
&mut serde_json::from_value::<Vec<RawHeaderNotification>>(result)?
.into_iter()
.collect(),
),
"blockchain.scripthash.subscribe" => {
let unserialized: ScriptNotification = serde_json::from_value(result)?;
let mut script_notifications = self.script_notifications.lock()?;
Expand Down Expand Up @@ -768,11 +762,6 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
}

fn block_headers_subscribe_raw(&self) -> Result<RawHeaderNotification, Error> {
let mut headers = self.headers.lock()?;
if headers.is_none() {
*headers = Some(VecDeque::new());
}

let req = Request::new_id(
self.last_id.fetch_add(1, Ordering::SeqCst),
"blockchain.headers.subscribe",
Expand All @@ -784,11 +773,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
}

fn block_headers_pop_raw(&self) -> Result<Option<RawHeaderNotification>, Error> {
let mut queue = self.headers.lock()?;
match queue.as_mut() {
None => Err(Error::NotSubscribedToHeaders),
Some(queue) => Ok(queue.pop_front()),
}
Ok(self.headers.lock()?.pop_front())
}

fn block_header_raw(&self, height: usize) -> Result<Vec<u8>, Error> {
Expand Down Expand Up @@ -1348,16 +1333,6 @@ mod test {
assert!(resp.height >= 639000);
}

#[test]
fn test_block_headers_subscribe_pop() {
let client = RawClient::new(get_test_server(), None).unwrap();
let resp = client.block_headers_pop();
assert_eq!(format!("{:?}", resp), "Err(NotSubscribedToHeaders)");
client.block_headers_subscribe().unwrap();
let resp = client.block_headers_pop();
assert!(resp.is_ok());
}

#[test]
fn test_script_subscribe() {
use std::str::FromStr;
Expand Down
4 changes: 0 additions & 4 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,6 @@ pub enum Error {
#[cfg(feature = "use-openssl")]
/// SSL Handshake failed with the server
SslHandshakeError(openssl::ssl::HandshakeError<std::net::TcpStream>),

/// Expecting notification on headers but we are not subscribed
NotSubscribedToHeaders,
}

impl Display for Error {
Expand Down Expand Up @@ -367,7 +364,6 @@ impl Display for Error {
Error::MissingDomain => f.write_str("Missing domain while it was explicitly asked to validate it"),
Error::CouldntLockReader => f.write_str("Couldn't take a lock on the reader mutex. This means that there's already another reader thread is running"),
Error::Mpsc => f.write_str("Broken IPC communication channel: the other thread probably has exited"),
Error::NotSubscribedToHeaders => write!(f, "Expecting notification on headers but we are not subscribed"),
}
}
}
Expand Down

0 comments on commit ed9bb09

Please sign in to comment.