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

fix: resolve DNS/hostnames for signer node_host config #4475

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libsigner/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub fn decode_http_body(headers: &HashMap<String, String>, mut buf: &[u8]) -> io
/// Return the HTTP reply, decoded if it was chunked
pub fn run_http_request<S: Read + Write>(
sock: &mut S,
host: &SocketAddr,
host: &str,
verb: &str,
path: &str,
content_type: Option<&str>,
Expand Down
15 changes: 6 additions & 9 deletions libsigner/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait SignerSession {
/// connect to the replica
fn connect(
&mut self,
host: SocketAddr,
host: String,
zone117x marked this conversation as resolved.
Show resolved Hide resolved
stackerdb_contract_id: QualifiedContractIdentifier,
) -> Result<(), RPCError>;
/// query the replica for a list of chunks
Expand Down Expand Up @@ -66,7 +66,7 @@ pub trait SignerSession {
/// signer session for a stackerdb instance
pub struct StackerDBSession {
/// host we're talking to
pub host: SocketAddr,
pub host: String,
netrome marked this conversation as resolved.
Show resolved Hide resolved
/// contract we're talking to
pub stackerdb_contract_id: QualifiedContractIdentifier,
/// connection to the replica
Expand All @@ -75,12 +75,9 @@ pub struct StackerDBSession {

impl StackerDBSession {
/// instantiate but don't connect
pub fn new(
host: SocketAddr,
stackerdb_contract_id: QualifiedContractIdentifier,
) -> StackerDBSession {
pub fn new(host: &str, stackerdb_contract_id: QualifiedContractIdentifier) -> StackerDBSession {
StackerDBSession {
host,
host: host.to_owned(),
stackerdb_contract_id,
sock: None,
}
Expand All @@ -89,7 +86,7 @@ impl StackerDBSession {
/// connect or reconnect to the node
fn connect_or_reconnect(&mut self) -> Result<(), RPCError> {
debug!("connect to {}", &self.host);
self.sock = Some(TcpStream::connect(self.host)?);
self.sock = Some(TcpStream::connect(&self.host)?);
Ok(())
}

Expand Down Expand Up @@ -134,7 +131,7 @@ impl SignerSession for StackerDBSession {
/// connect to the replica
fn connect(
&mut self,
host: SocketAddr,
host: String,
stackerdb_contract_id: QualifiedContractIdentifier,
) -> Result<(), RPCError> {
self.host = host;
Expand Down
8 changes: 4 additions & 4 deletions libsigner/src/tests/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ fn test_run_http_request_with_body() {

let result_chunked = run_http_request(
&mut msock_chunked,
&"127.0.0.1:20443".parse().unwrap(),
&"127.0.0.1:20443",
verb,
path,
content_type,
Expand All @@ -275,7 +275,7 @@ fn test_run_http_request_with_body() {

let result_plain = run_http_request(
&mut msock_plain,
&"127.0.0.1:20443".parse().unwrap(),
&"127.0.0.1:20443",
verb,
path,
content_type,
Expand Down Expand Up @@ -321,7 +321,7 @@ fn test_run_http_request_no_body() {

let result_chunked = run_http_request(
&mut msock_chunked,
&"127.0.0.1:20443".parse().unwrap(),
&"127.0.0.1:20443",
verb,
path,
content_type,
Expand All @@ -330,7 +330,7 @@ fn test_run_http_request_no_body() {
.unwrap();
let result_plain = run_http_request(
&mut msock_plain,
&"127.0.0.1:20443".parse().unwrap(),
&"127.0.0.1:20443",
verb,
path,
content_type,
Expand Down
1 change: 1 addition & 0 deletions stacks-signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
wsts = { workspace = true }
rand = { workspace = true }
url = "2.1.0"

[dev-dependencies]
clarity = { path = "../clarity", features = ["testing"] }
Expand Down
2 changes: 1 addition & 1 deletion stacks-signer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub enum Command {
pub struct StackerDBArgs {
/// The Stacks node to connect to
#[arg(long)]
pub host: SocketAddr,
pub host: String,
netrome marked this conversation as resolved.
Show resolved Hide resolved
/// The stacker-db contract to use. Must be in the format of "STACKS_ADDRESS.CONTRACT_NAME"
#[arg(short, long, value_parser = parse_contract)]
pub contract: QualifiedContractIdentifier,
Expand Down
6 changes: 3 additions & 3 deletions stacks-signer/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub(crate) mod tests {
let mut config =
GlobalConfig::load_from_file("./src/tests/conf/signer-0.toml").unwrap();
let (server, mock_server_addr) = mock_server_random();
config.node_host = mock_server_addr;
config.node_host = mock_server_addr.to_string();

let client = StacksClient::from(&config);
Self {
Expand Down Expand Up @@ -202,7 +202,7 @@ pub(crate) mod tests {

/// Create a mock server on a same port as in the config
pub fn mock_server_from_config(config: &GlobalConfig) -> TcpListener {
TcpListener::bind(config.node_host).unwrap()
TcpListener::bind(config.node_host.to_string()).unwrap()
}

/// Write a response to the mock server and return the request bytes
Expand Down Expand Up @@ -503,7 +503,7 @@ pub(crate) mod tests {
signer_slot_ids,
ecdsa_private_key: config.ecdsa_private_key,
stacks_private_key: config.stacks_private_key,
node_host: config.node_host,
node_host: config.node_host.to_string(),
mainnet: config.network.is_mainnet(),
dkg_end_timeout: config.dkg_end_timeout,
dkg_private_timeout: config.dkg_private_timeout,
Expand Down
6 changes: 2 additions & 4 deletions stacks-signer/src/client/stackerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
use std::net::SocketAddr;

use blockstack_lib::chainstate::nakamoto::signer_set::NakamotoSigners;
use blockstack_lib::chainstate::stacks::StacksTransaction;
use blockstack_lib::util_lib::boot::boot_code_addr;
Expand Down Expand Up @@ -55,7 +53,7 @@ pub struct StackerDB {
impl From<&SignerConfig> for StackerDB {
fn from(config: &SignerConfig) -> Self {
StackerDB::new(
config.node_host,
&config.node_host,
config.stacks_private_key,
config.mainnet,
config.reward_cycle,
Expand All @@ -66,7 +64,7 @@ impl From<&SignerConfig> for StackerDB {
impl StackerDB {
/// Create a new StackerDB client
pub fn new(
host: SocketAddr,
host: &str,
stacks_private_key: StacksPrivateKey,
is_mainnet: bool,
reward_cycle: u64,
Expand Down
20 changes: 6 additions & 14 deletions stacks-signer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub struct SignerConfig {
/// The private key for this signer
pub stacks_private_key: StacksPrivateKey,
/// The node host for this signer
pub node_host: SocketAddr,
pub node_host: String,
/// Whether this signer is running on mainnet or not
pub mainnet: bool,
/// timeout to gather DkgPublicShares messages
Expand All @@ -169,7 +169,7 @@ pub struct SignerConfig {
#[derive(Clone, Debug)]
pub struct GlobalConfig {
/// endpoint to the stacks node
pub node_host: SocketAddr,
pub node_host: String,
/// endpoint to the event receiver
pub endpoint: SocketAddr,
/// The Scalar representation of the private key for signer communication
Expand Down Expand Up @@ -254,17 +254,9 @@ impl TryFrom<RawConfigFile> for GlobalConfig {
/// Attempt to decode the raw config file's primitive types into our types.
/// NOTE: network access is required for this to work
fn try_from(raw_data: RawConfigFile) -> Result<Self, Self::Error> {
let node_host = raw_data
.node_host
.to_socket_addrs()
.map_err(|_| {
ConfigError::BadField("node_host".to_string(), raw_data.node_host.clone())
})?
.next()
.ok_or(ConfigError::BadField(
"node_host".to_string(),
raw_data.node_host.clone(),
))?;
url::Url::parse(&format!("http://{}", raw_data.node_host)).map_err(|_| {
ConfigError::BadField("node_host".to_string(), raw_data.node_host.clone())
})?;

let endpoint = raw_data
.endpoint
Expand Down Expand Up @@ -307,7 +299,7 @@ impl TryFrom<RawConfigFile> for GlobalConfig {
let nonce_timeout = raw_data.nonce_timeout_ms.map(Duration::from_millis);
let sign_timeout = raw_data.sign_timeout_ms.map(Duration::from_millis);
Ok(Self {
node_host,
node_host: raw_data.node_host,
endpoint,
stacks_private_key,
ecdsa_private_key,
Expand Down
13 changes: 6 additions & 7 deletions stacks-signer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ extern crate toml;

use std::fs::File;
use std::io::{self, BufRead, Write};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{channel, Receiver, Sender};
use std::time::Duration;
Expand Down Expand Up @@ -63,9 +62,9 @@ struct SpawnedSigner {
}

/// Create a new stacker db session
fn stackerdb_session(host: SocketAddr, contract: QualifiedContractIdentifier) -> StackerDBSession {
fn stackerdb_session(host: &str, contract: QualifiedContractIdentifier) -> StackerDBSession {
let mut session = StackerDBSession::new(host, contract.clone());
session.connect(host, contract).unwrap();
session.connect(host.to_string(), contract).unwrap();
session
}

Expand Down Expand Up @@ -160,28 +159,28 @@ fn process_sign_result(sign_res: &[OperationResult]) {

fn handle_get_chunk(args: GetChunkArgs) {
debug!("Getting chunk...");
let mut session = stackerdb_session(args.db_args.host, args.db_args.contract);
let mut session = stackerdb_session(&args.db_args.host, args.db_args.contract);
let chunk_opt = session.get_chunk(args.slot_id, args.slot_version).unwrap();
write_chunk_to_stdout(chunk_opt);
}

fn handle_get_latest_chunk(args: GetLatestChunkArgs) {
debug!("Getting latest chunk...");
let mut session = stackerdb_session(args.db_args.host, args.db_args.contract);
let mut session = stackerdb_session(&args.db_args.host, args.db_args.contract);
let chunk_opt = session.get_latest_chunk(args.slot_id).unwrap();
write_chunk_to_stdout(chunk_opt);
}

fn handle_list_chunks(args: StackerDBArgs) {
debug!("Listing chunks...");
let mut session = stackerdb_session(args.host, args.contract);
let mut session = stackerdb_session(&args.host, args.contract);
let chunk_list = session.list_chunks().unwrap();
println!("{}", serde_json::to_string(&chunk_list).unwrap());
}

fn handle_put_chunk(args: PutChunkArgs) {
debug!("Putting chunk...");
let mut session = stackerdb_session(args.db_args.host, args.db_args.contract);
let mut session = stackerdb_session(&args.db_args.host, args.db_args.contract);
let mut chunk = StackerDBChunkData::new(args.slot_id, args.slot_version, args.data);
chunk.sign(&args.private_key).unwrap();
let chunk_ack = session.put_chunk(&chunk).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion stacks-signer/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl RunLoop {
signer_slot_ids: signer_slot_ids.into_values().collect(),
ecdsa_private_key: self.config.ecdsa_private_key,
stacks_private_key: self.config.stacks_private_key,
node_host: self.config.node_host,
node_host: self.config.node_host.to_string(),
mainnet: self.config.network.is_mainnet(),
dkg_end_timeout: self.config.dkg_end_timeout,
dkg_private_timeout: self.config.dkg_private_timeout,
Expand Down
6 changes: 1 addition & 5 deletions testnet/stacks-node/src/nakamoto_node/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ impl BlockMinerThread {
let miners_contract_id = boot_code_id(MINERS_NAME, self.config.is_mainnet());
let stackerdbs = StackerDBs::connect(&self.config.get_stacker_db_file_path(), true)
.expect("FATAL: failed to connect to stacker DB");
let rpc_sock = self.config.node.rpc_bind.parse().expect(&format!(
"Failed to parse socket: {}",
&self.config.node.rpc_bind
));
let Some(miner_privkey) = self.config.miner.mining_key else {
warn!("No mining key configured, cannot mine");
return;
Expand Down Expand Up @@ -205,7 +201,7 @@ impl BlockMinerThread {
// Propose the block to the observing signers through the .miners stackerdb instance
let miner_contract_id = boot_code_id(MINERS_NAME, self.config.is_mainnet());
let mut miners_stackerdb =
StackerDBSession::new(rpc_sock, miner_contract_id);
StackerDBSession::new(&self.config.node.rpc_bind, miner_contract_id);
match miners_stackerdb.put_chunk(&chunk) {
Ok(ack) => {
info!("Proposed block to stackerdb: {ack:?}");
Expand Down
10 changes: 2 additions & 8 deletions testnet/stacks-node/src/tests/nakamoto_integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,13 +1827,6 @@ fn miner_writes_proposed_block_to_stackerdb() {
)
.unwrap();

let rpc_sock = naka_conf
.node
.rpc_bind
.clone()
.parse()
.expect("Failed to parse socket");

let sortdb = naka_conf.get_burnchain().open_sortition_db(true).unwrap();
let tip = SortitionDB::get_canonical_burn_chain_tip(sortdb.conn()).unwrap();
let miner_pubkey =
Expand All @@ -1844,7 +1837,8 @@ fn miner_writes_proposed_block_to_stackerdb() {

let chunk = std::thread::spawn(move || {
let miner_contract_id = boot_code_id(MINERS_NAME, false);
let mut miners_stackerdb = StackerDBSession::new(rpc_sock, miner_contract_id);
let mut miners_stackerdb =
StackerDBSession::new(&naka_conf.node.rpc_bind, miner_contract_id);
miners_stackerdb
.get_latest_chunk(slot_id)
.expect("Failed to get latest chunk from the miner slot ID")
Expand Down
11 changes: 1 addition & 10 deletions testnet/stacks-node/src/tests/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,15 +1285,6 @@ fn stackerdb_filter_bad_transactions() {
assert_ne!(current_signers_dkg, next_signers_dkg);

info!("------------------------- Submit Invalid Transactions -------------------------");
let host = signer_test
.running_nodes
.conf
.node
.rpc_bind
.to_socket_addrs()
.unwrap()
.next()
.unwrap();

let signer_private_key = signer_test
.signer_stacks_private_keys
Expand All @@ -1308,7 +1299,7 @@ fn stackerdb_filter_bad_transactions() {
// Must submit to the NEXT reward cycle slots as they are the ones looked at by the CURRENT miners
let signer_index = signer_test.get_signer_index(next_reward_cycle);
let mut stackerdb = StackerDB::new(
host,
&signer_test.running_nodes.conf.node.rpc_bind,
signer_private_key,
false,
next_reward_cycle,
Expand Down