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

Store deposit requests txs when extracted #628

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
52 changes: 45 additions & 7 deletions signer/src/block_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,43 @@ where
self.write_stacks_blocks(&stacks_blocks).await?;
self.write_bitcoin_block(&block).await?;

self.extract_deposit_requests(&block.txdata).await?;
self.extract_deposit_requests(&block.txdata, block.block_hash())
.await?;

Ok(())
}

async fn extract_deposit_requests(&mut self, txs: &[Transaction]) -> Result<(), Error> {
let deposit_request: Vec<model::DepositRequest> = txs
async fn extract_deposit_requests(
&mut self,
txs: &[Transaction],
block_hash: BlockHash,
) -> Result<(), Error> {
let (deposit_request, deposit_request_txs) = txs
.iter()
.filter_map(|tx| self.deposit_requests.remove(&tx.compute_txid()))
.flatten()
.map(model::DepositRequest::from)
.collect();
.map(|deposit| {
let mut tx_bytes = Vec::new();
deposit.tx.consensus_encode(&mut tx_bytes)?;

let tx = model::Transaction {
txid: deposit.tx.compute_txid().to_byte_array(),
tx: tx_bytes,
tx_type: model::TransactionType::DepositRequest,
block_hash: block_hash.to_byte_array(),
};

Ok((model::DepositRequest::from(deposit), tx))
})
.collect::<Result<Vec<_>, _>>()
.map_err(Error::BitcoinEncodeTransaction)?
.into_iter()
.unzip();
matteojug marked this conversation as resolved.
Show resolved Hide resolved

self.context
.get_storage_mut()
.write_bitcoin_transactions(deposit_request_txs)
.await?;
self.context
.get_storage_mut()
.write_deposit_requests(deposit_request)
Expand Down Expand Up @@ -535,6 +559,7 @@ mod tests {
let mut rng = rand::rngs::StdRng::seed_from_u64(365);
let mut test_harness = TestHarness::generate(&mut rng, 20, 0..5);

let block_hash = BlockHash::from_byte_array([1u8; 32]);
let lock_time = 150;
let max_fee = 32000;
let amount = 500_000;
Expand All @@ -558,7 +583,7 @@ mod tests {
// response.
let get_tx_resp0 = GetTxResponse {
tx: tx_setup0.tx.clone(),
block_hash: None,
block_hash: Some(block_hash.clone()),
confirmations: None,
block_time: None,
};
Expand Down Expand Up @@ -598,14 +623,27 @@ mod tests {
assert_eq!(block_observer.deposit_requests.len(), 1);

block_observer
.extract_deposit_requests(&[tx_setup0.tx.clone()])
.extract_deposit_requests(&[tx_setup0.tx.clone()], block_hash)
.await
.unwrap();
let storage = storage.lock().await;
assert_eq!(storage.deposit_requests.len(), 1);
let db_outpoint: (BitcoinTxId, u32) = (tx_setup0.tx.compute_txid().into(), 0);
assert!(storage.deposit_requests.get(&db_outpoint).is_some());

assert!(storage
.bitcoin_transactions_to_blocks
.get(&db_outpoint.0)
.is_some());
assert_eq!(
storage
.raw_transactions
.get(db_outpoint.0.as_byte_array())
.unwrap()
.tx_type,
model::TransactionType::DepositRequest
);

// Now the deposit_requests thing should be empty now, since we stored the things.
assert!(block_observer.deposit_requests.is_empty());
}
Expand Down
2 changes: 2 additions & 0 deletions signer/src/storage/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ impl super::DbWrite for SharedStore {

async fn write_bitcoin_transactions(&self, txs: Vec<model::Transaction>) -> Result<(), Error> {
for tx in txs {
self.write_transaction(&tx).await?;
let bitcoin_transaction = model::BitcoinTxRef {
txid: tx.txid.into(),
block_hash: tx.block_hash.into(),
Expand Down Expand Up @@ -757,6 +758,7 @@ impl super::DbWrite for SharedStore {
stacks_transactions: Vec<model::Transaction>,
) -> Result<(), Error> {
for tx in stacks_transactions {
self.write_transaction(&tx).await?;
let stacks_transaction = model::StacksTransaction {
txid: tx.txid.into(),
block_hash: tx.block_hash.into(),
Expand Down
Loading