Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanity check that bitcoind is running on the specified network #123

Merged
merged 3 commits into from
Sep 20, 2022

Conversation

mariocynicys
Copy link
Collaborator

Make sure that bitcoind is running on the same network teosd is running on. And error if not.

Fixes #113

teos/src/config.rs Outdated Show resolved Hide resolved
@mariocynicys mariocynicys force-pushed the match-bitcoin-and-teos-network branch from 3c946b3 to da00afb Compare September 18, 2022 08:55
@mariocynicys mariocynicys marked this pull request as ready for review September 18, 2022 08:56
@mariocynicys mariocynicys force-pushed the match-bitcoin-and-teos-network branch from da00afb to feeb45d Compare September 18, 2022 09:20
@mariocynicys
Copy link
Collaborator Author

This one requires some manual testing because we can't depend on bitcoind for unit tests.

@mariocynicys mariocynicys changed the title Fix: Sanity check that bitcoind is running on the specified network Sanity check that bitcoind is running on the specified network Sep 18, 2022
README.md Outdated Show resolved Hide resolved
teos/src/config.rs Outdated Show resolved Hide resolved
teos/src/config.rs Outdated Show resolved Hide resolved
@mariocynicys mariocynicys force-pushed the match-bitcoin-and-teos-network branch from feeb45d to 3e5baff Compare September 19, 2022 10:01
teos-common/src/test_utils.rs Outdated Show resolved Hide resolved
teos/src/config.rs Outdated Show resolved Hide resolved
teos/src/config.rs Outdated Show resolved Hide resolved
teos/src/config.rs Outdated Show resolved Hide resolved
teos/src/config.rs Outdated Show resolved Hide resolved
@mariocynicys mariocynicys force-pushed the match-bitcoin-and-teos-network branch 2 times, most recently from 5029b84 to ccef196 Compare September 19, 2022 17:03
Copy link
Member

@sr-gi sr-gi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits and small improvements. Look good otherwise.

Comment on lines 214 to 218
match self.btc_network.as_str() {
"mainnet" => self.btc_network = "main".into(),
"testnet" => self.btc_network = "test".into(),
_ => {}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get fancier here!

Suggested change
match self.btc_network.as_str() {
"mainnet" => self.btc_network = "main".into(),
"testnet" => self.btc_network = "test".into(),
_ => {}
};
if ["mainnet", "testnet"].contains(&self.btc_network.as_str()) {
self.btc_network = self.btc_network.trim_end_matches("net").into();
}

INSTALL.md Outdated
Please refer to the cargo documentation for more detailed instructions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A line break was introduced here.

Ok(_) => Ok(client),
Err(e) => Err(e),
// Test that bitcoind is reachable.
let btc_network = client.get_chain().await?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really need to store this, do we?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to log it out when it doesn't match our network.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right

Comment on lines 97 to 107
if btc_network != teos_network {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"bitcoind is running on {} but teosd is set to run on {}",
btc_network, teos_network
),
));
}

Ok(client)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if btc_network != teos_network {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"bitcoind is running on {} but teosd is set to run on {}",
btc_network, teos_network
),
));
}
Ok(client)
if client.get_chain().await? != teos_network {
Err(Error::new(
ErrorKind::InvalidInput,
format!(
"bitcoind is running on {} but teosd is set to run on {}",
btc_network, teos_network
),
))
} else {
Ok(client)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same logic ❓

Copy link
Member

@sr-gi sr-gi Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be, but as you mentioned the temp variable is needed for the logging. Mainly you're returning the client if the network match and erroring otherwise, so no need to force a return if there is no more code left.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk, I feel it's better to have all the checks above then Ok(something) at the end rather than integrating Ok(something) in the last check. But no major difference really. let me change this quickly.

Comment on lines +182 to +184
"main" => "bitcoin",
"test" => "testnet",
any => any,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit annoying that we have to cast it back and forth, but I guess it is how it is until we can simplify the codebase to use a single "bitcoind rpc"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna leave this here so we can improve it if it gets merged:

rust-bitcoin/rust-bitcoincore-rpc#247

@mariocynicys mariocynicys force-pushed the match-bitcoin-and-teos-network branch from ccef196 to 15ed584 Compare September 20, 2022 06:29
Copy link
Member

@sr-gi sr-gi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now.

I would just change the if/else thing to remove the return, but that's a nit, so up to you.

@mariocynicys mariocynicys force-pushed the match-bitcoin-and-teos-network branch from 15ed584 to 51940a5 Compare September 20, 2022 10:35
@mariocynicys mariocynicys force-pushed the match-bitcoin-and-teos-network branch from 51940a5 to 6461e7e Compare September 20, 2022 10:42
@sr-gi sr-gi merged commit 0eeef34 into talaia-labs:master Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

teosd doesn't refuse to run when bitcoind is not running on the same network
2 participants