Skip to content

Commit

Permalink
Merge pull request #4583 from stacks-network/feat/stacks-signer-fee-e…
Browse files Browse the repository at this point in the history
…stimate

Feat/stacks signer fee estimate
  • Loading branch information
jferrant committed Apr 5, 2024
2 parents 1ec9458 + 76b610b commit 8d4da57
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 151 deletions.
5 changes: 2 additions & 3 deletions stacks-signer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,16 @@ mod tests {
}

fn clarity_tuple_version(pox_addr: &PoxAddress) -> u8 {
pox_addr
*pox_addr
.as_clarity_tuple()
.expect("Failed to generate clarity tuple for pox address")
.get("version")
.expect("Expected version in clarity tuple")
.clone()
.expect_buff(1)
.expect("Expected version to be a u128")
.get(0)
.first()
.expect("Expected version to be a uint")
.clone()
}

#[test]
Expand Down
40 changes: 40 additions & 0 deletions stacks-signer/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub(crate) mod tests {
use blockstack_lib::net::api::getpoxinfo::{
RPCPoxCurrentCycleInfo, RPCPoxEpoch, RPCPoxInfoData, RPCPoxNextCycleInfo,
};
use blockstack_lib::net::api::postfeerate::{RPCFeeEstimate, RPCFeeEstimateResponse};
use blockstack_lib::util_lib::boot::boot_code_id;
use clarity::vm::costs::ExecutionCost;
use clarity::vm::types::TupleData;
Expand Down Expand Up @@ -398,6 +399,44 @@ pub(crate) mod tests {
format!("HTTP/1.1 200 OK\n\n{{\"okay\":true,\"result\":\"{hex}\"}}")
}

/// Build a response for the get_medium_estimated_fee_ustx_response request with a specific medium estimate
pub fn build_get_medium_estimated_fee_ustx_response(
medium_estimate: u64,
) -> (String, RPCFeeEstimateResponse) {
// Generate some random info
let fee_response = RPCFeeEstimateResponse {
estimated_cost: ExecutionCost {
write_length: thread_rng().next_u64(),
write_count: thread_rng().next_u64(),
read_length: thread_rng().next_u64(),
read_count: thread_rng().next_u64(),
runtime: thread_rng().next_u64(),
},
estimated_cost_scalar: thread_rng().next_u64(),
cost_scalar_change_by_byte: thread_rng().next_u32() as f64,
estimations: vec![
RPCFeeEstimate {
fee_rate: thread_rng().next_u32() as f64,
fee: thread_rng().next_u64(),
},
RPCFeeEstimate {
fee_rate: thread_rng().next_u32() as f64,
fee: medium_estimate,
},
RPCFeeEstimate {
fee_rate: thread_rng().next_u32() as f64,
fee: thread_rng().next_u64(),
},
],
};
let fee_response_json = serde_json::to_string(&fee_response)
.expect("Failed to serialize fee estimate response");
(
format!("HTTP/1.1 200 OK\n\n{fee_response_json}"),
fee_response,
)
}

/// Generate a signer config with the given number of signers and keys where the first signer is
/// obtained from the provided global config
pub fn generate_signer_config(
Expand Down Expand Up @@ -515,6 +554,7 @@ pub(crate) mod tests {
nonce_timeout: config.nonce_timeout,
sign_timeout: config.sign_timeout,
tx_fee_ustx: config.tx_fee_ustx,
max_tx_fee_ustx: config.max_tx_fee_ustx,
db_path: config.db_path.clone(),
}
}
Expand Down
14 changes: 6 additions & 8 deletions stacks-signer/src/client/stackerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl StackerDB {
}

/// Sends message (as a raw msg ID and bytes) to the .signers stacker-db with an
/// exponential backoff retry
/// exponential backoff retry
pub fn send_message_bytes_with_retry(
&mut self,
msg_id: &MessageSlotID,
Expand Down Expand Up @@ -224,9 +224,7 @@ impl StackerDB {
}

/// Get this signer's latest transactions from stackerdb
pub fn get_current_transactions_with_retry(
&mut self,
) -> Result<Vec<StacksTransaction>, ClientError> {
pub fn get_current_transactions(&mut self) -> Result<Vec<StacksTransaction>, ClientError> {
let Some(transactions_session) = self
.signers_message_stackerdb_sessions
.get_mut(&MessageSlotID::Transactions)
Expand All @@ -237,7 +235,7 @@ impl StackerDB {
}

/// Get the latest signer transactions from signer ids for the next reward cycle
pub fn get_next_transactions_with_retry(
pub fn get_next_transactions(
&mut self,
signer_ids: &[SignerSlotID],
) -> Result<Vec<StacksTransaction>, ClientError> {
Expand Down Expand Up @@ -272,7 +270,7 @@ mod tests {
use crate::config::GlobalConfig;

#[test]
fn get_signer_transactions_with_retry_should_succeed() {
fn get_signer_transactions_should_succeed() {
let config = GlobalConfig::load_from_file("./src/tests/conf/signer-0.toml").unwrap();
let signer_config = generate_signer_config(&config, 5, 20);
let mut stackerdb = StackerDB::from(&signer_config);
Expand All @@ -297,7 +295,7 @@ mod tests {
let message = signer_message.serialize_to_vec();

let signer_slot_ids = vec![SignerSlotID(0), SignerSlotID(1)];
let h = spawn(move || stackerdb.get_next_transactions_with_retry(&signer_slot_ids));
let h = spawn(move || stackerdb.get_next_transactions(&signer_slot_ids));
let mut response_bytes = b"HTTP/1.1 200 OK\n\n".to_vec();
response_bytes.extend(message);
let mock_server = mock_server_from_config(&config);
Expand All @@ -315,7 +313,7 @@ mod tests {
}

#[test]
fn send_signer_message_with_retry_should_succeed() {
fn send_signer_message_should_succeed() {
let config = GlobalConfig::load_from_file("./src/tests/conf/signer-1.toml").unwrap();
let signer_config = generate_signer_config(&config, 5, 20);
let mut stackerdb = StackerDB::from(&signer_config);
Expand Down

0 comments on commit 8d4da57

Please sign in to comment.