Skip to content

Commit

Permalink
add concatenate_blocks to TestData Params. fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiloc committed Jan 27, 2025
1 parent 30a8e49 commit f8df970
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 37 deletions.
5 changes: 5 additions & 0 deletions signer/src/api/new_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ mod tests {
num_deposit_requests_per_block: 1,
num_withdraw_requests_per_block: 1,
num_signers_per_request: 0,
concatenate_blocks: true,
};
let db = ctx.inner_storage();
let test_data = TestData::generate(&mut rng, &[], &test_params);
Expand Down Expand Up @@ -739,6 +740,7 @@ mod tests {
num_deposit_requests_per_block: 1,
num_withdraw_requests_per_block: 1,
num_signers_per_request: 0,
concatenate_blocks: true,
};
let db = ctx.inner_storage();
let test_data = TestData::generate(&mut rng, &[], &test_params);
Expand Down Expand Up @@ -787,6 +789,7 @@ mod tests {
num_deposit_requests_per_block: 2,
num_withdraw_requests_per_block: 2,
num_signers_per_request: 0,
concatenate_blocks: true,
};

let db = ctx.inner_storage();
Expand Down Expand Up @@ -859,6 +862,7 @@ mod tests {
num_deposit_requests_per_block: 2,
num_withdraw_requests_per_block: 2,
num_signers_per_request: 0,
concatenate_blocks: true,
};

let db = ctx.inner_storage();
Expand Down Expand Up @@ -920,6 +924,7 @@ mod tests {
num_deposit_requests_per_block: 2,
num_withdraw_requests_per_block: 2,
num_signers_per_request: 0,
concatenate_blocks: true,
};

let test_data = TestData::generate(&mut rng, &[], &test_params);
Expand Down
1 change: 1 addition & 0 deletions signer/src/request_decider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ mod tests {
num_deposit_requests_per_block: 5,
num_withdraw_requests_per_block: 5,
num_signers_per_request: 0,
concatenate_blocks: false,
};

let context = TestContext::builder()
Expand Down
1 change: 1 addition & 0 deletions signer/src/stacks/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ mod tests {
num_deposit_requests_per_block: 0,
num_withdraw_requests_per_block: 0,
num_signers_per_request: 0,
concatenate_blocks: true,
};
let test_data = TestData::generate(&mut rng, &[], &test_params);
test_data.write_to(&db).await;
Expand Down
45 changes: 17 additions & 28 deletions signer/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,9 @@ impl PgStore {
}
}

#[cfg(test)]
impl PgStore {}

impl From<sqlx::PgPool> for PgStore {
fn from(value: sqlx::PgPool) -> Self {
Self(value)
Expand Down Expand Up @@ -1930,36 +1933,22 @@ impl super::DbRead for PgStore {
) -> Result<Vec<model::DepositSigner>, Error> {
sqlx::query_as::<_, model::DepositSigner>(
r#"
WITH RECURSIVE context_window AS (
-- Anchor member: Initialize the recursion with the chain tip
SELECT block_hash, block_height, parent_hash, created_at, 1 AS depth
FROM sbtc_signer.bitcoin_blocks
WHERE block_hash = $1
UNION ALL
-- Recursive member: Fetch the parent block using the last block's parent_hash
SELECT parent.block_hash, parent.block_height, parent.parent_hash,
parent.created_at, last.depth + 1
FROM sbtc_signer.bitcoin_blocks parent
JOIN context_window last ON parent.block_hash = last.parent_hash
WHERE last.depth < $2
),
transactions_in_window AS (
SELECT transactions.txid
FROM context_window blocks_in_window
JOIN sbtc_signer.bitcoin_transactions transactions ON
transactions.block_hash = blocks_in_window.block_hash
WITH target_block AS (
SELECT blocks.block_hash, blocks.created_at
FROM sbtc_signer.bitcoin_blockchain_of($1, $2) chain
JOIN sbtc_signer.bitcoin_blocks blocks USING (block_hash)
ORDER BY chain.block_height ASC
LIMIT 1
)
SELECT
deposit_signers.txid
, deposit_signers.output_index
, deposit_signers.signer_pub_key
, deposit_signers.can_sign
, deposit_signers.can_accept
FROM transactions_in_window transactions
JOIN sbtc_signer.deposit_signers USING (txid)
WHERE deposit_signers.signer_pub_key = $3
ds.txid,
ds.output_index,
ds.signer_pub_key,
ds.can_sign,
ds.can_accept
FROM sbtc_signer.deposit_signers ds
WHERE ds.signer_pub_key = $3
AND ds.created_at >= (SELECT created_at FROM target_block)
"#,
)
.bind(chain_tip)
Expand Down
15 changes: 13 additions & 2 deletions signer/src/testing/storage/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct TestData {

/// transaction outputs
pub tx_outputs: Vec<model::TxOutput>,

bitcoin_blocks_ref: Vec<BitcoinBlockRef>,
}

impl TestData {
Expand All @@ -66,8 +68,14 @@ impl TestData {
let mut test_data = Self::new();

for _ in 0..params.num_bitcoin_blocks {
let (next_chunk, _) = test_data.new_block(rng, signer_keys, params, None);
let parent = if params.concatenate_blocks {
test_data.bitcoin_blocks_ref.last()
} else {
None
};
let (next_chunk, block_ref) = test_data.new_block(rng, signer_keys, params, parent);
test_data.push(next_chunk);
test_data.bitcoin_blocks_ref.push(block_ref);
}

test_data
Expand Down Expand Up @@ -114,7 +122,7 @@ impl TestData {
.collect();

let bitcoin_blocks = vec![block.clone()];

let bitcoin_blocks_refs = vec![BitcoinBlockRef::summarize(&block)];
(
Self {
bitcoin_blocks,
Expand All @@ -127,6 +135,7 @@ impl TestData {
stacks_transactions: withdraw_data.stacks_transactions,
transactions,
tx_outputs: Vec::new(),
bitcoin_blocks_ref: bitcoin_blocks_refs,
},
block.into(),
)
Expand Down Expand Up @@ -515,6 +524,8 @@ pub struct Params {
pub num_withdraw_requests_per_block: usize,
/// The number of signers to hallucinate per request
pub num_signers_per_request: usize,
/// Wheter to generate consecutive blocks or not
pub concatenate_blocks: bool,
}

impl BitcoinBlockRef {
Expand Down
1 change: 1 addition & 0 deletions signer/src/transaction_coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,7 @@ mod tests {
num_deposit_requests_per_block: 5,
num_withdraw_requests_per_block: 5,
num_signers_per_request: 7,
concatenate_blocks: false,
};

let context = TestContext::builder()
Expand Down
1 change: 1 addition & 0 deletions signer/src/transaction_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ mod tests {
num_deposit_requests_per_block: 5,
num_withdraw_requests_per_block: 5,
num_signers_per_request: 0,
concatenate_blocks: true,
};

let context = TestContext::builder()
Expand Down
1 change: 1 addition & 0 deletions signer/tests/integration/emily.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ where
num_deposit_requests_per_block: 0,
num_withdraw_requests_per_block: 0,
num_signers_per_request: 0,
concatenate_blocks: true,
};
let test_data = TestData::generate(rng, &signer_keys, &test_model_parameters);
test_data.write_to(&storage).await;
Expand Down
Loading

0 comments on commit f8df970

Please sign in to comment.