Repository: dusk-network/rusk (language: rust)

Issue:
node-data: Ease the access to specific data fields
#### Summary

To be able to access data from higher levels more easily, instead of going deep into the nested structs, we could add getters. This makes it easy to get the information that modules want to access and provide to the outside world about historical/executed transactions.

However, some getters would be ambiguous, for example the sender, recipient for a spent transaction or the nonce. This would create additional branching to check if the return type is `Some` or `None`.

#### Possible solution design or implementation

Implement these public getter functions for `node_data::ledger::SpentTransaction` or another way to achieve easier access to the data.



Discussion / hints:

Fix patch:
diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md
index 3b11f8d94f..74bd0dc51b 100644
--- a/core/CHANGELOG.md
+++ b/core/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Changed
+
+- Change `dusk_core::transfer::moonlight::Transaction::data` fn visibility to public
+
 ### Added
 
 - Add `METADATA::PUBLIC_SENDER` [#3341]
diff --git a/core/src/transfer.rs b/core/src/transfer.rs
index 4b2d8f8914..c88e6251be 100644
--- a/core/src/transfer.rs
+++ b/core/src/transfer.rs
@@ -151,8 +151,16 @@ impl Transaction {
         }
     }
 
-    /// Return the receiver of the transaction for Moonlight transactions, if it
+    /// Get the receiver of the transaction for Moonlight transactions, if it
     /// exists.
+    ///
+    /// # Returns
+    ///
+    /// - `Some(&AccountPublicKey)` if the transaction is a Moonlight
+    ///   transaction and the receiver is different from the sender.
+    /// - `None` if the transaction is a Moonlight transaction and the receiver
+    ///   is the same as the sender.
+    /// - `None` if the transaction is a Phoenix transaction.
     #[must_use]
     pub fn moonlight_receiver(&self) -> Option<&AccountPublicKey> {
         match self {
@@ -246,6 +254,15 @@ impl Transaction {
     }
 
     /// Return the contract call data, if there is any.
+    ///
+    /// Call data is present only when inter-contract calls happen.
+    ///
+    /// # Returns
+    ///
+    /// - `Some(&ContractCall)` if the transaction invokes another call to a
+    ///   contract.
+    /// - `None` if the transaction is an entrypoint call to a protocol contract
+    ///   without a second call attached to it.
     #[must_use]
     pub fn call(&self) -> Option<&ContractCall> {
         match self {
diff --git a/core/src/transfer/moonlight.rs b/core/src/transfer/moonlight.rs
index 2ad88c38da..cb86f830be 100644
--- a/core/src/transfer/moonlight.rs
+++ b/core/src/transfer/moonlight.rs
@@ -163,7 +163,7 @@ impl Transaction {
     }
 
     /// Return the receiver of the transaction if it's different from the
-    /// sender.
+    /// sender. Otherwise, return None.
     #[must_use]
     pub fn receiver(&self) -> Option<&AccountPublicKey> {
         if self.payload.sender == self.payload.receiver {
@@ -240,7 +240,7 @@ impl Transaction {
 
     /// Returns the transaction data, if it exists.
     #[must_use]
-    fn data(&self) -> Option<&TransactionData> {
+    pub fn data(&self) -> Option<&TransactionData> {
         self.payload.data.as_ref()
     }
 
diff --git a/node-data/CHANGELOG.md b/node-data/CHANGELOG.md
index 31448f2b9c..50379175c1 100644
--- a/node-data/CHANGELOG.md
+++ b/node-data/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 - Add PartialEq, Eq to `BlockState` [#3359]
+- Add `SpentTransaction::shielded` & `SpentTransaction::public` getter fn [#3464]
 
 ### Removed
 
@@ -28,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Add Types used for interacting with Dusk node 
 
 <!-- Issues -->
+[#3464]: https://github.com/dusk-network/rusk/issues/3464
 [#3359]: https://github.com/dusk-network/rusk/issues/3359
 [#3405]: https://github.com/dusk-network/rusk/issues/3405
 
diff --git a/node-data/src/ledger/transaction.rs b/node-data/src/ledger/transaction.rs
index b9ef349692..9692a7a9f8 100644
--- a/node-data/src/ledger/transaction.rs
+++ b/node-data/src/ledger/transaction.rs
@@ -7,7 +7,9 @@
 use std::io;
 
 use dusk_bytes::Serializable as DuskSerializable;
-use dusk_core::signatures::bls;
+use dusk_core::signatures::bls::PublicKey as AccountPublicKey;
+use dusk_core::transfer::moonlight::Transaction as MoonlightTransaction;
+use dusk_core::transfer::phoenix::Transaction as PhoenixTransaction;
 use dusk_core::transfer::Transaction as ProtocolTransaction;
 use serde::Serialize;
 use sha3::Digest;
@@ -46,14 +48,42 @@ impl From<ProtocolTransaction> for Transaction {
     }
 }
 
+/// A spent transaction is a transaction that has been included in a block and
+/// was executed.
 #[derive(Debug, Clone, Serialize)]
 pub struct SpentTransaction {
+    /// The transaction that was executed.
     pub inner: Transaction,
+    /// The height of the block in which the transaction was included.
     pub block_height: u64,
+    /// The amount of gas that was spent during the execution of the
+    /// transaction.
     pub gas_spent: u64,
+    /// An optional error message if the transaction execution yielded an
+    /// error.
     pub err: Option<String>,
 }
 
+impl SpentTransaction {
+    /// Returns the underlying public transaction, if it is one. Otherwise,
+    /// returns `None`.
+    pub fn public(&self) -> Option<&MoonlightTransaction> {
+        match &self.inner.inner {
+            ProtocolTransaction::Moonlight(public_tx) => Some(public_tx),
+            _ => None,
+        }
+    }
+
+    /// Returns the underlying shielded transaction, if it is one. Otherwise,
+    /// returns `None`.
+    pub fn shielded(&self) -> Option<&PhoenixTransaction> {
+        match &self.inner.inner {
+            ProtocolTransaction::Phoenix(shielded_tx) => Some(shielded_tx),
+            _ => None,
+        }
+    }
+}
+
 impl Transaction {
     /// Computes the hash digest of the entire transaction data.
     ///
@@ -129,7 +159,7 @@ impl Eq for SpentTransaction {}
 
 pub enum SpendingId {
     Nullifier([u8; 32]),
-    AccountNonce(bls::PublicKey, u64),
+    AccountNonce(AccountPublicKey, u64),
 }
 
 impl SpendingId {


Test patch: