Skip to content

Commit c110f0d

Browse files
committed
use prefix_hex for ser/deser
* simplify multiple_miners_empty_sortition and single_miner_empty_sortition tests
1 parent 08b4f28 commit c110f0d

File tree

5 files changed

+217
-257
lines changed

5 files changed

+217
-257
lines changed

libsigner/src/events.rs

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use blockstack_lib::chainstate::stacks::StacksTransaction;
2929
use blockstack_lib::net::api::postblock_proposal::{
3030
BlockValidateReject, BlockValidateResponse, ValidateRejectCode,
3131
};
32+
use blockstack_lib::net::api::{prefix_hex, prefix_opt_hex};
3233
use blockstack_lib::net::stackerdb::MINER_SLOT_COUNT;
3334
use blockstack_lib::util_lib::boot::boot_code_id;
3435
use blockstack_lib::version_string;
@@ -215,8 +216,9 @@ pub enum SignerEvent<T: SignerEventTrait> {
215216
/// The consensus hash of the block (either the tenure it was produced during for Stacks 3.0
216217
/// or the burn block that won the sortition in Stacks 2.0)
217218
consensus_hash: ConsensusHash,
218-
/// The signer sighash for the newly processed stacks block
219-
signer_sighash: Sha512Trunc256Sum,
219+
/// The signer sighash for the newly processed stacks block. If the newly processed block is a 2.0
220+
/// block, there is *no* signer sighash
221+
signer_sighash: Option<Sha512Trunc256Sum>,
220222
/// The block height for the newly processed stacks block
221223
block_height: u64,
222224
},
@@ -559,86 +561,50 @@ impl<T: SignerEventTrait> TryFrom<BlockValidateResponse> for SignerEvent<T> {
559561

560562
#[derive(Debug, Deserialize)]
561563
struct BurnBlockEvent {
562-
burn_block_hash: String,
564+
#[serde(with = "prefix_hex")]
565+
burn_block_hash: BurnchainHeaderHash,
563566
burn_block_height: u64,
564567
reward_recipients: Vec<serde_json::Value>,
565568
reward_slot_holders: Vec<String>,
566569
burn_amount: u64,
567-
consensus_hash: String,
570+
#[serde(with = "prefix_hex")]
571+
consensus_hash: ConsensusHash,
568572
}
569573

570574
impl<T: SignerEventTrait> TryFrom<BurnBlockEvent> for SignerEvent<T> {
571575
type Error = EventError;
572576

573577
fn try_from(burn_block_event: BurnBlockEvent) -> Result<Self, Self::Error> {
574-
let burn_header_hash = burn_block_event
575-
.burn_block_hash
576-
.get(2..)
577-
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
578-
.and_then(|hex| {
579-
BurnchainHeaderHash::from_hex(hex)
580-
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
581-
})?;
582-
583-
let consensus_hash = burn_block_event
584-
.consensus_hash
585-
.get(2..)
586-
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
587-
.and_then(|hex| {
588-
ConsensusHash::from_hex(hex)
589-
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
590-
})?;
591-
592578
Ok(SignerEvent::NewBurnBlock {
593579
burn_height: burn_block_event.burn_block_height,
594580
received_time: SystemTime::now(),
595-
burn_header_hash,
596-
consensus_hash,
581+
burn_header_hash: burn_block_event.burn_block_hash,
582+
consensus_hash: burn_block_event.consensus_hash,
597583
})
598584
}
599585
}
600586

601587
#[derive(Debug, Deserialize)]
602588
struct BlockEvent {
603-
index_block_hash: String,
604-
signer_signature_hash: String,
605-
consensus_hash: String,
589+
#[serde(with = "prefix_hex")]
590+
index_block_hash: StacksBlockId,
591+
#[serde(with = "prefix_opt_hex")]
592+
signer_signature_hash: Option<Sha512Trunc256Sum>,
593+
#[serde(with = "prefix_hex")]
594+
consensus_hash: ConsensusHash,
595+
#[serde(with = "prefix_hex")]
596+
block_hash: BlockHeaderHash,
606597
block_height: u64,
607598
}
608599

609600
impl<T: SignerEventTrait> TryFrom<BlockEvent> for SignerEvent<T> {
610601
type Error = EventError;
611602

612603
fn try_from(block_event: BlockEvent) -> Result<Self, Self::Error> {
613-
let signer_sighash = block_event
614-
.signer_signature_hash
615-
.get(2..)
616-
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
617-
.and_then(|hex| {
618-
Sha512Trunc256Sum::from_hex(hex)
619-
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
620-
})?;
621-
let consensus_hash = block_event
622-
.consensus_hash
623-
.get(2..)
624-
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
625-
.and_then(|hex| {
626-
ConsensusHash::from_hex(hex)
627-
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
628-
})?;
629-
let block_id = block_event
630-
.index_block_hash
631-
.get(2..)
632-
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
633-
.and_then(|hex| {
634-
StacksBlockId::from_hex(hex)
635-
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
636-
})?;
637-
638604
Ok(SignerEvent::NewBlock {
639-
block_id,
640-
signer_sighash,
641-
consensus_hash,
605+
signer_sighash: block_event.signer_signature_hash,
606+
block_id: block_event.index_block_hash,
607+
consensus_hash: block_event.consensus_hash,
642608
block_height: block_event.block_height,
643609
})
644610
}

stacks-signer/src/v0/signer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ impl SignerTrait<SignerMessage> for Signer {
341341
consensus_hash,
342342
signer_sighash,
343343
} => {
344+
let Some(signer_sighash) = signer_sighash else {
345+
debug!("{self}: received a new block event for a pre-nakamoto block, no processing necessary");
346+
return;
347+
};
344348
debug!(
345349
"{self}: Received a new block event.";
346350
"block_id" => %block_id,

stackslib/src/net/api/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use stacks_common::codec::read_next;
2020
use stacks_common::types::chainstate::{
2121
BlockHeaderHash, BurnchainHeaderHash, ConsensusHash, SortitionId, StacksBlockId,
2222
};
23-
use stacks_common::util::hash::Hash160;
23+
use stacks_common::util::hash::{Hash160, Sha512Trunc256Sum};
2424
use stacks_common::util::HexError;
2525

2626
use crate::burnchains::Txid;
@@ -244,3 +244,4 @@ impl_hex_deser!(VRFSeed);
244244
impl_hex_deser!(ConsensusHash);
245245
impl_hex_deser!(BlockHeaderHash);
246246
impl_hex_deser!(Hash160);
247+
impl_hex_deser!(Sha512Trunc256Sum);

testnet/stacks-node/src/tests/signer/mod.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ use stacks_signer::runloop::{SignerResult, State, StateInfo};
5050
use stacks_signer::v0::signer_state::{LocalStateMachine, MinerState};
5151
use stacks_signer::{Signer, SpawnedSigner};
5252

53-
use super::make_stacks_transfer;
5453
use super::nakamoto_integrations::{
5554
check_nakamoto_empty_block_heuristics, next_block_and, wait_for,
5655
};
5756
use super::neon_integrations::{get_account, get_sortition_info_ch, submit_tx_fallible};
57+
use super::{make_contract_call, make_contract_publish, make_stacks_transfer};
5858
use crate::neon::Counters;
5959
use crate::run_loop::boot_nakamoto;
6060
use crate::tests::bitcoin_regtest::BitcoinCoreController;
@@ -448,6 +448,65 @@ impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<Sp
448448
submit_tx_fallible(&http_origin, &transfer_tx).map(|resp| (resp, sender_nonce))
449449
}
450450

451+
/// Submit a burn block dependent contract for publishing
452+
/// and wait until it is included in a block
453+
pub fn submit_burn_block_contract_and_wait(
454+
&mut self,
455+
sender_sk: &StacksPrivateKey,
456+
) -> Result<String, String> {
457+
let http_origin = format!("http://{}", &self.running_nodes.conf.node.rpc_bind);
458+
let sender_addr = to_addr(&sender_sk);
459+
let sender_nonce = get_account(&http_origin, &sender_addr).nonce;
460+
let burn_height_contract = "
461+
(define-data-var local-burn-block-ht uint u0)
462+
(define-public (run-update)
463+
(ok (var-set local-burn-block-ht burn-block-height)))
464+
";
465+
let contract_tx = make_contract_publish(
466+
&sender_sk,
467+
0,
468+
1000,
469+
self.running_nodes.conf.burnchain.chain_id,
470+
"burn-height-local",
471+
burn_height_contract,
472+
);
473+
let txid = submit_tx_fallible(&http_origin, &contract_tx)?;
474+
475+
wait_for(120, || {
476+
let next_nonce = get_account(&http_origin, &sender_addr).nonce;
477+
Ok(next_nonce > sender_nonce)
478+
})
479+
.map(|()| txid)
480+
}
481+
482+
/// Submit a burn block dependent contract-call
483+
/// and wait until it is included in a block
484+
pub fn submit_burn_block_call_and_wait(
485+
&mut self,
486+
sender_sk: &StacksPrivateKey,
487+
) -> Result<String, String> {
488+
let http_origin = format!("http://{}", &self.running_nodes.conf.node.rpc_bind);
489+
let sender_addr = to_addr(&sender_sk);
490+
let sender_nonce = get_account(&http_origin, &sender_addr).nonce;
491+
let contract_call_tx = make_contract_call(
492+
&sender_sk,
493+
sender_nonce,
494+
1000,
495+
self.running_nodes.conf.burnchain.chain_id,
496+
&sender_addr,
497+
"burn-height-local",
498+
"run-update",
499+
&[],
500+
);
501+
let txid = submit_tx_fallible(&http_origin, &contract_call_tx)?;
502+
503+
wait_for(120, || {
504+
let next_nonce = get_account(&http_origin, &sender_addr).nonce;
505+
Ok(next_nonce > sender_nonce)
506+
})
507+
.map(|()| txid)
508+
}
509+
451510
/// Get the local state machines and most recent peer info from the stacks-node,
452511
/// waiting until all of the signers have updated their state machines to
453512
/// reflect the most recent burn block.

0 commit comments

Comments
 (0)