Skip to content

Commit

Permalink
rpc: don't try to run some rpcs if in ibd
Browse files Browse the repository at this point in the history
Some RPCs timeout if we try to make them while on IBD, and this gets the
RPC server stuck forever. This commit forbids running them during IBD,
and return an error.
  • Loading branch information
Davidson-Souza committed Sep 4, 2024
1 parent 240df41 commit e2f014b
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion florestad/src/json_rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ impl Rpc for RpcImpl {
.any(|input| input.previous_output == expected_input)
})
}

if self.chain.is_in_idb() {
return Err(jsonrpc_core::Error {
code: Error::InInitialBlockDownload.into(),
message: Error::InInitialBlockDownload.to_string(),
data: None,
});
}

// can't proceed without block filters
if self.block_filter_storage.is_none() {
return Err(jsonrpc_core::Error {
Expand Down Expand Up @@ -175,6 +184,14 @@ impl Rpc for RpcImpl {
}

fn add_node(&self, node: String) -> Result<bool> {
if self.chain.is_in_idb() {
return Err(jsonrpc_core::Error {
code: Error::InInitialBlockDownload.into(),
message: Error::InInitialBlockDownload.to_string(),
data: None,
});
}

let node = node.split(':').collect::<Vec<&str>>();
let (ip, port) = if node.len() == 2 {
(node[0], node[1].parse().map_err(|_| Error::InvalidPort)?)
Expand Down Expand Up @@ -368,9 +385,18 @@ impl Rpc for RpcImpl {
}

fn get_block(&self, hash: BlockHash, verbosity: Option<u8>) -> Result<Value> {
let is_genesis = self.chain.get_block_hash(0).unwrap().eq(&hash);
if self.chain.is_in_idb() && !is_genesis {
return Err(jsonrpc_core::Error {
code: Error::InInitialBlockDownload.into(),
message: Error::InInitialBlockDownload.to_string(),
data: None,
});
}

let verbosity = verbosity.unwrap_or(1);

let block = if self.chain.get_block_hash(0).unwrap().eq(&hash) {
let block = if is_genesis {
Some(genesis_block(self.network))
} else {
self.node.get_block(hash).map_err(|_| Error::Chain)?
Expand Down Expand Up @@ -438,6 +464,14 @@ impl Rpc for RpcImpl {
}

fn get_peer_info(&self) -> Result<Vec<PeerInfo>> {
if self.chain.is_in_idb() {
return Err(jsonrpc_core::Error {
code: Error::InInitialBlockDownload.into(),
message: Error::InInitialBlockDownload.to_string(),
data: None,
});
}

let peers = self.node.get_peer_info();
if let Ok(peers) = peers {
return Ok(peers);
Expand Down

0 comments on commit e2f014b

Please sign in to comment.