Skip to content

Commit 953331f

Browse files
authored
use McTxHash in await_tx (#864)
1 parent 42362a9 commit 953331f

File tree

9 files changed

+28
-33
lines changed

9 files changed

+28
-33
lines changed

toolkit/smart-contracts/offchain/src/assemble_and_submit_tx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ogmios_client::query_ledger_state::QueryUtxoByUtxoId;
55
use ogmios_client::{
66
query_ledger_state::QueryLedgerState, query_network::QueryNetwork, transactions::Transactions,
77
};
8-
use sidechain_domain::{McTxHash, UtxoId};
8+
use sidechain_domain::McTxHash;
99

1010
/// Adds `witnesses` to `transaction` and submits it with `ogmios_client`.
1111
pub async fn assemble_and_submit_tx<
@@ -38,7 +38,7 @@ pub async fn assemble_and_submit_tx<
3838
let tx_id = McTxHash(res.transaction.id);
3939
log::info!("Transaction submitted: {}", hex::encode(tx_id.0));
4040

41-
await_tx.await_tx_output(ogmios_client, UtxoId::new(tx_id.0, 0)).await?;
41+
await_tx.await_tx_output(ogmios_client, tx_id).await?;
4242

4343
Ok(tx_id)
4444
}

toolkit/smart-contracts/offchain/src/await_tx.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use anyhow::anyhow;
22
use ogmios_client::query_ledger_state::QueryUtxoByUtxoId;
3-
use sidechain_domain::UtxoId;
3+
use sidechain_domain::{McTxHash, UtxoId};
44
use std::time::Duration;
55
use tokio_retry::{Retry, strategy::FixedInterval};
66

77
/// Trait for different strategies of waiting for a Cardano transaction to complete.
88
pub trait AwaitTx {
99
#[allow(async_fn_in_trait)]
1010
/// This is used for waiting until the output of a submitted transaction can be observed.
11-
/// TODO: make this take a Transaction ID instead of a UtxoId
1211
async fn await_tx_output<C: QueryUtxoByUtxoId>(
1312
&self,
1413
client: &C,
15-
utxo_id: UtxoId,
14+
tx_hash: McTxHash,
1615
) -> anyhow::Result<()>;
1716
}
1817

