Skip to content

Commit

Permalink
CRC: add config tests and remove config log
Browse files Browse the repository at this point in the history
Signed-off-by: Jacinta Ferrant <jacinta@trustmachines.co>
  • Loading branch information
jferrant committed Apr 2, 2024
1 parent a112bf5 commit 110d04e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
5 changes: 2 additions & 3 deletions stacks-signer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,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
118 changes: 118 additions & 0 deletions stacks-signer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ impl GlobalConfig {
}

/// Helper function for building a signer config for each provided signer private key
#[allow(clippy::too_many_arguments)]
pub fn build_signer_config_tomls(
stacks_private_keys: &[StacksPrivateKey],
node_host: &str,
Expand All @@ -348,6 +349,7 @@ pub fn build_signer_config_tomls(
run_stamp: u16,
mut port_start: usize,
max_tx_fee_ustx: Option<u64>,
tx_fee_ustx: Option<u64>,
) -> Vec<String> {
let mut signer_config_tomls = vec![];

Expand Down Expand Up @@ -393,6 +395,15 @@ max_tx_fee_ustx = {max_tx_fee_ustx}
)
}

if let Some(tx_fee_ustx) = tx_fee_ustx {
signer_config_toml = format!(
r#"
{signer_config_toml}
tx_fee_ustx = {tx_fee_ustx}
"#
)
}

signer_config_tomls.push(signer_config_toml);
}

Expand Down Expand Up @@ -423,11 +434,118 @@ mod tests {
rand::random(),
3000,
None,
None,
);

let config =
RawConfigFile::load_from_str(&config_tomls[0]).expect("Failed to parse config file");

assert_eq!(config.auth_password, "melon");
assert!(config.max_tx_fee_ustx.is_none());
assert!(config.tx_fee_ustx.is_none());
}

#[test]
fn fee_options_should_deserialize_correctly() {
let pk = StacksPrivateKey::from_hex(
"eb05c83546fdd2c79f10f5ad5434a90dd28f7e3acb7c092157aa1bc3656b012c01",
)
.unwrap();

let node_host = "localhost";
let network = Network::Testnet;
let password = "melon";

// Test both max_tx_fee_ustx and tx_fee_ustx are unspecified
let config_tomls = build_signer_config_tomls(
&[pk],
node_host,
None,
&network,
password,
rand::random(),
3000,
None,
None,
);

let config =
RawConfigFile::load_from_str(&config_tomls[0]).expect("Failed to parse config file");

assert!(config.max_tx_fee_ustx.is_none());
assert!(config.tx_fee_ustx.is_none());

let config = GlobalConfig::try_from(config).expect("Failed to parse config");
assert!(config.max_tx_fee_ustx.is_none());
assert_eq!(config.tx_fee_ustx, TX_FEE_USTX);

// Test both max_tx_fee_ustx and tx_fee_ustx are specified
let max_tx_fee_ustx = Some(1000);
let tx_fee_ustx = Some(2000);
let config_tomls = build_signer_config_tomls(
&[pk],
node_host,
None,
&network,
password,
rand::random(),
3000,
max_tx_fee_ustx,
tx_fee_ustx,
);

let config =
RawConfigFile::load_from_str(&config_tomls[0]).expect("Failed to parse config file");

assert_eq!(config.max_tx_fee_ustx, max_tx_fee_ustx);
assert_eq!(config.tx_fee_ustx, tx_fee_ustx);

// Test only max_tx_fee_ustx is specified
let max_tx_fee_ustx = Some(1000);
let config_tomls = build_signer_config_tomls(
&[pk],
node_host,
None,
&network,
password,
rand::random(),
3000,
max_tx_fee_ustx,
None,
);

let config =
RawConfigFile::load_from_str(&config_tomls[0]).expect("Failed to parse config file");

assert_eq!(config.max_tx_fee_ustx, max_tx_fee_ustx);
assert!(config.tx_fee_ustx.is_none());

let config = GlobalConfig::try_from(config).expect("Failed to parse config");
assert_eq!(config.max_tx_fee_ustx, max_tx_fee_ustx);
assert_eq!(config.tx_fee_ustx, TX_FEE_USTX);

// Test only tx_fee_ustx is specified
let tx_fee_ustx = Some(1000);
let config_tomls = build_signer_config_tomls(
&[pk],
node_host,
None,
&network,
password,
rand::random(),
3000,
None,
tx_fee_ustx,
);

let config =
RawConfigFile::load_from_str(&config_tomls[0]).expect("Failed to parse config file");

assert!(config.max_tx_fee_ustx.is_none());
assert_eq!(config.tx_fee_ustx, tx_fee_ustx);

let config = GlobalConfig::try_from(config).expect("Failed to parse config");
assert!(config.max_tx_fee_ustx.is_none());
assert_eq!(Some(config.tx_fee_ustx), tx_fee_ustx);
}
}
3 changes: 1 addition & 2 deletions stacks-signer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ fn write_chunk_to_stdout(chunk_opt: Option<Vec<u8>>) {
// Spawn a running signer and return its handle, command sender, and result receiver
fn spawn_running_signer(path: &PathBuf) -> SpawnedSigner {
let config = GlobalConfig::try_from(path).unwrap();
let config_str = format!("{:?}", config);
let endpoint = config.endpoint;
let (cmd_send, cmd_recv) = channel();
let (res_send, res_recv) = channel();
Expand All @@ -93,7 +92,6 @@ fn spawn_running_signer(path: &PathBuf) -> SpawnedSigner {
let mut signer: Signer<RunLoopCommand, Vec<OperationResult>, RunLoop, SignerEventReceiver> =
Signer::new(runloop, ev, cmd_recv, res_send);
let running_signer = signer.spawn(endpoint).unwrap();
println!("Signer successfully configured: {config_str}");
SpawnedSigner {
running_signer,
cmd_send,
Expand Down Expand Up @@ -297,6 +295,7 @@ fn handle_generate_files(args: GenerateFilesArgs) {
rand::random(),
3000,
None,
None,
);
debug!("Built {:?} signer config tomls.", signer_config_tomls.len());
for (i, file_contents) in signer_config_tomls.iter().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions stacks-signer/src/signerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ CREATE TABLE IF NOT EXISTS blocks (
PRIMARY KEY (reward_cycle, signer_signature_hash)
)";

const CREATE_SIGNER_STATE_TABLE: &'static str = "
const CREATE_SIGNER_STATE_TABLE: &str = "
CREATE TABLE IF NOT EXISTS signer_states (
reward_cycle INTEGER PRIMARY KEY,
state TEXT NOT NULL
Expand Down Expand Up @@ -88,7 +88,7 @@ impl SignerDb {
let result: Option<String> = query_row(
&self.db,
"SELECT state FROM signer_states WHERE reward_cycle = ?",
&[u64_to_sql(reward_cycle)?],
[u64_to_sql(reward_cycle)?],
)?;

try_deserialize(result)
Expand Down
1 change: 1 addition & 0 deletions testnet/stacks-node/src/tests/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl SignerTest {
run_stamp,
3000,
Some(100_000),
None,
);

let mut running_signers = Vec::new();
Expand Down

0 comments on commit 110d04e

Please sign in to comment.