Skip to content

Commit

Permalink
zcash_client_sqlite: Add target_height column to transactions table.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Aug 2, 2024
1 parent dc595ff commit 8989446
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion zcash_client_sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ impl<P: consensus::Parameters> WalletWrite for WalletDb<rusqlite::Connection, P>
d_tx: DecryptedTransaction<AccountId>,
) -> Result<(), Self::Error> {
self.transactionally(|wdb| {
let tx_ref = wallet::put_tx_data(wdb.conn.0, d_tx.tx(), None, None)?;
let tx_ref = wallet::put_tx_data(wdb.conn.0, d_tx.tx(), None, None, None)?;
let funding_accounts = wallet::get_funding_accounts(wdb.conn.0, d_tx.tx())?;

// TODO(#1305): Correctly track accounts that fund each transaction output.
Expand Down
7 changes: 5 additions & 2 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,7 @@ pub(crate) fn store_transaction_to_be_sent<P: consensus::Parameters>(
sent_tx.tx(),
Some(sent_tx.fee_amount()),
Some(sent_tx.created()),
chain_tip_height(wdb.conn.0)?.map(|h| h + 1),
)?;

// Mark notes as spent.
Expand Down Expand Up @@ -2357,10 +2358,11 @@ pub(crate) fn put_tx_data(
tx: &Transaction,
fee: Option<NonNegativeAmount>,
created_at: Option<time::OffsetDateTime>,
target_height: Option<BlockHeight>,
) -> Result<TxRef, SqliteClientError> {
let mut stmt_upsert_tx_data = conn.prepare_cached(
"INSERT INTO transactions (txid, created, expiry_height, raw, fee)
VALUES (:txid, :created_at, :expiry_height, :raw, :fee)
"INSERT INTO transactions (txid, created, expiry_height, raw, fee, target_height)
VALUES (:txid, :created_at, :expiry_height, :raw, :fee, :target_height)
ON CONFLICT (txid) DO UPDATE
SET expiry_height = :expiry_height,
raw = :raw,
Expand All @@ -2378,6 +2380,7 @@ pub(crate) fn put_tx_data(
":expiry_height": u32::from(tx.expiry_height()),
":raw": raw_tx,
":fee": fee.map(u64::from),
":target_height": target_height.map(u32::from),
];

stmt_upsert_tx_data
Expand Down
1 change: 1 addition & 0 deletions zcash_client_sqlite/src/wallet/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ CREATE TABLE "transactions" (
raw BLOB,
fee INTEGER,
confirmed_unmined INTEGER,
target_height INTEGER,
FOREIGN KEY (block) REFERENCES blocks(height),
CONSTRAINT height_consistency CHECK (block IS NULL OR mined_height = block)
)"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ mod tests {
builder::{BuildConfig, BuildResult, Builder},
components::{amount::NonNegativeAmount, transparent},
fees::fixed,
Transaction,
},
zip32::{self, Scope},
};
Expand All @@ -322,7 +323,7 @@ mod tests {
memo_repr, parse_scope,
sapling::ReceivedSaplingOutput,
},
AccountId, WalletDb,
AccountId, TxRef, WalletDb,
};

// These must be different.
Expand Down Expand Up @@ -471,6 +472,41 @@ mod tests {
Ok(())
}

/// This reproduces [`crate::wallet::put_tx_data`] as it was at the time
/// of the creation of this migration.
fn put_tx_data(
conn: &rusqlite::Connection,
tx: &Transaction,
fee: Option<NonNegativeAmount>,
created_at: Option<time::OffsetDateTime>,
) -> Result<TxRef, SqliteClientError> {
let mut stmt_upsert_tx_data = conn.prepare_cached(
"INSERT INTO transactions (txid, created, expiry_height, raw, fee)
VALUES (:txid, :created_at, :expiry_height, :raw, :fee)
ON CONFLICT (txid) DO UPDATE
SET expiry_height = :expiry_height,
raw = :raw,
fee = IFNULL(:fee, fee)
RETURNING id_tx",
)?;

let txid = tx.txid();
let mut raw_tx = vec![];
tx.write(&mut raw_tx)?;

let tx_params = named_params![
":txid": &txid.as_ref()[..],
":created_at": created_at,
":expiry_height": u32::from(tx.expiry_height()),
":raw": raw_tx,
":fee": fee.map(u64::from),
];

stmt_upsert_tx_data
.query_row(tx_params, |row| row.get::<_, i64>(0).map(TxRef))
.map_err(SqliteClientError::from)
}

#[test]
fn receiving_key_scopes_migration_enhanced() {
let params = Network::TestNetwork;
Expand Down Expand Up @@ -504,7 +540,7 @@ mod tests {

db_data
.transactionally::<_, _, rusqlite::Error>(|wdb| {
let tx_ref = crate::wallet::put_tx_data(wdb.conn.0, d_tx.tx(), None, None).unwrap();
let tx_ref = put_tx_data(wdb.conn.0, d_tx.tx(), None, None).unwrap();

let mut spending_account_id: Option<AccountId> = None;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! A migration to add the `tx_retrieval_queue` table to the database.

use rusqlite::Transaction;
use rusqlite::{named_params, Transaction};
use schemer_rusqlite::RusqliteMigration;
use std::collections::HashSet;
use uuid::Uuid;
use zcash_primitives::transaction::builder::DEFAULT_TX_EXPIRY_DELTA;

use crate::wallet::init::WalletMigrationError;

Expand Down Expand Up @@ -38,15 +39,25 @@ impl RusqliteMigration for Migration {
dependent_transaction_id INTEGER,
FOREIGN KEY (dependent_transaction_id) REFERENCES transactions(id_tx)
);
ALTER TABLE transactions ADD COLUMN confirmed_unmined INTEGER;",
ALTER TABLE transactions ADD COLUMN confirmed_unmined INTEGER;
ALTER TABLE transactions ADD COLUMN target_height INTEGER;",
)?;

transaction.execute(
"UPDATE transactions
SET target_height = expiry_height - :default_expiry_delta
WHERE expiry_height IS NOT NULL AND created IS NOT NULL",
named_params![":default_expiry_delta": DEFAULT_TX_EXPIRY_DELTA],
)?;

Ok(())
}

fn down(&self, transaction: &Transaction) -> Result<(), WalletMigrationError> {
transaction.execute_batch(
"ALTER TABLE transactions DROP COLUMN confirmed_unmined;
"ALTER TABLE transactions DROP COLUMN target_height;
ALTER TABLE transactions DROP COLUMN confirmed_unmined;
DROP TABLE tx_retrieval_queue;",
)?;

Expand Down
2 changes: 1 addition & 1 deletion zcash_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use super::components::sapling::zip212_enforcement;

/// Since Blossom activation, the default transaction expiry delta should be 40 blocks.
/// <https://zips.z.cash/zip-0203#changes-for-blossom>
const DEFAULT_TX_EXPIRY_DELTA: u32 = 40;
pub const DEFAULT_TX_EXPIRY_DELTA: u32 = 40;

/// Errors that can occur during fee calculation.
#[derive(Debug)]
Expand Down

0 comments on commit 8989446

Please sign in to comment.