Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Remove withdrawals related code from signer #970

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 128 additions & 12 deletions signer/src/testing/transaction_coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;

use crate::bitcoin::rpc::GetTxResponse;
use crate::bitcoin::utxo::SignerUtxo;
use crate::bitcoin::MockBitcoinInteract;
use crate::context::Context;
Expand All @@ -21,15 +22,25 @@ use crate::stacks::api::AccountInfo;
use crate::stacks::api::MockStacksInteract;
use crate::stacks::api::SubmitTxResponse;
use crate::storage::model;
use crate::storage::model::ScriptPubKey;
use crate::storage::model::StacksBlock;
use crate::storage::model::StacksBlockHash;
use crate::storage::model::StacksPrincipal;
use crate::storage::model::StacksTxId;
use crate::storage::model::WithdrawalRequest;
use crate::storage::DbRead;
use crate::storage::DbWrite;
use crate::testing;
use crate::testing::dummy::stacks_block;
use crate::testing::storage::model::TestData;
use crate::testing::wsts::SignerSet;
use crate::transaction_coordinator;
use clarity::types::chainstate::StacksAddress;
use clarity::util::hash::Hash160;
use clarity::vm::types::PrincipalData;

use blockstack_lib::net::api::getcontractsrc::ContractSrcResponse;
use emily_client::models::deposit;
use fake::Fake as _;
use fake::Faker;
use rand::SeedableRng as _;
Expand Down Expand Up @@ -285,38 +296,143 @@ where
assert_eq!(first_script_pubkey, aggregate_key.signers_script_pubkey());
}

/// Assert that a coordinator should be able to skip the deployment the sbtc contracts
/// if they are already deployed.
pub async fn assert_skips_deploy_sbtc_contracts(mut self) {
/// Assert that a coordinator should be able to coordiante a signing round
pub async fn assert_should_ignore_withdrawals(mut self) {
let mut rng = rand::rngs::StdRng::seed_from_u64(46);
let network = network::InMemoryNetwork::new();
let signer_info = testing::wsts::generate_signer_info(&mut rng, self.num_signers as usize);

let mut testing_signer_set =
testing::wsts::SignerSet::new(&signer_info, self.signing_threshold as u32, || {
network.connect()
});

let (aggregate_key, bitcoin_chain_tip, mut test_data) = self
.prepare_database_and_run_dkg(&mut rng, &mut testing_signer_set)
.await;
let storage = self.context.storage.clone();
let block_hash = storage
.get_bitcoin_canonical_chain_tip()
.await
.unwrap()
.unwrap();

println!("old hash: {:?}", block_hash);
let withdrawals = storage
.get_pending_withdrawal_requests(&block_hash, self.context_window)
.await
.unwrap();
let deposits = storage
.get_pending_deposit_requests(&block_hash, self.context_window)
.await
.unwrap();
// Ensure that there are withdrawals to test
assert!(!withdrawals.is_empty());
let original_test_data = test_data.clone();

let tx_1 = bitcoin::Transaction {
output: vec![bitcoin::TxOut {
value: bitcoin::Amount::from_sat(1_337_000_000_000),
script_pubkey: aggregate_key.signers_script_pubkey(),
}],
let tx = bitcoin::Transaction {
output: vec![
bitcoin::TxOut {
value: bitcoin::Amount::from_sat(42),
script_pubkey: aggregate_key.signers_script_pubkey(),
},
bitcoin::TxOut {
value: bitcoin::Amount::from_sat(123),
script_pubkey: bitcoin::ScriptBuf::new(),
},
],
..EMPTY_BITCOIN_TX
};
let (block, block_ref) = test_data.new_block(
&mut rng,
&testing_signer_set.signer_keys(),
&self.test_model_parameters,
Some(&bitcoin_chain_tip),
);
test_data.push(block);
test_data.push_bitcoin_txs(
&bitcoin_chain_tip,
vec![(model::TransactionType::SbtcTransaction, tx_1.clone())],
&block_ref,
vec![(model::TransactionType::SbtcTransaction, tx.clone())],
);
test_data.withdraw_requests.push(WithdrawalRequest {
request_id: 1,
txid: StacksTxId::from([1; 32]),
block_hash: StacksBlockHash::from([1; 32]),
recipient: ScriptPubKey::from_bytes(vec![1; 20]),
amount: 42,
max_fee: 1,
sender_address: StacksPrincipal::from(PrincipalData::from(StacksAddress {
version: 0,
bytes: Hash160::from([1; 20]),
})),
});


let stacks_tip = original_test_data.stacks_blocks.last().unwrap();

test_data.stacks_blocks.push(StacksBlock {
block_hash: StacksBlockHash::from([1; 32]),
parent_hash: stacks_tip.block_hash,
block_height: stacks_tip.block_height + 1,
bitcoin_anchor: block_hash,
});

test_data.remove(original_test_data);
self.write_test_data(&test_data).await;
let block_hash2 = storage
.get_bitcoin_canonical_chain_tip()
.await
.unwrap()
.unwrap();
println!("new hash: {:?}", block_hash2);
let withdrawals2 = self.context.storage
.get_pending_withdrawal_requests(&block_hash2, self.context_window)
.await
.unwrap();
let deposits2 = self.context.storage
.get_pending_deposit_requests(&block_hash2, self.context_window)
.await
.unwrap();

// Check that we actually test something.
let deposits_set = deposits.iter().collect::<std::collections::HashSet<_>>();
let deposits2_set = deposits2.iter().collect::<std::collections::HashSet<_>>();
assert_ne!(deposits, deposits2);
assert!(!deposits_set.is_subset(&deposits2_set));
// Check that the withdrawals are the same
assert_eq!(withdrawals, withdrawals2);
}

/// Assert that a coordinator should be able to skip the deployment the sbtc contracts
/// if they are already deployed.
pub async fn assert_skips_deploy_sbtc_contracts(mut self) {
let mut rng = rand::rngs::StdRng::seed_from_u64(46);
let network = network::InMemoryNetwork::new();
let signer_info = testing::wsts::generate_signer_info(&mut rng, self.num_signers as usize);

let mut testing_signer_set =
testing::wsts::SignerSet::new(&signer_info, self.signing_threshold as u32, || {
network.connect()
});

let (aggregate_key, bitcoin_chain_tip, mut test_data) = self
.prepare_database_and_run_dkg(&mut rng, &mut testing_signer_set)
.await;

let original_test_data = test_data.clone();

let tx_1 = bitcoin::Transaction {
output: vec![bitcoin::TxOut {
value: bitcoin::Amount::from_sat(1_337_000_000_000),
script_pubkey: aggregate_key.signers_script_pubkey(),
}],
..EMPTY_BITCOIN_TX
};
test_data.push_bitcoin_txs(
&bitcoin_chain_tip,
vec![(model::TransactionType::SbtcTransaction, tx_1.clone())],
);

test_data.remove(original_test_data);
self.write_test_data(&test_data).await;

self.context
.with_bitcoin_client(|client| {
Expand Down
5 changes: 5 additions & 0 deletions signer/src/transaction_coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,11 @@ mod tests {
more_asserts::assert_gt!(start.elapsed(), delay + baseline_elapsed);
}

#[tokio::test]
async fn should_ignore_withdrawals() {
test_environment().assert_should_ignore_withdrawals().await;
}

#[tokio::test]
async fn should_get_signer_utxo_simple() {
test_environment().assert_get_signer_utxo_simple().await;
Expand Down
Loading