Add `WalletRead::get_account_ids` function

This commit is contained in:
Andrew Arnott 2024-01-09 20:58:17 -07:00
parent a99e842404
commit 69d92808f3
No known key found for this signature in database
GPG Key ID: 251505B99C25745D
5 changed files with 47 additions and 0 deletions

7
.editorconfig Normal file
View File

@ -0,0 +1,7 @@
# EditorConfig is awesome:http://EditorConfig.org
# top-most EditorConfig file
root = true
[*.md]
indent_style = space

View File

@ -140,6 +140,7 @@ and this library adheres to Rust's notion of
- `get_spendable_sapling_notes`, `select_spendable_sapling_notes`, and
`get_unspent_transparent_outputs` have been removed; use
`data_api::InputSource` instead.
- Added `get_account_ids`
- `wallet::{propose_shielding, shield_transparent_funds}` now takes their
`min_confirmations` arguments as `u32` rather than a `NonZeroU32` to permit
implmentations to enable zero-conf shielding.

View File

@ -560,6 +560,9 @@ pub trait WalletRead {
account: AccountId,
max_height: BlockHeight,
) -> Result<HashMap<TransparentAddress, Amount>, Self::Error>;
/// Returns a vector with the IDs of all accounts known to this wallet.
fn get_account_ids(&self) -> Result<Vec<AccountId>, Self::Error>;
}
/// Metadata describing the sizes of the zcash note commitment trees as of a particular block.
@ -1283,6 +1286,10 @@ pub mod testing {
) -> Result<HashMap<TransparentAddress, Amount>, Self::Error> {
Ok(HashMap::new())
}
fn get_account_ids(&self) -> Result<Vec<AccountId>, Self::Error> {
Ok(Vec::new())
}
}
impl WalletWrite for MockWalletDb {

View File

@ -372,6 +372,10 @@ impl<C: Borrow<rusqlite::Connection>, P: consensus::Parameters> WalletRead for W
) -> Result<Vec<(AccountId, orchard::note::Nullifier)>, Self::Error> {
todo!()
}
fn get_account_ids(&self) -> Result<Vec<AccountId>, Self::Error> {
wallet::get_account_ids(self.conn.borrow())
}
}
impl<P: consensus::Parameters> WalletWrite for WalletDb<rusqlite::Connection, P> {

View File

@ -1458,6 +1458,20 @@ pub(crate) fn get_transparent_balances<P: consensus::Parameters>(
Ok(res)
}
/// Returns a vector with the IDs of all accounts known to this wallet.
pub(crate) fn get_account_ids(conn: &rusqlite::Connection) -> Result<Vec<AccountId>, SqliteClientError> {
let mut stmt = conn.prepare("SELECT account FROM accounts")?;
let mut rows = stmt.query([])?;
let mut result = Vec::new();
while let Some(row) = rows.next()? {
let id: u32 = row.get(0)?;
result.push(AccountId::try_from(id).map_err(|_| {
SqliteClientError::CorruptedData("Account ID out of range".to_string())
})?);
}
Ok(result)
}
/// Inserts information about a scanned block into the database.
pub(crate) fn put_block(
conn: &rusqlite::Transaction<'_>,
@ -2128,6 +2142,20 @@ mod tests {
assert_matches!(res2, Err(_));
}
#[test]
fn get_account_ids() {
use crate::testing::TestBuilder;
let st = TestBuilder::new()
.with_test_account(AccountBirthday::from_sapling_activation)
.build();
assert_eq!(
vec![AccountId::try_from(0).unwrap()],
st.wallet().get_account_ids().unwrap()
);
}
#[test]
#[cfg(feature = "transparent-inputs")]
fn transparent_balance_across_shielding() {