Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request stacks-network#4323 from stacks-network/feat/large…
Browse files Browse the repository at this point in the history
…r-stackerdb

Paging in StackerDB and update to .signers
  • Loading branch information
jcnelson authored Feb 10, 2024
2 parents efbef05 + 0772e75 commit 606838d
Show file tree
Hide file tree
Showing 36 changed files with 1,274 additions and 541 deletions.
16 changes: 16 additions & 0 deletions clarity/src/vm/database/clarity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,22 @@ impl<'a> ClarityDatabase<'a> {
.map_err(|e| e.into())
}

/// Set a metadata entry if it hasn't already been set, yielding
/// a runtime error if it was. This should only be called by post-nakamoto
/// contexts.
pub fn try_set_metadata(
&mut self,
contract_identifier: &QualifiedContractIdentifier,
key: &str,
data: &str,
) -> Result<()> {
if self.store.has_metadata_entry(contract_identifier, key) {
Err(Error::Runtime(RuntimeErrorType::MetadataAlreadySet, None))
} else {
Ok(self.store.insert_metadata(contract_identifier, key, data)?)
}
}

fn insert_metadata<T: ClaritySerializable>(
&mut self,
contract_identifier: &QualifiedContractIdentifier,
Expand Down
7 changes: 5 additions & 2 deletions libsigner/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use stacks_common::codec::{
read_next, read_next_at_most, read_next_exact, write_next, Error as CodecError,
StacksMessageCodec,
};
pub use stacks_common::consts::SIGNER_SLOTS_PER_USER;
use stacks_common::util::hash::Sha512Trunc256Sum;
use tiny_http::{
Method as HttpMethod, Request as HttpRequest, Response as HttpResponse, Server as HttpServer,
Expand Down Expand Up @@ -345,8 +346,10 @@ fn process_stackerdb_event(
.filter_map(|chunk| read_next::<NakamotoBlock, _>(&mut &chunk.data[..]).ok())
.collect();
SignerEvent::ProposedBlocks(blocks)
} else if event.contract_id.name.to_string() == SIGNERS_NAME {
// TODO: fix this to be against boot_code_id(SIGNERS_NAME, is_mainnet) when .signers is deployed
} else if event.contract_id.name.to_string().starts_with(SIGNERS_NAME)
&& event.contract_id.issuer.1 == [0u8; 20]
{
// signer-XXX-YYY boot contract
let signer_messages: Vec<SignerMessage> = event
.modified_slots
.iter()
Expand Down
3 changes: 1 addition & 2 deletions libsigner/src/libsigner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ pub use crate::events::{
EventReceiver, EventStopSignaler, SignerEvent, SignerEventReceiver, SignerStopSignaler,
};
pub use crate::messages::{
BlockRejection, BlockResponse, RejectCode, SignerMessage, BLOCK_SLOT_ID, SIGNER_SLOTS_PER_USER,
TRANSACTIONS_SLOT_ID,
BlockRejection, BlockResponse, RejectCode, SignerMessage, BLOCK_MSG_ID, TRANSACTIONS_MSG_ID,
};
pub use crate::runloop::{RunningSigner, Signer, SignerRunLoop};
pub use crate::session::{SignerSession, StackerDBSession};
77 changes: 36 additions & 41 deletions libsigner/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::sync::mpsc::Sender;
use std::sync::Arc;

use blockstack_lib::chainstate::nakamoto::NakamotoBlock;
use blockstack_lib::chainstate::stacks::boot::{MINERS_NAME, SIGNERS_NAME};
use blockstack_lib::chainstate::stacks::events::StackerDBChunksEvent;
use blockstack_lib::chainstate::stacks::{StacksTransaction, ThresholdSignature};
use blockstack_lib::net::api::postblock_proposal::{
Expand All @@ -36,6 +35,7 @@ use stacks_common::codec::{
read_next, read_next_at_most, read_next_exact, write_next, Error as CodecError,
StacksMessageCodec,
};
use stacks_common::consts::SIGNER_SLOTS_PER_USER;
use stacks_common::util::hash::Sha512Trunc256Sum;
use tiny_http::{
Method as HttpMethod, Request as HttpRequest, Response as HttpResponse, Server as HttpServer,
Expand All @@ -54,26 +54,21 @@ use wsts::state_machine::signer;
use crate::http::{decode_http_body, decode_http_request};
use crate::EventError;

/// Temporary placeholder for the number of slots allocated to a stacker-db writer. This will be retrieved from the stacker-db instance in the future
/// See: https://github.com/stacks-network/stacks-blockchain/issues/3921
/// Is equal to the number of message types
pub const SIGNER_SLOTS_PER_USER: u32 = 12;

// The slot IDS for each message type
const DKG_BEGIN_SLOT_ID: u32 = 0;
const DKG_PRIVATE_BEGIN_SLOT_ID: u32 = 1;
const DKG_END_BEGIN_SLOT_ID: u32 = 2;
const DKG_END_SLOT_ID: u32 = 3;
const DKG_PUBLIC_SHARES_SLOT_ID: u32 = 4;
const DKG_PRIVATE_SHARES_SLOT_ID: u32 = 5;
const NONCE_REQUEST_SLOT_ID: u32 = 6;
const NONCE_RESPONSE_SLOT_ID: u32 = 7;
const SIGNATURE_SHARE_REQUEST_SLOT_ID: u32 = 8;
const SIGNATURE_SHARE_RESPONSE_SLOT_ID: u32 = 9;
const DKG_BEGIN_MSG_ID: u32 = 0;
const DKG_PRIVATE_BEGIN_MSG_ID: u32 = 1;
const DKG_END_BEGIN_MSG_ID: u32 = 2;
const DKG_END_MSG_ID: u32 = 3;
const DKG_PUBLIC_SHARES_MSG_ID: u32 = 4;
const DKG_PRIVATE_SHARES_MSG_ID: u32 = 5;
const NONCE_REQUEST_MSG_ID: u32 = 6;
const NONCE_RESPONSE_MSG_ID: u32 = 7;
const SIGNATURE_SHARE_REQUEST_MSG_ID: u32 = 8;
const SIGNATURE_SHARE_RESPONSE_MSG_ID: u32 = 9;
/// The slot ID for the block response for miners to observe
pub const BLOCK_SLOT_ID: u32 = 10;
pub const BLOCK_MSG_ID: u32 = 10;
/// The slot ID for the transactions list for miners and signers to observe
pub const TRANSACTIONS_SLOT_ID: u32 = 11;
pub const TRANSACTIONS_MSG_ID: u32 = 11;

define_u8_enum!(SignerMessageTypePrefix {
BlockResponse = 0,
Expand Down Expand Up @@ -179,6 +174,29 @@ pub enum SignerMessage {
Transactions(Vec<StacksTransaction>),
}

impl SignerMessage {
/// Helper function to determine the slot ID for the provided stacker-db writer id
pub fn msg_id(&self) -> u32 {
let msg_id = match self {
Self::Packet(packet) => match packet.msg {
Message::DkgBegin(_) => DKG_BEGIN_MSG_ID,
Message::DkgPrivateBegin(_) => DKG_PRIVATE_BEGIN_MSG_ID,
Message::DkgEndBegin(_) => DKG_END_BEGIN_MSG_ID,
Message::DkgEnd(_) => DKG_END_MSG_ID,
Message::DkgPublicShares(_) => DKG_PUBLIC_SHARES_MSG_ID,
Message::DkgPrivateShares(_) => DKG_PRIVATE_SHARES_MSG_ID,
Message::NonceRequest(_) => NONCE_REQUEST_MSG_ID,
Message::NonceResponse(_) => NONCE_RESPONSE_MSG_ID,
Message::SignatureShareRequest(_) => SIGNATURE_SHARE_REQUEST_MSG_ID,
Message::SignatureShareResponse(_) => SIGNATURE_SHARE_RESPONSE_MSG_ID,
},
Self::BlockResponse(_) => BLOCK_MSG_ID,
Self::Transactions(_) => TRANSACTIONS_MSG_ID,
};
msg_id
}
}

impl StacksMessageCodec for SignerMessage {
fn consensus_serialize<W: Write>(&self, fd: &mut W) -> Result<(), CodecError> {
write_next(fd, &(SignerMessageTypePrefix::from(self) as u8))?;
Expand Down Expand Up @@ -1021,29 +1039,6 @@ impl From<BlockValidateReject> for SignerMessage {
}
}

impl SignerMessage {
/// Helper function to determine the slot ID for the provided stacker-db writer id
pub fn slot_id(&self, id: u32) -> u32 {
let slot_id = match self {
Self::Packet(packet) => match packet.msg {
Message::DkgBegin(_) => DKG_BEGIN_SLOT_ID,
Message::DkgPrivateBegin(_) => DKG_PRIVATE_BEGIN_SLOT_ID,
Message::DkgEndBegin(_) => DKG_END_BEGIN_SLOT_ID,
Message::DkgEnd(_) => DKG_END_SLOT_ID,
Message::DkgPublicShares(_) => DKG_PUBLIC_SHARES_SLOT_ID,
Message::DkgPrivateShares(_) => DKG_PRIVATE_SHARES_SLOT_ID,
Message::NonceRequest(_) => NONCE_REQUEST_SLOT_ID,
Message::NonceResponse(_) => NONCE_RESPONSE_SLOT_ID,
Message::SignatureShareRequest(_) => SIGNATURE_SHARE_REQUEST_SLOT_ID,
Message::SignatureShareResponse(_) => SIGNATURE_SHARE_RESPONSE_SLOT_ID,
},
Self::BlockResponse(_) => BLOCK_SLOT_ID,
Self::Transactions(_) => TRANSACTIONS_SLOT_ID,
};
SIGNER_SLOTS_PER_USER * id + slot_id
}
}

#[cfg(test)]
mod test {

Expand Down
4 changes: 1 addition & 3 deletions libsigner/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ impl SignerRunLoop<Vec<SignerEvent>, Command> for SimpleRunLoop {
/// and the signer runloop.
#[test]
fn test_simple_signer() {
let contract_id =
QualifiedContractIdentifier::parse("ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.signers")
.unwrap(); // TODO: change to boot_code_id(SIGNERS_NAME, false) when .signers is deployed
let contract_id = boot_code_id(SIGNERS_NAME, false);
let ev = SignerEventReceiver::new(vec![contract_id.clone()], false);
let (_cmd_send, cmd_recv) = channel();
let (res_send, _res_recv) = channel();
Expand Down
4 changes: 4 additions & 0 deletions stacks-common/src/libcommon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ pub mod consts {
pub const MINER_REWARD_MATURITY: u64 = 100;

pub const STACKS_EPOCH_MAX: u64 = i64::MAX as u64;

/// The number of StackerDB slots each signing key needs
/// to use to participate in DKG and block validation signing.
pub const SIGNER_SLOTS_PER_USER: u32 = 12;
}
6 changes: 6 additions & 0 deletions stacks-signer/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ pub enum ClientError {
/// Backoff retry timeout
#[error("Backoff retry timeout occurred. Stacks node may be down.")]
RetryTimeout,
/// Not connected
#[error("Not connected")]
NotConnected,
/// Invalid signing key
#[error("Signing key not represented in the list of signers")]
InvalidSigningKey,
/// Clarity interpreter error
#[error("Clarity interpreter error: {0}")]
ClarityError(ClarityError),
Expand Down
Loading

0 comments on commit 606838d

Please sign in to comment.