From 67d82440d68295322dfedbb15244127c4f75f3a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20S=C3=A1nchez=20Terraf?= Date: Tue, 26 Nov 2024 15:15:51 -0300 Subject: [PATCH 1/3] Get UTxO by TxIns --- .../localstate/queries_v16/codec.rs | 6 ++-- .../localstate/queries_v16/mod.rs | 32 ++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/pallas-network/src/miniprotocols/localstate/queries_v16/codec.rs b/pallas-network/src/miniprotocols/localstate/queries_v16/codec.rs index d06763f1..be30f3a5 100644 --- a/pallas-network/src/miniprotocols/localstate/queries_v16/codec.rs +++ b/pallas-network/src/miniprotocols/localstate/queries_v16/codec.rs @@ -71,10 +71,10 @@ impl Encode<()> for BlockQuery { e.array(1)?; e.u16(14)?; } - BlockQuery::GetUTxOByTxIn(_) => { + BlockQuery::GetUTxOByTxIn(txin) => { e.array(2)?; e.u16(15)?; - e.encode(2)?; + e.encode(txin)?; } BlockQuery::GetStakePools => { e.array(1)?; @@ -144,7 +144,7 @@ impl<'b> Decode<'b, ()> for BlockQuery { // 12 => Ok(Self::DebugNewEpochState), 13 => Ok(Self::DebugChainDepState), 14 => Ok(Self::GetRewardProvenance), - // 15 => Ok(Self::GetUTxOByTxIn(())), + 15 => Ok(Self::GetUTxOByTxIn(d.decode()?)), 16 => Ok(Self::GetStakePools), // 17 => Ok(Self::GetStakePoolParams(())), 18 => Ok(Self::GetRewardInfoPools), diff --git a/pallas-network/src/miniprotocols/localstate/queries_v16/mod.rs b/pallas-network/src/miniprotocols/localstate/queries_v16/mod.rs index 5e4e8646..409099bd 100644 --- a/pallas-network/src/miniprotocols/localstate/queries_v16/mod.rs +++ b/pallas-network/src/miniprotocols/localstate/queries_v16/mod.rs @@ -37,7 +37,7 @@ pub enum BlockQuery { DebugNewEpochState, DebugChainDepState, GetRewardProvenance, - GetUTxOByTxIn(AnyCbor), + GetUTxOByTxIn(TxIns), GetStakePools, GetStakePoolParams(AnyCbor), GetRewardInfoPools, @@ -236,9 +236,25 @@ pub struct UTxOByAddress { pub utxo: KeyValuePairs, } +pub type UTxOByTxin = UTxOByAddress; + // Bytes CDDL -> #6.121([ * #6.121([ *datum ]) ]) pub type Datum = (Era, TagWrap); +// From `pallas-primitives`, with fewer `derive`s +#[derive( + Encode, Decode, Debug, PartialEq, Eq, PartialOrd, Ord, Clone, +)] +pub struct TransactionInput { + #[n(0)] + pub transaction_id: Hash<32>, + + #[n(1)] + pub index: u64, +} + +pub type TxIns = BTreeSet; + #[derive(Debug, PartialEq, Eq, Clone)] pub enum TransactionOutput { Current(PostAlonsoTransactionOutput), @@ -477,3 +493,17 @@ pub async fn get_genesis_config( Ok(result) } + +/// Get a subset of the UTxO, filtered by transaction input. +pub async fn get_utxo_by_txin( + client: &mut Client, + era: u16, + txins: TxIns, +) -> Result { + let query = BlockQuery::GetUTxOByTxIn(txins); + let query = LedgerQuery::BlockQuery(era, query); + let query = Request::LedgerQuery(query); + let result = client.query(query).await?; + + Ok(result) +} From 851f8d7363f7d8b4e8e86f9d0bf54e4eae0f6b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20S=C3=A1nchez=20Terraf?= Date: Tue, 26 Nov 2024 15:31:29 -0300 Subject: [PATCH 2/3] GetUTxOByTxIn at examples --- examples/n2c-miniprotocols/src/main.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/n2c-miniprotocols/src/main.rs b/examples/n2c-miniprotocols/src/main.rs index de784c37..f3cbb7c3 100644 --- a/examples/n2c-miniprotocols/src/main.rs +++ b/examples/n2c-miniprotocols/src/main.rs @@ -7,18 +7,33 @@ use pallas::{ facades::NodeClient, miniprotocols::{ chainsync, - localstate::queries_v16::{self, Addr, Addrs}, + localstate::queries_v16::{ + self, Addr, Addrs, TransactionInput, + }, Point, PRE_PRODUCTION_MAGIC, }, }, + crypto::hash::Hash, }; use tracing::info; +use hex::FromHex; async fn do_localstate_query(client: &mut NodeClient) { let client = client.statequery(); client.acquire(None).await.unwrap(); + // Get UTxO from a (singleton) set of tx inputs. + let transaction_id = Hash::<32>::from(<[u8; 32]>::from_hex( + "15244950ed56a3af61a00f62584779fb53a9f3910468013a2b00b94b8bbc10e0" + ).unwrap()); + let tx_in = TransactionInput { transaction_id, index: 0 }; + let mut txins = BTreeSet::new(); + txins.insert(tx_in); + + let result = queries_v16::get_utxo_by_txin(client, 6, txins).await.unwrap(); + info!("result: {:?}", result); + let result = queries_v16::get_chain_point(client).await.unwrap(); info!("result: {:?}", result); From fa837fb5a164e22e105b40fc03f0f7ccc9d99d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20S=C3=A1nchez=20Terraf?= Date: Tue, 26 Nov 2024 15:47:23 -0300 Subject: [PATCH 3/3] Unwrapping a try_join to expose errors --- pallas-network/tests/protocols.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pallas-network/tests/protocols.rs b/pallas-network/tests/protocols.rs index fe8b2650..9c807e36 100644 --- a/pallas-network/tests/protocols.rs +++ b/pallas-network/tests/protocols.rs @@ -260,7 +260,7 @@ pub async fn blockfetch_server_and_client_happy_path() { client_bf.send_done().await.unwrap(); }); - _ = tokio::join!(client, server); + tokio::try_join!(client, server).unwrap(); } #[tokio::test] @@ -457,7 +457,7 @@ pub async fn chainsync_server_and_client_happy_path_n2n() { client_cs.send_done().await.unwrap(); }); - _ = tokio::join!(client, server); + tokio::try_join!(client, server).unwrap(); } #[cfg(unix)] @@ -1092,7 +1092,7 @@ pub async fn local_state_query_server_and_client_happy_path() { client.statequery().send_done().await.unwrap(); }); - _ = tokio::join!(client, server); + tokio::try_join!(client, server).unwrap(); } #[tokio::test] @@ -1250,7 +1250,7 @@ pub async fn txsubmission_server_and_client_happy_path_n2n() { assert_eq!(*client_txsub.state(), txsubmission::State::Done); }); - _ = tokio::join!(client, server); + tokio::try_join!(client, server).unwrap(); } #[tokio::test]