@@ -38,9 +37,10 @@ impl AwaitTx for FixedDelayRetries {
3837
async fn await_tx_output<C: QueryUtxoByUtxoId>(
3938
&self,
4039
client: &C,
41-
utxo_id: UtxoId,
40+
tx_hash: McTxHash,
4241
) -> anyhow::Result<()> {
4342
let strategy = FixedInterval::new(self.delay).take(self.retries);
43+
let utxo_id = UtxoId::new(tx_hash.0, 0);
4444
let _ = Retry::spawn(strategy, || async {
4545
log::info!("Probing for transaction output '{}'", utxo_id);
4646
let utxo = client.query_utxo_by_id(utxo_id).await.map_err(|_| ())?;
@@ -69,7 +69,7 @@ pub(crate) mod mock {
6969
async fn await_tx_output<Q: QueryUtxoByUtxoId>(
7070
&self,
7171
_query: &Q,
72-
_utxo_id: sidechain_domain::UtxoId,
72+
_utxo_id: sidechain_domain::McTxHash,
7373
) -> anyhow::Result<()> {
7474
Ok(())
7575
}
@@ -84,15 +84,15 @@ mod tests {
8484
query_ledger_state::QueryUtxoByUtxoId,
8585
types::{OgmiosTx, OgmiosUtxo},
8686
};
87-
use sidechain_domain::{McTxHash, UtxoId, UtxoIndex};
87+
use sidechain_domain::McTxHash;
8888
use std::{cell::RefCell, time::Duration};
8989

9090
#[tokio::test]
9191
async fn immediate_success() {
9292
let mock =
9393
MockQueryUtxoByUtxoId { responses: RefCell::new(vec![Ok(Some(awaited_utxo()))]) };
9494
FixedDelayRetries::new(Duration::from_millis(1), 3)
95-
.await_tx_output(&mock, awaited_utxo_id())
95+
.await_tx_output(&mock, awaited_tx_hash())
9696
.await
9797
.unwrap();
9898
}
@@ -103,7 +103,7 @@ mod tests {
103103
responses: RefCell::new(vec![Ok(None), Ok(Some(awaited_utxo()))]),
104104
};
105105
FixedDelayRetries::new(Duration::from_millis(1), 3)
106-
.await_tx_output(&mock, awaited_utxo_id())
106+
.await_tx_output(&mock, awaited_tx_hash())
107107
.await
108108
.unwrap();
109109
}
@@ -113,7 +113,7 @@ mod tests {
113113
let mock =
114114
MockQueryUtxoByUtxoId { responses: RefCell::new(vec![Ok(None), Ok(None), Ok(None)]) };
115115
let result = FixedDelayRetries::new(Duration::from_millis(1), 2)
116-
.await_tx_output(&mock, awaited_utxo_id())
116+
.await_tx_output(&mock, awaited_tx_hash())
117117
.await;
118118
assert!(result.is_err())
119119
}
@@ -128,7 +128,7 @@ mod tests {
128128
]),
129129
};
130130
let result = FixedDelayRetries::new(Duration::from_millis(1), 2)
131-
.await_tx_output(&mock, awaited_utxo_id())
131+
.await_tx_output(&mock, awaited_tx_hash())
132132
.await;
133133
assert!(result.is_err())
134134
}
@@ -142,16 +142,16 @@ mod tests {
142142
&self,
143143
utxo: sidechain_domain::UtxoId,
144144
) -> Result<Option<OgmiosUtxo>, OgmiosClientError> {
145-
if utxo == awaited_utxo_id() {
145+
if utxo.tx_hash == awaited_tx_hash() {
146146
self.responses.borrow_mut().pop().unwrap()
147147
} else {
148148
Ok(None)
149149
}
150150
}
151151
}
152152

153-
fn awaited_utxo_id() -> UtxoId {
154-
UtxoId { tx_hash: McTxHash([7u8; 32]), index: UtxoIndex(1) }
153+
fn awaited_tx_hash() -> McTxHash {
154+
McTxHash([7u8; 32])
155155
}
156156

157157
fn awaited_utxo() -> OgmiosUtxo {

toolkit/smart-contracts/offchain/src/d_param/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub async fn upsert_d_param<
8787
},
8888
};
8989
if let Some(TransactionSubmitted(tx_hash)) = tx_hash_opt {
90-
await_tx.await_tx_output(client, UtxoId::new(tx_hash.0, 0)).await?;
90+
await_tx.await_tx_output(client, tx_hash).await?;
9191
}
9292
Ok(tx_hash_opt)
9393
}

toolkit/smart-contracts/offchain/src/governed_map/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub async fn run_insert<
7474
},
7575
};
7676
if let Some(TransactionSubmitted(tx_hash)) = tx_hash_opt {
77-
await_tx.await_tx_output(ogmios_client, UtxoId::new(tx_hash.0, 0)).await?;
77+
await_tx.await_tx_output(ogmios_client, tx_hash).await?;
7878
}
7979
Ok(tx_hash_opt)
8080
}
@@ -205,7 +205,7 @@ pub async fn run_update<
205205
};
206206

207207
if let Some(TransactionSubmitted(tx_hash)) = tx_hash_opt {
208-
await_tx.await_tx_output(ogmios_client, UtxoId::new(tx_hash.0, 0)).await?;
208+
await_tx.await_tx_output(ogmios_client, tx_hash).await?;
209209
}
210210
Ok(tx_hash_opt)
211211
}
@@ -338,7 +338,7 @@ pub async fn run_remove<
338338
),
339339
};
340340
if let Some(TransactionSubmitted(tx_hash)) = tx_hash_opt {
341-
await_tx.await_tx_output(ogmios_client, UtxoId::new(tx_hash.0, 0)).await?;
341+
await_tx.await_tx_output(ogmios_client, tx_hash).await?;
342342
}
343343
Ok(tx_hash_opt)
344344
}
@@ -501,7 +501,7 @@ pub async fn run_insert_with_force<
501501
);
502502

503503
if let Some(TransactionSubmitted(tx_hash)) = tx_hash_opt {
504-
await_tx.await_tx_output(ogmios_client, UtxoId::new(tx_hash.0, 0)).await?;
504+
await_tx.await_tx_output(ogmios_client, tx_hash).await?;
505505
}
506506
Ok(tx_hash_opt)
507507
}

