feat: test that aggregate key was properly set

This commit is contained in:
Hank Stoever
2024-02-28 10:29:35 -08:00
parent ea58fc72b3
commit 460d80ba4e
3 changed files with 44 additions and 16 deletions

View File

@@ -483,11 +483,12 @@ pub fn boot_to_epoch_3(
info!("Bootstrapped to Epoch-3.0 boundary, Epoch2x miner should stop");
}
fn is_key_set_for_cycle(
/// Use the read-only API to get the aggregate key for a given reward cycle
pub fn get_key_for_cycle(
reward_cycle: u64,
is_mainnet: bool,
http_origin: &str,
) -> Result<bool, String> {
) -> Result<Option<Vec<u8>>, String> {
let client = reqwest::blocking::Client::new();
let boot_address = StacksAddress::burn_address(is_mainnet);
let path = format!("http://{http_origin}/v2/contracts/call-read/{boot_address}/signers-voting/get-approved-aggregate-key");
@@ -513,10 +514,29 @@ fn is_key_set_for_cycle(
)
.map_err(|_| "Failed to deserialize Clarity value")?;
result_value
let buff_opt = result_value
.expect_optional()
.map(|v| v.is_some())
.map_err(|_| "Response is not optional".to_string())
.expect("Expected optional type");
match buff_opt {
Some(buff_val) => {
let buff = buff_val
.expect_buff(33)
.map_err(|_| "Failed to get buffer value")?;
Ok(Some(buff))
}
None => Ok(None),
}
}
/// Use the read-only to check if the aggregate key is set for a given reward cycle
pub fn is_key_set_for_cycle(
reward_cycle: u64,
is_mainnet: bool,
http_origin: &str,
) -> Result<bool, String> {
let key = get_key_for_cycle(reward_cycle, is_mainnet, &http_origin)?;
Ok(key.is_some())
}
fn signer_vote_if_needed(
@@ -1980,7 +2000,7 @@ fn vote_for_aggregate_key_burn_op() {
info!("Submitted vote for aggregate key op at height {block_height}, mining a few blocks...");
// the second block should process the vote, after which the balaces should be unchanged
// the second block should process the vote, after which the vote should be set
for _i in 0..2 {
next_block_and_mine_commit(
&mut btc_regtest_controller,
@@ -2021,6 +2041,13 @@ fn vote_for_aggregate_key_burn_op() {
"Expected vote for aggregate key op"
);
// Check that the correct key was set
let saved_key = get_key_for_cycle(reward_cycle, false, &naka_conf.node.rpc_bind)
.expect("Expected to be able to check key is set after voting")
.expect("Expected aggregate key to be set");
assert_eq!(saved_key, aggregate_key.as_bytes().to_vec());
coord_channel
.lock()
.expect("Mutex poisoned")

View File

@@ -85,6 +85,7 @@ use crate::neon_node::RelayerThread;
use crate::operations::BurnchainOpSigner;
use crate::stacks_common::types::PrivateKey;
use crate::syncctl::PoxSyncWatchdogComms;
use crate::tests::nakamoto_integrations::get_key_for_cycle;
use crate::util::hash::{MerkleTree, Sha512Trunc256Sum};
use crate::util::secp256k1::MessageSignature;
use crate::{neon, BitcoinRegtestController, BurnchainController, Config, ConfigFile, Keychain};
@@ -2245,8 +2246,6 @@ fn vote_for_aggregate_key_burn_op_test() {
let spender_stx_addr: StacksAddress = to_addr(&spender_sk);
let spender_addr: PrincipalData = spender_stx_addr.clone().into();
let recipient_sk = StacksPrivateKey::new();
let recipient_addr = to_addr(&recipient_sk);
let pox_pubkey = Secp256k1PublicKey::from_hex(
"02f006a09b59979e2cb8449f58076152af6b124aa29b948a3714b8d5f15aa94ede",
)
@@ -2260,17 +2259,12 @@ fn vote_for_aggregate_key_burn_op_test() {
let (mut conf, _miner_account) = neon_integration_test_conf();
let first_bal = 6_000_000_000 * (core::MICROSTACKS_PER_STACKS as u64);
let second_bal = 2_000_000_000 * (core::MICROSTACKS_PER_STACKS as u64);
let stacked_bal = 1_000_000_000 * (core::MICROSTACKS_PER_STACKS as u128);
conf.initial_balances.push(InitialBalance {
address: spender_addr.clone(),
amount: first_bal,
});
conf.initial_balances.push(InitialBalance {
address: recipient_addr.clone().into(),
amount: second_bal,
});
// update epoch info so that Epoch 2.1 takes effect
conf.burnchain.epochs = Some(vec![
@@ -2395,7 +2389,7 @@ fn vote_for_aggregate_key_burn_op_test() {
let pox_addr = PoxAddress::Standard(spender_stx_addr, Some(AddressHashMode::SerializeP2PKH));
let mut block_height = channel.get_sortitions_processed();
let mut block_height = btc_regtest_controller.get_headers_height();
let reward_cycle = burnchain_config
.block_height_to_reward_cycle(block_height)
@@ -2460,7 +2454,7 @@ fn vote_for_aggregate_key_burn_op_test() {
// Wait a few blocks to be registered
for _i in 0..5 {
next_block_and_wait(&mut btc_regtest_controller, &blocks_processed);
block_height = channel.get_sortitions_processed();
block_height = btc_regtest_controller.get_headers_height();
}
let reward_cycle = burnchain_config
@@ -2547,6 +2541,13 @@ fn vote_for_aggregate_key_burn_op_test() {
"Expected vote for aggregate key op"
);
// Check that the correct key was set
let saved_key = get_key_for_cycle(reward_cycle, false, &conf.node.rpc_bind)
.expect("Expected to be able to check key is set after voting")
.expect("Expected aggregate key to be set");
assert_eq!(saved_key, aggregate_key.as_bytes().to_vec());
test_observer::clear();
channel.stop_chains_coordinator();
}