Skip to content

Commit

Permalink
Merge pull request zcash#1112 from nuttycom/get_accounts
Browse files Browse the repository at this point in the history
Add WalletRead::get_account_ids (subsumes zcash#1106 to fix formatting issues)
  • Loading branch information
nuttycom authored Jan 11, 2024
2 parents a99e842 + 6033671 commit 47d4328
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# EditorConfig is awesome:http://EditorConfig.org

# top-most EditorConfig file
root = true

[*.md]
indent_style = space
1 change: 1 addition & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions zcash_client_backend/src/data_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions zcash_client_sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
30 changes: 30 additions & 0 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,22 @@ 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<'_>,
Expand Down Expand Up @@ -2128,6 +2144,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() {
Expand Down

0 comments on commit 47d4328

Please sign in to comment.