toolkit/smart-contracts/offchain/src/init_governance/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@ pub async fn run_init_governance<
7676
let result = client.submit_transaction(&signed_transaction.to_bytes()).await?;
7777
let tx_id = result.transaction.id;
7878
log::info!("✅ Transaction submitted. ID: {}", hex::encode(tx_id));
79-
await_tx.await_tx_output(client, UtxoId::new(tx_id, 0)).await?;
79+
await_tx.await_tx_output(client, McTxHash(tx_id)).await?;
8080
Ok(InitGovernanceResult { tx_hash: McTxHash(tx_id), genesis_utxo: genesis_utxo.utxo_id() })
8181
}

toolkit/smart-contracts/offchain/src/multisig.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ogmios_client::{
1717
transactions::Transactions,
1818
};
1919
use serde::{Serialize, Serializer};
20-
use sidechain_domain::{McTxHash, UtxoId, UtxoIndex, crypto::blake2b};
20+
use sidechain_domain::{McTxHash, crypto::blake2b};
2121

2222
/// Successful smart contracts offchain results in either transaction submission or creating transaction that has to be signed by the governance authorities
2323
#[derive(Clone, Debug, Serialize)]
@@ -173,7 +173,7 @@ async fn transfer_to_temporary_wallet<T: Transactions + QueryUtxoByUtxoId, A: Aw
173173
&hex::encode(tx_hash)
174174
);
175175
client.submit_transaction(&payment_ctx.sign(&funding_tx).to_bytes()).await?;
176-
await_tx.await_tx_output(client, UtxoId::new(tx_hash, 0)).await?;
176+
await_tx.await_tx_output(client, McTxHash(tx_hash)).await?;
177177
Ok(())
178178
}
179179

@@ -232,12 +232,7 @@ where
232232
})?;
233233
let tx_id = McTxHash(res.transaction.id);
234234
log::info!("'{}' transaction submitted: {}", tx_name, hex::encode(tx_id.0));
235-
await_tx
236-
.await_tx_output(
237-
client,
238-
UtxoId { tx_hash: McTxHash(res.transaction.id), index: UtxoIndex(0) },
239-
)
240-
.await?;
235+
await_tx.await_tx_output(client, McTxHash(res.transaction.id)).await?;
241236
Ok(tx_id)
242237
}
243238

toolkit/smart-contracts/offchain/src/register.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub async fn run_register<
8383
})?;
8484
let tx_id = result.transaction.id;
8585
log::info!("✅ Transaction submitted. ID: {}", hex::encode(result.transaction.id));
86-
await_tx.await_tx_output(client, UtxoId::new(tx_id, 0)).await?;
86+
await_tx.await_tx_output(client, McTxHash(tx_id)).await?;
8787

8888
Ok(Some(McTxHash(result.transaction.id)))
8989
}
@@ -136,7 +136,7 @@ pub async fn run_deregister<
136136
})?;
137137
let tx_id = result.transaction.id;
138138
log::info!("✅ Transaction submitted. ID: {}", hex::encode(result.transaction.id));
139-
await_tx.await_tx_output(client, UtxoId::new(tx_id, 0)).await?;
139+
await_tx.await_tx_output(client, McTxHash(tx_id)).await?;
140140

141141
Ok(Some(McTxHash(result.transaction.id)))
142142
}

toolkit/smart-contracts/offchain/src/reserve/release.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub async fn release_reserve_funds<
8686
})?;
8787
let tx_id = res.transaction.id;
8888
log::info!("Reserve release transaction submitted: {}", hex::encode(tx_id));
89-
await_tx.await_tx_output(client, UtxoId::new(tx_id, 0)).await?;
89+
await_tx.await_tx_output(client, McTxHash(tx_id)).await?;
9090

9191
Ok(McTxHash(tx_id))
9292
}

toolkit/smart-contracts/offchain/tests/integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ async fn initial_transaction<T: Transactions + QueryUtxoByUtxoId>(
443443
.map_err(|e| e.to_string())
444444
.map(|response| McTxHash(response.transaction.id))?;
445445
FixedDelayRetries::new(Duration::from_millis(500), 100)
446-
.await_tx_output(client, UtxoId::new(tx_hash.0, 0))
446+
.await_tx_output(client, tx_hash)
447447
.await
448448
.map_err(|e| e.to_string())?;
449449
Ok(tx_hash)

0 commit comments

Comments
 (0)