Skip to content

Commit 970b8c4

Browse files
committed
CRC: get mainnet flag from stacks_client directly
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent a2c912d commit 970b8c4

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

libsigner/src/v0/messages.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ pub struct PeerInfo {
275275
pub pox_consensus: ConsensusHash,
276276
/// The server version
277277
pub server_version: String,
278+
/// The network id
279+
pub network_id: u32,
278280
}
279281

280282
impl StacksMessageCodec for PeerInfo {
@@ -287,6 +289,7 @@ impl StacksMessageCodec for PeerInfo {
287289
fd.write_all(self.server_version.as_bytes())
288290
.map_err(CodecError::WriteError)?;
289291
write_next(fd, &self.pox_consensus)?;
292+
write_next(fd, &self.network_id)?;
290293
Ok(())
291294
}
292295

@@ -305,13 +308,15 @@ impl StacksMessageCodec for PeerInfo {
305308
)
306309
})?;
307310
let pox_consensus = read_next::<ConsensusHash, _>(fd)?;
311+
let network_id = read_next(fd)?;
308312
Ok(Self {
309313
burn_block_height,
310314
stacks_tip_consensus_hash,
311315
stacks_tip,
312316
stacks_tip_height,
313317
server_version,
314318
pox_consensus,
319+
network_id,
315320
})
316321
}
317322
}
@@ -321,18 +326,15 @@ impl StacksMessageCodec for PeerInfo {
321326
pub struct MockProposal {
322327
/// The view of the stacks node peer information at the time of the mock proposal
323328
pub peer_info: PeerInfo,
324-
/// The chain id for the mock proposal
325-
pub chain_id: u32,
326329
/// The miner's signature across the peer info
327330
signature: MessageSignature,
328331
}
329332

330333
impl MockProposal {
331334
/// Create a new mock proposal data struct from the provided peer info, chain id, and private key.
332-
pub fn new(peer_info: PeerInfo, chain_id: u32, stacks_private_key: &StacksPrivateKey) -> Self {
335+
pub fn new(peer_info: PeerInfo, stacks_private_key: &StacksPrivateKey) -> Self {
333336
let mut sig = Self {
334337
signature: MessageSignature::empty(),
335-
chain_id,
336338
peer_info,
337339
};
338340
sig.sign(stacks_private_key)
@@ -342,7 +344,8 @@ impl MockProposal {
342344

343345
/// The signature hash for the mock proposal
344346
pub fn miner_signature_hash(&self) -> Sha256Sum {
345-
let domain_tuple = make_structured_data_domain("mock-miner", "1.0.0", self.chain_id);
347+
let domain_tuple =
348+
make_structured_data_domain("mock-miner", "1.0.0", self.peer_info.network_id);
346349
let data_tuple = Value::Tuple(
347350
TupleData::from_data(vec![
348351
(
@@ -375,7 +378,8 @@ impl MockProposal {
375378

376379
/// The signature hash including the miner's signature. Used by signers.
377380
fn signer_signature_hash(&self) -> Sha256Sum {
378-
let domain_tuple = make_structured_data_domain("mock-signer", "1.0.0", self.chain_id);
381+
let domain_tuple =
382+
make_structured_data_domain("mock-signer", "1.0.0", self.peer_info.network_id);
379383
let data_tuple = Value::Tuple(
380384
TupleData::from_data(vec![
381385
(
@@ -413,18 +417,15 @@ impl MockProposal {
413417
impl StacksMessageCodec for MockProposal {
414418
fn consensus_serialize<W: Write>(&self, fd: &mut W) -> Result<(), CodecError> {
415419
self.peer_info.consensus_serialize(fd)?;
416-
write_next(fd, &self.chain_id)?;
417420
write_next(fd, &self.signature)?;
418421
Ok(())
419422
}
420423

421424
fn consensus_deserialize<R: Read>(fd: &mut R) -> Result<Self, CodecError> {
422425
let peer_info = PeerInfo::consensus_deserialize(fd)?;
423-
let chain_id = read_next::<u32, _>(fd)?;
424426
let signature = read_next::<MessageSignature, _>(fd)?;
425427
Ok(Self {
426428
peer_info,
427-
chain_id,
428429
signature,
429430
})
430431
}
@@ -1024,26 +1025,26 @@ mod test {
10241025
let stacks_tip_height = thread_rng().next_u64();
10251026
let server_version = "0.0.0".to_string();
10261027
let pox_consensus_byte: u8 = thread_rng().gen();
1028+
let network_byte: u8 = thread_rng().gen_range(0..=1);
1029+
let network_id = if network_byte == 1 {
1030+
CHAIN_ID_TESTNET
1031+
} else {
1032+
CHAIN_ID_MAINNET
1033+
};
10271034
PeerInfo {
10281035
burn_block_height,
10291036
stacks_tip_consensus_hash: ConsensusHash([stacks_tip_consensus_byte; 20]),
10301037
stacks_tip: BlockHeaderHash([stacks_tip_byte; 32]),
10311038
stacks_tip_height,
10321039
server_version,
10331040
pox_consensus: ConsensusHash([pox_consensus_byte; 20]),
1041+
network_id,
10341042
}
10351043
}
10361044
fn random_mock_proposal() -> MockProposal {
1037-
let chain_byte: u8 = thread_rng().gen_range(0..=1);
1038-
let chain_id = if chain_byte == 1 {
1039-
CHAIN_ID_TESTNET
1040-
} else {
1041-
CHAIN_ID_MAINNET
1042-
};
10431045
let peer_info = random_peer_data();
10441046
MockProposal {
10451047
peer_info,
1046-
chain_id,
10471048
signature: MessageSignature::empty(),
10481049
}
10491050
}

stacks-signer/src/cli.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,6 @@ pub struct MonitorSignersArgs {
266266
/// The Stacks node to connect to
267267
#[arg(long)]
268268
pub host: String,
269-
/// Whether the node is mainnet. Default is false.
270-
#[arg(long, default_value = "false")]
271-
pub mainnet: bool,
272269
/// Set the polling interval in seconds.
273270
#[arg(long, short, default_value = "60")]
274271
pub interval: u64,

stacks-signer/src/client/stacks_client.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub struct StacksClient {
7575
/// The chain we are interacting with
7676
chain_id: u32,
7777
/// Whether we are mainnet or not
78-
mainnet: bool,
78+
pub mainnet: bool,
7979
/// The Client used to make HTTP connects
8080
stacks_node_client: reqwest::blocking::Client,
8181
/// the auth password for the stacks node
@@ -135,6 +135,28 @@ impl StacksClient {
135135
}
136136
}
137137

138+
/// Create a new signer StacksClient and attempt to connect to the stacks node to determine the version
139+
pub fn try_from_host(
140+
stacks_private_key: StacksPrivateKey,
141+
node_host: String,
142+
auth_password: String,
143+
) -> Result<Self, ClientError> {
144+
let mut stacks_client = Self::new(stacks_private_key, node_host, auth_password, true);
145+
let pubkey = StacksPublicKey::from_private(&stacks_private_key);
146+
let info = stacks_client.get_peer_info()?;
147+
if info.network_id == CHAIN_ID_MAINNET {
148+
stacks_client.mainnet = true;
149+
stacks_client.chain_id = CHAIN_ID_MAINNET;
150+
stacks_client.tx_version = TransactionVersion::Mainnet;
151+
} else {
152+
stacks_client.mainnet = false;
153+
stacks_client.chain_id = CHAIN_ID_TESTNET;
154+
stacks_client.tx_version = TransactionVersion::Testnet;
155+
}
156+
stacks_client.stacks_address = StacksAddress::p2pkh(stacks_client.mainnet, &pubkey);
157+
Ok(stacks_client)
158+
}
159+
138160
/// Get our signer address
139161
pub const fn get_signer_address(&self) -> &StacksAddress {
140162
&self.stacks_address

stacks-signer/src/monitor_signers.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ pub struct RewardCycleState {
5151
}
5252

5353
impl SignerMonitor {
54-
/// Create a new `SignerMonitor` instance from the given command line args
54+
/// Create a new `SignerMonitor` instance
5555
pub fn new(args: MonitorSignersArgs) -> Self {
5656
url::Url::parse(&format!("http://{}", args.host)).expect("Failed to parse node host");
57-
let stacks_client = StacksClient::new(
57+
let stacks_client = StacksClient::try_from_host(
5858
StacksPrivateKey::new(), // We don't need a private key to read
5959
args.host.clone(),
6060
"FOO".to_string(), // We don't care about authorized paths. Just accessing public info
61-
args.mainnet,
62-
);
61+
)
62+
.expect("Failed to connect to provided host.");
6363
Self {
6464
stacks_client,
6565
cycle_state: RewardCycleState::default(),
@@ -93,7 +93,7 @@ impl SignerMonitor {
9393
for entry in entries {
9494
let public_key = StacksPublicKey::from_slice(entry.signing_key.as_slice())
9595
.expect("Failed to convert signing key to StacksPublicKey");
96-
let stacks_address = StacksAddress::p2pkh(self.args.mainnet, &public_key);
96+
let stacks_address = StacksAddress::p2pkh(self.stacks_client.mainnet, &public_key);
9797
self.cycle_state
9898
.signers_keys
9999
.insert(stacks_address, public_key);
@@ -228,8 +228,8 @@ impl SignerMonitor {
228228
.cycle_state
229229
.reward_cycle
230230
.expect("BUG: reward cycle not set");
231-
let contract =
232-
MessageSlotID::BlockResponse.stacker_db_contract(self.args.mainnet, reward_cycle);
231+
let contract = MessageSlotID::BlockResponse
232+
.stacker_db_contract(self.stacks_client.mainnet, reward_cycle);
233233
info!(
234234
"Monitoring signers stackerdb. Polling interval: {} secs, Max message age: {} secs, Reward cycle: {reward_cycle}, StackerDB contract: {contract}",
235235
self.args.interval, self.args.max_age
@@ -252,7 +252,7 @@ impl SignerMonitor {
252252
.reward_cycle
253253
.expect("BUG: reward cycle not set");
254254
let contract = MessageSlotID::BlockResponse
255-
.stacker_db_contract(self.args.mainnet, reward_cycle);
255+
.stacker_db_contract(self.stacks_client.mainnet, reward_cycle);
256256
info!(
257257
"Reward cycle has changed to {reward_cycle}. Updating stacker db session to StackerDB contract {contract}.",
258258
);

0 commit comments

Comments
 (0)