Skip to content

Commit

Permalink
Add SignerEvent::NewNakamotoBlock and do not update a block to Global…
Browse files Browse the repository at this point in the history
…lyAccepted until node processes the new block successfully

Signed-off-by: Jacinta Ferrant <[email protected]>
  • Loading branch information
jferrant committed Nov 27, 2024
1 parent 7267cd0 commit 7fefa19
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 16 deletions.
69 changes: 61 additions & 8 deletions libsigner/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ pub enum SignerEvent<T: SignerEventTrait> {
/// the time at which this event was received by the signer's event processor
received_time: SystemTime,
},
/// A new processed Nakamoto block was received from the node with the given block hash
NewNakamotoBlock {
/// The block header hash for the newly processed stacks block
block_hash: Sha512Trunc256Sum,
/// The block height for the newly processed stacks block
block_height: u64,
/// The block timestamp for the newly processed stacks block
block_time: u64,
},
}

/// Trait to implement a stop-signaler for the event receiver thread.
Expand Down Expand Up @@ -311,16 +320,15 @@ impl<T: SignerEventTrait> EventReceiver<T> for SignerEventReceiver<T> {
} else if request.url() == "/shutdown" {
event_receiver.stop_signal.store(true, Ordering::SeqCst);
return Err(EventError::Terminated);
} else if request.url() == "/new_block" {
process_new_block(request)
} else {
let url = request.url().to_string();
// `/new_block` is expected, but not specifically handled. do not log.
if &url != "/new_block" {
debug!(
"[{:?}] next_event got request with unexpected url {}, return OK so other side doesn't keep sending this",
event_receiver.local_addr,
url
);
}
debug!(
"[{:?}] next_event got request with unexpected url {}, return OK so other side doesn't keep sending this",
event_receiver.local_addr,
url
);
ack_dispatcher(request);
Err(EventError::UnrecognizedEvent(url))
}
Expand Down Expand Up @@ -540,6 +548,51 @@ fn process_new_burn_block_event<T: SignerEventTrait>(
Ok(event)
}

/// Process a new burn block event from the node
fn process_new_block<T: SignerEventTrait>(
mut request: HttpRequest,
) -> Result<SignerEvent<T>, EventError> {
debug!("Got new_block event");
let mut body = String::new();
if let Err(e) = request.as_reader().read_to_string(&mut body) {
error!("Failed to read body: {:?}", &e);

if let Err(e) = request.respond(HttpResponse::empty(200u16)) {
error!("Failed to respond to request: {:?}", &e);
}
return Err(EventError::MalformedRequest(format!(
"Failed to read body: {:?}",
&e
)));
}
#[derive(Debug, Deserialize)]
struct TempBlockEvent {
block_hash: String,
block_height: u64,
block_time: u64,
}

let temp: TempBlockEvent = serde_json::from_slice(body.as_bytes())
.map_err(|e| EventError::Deserialize(format!("Could not decode body to JSON: {:?}", &e)))?;
let block_hash: Sha512Trunc256Sum = temp
.block_hash
.get(2..)
.ok_or_else(|| EventError::Deserialize("Hex string should be 0x prefixed".into()))
.and_then(|hex| {
Sha512Trunc256Sum::from_hex(hex)
.map_err(|e| EventError::Deserialize(format!("Invalid hex string: {e}")))
})?;
let event = SignerEvent::NewNakamotoBlock {
block_hash,
block_height: temp.block_height,
block_time: temp.block_time,
};
if let Err(e) = request.respond(HttpResponse::empty(200u16)) {
error!("Failed to respond to request: {:?}", &e);
}
Ok(event)
}

pub fn get_signers_db_signer_set_message_id(name: &str) -> Option<(u32, u32)> {
// Splitting the string by '-'
let parts: Vec<&str> = name.split('-').collect();
Expand Down
11 changes: 3 additions & 8 deletions stacks-signer/src/signerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,13 +811,8 @@ impl SignerDb {
block_sighash: &Sha512Trunc256Sum,
ts: u64,
) -> Result<(), DBError> {
let qry = "UPDATE blocks SET broadcasted = ?1, block_info = json_set(block_info, '$.state', ?2) WHERE reward_cycle = ?3 AND signer_signature_hash = ?4";
let args = params![
u64_to_sql(ts)?,
BlockState::GloballyAccepted.to_string(),
u64_to_sql(reward_cycle)?,
block_sighash
];
let qry = "UPDATE blocks SET broadcasted = ?1 WHERE reward_cycle = ?2 AND signer_signature_hash = ?3";
let args = params![u64_to_sql(ts)?, u64_to_sql(reward_cycle)?, block_sighash];

debug!("Marking block {} as broadcasted at {}", block_sighash, ts);
self.db.execute(qry, args)?;
Expand Down Expand Up @@ -1220,7 +1215,7 @@ mod tests {
.expect("Unable to get block from db")
.expect("Unable to get block from db")
.state,
BlockState::GloballyAccepted
BlockState::Unprocessed
);
db.insert_block(&block_info_1)
.expect("Unable to insert block into db a second time");
Expand Down
30 changes: 30 additions & 0 deletions stacks-signer/src/v0/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl SignerTrait<SignerMessage> for Signer {
Some(SignerEvent::BlockValidationResponse(_))
| Some(SignerEvent::MinerMessages(..))
| Some(SignerEvent::NewBurnBlock { .. })
| Some(SignerEvent::NewNakamotoBlock { .. })
| Some(SignerEvent::StatusCheck)
| None => None,
Some(SignerEvent::SignerMessages(msg_parity, ..)) => Some(u64::from(*msg_parity) % 2),
Expand Down Expand Up @@ -246,6 +247,35 @@ impl SignerTrait<SignerMessage> for Signer {
});
*sortition_state = None;
}
SignerEvent::NewNakamotoBlock {
block_hash,
block_height,
block_time,
} => {
debug!(
"{self}: Received a new block event.";
"block_hash" => %block_hash,
"block_height" => block_height,
"block_time" => block_time
);
if let Ok(Some(mut block_info)) = self
.signer_db
.block_lookup(self.reward_cycle, block_hash)
.inspect_err(|e| warn!("{self}: Failed to load block state: {e:?}"))
{
if block_info.state == BlockState::GloballyAccepted {
// We have already globally accepted this block. Do nothing.
return;
}
if let Err(e) = block_info.mark_globally_accepted() {
warn!("{self}: Failed to mark block as globally accepted: {e:?}");
return;
}
if let Err(e) = self.signer_db.insert_block(&block_info) {
warn!("{self}: Failed to update block state to globally accepted: {e:?}");
}
}
}
}
}

Expand Down

0 comments on commit 7fefa19

Please sign in to comment.