diff --git a/chain-runtimes/zcash/.cargo/config.toml b/chain-runtimes/zcash/.cargo/config.toml index 031c7fde5..b51ae9309 100644 --- a/chain-runtimes/zcash/.cargo/config.toml +++ b/chain-runtimes/zcash/.cargo/config.toml @@ -2,3 +2,8 @@ # it fails with "the wasm* targets are not supported by default". [target.wasm32-unknown-unknown] rustflags = ['--cfg', 'getrandom_backend="wasm_js"'] + +# NU7 exists in this zcash_* release line only under this cfg; the patched +# zcash_protocol gives it the mainnet height. Joined with the target flags above. +[target.'cfg(all())'] +rustflags = ['--cfg', 'zcash_unstable="nu7"'] diff --git a/chain-runtimes/zcash/Cargo.lock b/chain-runtimes/zcash/Cargo.lock index 1509b9aab..633768ad3 100644 --- a/chain-runtimes/zcash/Cargo.lock +++ b/chain-runtimes/zcash/Cargo.lock @@ -1655,7 +1655,7 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" [[package]] name = "onekey-zcash-keys" -version = "0.1.0-alpha.12" +version = "0.1.0-alpha.15" dependencies = [ "bip0039", "bip32", @@ -1688,7 +1688,7 @@ dependencies = [ [[package]] name = "onekey-zcash-runtime" -version = "0.1.0-alpha.12" +version = "0.1.0-alpha.15" dependencies = [ "blake2b_simd", "console_error_panic_hook", @@ -1738,9 +1738,10 @@ dependencies = [ [[package]] name = "onekey-zcash-send-policy" -version = "0.1.0-alpha.12" +version = "0.1.0-alpha.15" dependencies = [ "serde", + "serde_json", "zcash_primitives", "zcash_protocol", ] @@ -3526,8 +3527,6 @@ dependencies = [ [[package]] name = "zcash_protocol" version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "314329b91ec4bbb517441840e47d0b2029bf0b946f086980c96c889c2d92dc5d" dependencies = [ "corez", "document-features", diff --git a/chain-runtimes/zcash/Cargo.toml b/chain-runtimes/zcash/Cargo.toml index aa6395fb5..b98ad00be 100644 --- a/chain-runtimes/zcash/Cargo.toml +++ b/chain-runtimes/zcash/Cargo.toml @@ -8,13 +8,13 @@ members = [ exclude = ["vendor/sqlite-wasm-vfs-0.2.0"] [workspace.package] -version = "0.1.0-alpha.12" +version = "0.1.0-alpha.15" edition = "2021" license = "Apache-2.0" rust-version = "1.85" # Unmodified crates.io releases, except zcash_client_sqlite's Cargo.toml drops rusqlite "bundled" -# (needs a libc sysroot wasm32 lacks). See patches/manifest.json. +# (needs a libc sysroot wasm32 lacks) and zcash_protocol gains NU7 mainnet. See patches/manifest.json. [workspace.dependencies] blake2b_simd = "1" zcash_client_backend = { version = "0.24.0", default-features = false, features = ["orchard", "transparent-inputs", "lightwalletd-tonic", "sync", "pczt"] } @@ -64,6 +64,8 @@ tonic-web-wasm-client = "0.9.1" # Run `bash scripts/vendor-deps.sh` first to create vendor/. [patch.crates-io] +# Sets NU7's mainnet height and real branch ID; needs the nu7 cfg in .cargo/config.toml. +zcash_protocol = { path = "vendor/zcash_protocol-0.10.5" } zcash_client_sqlite = { path = "vendor/zcash_client_sqlite-0.22.0" } tonic-web-wasm-client = { path = "vendor/tonic-web-wasm-client-0.9.1" } diff --git a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/bindgen.rs b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/bindgen.rs index 41001a66a..f72744ee8 100644 --- a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/bindgen.rs +++ b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/bindgen.rs @@ -5,6 +5,16 @@ use zeroize::Zeroizing; use crate::{account_keys, keys, review, sign, transparent_prevouts, transparent_send}; +/// Sets when mainnet fees drop to the ZIP 2009 rate, for every later quote, build and +/// balance: `"null"` disables the switch and a height delays it; absent or malformed +/// keeps the default. It never advances the switch. +#[wasm_bindgen(js_name = setZip2009MainnetHeight)] +pub fn set_zip2009_mainnet_height(json: Option) { + onekey_zcash_send_policy::set_zip2009_switch( + onekey_zcash_send_policy::Zip2009Switch::from_host_json(json.as_deref()), + ); +} + #[wasm_bindgen(js_name = transactionPolicy)] pub fn transaction_policy(target_height: u32) -> Result { serde_json::to_string(&onekey_zcash_send_policy::transaction_policy( diff --git a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_prevouts.rs b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_prevouts.rs index 1e845c059..3e9eafe8f 100644 --- a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_prevouts.rs +++ b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_prevouts.rs @@ -119,4 +119,20 @@ mod tests { serde_json::from_str(&parse_json(&request.to_string()).unwrap()).unwrap(); assert_eq!(parsed[0]["isCoinbase"], true); } + + #[test] + fn parses_outputs_of_an_nu7_transaction() { + let mut request = request(); + let mut bytes = hex::decode(request["transactions"][1]["rawTx"].as_str().unwrap()).unwrap(); + // v6 header: version, version group id, then the consensus branch id. + bytes[8..12].copy_from_slice(&u32::from(BranchId::Nu7).to_le_bytes()); + let tx = Transaction::read(&bytes[..], BranchId::Sprout).unwrap(); + assert_eq!(tx.consensus_branch_id(), BranchId::Nu7); + request["transactions"] = json!([{ + "txid": tx.txid().to_string(), "rawTx": hex::encode(bytes), + }]); + let parsed: Value = + serde_json::from_str(&parse_json(&request.to_string()).unwrap()).unwrap(); + assert_eq!(parsed[0]["outputs"][0]["valueZat"], "1000"); + } } diff --git a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs index eb8812138..8847a4698 100644 --- a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs +++ b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send.rs @@ -38,7 +38,6 @@ use crate::{ error::{ErrorCode, KeysError, Result}, }; -const MAX_EXPIRY_DELTA: u32 = onekey_zcash_send_policy::EXPIRY_DELTA; const XPRV_VERSION: [u8; 4] = [0x04, 0x88, 0xad, 0xe4]; const ACCOUNT_XPRV_DEPTH: u8 = 3; @@ -242,12 +241,16 @@ fn plan_transaction(request: TransparentTxRequest) -> Result { ) })?; let target_height = BlockHeight::from_u32(request.target_height); - if BranchId::for_height(&MAIN_NETWORK, target_height) != BranchId::Nu6_3 { + // Built as v6, which needs NU6.3 or later. + if !matches!( + BranchId::for_height(&MAIN_NETWORK, target_height), + BranchId::Nu6_3 | BranchId::Nu7 + ) { return Err(KeysError::with( ErrorCode::InvalidTargetHeight, serde_json::json!({ "targetHeight": request.target_height, - "requiredBranch": "Nu6.3", + "requiredBranch": "Nu6.3+", }), )); } @@ -273,7 +276,7 @@ fn plan_transaction(request: TransparentTxRequest) -> Result { serde_json::json!({ "targetHeight": request.target_height, "expiryHeight": request.expiry_height, - "maximumDelta": MAX_EXPIRY_DELTA, + "maximumDelta": onekey_zcash_send_policy::expiry_delta(&MAIN_NETWORK, target_height), }), )); } @@ -567,6 +570,7 @@ fn build_transaction( account_key: &AccountPrivKey, ) -> Result { let quote = plan.quote(); + let branch = BranchId::for_height(&MAIN_NETWORK, plan.target_height); let mut builder = TransparentBuilder::empty(); let mut signing_set = TransparentSigningSet::new(); @@ -626,7 +630,7 @@ fn build_transaction( ) })?; let unsigned_tx = TransactionData::::from_parts_v6( - BranchId::Nu6_3, + branch, 0, plan.expiry_height, Some(transparent_bundle.clone()), @@ -649,7 +653,7 @@ fn build_transaction( ) .map_err(|e| KeysError::new(ErrorCode::TransactionBuildFailed).detail(e))?; let transaction = TransactionData::::from_parts_v6( - BranchId::Nu6_3, + branch, 0, plan.expiry_height, Some(authorized_bundle), @@ -1024,6 +1028,25 @@ mod tests { } } + #[test] + fn fee_quotes_follow_a_delayed_or_disabled_switch() { + use onekey_zcash_send_policy::{set_zip2009_switch, Zip2009Switch}; + let quote_at = |height: u32| { + let mut request: serde_json::Value = + serde_json::from_str(&request_json(&[100_000], true)).unwrap(); + request["targetHeight"] = height.into(); + request["expiryHeight"] = (height + 40).into(); + let quote: serde_json::Value = + serde_json::from_str("e_json(&request.to_string()).unwrap()).unwrap(); + quote["feeZat"].clone() + }; + set_zip2009_switch(Zip2009Switch::Disabled); + assert_eq!(quote_at(3_590_100), "10000"); + set_zip2009_switch(Zip2009Switch::from_host_json(Some("3600000"))); + assert_eq!(quote_at(3_600_099), "10000"); + assert_eq!(quote_at(3_600_100), "2000"); + } + #[test] fn a_transparent_quote_needs_eight_valid_blocks_and_at_most_forty() { for delta in [0, 7, 41, 100] { @@ -1347,6 +1370,30 @@ mod tests { "81004865cdc4217f34aafd1d4b307b3cc5d60fcee7afdb4a829983d394ae39cd" ); } + + #[test] + fn builds_under_nu7_from_its_activation() { + use zcash_protocol::consensus::{NetworkUpgrade, Parameters}; + let nu7 = u32::from(MAIN_NETWORK.activation_height(NetworkUpgrade::Nu7).unwrap()); + let mut request: serde_json::Value = + serde_json::from_str(&request_json(&[100_000], true)).unwrap(); + request["targetHeight"] = nu7.into(); + request["expiryHeight"] = (nu7 + onekey_zcash_send_policy::NU7_EXPIRY_DELTA).into(); + let raw = build_with_account_xprv_json(&request.to_string(), &xprv()).unwrap(); + let result: TransparentBuildResult = serde_json::from_str(&raw).unwrap(); + let tx = zcash_primitives::transaction::Transaction::read( + &hex::decode(&result.raw_tx).unwrap()[..], + BranchId::Sprout, + ) + .unwrap(); + assert_eq!(tx.consensus_branch_id(), BranchId::Nu7); + assert_eq!(tx.txid().to_string(), result.txid); + + // Before activation the expiry must stay below NU7. + request["targetHeight"] = (nu7 - 20).into(); + let error = build_with_account_xprv_json(&request.to_string(), &xprv()).unwrap_err(); + assert_eq!(error.code, ErrorCode::InvalidTargetHeight); + } } fn require_transparent(address: RecipientAddress) -> Result { diff --git a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send/shielding.rs b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send/shielding.rs index c25de416c..9bda2d6d0 100644 --- a/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send/shielding.rs +++ b/chain-runtimes/zcash/crates/zcash-keys-runtime/src/transparent_send/shielding.rs @@ -441,7 +441,8 @@ fn validate_original( let expected_anchor = has_ironwood.then(|| orchard::Anchor::empty_tree().to_bytes()); if *global.tx_version() != zcash_protocol::constants::V6_TX_VERSION || *global.version_group_id() != zcash_protocol::constants::V6_VERSION_GROUP_ID - || *global.consensus_branch_id() != u32::from(BranchId::Nu6_3) + || *global.consensus_branch_id() + != u32::from(BranchId::for_height(&MAIN_NETWORK, plan.target_height)) || *global.expiry_height() != u32::from(plan.expiry_height) || pczt::common::determine_lock_time(global, pczt.transparent().inputs()) != Some(0) || !pczt.sapling().spends().is_empty() diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/account.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/account.rs index 5820484ab..c26685cf2 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/account.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/account.rs @@ -10,7 +10,7 @@ use zcash_client_backend::data_api::{ Account as _, AccountBalance, AccountBirthday, AccountPurpose, AccountSource, Balance, WalletCommitmentTrees, WalletRead, WalletWrite, Zip32Derivation, }; -use zcash_client_sqlite::error::SqliteClientError; +use zcash_client_sqlite::{error::SqliteClientError, ExtensionTransaction}; use zcash_keys::keys::UnifiedFullViewingKey; use crate::error::{ErrorCode, Result, RuntimeError}; @@ -238,7 +238,7 @@ pub(crate) fn restart_with_birthday( .expose_uuid() .to_string(); // The old birthday's unscanned ranges would find what the new one excludes. - prune_scan_queue_below_wallet_birthday(wallet)?; + prune_scan_queue_below_wallet_birthday(wallet, ext)?; crate::tx_state::prune_orphaned_rows(ext)?; Ok(uuid) }) @@ -469,26 +469,23 @@ pub fn remove_account(db: &mut Db, account_uuid: &str) -> Result<()> { ) .detail(e) })?; - prune_scan_queue_below_wallet_birthday(wallet)?; + prune_scan_queue_below_wallet_birthday(wallet, ext)?; crate::tx_state::prune_orphaned_rows(ext) }) } /// delete_account leaves the scan queue alone, so ranges queued only for a -/// removed account's earlier birthday would still be scanned for nobody. -fn prune_scan_queue_below_wallet_birthday(wallet: &mut W) -> Result<()> +/// removed account's earlier birthday would still be scanned for nobody. A paused +/// account still counts: its remaining history waits for it. +fn prune_scan_queue_below_wallet_birthday( + wallet: &mut W, + ext: &ExtensionTransaction<'_>, +) -> Result<()> where W: WalletWrite, ::Error: std::fmt::Display, { - let birthday = wallet.get_wallet_birthday().map_err(|e| { - RuntimeError::with( - ErrorCode::DatabaseError, - json!({ "operation": "getWalletBirthday" }), - ) - .detail(e) - })?; - if let Some(birthday) = birthday { + if let Some(birthday) = crate::scan_floor::earliest_birthday(ext)? { wallet .prune_scan_queue_below(birthday, Some(ScanPriority::OpenAdjacent)) .map_err(|e| { diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/bindgen.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/bindgen.rs index 001f57afc..15efd84b6 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/bindgen.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/bindgen.rs @@ -20,6 +20,16 @@ pub fn set_request_handler(handler: Option) { network::set_request_handler(handler); } +/// Sets when mainnet fees drop to the ZIP 2009 rate, for every later quote, build and +/// balance: `"null"` disables the switch and a height delays it; absent or malformed +/// keeps the default. It never advances the switch. +#[wasm_bindgen(js_name = setZip2009MainnetHeight)] +pub fn set_zip2009_mainnet_height(json: Option) { + onekey_zcash_send_policy::set_zip2009_switch( + onekey_zcash_send_policy::Zip2009Switch::from_host_json(json.as_deref()), + ); +} + /// Installs the storage layer. Must be awaited once before any other call. Idempotent. #[wasm_bindgen(js_name = init)] pub async fn init() -> Result<(), JsValue> { @@ -497,8 +507,8 @@ async fn barrier_after_irreversible(params: serde_json::Value) -> Result<(), cra /// stores it unless the node definitively rejected it. /// /// Returns `{ txid, broadcast: "accepted" | "rejected" | "unknown", error? }`. -/// - `rejected`: the node definitively judged it consensus-invalid or expired; it can never be -/// mined. It is not stored and its notes are spendable again at once (if an earlier submit +/// - `rejected`: the node reported it expired and the local chain has passed its expiry; it can +/// never be mined. It is not stored and its notes are spendable again at once (if an earlier submit /// stored it, it is marked rejected and its notes return at its expiry height). /// - `unknown`: delivery to the node is uncertain. The transaction is stored and stays pending /// until mined or expired; the host does not resubmit it and must not treat it as failed. @@ -549,7 +559,7 @@ pub fn pending_broadcast_txids(account_uuid: String) -> Result /// Resends a stored pending transaction. Irreversible. For an explicit user request only: /// sync does not call it, and an unmined send otherwise stays pending until mined or expired. /// -/// Throws `BROADCAST_REJECTED` when the node definitively rejects it (do not retry); +/// Throws `BROADCAST_REJECTED` when the node reports it expired and the local chain agrees (do not retry); /// `BROADCAST_OUTCOME_UNKNOWN` when the node errored without proving it can never be mined; /// `NETWORK_ERROR` on transport failure. In the latter two cases the transaction stays pending /// until mined or expired. @@ -559,7 +569,9 @@ pub async fn broadcast_transaction(txid: String) -> Result<(), JsValue> { let r = match tx_state::assert_not_rejected(&mut db, &txid) .and_then(|_| send::raw_transaction(&db, &txid)) { - Ok(raw) => network::broadcast(&mut client, raw).await, + Ok(raw) => network::broadcast(&mut client, raw) + .await + .map_err(|e| send::confirm_stored_rejection(&db, &txid, e)), Err(e) => Err(e), }; let r = match r { diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/lib.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/lib.rs index fd0ed1f86..2bf771be7 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/lib.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/lib.rs @@ -12,6 +12,7 @@ mod circuit_keys; pub mod clock; pub mod error; pub mod history; +pub mod scan_floor; pub mod send; pub mod stats; pub mod storage; diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/network.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/network.rs index 6291f246e..54a285318 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/network.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/network.rs @@ -252,35 +252,21 @@ fn broadcast_outcome_ambiguous(message: &str) -> bool { .any(|needle| message.contains(needle)) } -/// Only replies proving these bytes can never be mined count as a rejection. +/// Only an expiry reply can count as a rejection, and the submit path confirms it +/// against the local chain before releasing any input. /// -/// A rejection lets the user resend with other notes, and both could be mined if -/// the original propagated. So this is an allowlist (consensus-invalid or expired); -/// transient or policy replies stay an unknown outcome. +/// A rejection lets the user resend with other notes, and both could be mined if the +/// original propagated. Every other consensus reason describes contents this runtime +/// built and verified before sending, so a node claiming one is faulty or lying: +/// those, like transient or policy replies, stay an unknown outcome. fn broadcast_definitely_invalid(message: &str) -> bool { if broadcast_outcome_ambiguous(message) { return false; } - let message = message.to_lowercase(); - // These reasons reject immutable transaction contents. A broad `bad-txns-` - // prefix also includes chain-dependent anchors, nullifiers and relay policy. - const DEFINITIVE_REASONS: &[&str] = &[ - "tx-overwinter-expired", - "bad-txns-vin-empty", - "bad-txns-vout-empty", - "bad-txns-oversize", - "bad-txns-vout-negative", - "bad-txns-vout-toolarge", - "bad-txns-txouttotal-toolarge", - "bad-txns-txintotal-toolarge", - "bad-txns-inputs-duplicate", - "bad-txns-in-belowout", - "bad-txns-fee-outofrange", - "bad-txns-orchard-verification-failed", - ]; message + .to_lowercase() .split(|character: char| !character.is_ascii_alphanumeric() && character != '-') - .any(|reason| DEFINITIVE_REASONS.contains(&reason)) + .any(|reason| reason == "tx-overwinter-expired") } fn broadcast_already_known(message: &str) -> bool { @@ -373,15 +359,18 @@ mod tests { } #[test] - fn only_consensus_invalid_or_expired_replies_count_as_a_rejection() { - for message in [ - "tx-overwinter-expired", - "bad-txns-orchard-verification-failed", - "16: bad-txns-vout-negative", - ] { + fn only_an_expiry_reply_counts_as_a_rejection() { + for message in ["tx-overwinter-expired", "18: TX-OVERWINTER-EXPIRED"] { assert!(broadcast_definitely_invalid(message), "{message}"); } for message in [ + // Contents this runtime built and verified; a node claiming them is wrong. + "bad-txns-orchard-verification-failed", + "16: bad-txns-vout-negative", + "bad-txns-vin-empty", + "bad-txns-oversize", + "bad-txns-in-belowout", + "bad-txns-fee-outofrange", "bad-txns-inputs-spent", "bad-txns-inputs-missingorspent", "bad-txns-shielded-requirements-not-met", diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/scan_floor.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/scan_floor.rs new file mode 100644 index 000000000..b42c1ec99 --- /dev/null +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/scan_floor.rs @@ -0,0 +1,97 @@ +//! Paused accounts stay in the shared database, but their history no longer sets where +//! scanning starts or which notes count as spendable. The patched `zcash_client_sqlite` +//! derives that floor from the accounts not listed in [`TABLE`]. + +use std::collections::BTreeSet; + +use serde_json::json; +use zcash_client_backend::data_api::{scanning::ScanRange, WalletRead}; +use zcash_client_sqlite::{AccountUuid, ExtensionTransaction}; +use zcash_protocol::consensus::BlockHeight; + +use crate::error::{ErrorCode, Result, RuntimeError}; +use crate::wallet::Db; + +/// Created by the patched upstream migrator, before its migrations run. +pub(crate) const TABLE: &str = "ext_onekey_paused_accounts"; + +fn db_err(operation: &str, e: impl std::fmt::Display) -> RuntimeError { + RuntimeError::with(ErrorCode::DatabaseError, json!({ "operation": operation })).detail(e) +} + +fn hex(uuid: &AccountUuid) -> String { + uuid.expose_uuid() + .as_bytes() + .iter() + .map(|b| format!("{b:02X}")) + .collect() +} + +/// Pauses every account outside `active`. Writes only when the set changes. +pub fn pause_inactive_accounts(db: &mut Db, active: &[AccountUuid]) -> Result<()> { + let paused: Vec = db + .get_account_ids() + .map_err(|e| db_err("getAccountIds", e))? + .into_iter() + .filter(|id| !active.contains(id)) + .collect(); + let wanted: BTreeSet = paused.iter().map(hex).collect(); + db.transactionally_with_extension(|_, ext| { + let current: BTreeSet = ext + .query_row( + &format!("SELECT IFNULL(group_concat(hex(account_uuid)), '') FROM {TABLE}"), + [], + |row| row.get::<_, String>(0), + ) + .map_err(|e| db_err("readPausedAccounts", e))? + .split(',') + .filter(|id| !id.is_empty()) + .map(str::to_owned) + .collect(); + if current == wanted { + return Ok(()); + } + ext.execute(&format!("DELETE FROM {TABLE}"), []) + .map_err(|e| db_err("clearPausedAccounts", e))?; + for id in &paused { + ext.execute( + &format!("INSERT INTO {TABLE} (account_uuid) VALUES (?1)"), + [id.expose_uuid().as_bytes().to_vec()], + ) + .map_err(|e| db_err("pauseAccount", e))?; + } + Ok(()) + }) +} + +/// The earliest birthday of every account, paused ones included. Pruning the scan queue +/// below anything later would drop a paused account's remaining history. +pub fn earliest_birthday(ext: &ExtensionTransaction<'_>) -> Result> { + ext.query_row("SELECT MIN(birthday_height) FROM accounts", [], |row| { + row.get::<_, Option>(0) + }) + .map(|height| height.map(BlockHeight::from_u32)) + .map_err(|e| db_err("earliestBirthday", e)) +} + +/// The ranges at or above the scan floor; one crossing it starts at the floor. A paused +/// account's older ranges stay queued until it scans again. +pub fn above_floor(db: &Db, ranges: Vec) -> Result> { + let Some(floor) = db + .get_wallet_birthday() + .map_err(|e| db_err("walletBirthday", e))? + else { + return Ok(ranges); + }; + Ok(ranges + .into_iter() + .filter_map(|range| { + let end = range.block_range().end; + if end <= floor { + return None; + } + let start = range.block_range().start.max(floor); + Some(ScanRange::from_parts(start..end, range.priority())) + }) + .collect()) +} diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/send.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/send.rs index 5dd2b71c7..c498387a5 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/send.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/send.rs @@ -1006,8 +1006,9 @@ pub enum SubmitOutcome { Accepted { txid: String, }, - /// The trusted node rejected it. It is not stored and its notes are spendable - /// again, unless an earlier submit stored it: then they return at its expiry. + /// The node reported it expired and the local chain agrees. It is not stored and its + /// notes are spendable again, unless an earlier submit stored it: then they return at + /// its expiry. Rejected { txid: String, error: RuntimeError, @@ -1041,6 +1042,7 @@ fn finish_submission( result: Result<()>, ) -> Result { let txid = staged.txid.to_string(); + let result = result.map_err(|error| confirm_rejection(db, staged.lock_expiry, error)); let result = match result.map_err(first_submit_rejection) { Err(Ok(error)) => { return Ok(match release_rejected(db, account_uuid, &staged, &error) { @@ -1154,6 +1156,14 @@ where /// Reads a stored transaction's raw bytes from the wallet for broadcast. pub fn raw_transaction(db: &Db, txid: &str) -> Result> { + let tx = stored_transaction(db, txid)?; + let mut raw = Vec::new(); + tx.write(&mut raw) + .map_err(|e| build_err("serializeTransaction", e))?; + Ok(raw) +} + +fn stored_transaction(db: &Db, txid: &str) -> Result { let bytes = hex::decode(txid) .ok() .filter(|b| b.len() == 32) @@ -1166,20 +1176,45 @@ pub fn raw_transaction(db: &Db, txid: &str) -> Result> { internal[i] = *b; } - let tx = db - .get_transaction(TxId::from_bytes(internal)) + db.get_transaction(TxId::from_bytes(internal)) .map_err(|e| build_err("getTransaction", e))? .ok_or_else(|| { RuntimeError::with( ErrorCode::TransactionBuildError, json!({ "stage": "getTransaction", "txid": txid }), ) - })?; + }) +} - let mut raw = Vec::new(); - tx.write(&mut raw) - .map_err(|e| build_err("serializeTransaction", e))?; - Ok(raw) +/// A node's expiry reply holds only once the local chain has passed the expiry; +/// until then the transaction may still be mined, so the outcome stays unknown. +fn confirm_rejection(db: &Db, expiry: BlockHeight, error: RuntimeError) -> RuntimeError { + if error.code != ErrorCode::BroadcastRejected { + return error; + } + let expired = db + .chain_height() + .ok() + .flatten() + .is_some_and(|tip| tip >= expiry); + if expired { + error + } else { + RuntimeError { + code: ErrorCode::BroadcastOutcomeUnknown, + ..error + } + } +} + +/// `confirm_rejection` for a stored transaction; one that never expires stays unknown. +pub fn confirm_stored_rejection(db: &Db, txid: &str, error: RuntimeError) -> RuntimeError { + let expiry = stored_transaction(db, txid) + .ok() + .map(|tx| tx.expiry_height()) + .filter(|height| u32::from(*height) != 0) + .unwrap_or(BlockHeight::from_u32(u32::MAX)); + confirm_rejection(db, expiry, error) } /// Proof types supported by this wallet runtime. @@ -1317,6 +1352,42 @@ mod submit_state_tests { assert_eq!(held_state(&mut db), (None, false)); } + // The fixture's local tip is 1000 and its transaction expires at 1040. + #[test] + fn an_expiry_reply_before_the_local_expiry_stays_unknown() { + let (db, staged, _) = lock_fixture(false); + let error = confirm_rejection( + &db, + staged.lock_expiry, + RuntimeError::new(ErrorCode::BroadcastRejected), + ); + assert_eq!(error.code, ErrorCode::BroadcastOutcomeUnknown); + } + + #[test] + fn an_expiry_reply_the_local_chain_confirms_is_a_rejection() { + let (db, ..) = lock_fixture(false); + for expiry in [999, 1000] { + let error = confirm_rejection( + &db, + BlockHeight::from_u32(expiry), + RuntimeError::new(ErrorCode::BroadcastRejected), + ); + assert_eq!(error.code, ErrorCode::BroadcastRejected, "{expiry}"); + } + } + + #[test] + fn other_errors_pass_the_expiry_check_unchanged() { + let (db, staged, _) = lock_fixture(false); + let error = confirm_rejection( + &db, + staged.lock_expiry, + RuntimeError::new(ErrorCode::NetworkError), + ); + assert_eq!(error.code, ErrorCode::NetworkError); + } + #[test] fn failed_release_rolls_back_the_unlock_and_keeps_recovery_state() { let (mut db, staged, account) = lock_fixture(true); diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/sync.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/sync.rs index 8fa3a2b2c..9dc8c9929 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/sync.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/sync.rs @@ -395,6 +395,9 @@ pub async fn step( .map_err(|e| db_err("getUnifiedFullViewingKeys", e))?; let active_scanning_ufvks = select_active_scanning_ufvks(¶ms, wallet_ufvks, active_ufvks)?; let active_account_count = active_scanning_ufvks.len(); + // Accounts left out of this step are paused: their history stops setting the floor. + let active_ids: Vec<_> = active_scanning_ufvks.iter().map(|(id, _)| *id).collect(); + crate::scan_floor::pause_inactive_accounts(db, &active_ids)?; let scanning_keys = ScanningKeys::from_account_ufvks(active_scanning_ufvks); let cache = MemoryBlockCache::new(); @@ -405,9 +408,11 @@ pub async fn step( let mut scanned_priority: Option = None; while batches_done < max_batches { - let suggested = db - .suggest_scan_ranges() - .map_err(|e| db_err("suggestScanRanges", e))?; + let suggested = crate::scan_floor::above_floor( + db, + db.suggest_scan_ranges() + .map_err(|e| db_err("suggestScanRanges", e))?, + )?; // Special-priority and empty passes need no window/shard queries. let window = if preferred @@ -534,9 +539,11 @@ pub async fn step( Ok(0) }; - let remaining = db - .suggest_scan_ranges() - .map_err(|e| db_err("suggestScanRanges", e))?; + let remaining = crate::scan_floor::above_floor( + db, + db.suggest_scan_ranges() + .map_err(|e| db_err("suggestScanRanges", e))?, + )?; let remaining_blocks: u64 = remaining.iter().map(|r| r.len() as u64).sum(); Ok(json!({ @@ -872,11 +879,14 @@ fn scan_heights(db: &Db) -> Result<(Option, Option)> { Ok((Some(u32::from(chain_tip)), Some(fully_scanned))) } -/// Read-only: the ranges the wallet currently suggests scanning. No network requests. +/// Read-only: the ranges the wallet currently suggests scanning, above the scan floor (the +/// host reads `totalBlocks` as remaining work). No network requests. pub fn peek(db: &Db) -> Result { - let ranges = db - .suggest_scan_ranges() - .map_err(|e| db_err("suggestScanRanges", e))?; + let ranges = crate::scan_floor::above_floor( + db, + db.suggest_scan_ranges() + .map_err(|e| db_err("suggestScanRanges", e))?, + )?; let items: Vec<_> = ranges .iter() @@ -1140,6 +1150,24 @@ mod tests { assert!(parse_enhancement(&trailing, BranchId::Nu6_2, expected).is_err()); } + #[test] + fn enhancement_parses_an_nu7_transaction() { + let fixture: serde_json::Value = serde_json::from_str(include_str!( + "../../zcash-keys-runtime/tests/fixtures/previous-transactions.json" + )) + .unwrap(); + let mut raw = hex::decode(fixture["transactions"][1]["rawTx"].as_str().unwrap()).unwrap(); + // v6 header: version, version group id, then the consensus branch id. + raw[8..12].copy_from_slice(&u32::from(BranchId::Nu7).to_le_bytes()); + let nu7 = Network::MainNetwork + .activation_height(NetworkUpgrade::Nu7) + .unwrap(); + let branch = BranchId::for_height(&Network::MainNetwork, nu7); + let expected = Transaction::read(&raw[..], branch).unwrap().txid(); + let tx = parse_enhancement(&raw, branch, expected).unwrap(); + assert_eq!(tx.consensus_branch_id(), BranchId::Nu7); + } + fn test_ufvk(network: &Network, seed_byte: u8) -> UnifiedFullViewingKey { UnifiedSpendingKey::from_seed( network, diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/sync_tests.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/sync_tests.rs index 486d5e532..9deda55cd 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/sync_tests.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/sync_tests.rs @@ -1721,3 +1721,145 @@ fn missing_root_guard_prevents_a_real_subtree_discontinuity_retry_loop() { height(f.birthday + 6) ); } + +/// Scans what the production step would: ranges clipped to the scan floor. +fn finish_above_floor(db: &mut Db, f: &Fixture, batch: u32, active: &[String]) { + for _ in 0..5000 { + let suggested = + crate::scan_floor::above_floor(db, db.suggest_scan_ranges().unwrap()).unwrap(); + let Some(r) = suggested.into_iter().min_by_key(|r| r.block_range().start) else { + return; + }; + let start = u32::from(r.block_range().start); + scan(db, f, start, batch.min(r.len() as u32), active).unwrap(); + } + panic!("scan above the floor did not converge"); +} + +fn account_at(db: &Db, birthday: u32) -> AccountUuid { + db.get_account_ids() + .unwrap() + .into_iter() + .find(|a| db.get_account_birthday(*a).unwrap() == height(birthday)) + .unwrap() +} + +/// (Orchard, Ironwood) as the balance shows it and as a send can select it. +fn spendable(db: &mut Db, account: AccountUuid, tip: u32) -> [(u64, u64); 2] { + use zcash_client_backend::data_api::{ + wallet::{input_selection::LockFilter, ConfirmationsPolicy, TargetHeight}, + InputSource, MaxSpendMode, TargetValue, + }; + use zcash_protocol::ShieldedPool; + let summary = db + .get_wallet_summary(ConfirmationsPolicy::MIN) + .unwrap() + .unwrap(); + let balance = &summary.account_balances()[&account]; + let shown = ( + balance.orchard_balance().spendable_value().into_u64(), + balance.ironwood_balance().spendable_value().into_u64(), + ); + let select = |db: &mut Db, pool| { + let notes = db + .select_spendable_notes( + account, + TargetValue::AllFunds(MaxSpendMode::MaxSpendable), + &[pool], + TargetHeight::from(height(tip + 1)), + ConfirmationsPolicy::MIN, + &[], + LockFilter::Unfiltered, + ) + .unwrap(); + notes + .orchard() + .iter() + .chain(notes.ironwood()) + .map(|n| n.note().value().inner()) + .sum::() + }; + let selectable = ( + select(db, ShieldedPool::Orchard), + select(db, ShieldedPool::Ironwood), + ); + [shown, selectable] +} + +#[test] +fn a_paused_account_does_not_hold_back_an_active_one() { + let f = fixture(); + let b_birthday = f.birthday + 20; + let b_key = &f.keys[1..2]; + + let mut alone = crate::wallet::open_and_migrate("test", ":memory:").unwrap(); + let b_alone = import(&mut alone, f, 1, 20); + alone.put_sapling_subtree_roots(0, &f.sapling).unwrap(); + alone.put_orchard_subtree_roots(0, &f.orchard).unwrap(); + alone.put_ironwood_subtree_roots(0, &f.ironwood).unwrap(); + alone.update_chain_tip(height(f.tip)).unwrap(); + finish_above_floor(&mut alone, f, 53, b_key); + let expected = spendable(&mut alone, b_alone, f.tip); + assert!(expected[0].0 > 0 && expected[0].1 > 0); + + let mut db = wallet(f, &[0, 20]); + let b = account_at(&db, b_birthday); + crate::scan_floor::pause_inactive_accounts(&mut db, &[b]).unwrap(); + assert_eq!(db.get_wallet_birthday().unwrap(), Some(height(b_birthday))); + finish_above_floor(&mut db, f, 53, b_key); + // Same as B alone, both as shown and as a send selects it. + assert_eq!(spendable(&mut db, b, f.tip), expected); + // The paused account's history is still queued for its return, but it is not + // remaining work for the active one. + assert!(db + .suggest_scan_ranges() + .unwrap() + .iter() + .any(|r| u32::from(r.block_range().end) <= b_birthday)); + let peek: serde_json::Value = serde_json::from_str(&peek(&db).unwrap()).unwrap(); + assert_eq!(peek["totalBlocks"], 0); +} + +#[test] +fn a_resumed_account_catches_up_as_if_never_paused() { + let f = fixture(); + let b_birthday = f.birthday + 20; + let mut db = wallet(f, &[0, 20]); + let b = account_at(&db, b_birthday); + crate::scan_floor::pause_inactive_accounts(&mut db, &[b]).unwrap(); + finish_above_floor(&mut db, f, 53, &f.keys[1..2]); + + let a = account_at(&db, f.birthday); + crate::scan_floor::pause_inactive_accounts(&mut db, &[a, b]).unwrap(); + assert_eq!(db.get_wallet_birthday().unwrap(), Some(height(f.birthday))); + // The host requeues what was scanned without the resumed key. + queue_rescan_from(&mut db, b_birthday).unwrap(); + finish(&mut db, f, Order::Window, 53, &f.keys[..2]); + + let mut reference = wallet(f, &[0, 20]); + finish(&mut reference, f, Order::Ascending, 53, &f.keys[..2]); + assert_same(snapshot(&mut db, f), &snapshot(&mut reference, f)); +} + +#[test] +fn removing_an_account_keeps_a_paused_accounts_history_and_rows() { + let f = fixture(); + let mut db = wallet(f, &[0, 20, 30]); + let a = account_at(&db, f.birthday); + let b = account_at(&db, f.birthday + 20); + let c = account_at(&db, f.birthday + 30); + crate::scan_floor::pause_inactive_accounts(&mut db, &[b, c]).unwrap(); + crate::account::remove_account(&mut db, &c.expose_uuid().to_string()).unwrap(); + // Pruning follows every account's birthday, the paused one's included. + assert!(db + .suggest_scan_ranges() + .unwrap() + .iter() + .any(|r| u32::from(r.block_range().start) < f.birthday + 20)); + + crate::account::remove_account(&mut db, &a.expose_uuid().to_string()).unwrap(); + assert_eq!(rows(&mut db, "SELECT * FROM ext_onekey_paused_accounts"), Vec::::new()); + // With every account paused, the floor is the earliest birthday of all. + crate::scan_floor::pause_inactive_accounts(&mut db, &[]).unwrap(); + assert_eq!(db.get_wallet_birthday().unwrap(), Some(height(f.birthday + 20))); +} diff --git a/chain-runtimes/zcash/crates/zcash-runtime/src/tx_state.rs b/chain-runtimes/zcash/crates/zcash-runtime/src/tx_state.rs index e7115b570..898f471cb 100644 --- a/chain-runtimes/zcash/crates/zcash-runtime/src/tx_state.rs +++ b/chain-runtimes/zcash/crates/zcash-runtime/src/tx_state.rs @@ -355,6 +355,14 @@ pub fn prune_orphaned_rows(ext: &ExtensionTransaction<'_>) -> Result<()> { [], ) .map_err(|error| db_err("pruneSubmissions", error))?; + ext.execute( + &format!( + "DELETE FROM {} WHERE account_uuid NOT IN (SELECT uuid FROM accounts)", + crate::scan_floor::TABLE + ), + [], + ) + .map_err(|error| db_err("prunePausedAccounts", error))?; Ok(()) } diff --git a/chain-runtimes/zcash/crates/zcash-send-policy/Cargo.toml b/chain-runtimes/zcash/crates/zcash-send-policy/Cargo.toml index 0a587ca69..dda8fbc9e 100644 --- a/chain-runtimes/zcash/crates/zcash-send-policy/Cargo.toml +++ b/chain-runtimes/zcash/crates/zcash-send-policy/Cargo.toml @@ -9,3 +9,4 @@ rust-version.workspace = true zcash_primitives.workspace = true zcash_protocol.workspace = true serde = { workspace = true, features = ["derive"] } +serde_json.workspace = true diff --git a/chain-runtimes/zcash/crates/zcash-send-policy/src/lib.rs b/chain-runtimes/zcash/crates/zcash-send-policy/src/lib.rs index 3aad3894d..126e305d5 100644 --- a/chain-runtimes/zcash/crates/zcash-send-policy/src/lib.rs +++ b/chain-runtimes/zcash/crates/zcash-send-policy/src/lib.rs @@ -1,8 +1,10 @@ //! Shared transaction policy for the wallet runtime, stateless signer and host UI. +use std::cell::Cell; + use zcash_primitives::transaction::fees::zip317::{self, FeeRule}; use zcash_protocol::{ - consensus::{BlockHeight, BranchId, NetworkType, Parameters}, + consensus::{BlockHeight, BranchId, NetworkType, NetworkUpgrade, Parameters}, value::Zatoshis, }; @@ -10,12 +12,79 @@ pub const ZIP2009_MAINNET_HEIGHT: u32 = 3_590_000; // Match Ledger's rollout margin; `target` is the next block, not the synced tip. pub const ZIP2009_SWITCH_MARGIN: u32 = 100; pub const EXPIRY_DELTA: u32 = 40; +// Blocks come every 25 s from NU7; 120 keeps the same ~50 minutes, as upstream does. +pub const NU7_EXPIRY_DELTA: u32 = 120; pub const MIN_VALID_BLOCKS: u32 = 8; +/// The mainnet height sends move to the ZIP 2009 fee at, configured as Ledger Live +/// does: the host may delay or disable the switch, never advance it, since nodes +/// refuse the lower fee before the default height. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Zip2009Switch { + At(u32), + Disabled, +} + +impl Zip2009Switch { + /// A host value: absent or malformed keeps the default, null disables. + pub fn from_host(value: Option>) -> Self { + match value { + Some(None) => Self::Disabled, + Some(Some(height)) + if height.fract() == 0.0 && (0.0..=f64::from(u32::MAX)).contains(&height) => + { + Self::At((height as u32).max(ZIP2009_MAINNET_HEIGHT)) + } + _ => Self::default(), + } + } + + /// A host value as JSON: `null` disables, a height delays; absent or malformed + /// keeps the default. + pub fn from_host_json(json: Option<&str>) -> Self { + Self::from_host( + json.map(|json| serde_json::from_str::>(json).unwrap_or(Some(f64::NAN))), + ) + } + + fn applies_at(self, target: BlockHeight) -> bool { + match self { + Self::Disabled => false, + Self::At(height) => { + u32::from(target) + >= height + .max(ZIP2009_MAINNET_HEIGHT) + .saturating_add(ZIP2009_SWITCH_MARGIN) + } + } + } +} + +impl Default for Zip2009Switch { + fn default() -> Self { + Self::At(ZIP2009_MAINNET_HEIGHT) + } +} + +thread_local! { + // Runtime-wide, like the node URL, so every fee use, vendored ones included, + // agrees. + static ZIP2009: Cell = + const { Cell::new(Zip2009Switch::At(ZIP2009_MAINNET_HEIGHT)) }; +} + +/// Sets the switch every later fee in this runtime uses. +pub fn set_zip2009_switch(switch: Zip2009Switch) { + ZIP2009.with(|cell| cell.set(switch)); +} + +pub fn zip2009_switch() -> Zip2009Switch { + ZIP2009.with(Cell::get) +} + +/// Test networks always price at the ZIP 2009 fee. pub fn marginal_fee(params: &P, target: BlockHeight) -> Zatoshis { - if params.network_type() != NetworkType::Main - || u32::from(target) >= ZIP2009_MAINNET_HEIGHT + ZIP2009_SWITCH_MARGIN - { + if params.network_type() != NetworkType::Main || zip2009_switch().applies_at(target) { Zatoshis::const_from_u64(1_000) } else { zip317::MARGINAL_FEE @@ -32,9 +101,17 @@ pub fn fee_rule(params: &P, target: BlockHeight) -> FeeRule { .expect("standard ZIP-317 input and output sizes are nonzero") } +pub fn expiry_delta(params: &P, target: BlockHeight) -> u32 { + if params.is_nu_active(NetworkUpgrade::Nu7, target) { + NU7_EXPIRY_DELTA + } else { + EXPIRY_DELTA + } +} + pub fn expiry_height(params: &P, target: BlockHeight) -> Option { let target_u32 = u32::from(target); - let expiry = target_u32.checked_add(EXPIRY_DELTA)?; + let expiry = target_u32.checked_add(expiry_delta(params, target))?; let next_upgrade = BranchId::for_height(params, target) .height_bounds(params) .and_then(|(_, upper)| upper); @@ -62,7 +139,13 @@ pub fn transaction_policy(params: &P, target: BlockHeight) -> Tra #[cfg(test)] mod tests { use super::*; - use zcash_protocol::consensus::{NetworkUpgrade, MAIN_NETWORK, TEST_NETWORK}; + use zcash_protocol::consensus::{MAIN_NETWORK, TEST_NETWORK}; + + // Each test runs on its own thread, so the setting stays local to it. + fn mainnet_fee(height: u32, zip2009: Zip2009Switch) -> u64 { + set_zip2009_switch(zip2009); + u64::from(fee_rule(&MAIN_NETWORK, BlockHeight::from_u32(height)).marginal_fee()) + } #[test] fn fees_switch_at_the_target_block() { @@ -72,15 +155,77 @@ mod tests { (3_590_099, 5_000), (3_590_100, 1_000), ] { - let target = BlockHeight::from_u32(height); + assert_eq!(mainnet_fee(height, Zip2009Switch::default()), expected); + set_zip2009_switch(Zip2009Switch::Disabled); assert_eq!( - u64::from(fee_rule(&MAIN_NETWORK, target).marginal_fee()), - expected + u64::from(marginal_fee(&TEST_NETWORK, BlockHeight::from_u32(height))), + 1_000 ); - assert_eq!(u64::from(marginal_fee(&TEST_NETWORK, target)), 1_000); } } + #[test] + fn the_host_can_delay_or_disable_the_switch_but_not_advance_it() { + let delayed = Zip2009Switch::from_host(Some(Some(3_700_000.0))); + assert_eq!(mainnet_fee(3_700_099, delayed), 5_000); + assert_eq!(mainnet_fee(3_700_100, delayed), 1_000); + + let advanced = Zip2009Switch::from_host(Some(Some(3_000_000.0))); + assert_eq!(advanced, Zip2009Switch::default()); + assert_eq!(mainnet_fee(3_590_099, advanced), 5_000); + assert_eq!(mainnet_fee(3_590_099, Zip2009Switch::At(1)), 5_000); + + let disabled = Zip2009Switch::from_host(Some(None)); + assert_eq!(mainnet_fee(u32::MAX, disabled), 5_000); + // The margin saturates instead of wrapping to an early height. + assert_eq!(mainnet_fee(3_590_100, Zip2009Switch::At(u32::MAX)), 5_000); + } + + #[test] + fn a_json_host_value_follows_the_same_rule() { + assert_eq!( + Zip2009Switch::from_host_json(None), + Zip2009Switch::default() + ); + assert_eq!( + Zip2009Switch::from_host_json(Some("null")), + Zip2009Switch::Disabled + ); + assert_eq!( + Zip2009Switch::from_host_json(Some("3700000")), + Zip2009Switch::At(3_700_000) + ); + assert_eq!( + Zip2009Switch::from_host_json(Some("\"3700000\"")), + Zip2009Switch::default() + ); + assert_eq!( + Zip2009Switch::from_host_json(Some("{")), + Zip2009Switch::default() + ); + } + + #[test] + fn an_absent_or_malformed_height_keeps_the_default_switch() { + for value in [ + None, + Some(Some(f64::NAN)), + Some(Some(-1.0)), + Some(Some(3_600_000.5)), + ] { + assert_eq!(Zip2009Switch::from_host(value), Zip2009Switch::default()); + } + assert_eq!( + Zip2009Switch::from_host(Some(Some(f64::from(u32::MAX) + 1.0))), + Zip2009Switch::default() + ); + } + + #[test] + fn the_runtime_starts_on_the_default_switch() { + assert_eq!(zip2009_switch(), Zip2009Switch::default()); + } + #[test] fn expiry_stays_in_the_current_consensus_epoch() { let upgrade = MAIN_NETWORK @@ -96,6 +241,35 @@ mod tests { ); } + #[test] + fn nu7_activates_on_mainnet_with_its_branch_id() { + let nu7 = MAIN_NETWORK.activation_height(NetworkUpgrade::Nu7).unwrap(); + assert_eq!(u32::from(nu7), 3_543_405); + assert_eq!( + BranchId::for_height(&MAIN_NETWORK, nu7 - 1), + BranchId::Nu6_3 + ); + assert_eq!(BranchId::for_height(&MAIN_NETWORK, nu7), BranchId::Nu7); + assert_eq!(u32::from(BranchId::Nu7), 0x7719_0ad9); + assert_eq!(BranchId::try_from(0x7719_0ad9), Ok(BranchId::Nu7)); + assert_eq!( + TEST_NETWORK.activation_height(NetworkUpgrade::Nu7), + Some(BlockHeight::from_u32(4_465_026)) + ); + } + + #[test] + fn expiry_keeps_its_duration_across_nu7() { + let nu7 = MAIN_NETWORK.activation_height(NetworkUpgrade::Nu7).unwrap(); + // An NU6.3 transaction cannot be mined from NU7 on. + assert_eq!(expiry_height(&MAIN_NETWORK, nu7 - 20), Some(nu7 - 1)); + assert_eq!(expiry_height(&MAIN_NETWORK, nu7 - 8), None); + assert_eq!( + expiry_height(&MAIN_NETWORK, nu7), + Some(nu7 + NU7_EXPIRY_DELTA) + ); + } + #[test] fn overflow_cannot_create_a_nonexpiring_transaction() { assert_eq!( @@ -104,6 +278,6 @@ mod tests { ); let policy = transaction_policy(&MAIN_NETWORK, BlockHeight::from_u32(3_590_100)); assert_eq!(policy.minimum_fee_zat, "2000"); - assert_eq!(policy.expiry_height, Some(3_590_140)); + assert_eq!(policy.expiry_height, Some(3_590_220)); } } diff --git a/chain-runtimes/zcash/patches/manifest.json b/chain-runtimes/zcash/patches/manifest.json index 9af7c348f..2766b8c9f 100644 --- a/chain-runtimes/zcash/patches/manifest.json +++ b/chain-runtimes/zcash/patches/manifest.json @@ -1,4 +1,10 @@ [ + { + "name": "zcash_protocol", + "version": "0.10.5", + "sha256": "314329b91ec4bbb517441840e47d0b2029bf0b946f086980c96c889c2d92dc5d", + "patch": "zcash_protocol-0.10.5-nu7-mainnet.patch" + }, { "name": "zcash_client_sqlite", "version": "0.22.0", diff --git a/chain-runtimes/zcash/patches/zcash_client_sqlite-0.22.0-no-bundled.patch b/chain-runtimes/zcash/patches/zcash_client_sqlite-0.22.0-no-bundled.patch index 2baa1456b..4aaad30aa 100644 --- a/chain-runtimes/zcash/patches/zcash_client_sqlite-0.22.0-no-bundled.patch +++ b/chain-runtimes/zcash/patches/zcash_client_sqlite-0.22.0-no-bundled.patch @@ -69,6 +69,209 @@ let overridable_owners = overridable_owners_rarray(lock_filter); let mut sql_params: Vec<(&str, &dyn ToSql)> = vec![ (":account_uuid", &account_uuid), +--- a/src/wallet/db.rs ++++ b/src/wallet/db.rs +@@ -17,6 +17,7 @@ + use zcash_protocol::consensus::{NetworkUpgrade, Parameters}; + + use crate::wallet::scanning::priority_code; ++use rusqlite::OptionalExtension; + + /// Stores information about the accounts that the wallet is tracking. + /// +@@ -1755,6 +1756,14 @@ + FROM unioned + GROUP BY transaction_id, output_pool, output_index"; + ++/// OneKey: the scan and spending floor. Accounts listed in `ext_onekey_paused_accounts` keep ++/// their data but do not hold it back; with every account paused it is the earliest ++/// birthday of all, which is the upstream wallet birthday. ++pub(crate) const ONEKEY_SCAN_FLOOR_SQL: &str = "COALESCE( ++ (SELECT MIN(birthday_height) FROM accounts ++ WHERE uuid NOT IN (SELECT account_uuid FROM ext_onekey_paused_accounts)), ++ (SELECT MIN(birthday_height) FROM accounts))"; ++ + /// Combines the Sapling tree shards and scan ranges. + /// + /// Note that in regtest mode when the Sapling NU has no activation height, the +@@ -1797,7 +1806,7 @@ + pub(super) fn view_sapling_shard_unscanned_ranges() -> String { + format!( + "CREATE VIEW v_sapling_shard_unscanned_ranges AS +- WITH wallet_birthday AS (SELECT MIN(birthday_height) AS height FROM accounts) ++ WITH wallet_birthday AS (SELECT {} AS height) + SELECT + shard_index, + start_position, +@@ -1812,6 +1821,7 @@ + INNER JOIN wallet_birthday + WHERE priority > {} + AND block_range_end > wallet_birthday.height", ++ ONEKEY_SCAN_FLOOR_SQL, + priority_code(&ScanPriority::Scanned) + ) + } +@@ -1877,7 +1887,7 @@ + pub(super) fn view_orchard_shard_unscanned_ranges() -> String { + format!( + "CREATE VIEW v_orchard_shard_unscanned_ranges AS +- WITH wallet_birthday AS (SELECT MIN(birthday_height) AS height FROM accounts) ++ WITH wallet_birthday AS (SELECT {} AS height) + SELECT + shard_index, + start_position, +@@ -1892,6 +1902,7 @@ + INNER JOIN wallet_birthday + WHERE priority > {} + AND block_range_end > wallet_birthday.height", ++ ONEKEY_SCAN_FLOOR_SQL, + priority_code(&ScanPriority::Scanned), + ) + } +@@ -1958,7 +1969,7 @@ + pub(super) fn view_ironwood_shard_unscanned_ranges() -> String { + format!( + "CREATE VIEW v_ironwood_shard_unscanned_ranges AS +- WITH wallet_birthday AS (SELECT MIN(birthday_height) AS height FROM accounts) ++ WITH wallet_birthday AS (SELECT {} AS height) + SELECT + shard_index, + start_position, +@@ -1973,6 +1984,7 @@ + INNER JOIN wallet_birthday + WHERE priority > {} + AND block_range_end > wallet_birthday.height", ++ ONEKEY_SCAN_FLOOR_SQL, + priority_code(&ScanPriority::Scanned), + ) + } +@@ -2035,3 +2047,67 @@ + GROUP BY + address_id, account_id, key_scope, + diversifier_index_be, transparent_child_index"; ++ ++ ++/// OneKey: [`VIEW_ORCHARD_SHARDS_SCAN_STATE`] and its siblings, except that an unscanned range ++/// below the scan floor no longer marks its shard unscanned: it waits for a paused account. ++fn onekey_view_shards_scan_state(pool: &str) -> String { ++ format!( ++ "CREATE VIEW v_{pool}_shards_scan_state AS ++SELECT ++ shard_index, ++ start_position, ++ end_position_exclusive, ++ subtree_start_height, ++ subtree_end_height, ++ contains_marked, ++ MAX(priority) AS max_priority ++FROM v_{pool}_shard_scan_ranges ++WHERE NOT (priority > {} AND block_range_end <= IFNULL(({}), 0)) ++GROUP BY ++ shard_index, ++ start_position, ++ end_position_exclusive, ++ subtree_start_height, ++ subtree_end_height, ++ contains_marked", ++ priority_code(&ScanPriority::Scanned), ++ ONEKEY_SCAN_FLOOR_SQL, ++ ) ++} ++ ++/// OneKey: recreates the shard views whose definitions predate the scan floor, so existing ++/// databases follow [`ONEKEY_SCAN_FLOOR_SQL`]. Returns whether anything was recreated. ++pub(crate) fn rebuild_onekey_shard_views(conn: &mut rusqlite::Connection) -> Result { ++ let views = [ ++ ("v_sapling_shard_unscanned_ranges", view_sapling_shard_unscanned_ranges()), ++ ("v_sapling_shards_scan_state", onekey_view_shards_scan_state("sapling")), ++ ("v_orchard_shard_unscanned_ranges", view_orchard_shard_unscanned_ranges()), ++ ("v_orchard_shards_scan_state", onekey_view_shards_scan_state("orchard")), ++ ("v_ironwood_shard_unscanned_ranges", view_ironwood_shard_unscanned_ranges()), ++ ("v_ironwood_shards_scan_state", onekey_view_shards_scan_state("ironwood")), ++ ]; ++ let mut stale = Vec::new(); ++ for (name, sql) in &views { ++ let current: Option = conn ++ .query_row( ++ "SELECT sql FROM sqlite_master WHERE type = 'view' AND name = ?1", ++ [name], ++ |row| row.get(0), ++ ) ++ .optional()?; ++ if current.as_deref() != Some(sql.as_str()) { ++ stale.push((name, sql)); ++ } ++ } ++ if stale.is_empty() { ++ return Ok(false); ++ } ++ // Views resolve their dependencies when queried, so each can be replaced on its own. ++ let tx = conn.transaction()?; ++ for (name, sql) in stale { ++ tx.execute_batch(&format!("DROP VIEW IF EXISTS {name}; {sql};"))?; ++ } ++ tx.commit()?; ++ Ok(true) ++} +--- a/src/wallet/init.rs ++++ b/src/wallet/init.rs +@@ -597,6 +597,16 @@ + .execute_batch("PRAGMA foreign_keys = OFF;") + .map_err(|e| MigratorError::Adapter(WalletMigrationError::from(e)))?; + ++ // OneKey: the scan floor reads this while migrations run (see `db::ONEKEY_SCAN_FLOOR_SQL`). ++ wdb.conn ++ .borrow() ++ .execute_batch( ++ "CREATE TABLE IF NOT EXISTS ext_onekey_paused_accounts ( ++ account_uuid BLOB PRIMARY KEY NOT NULL ++ )", ++ ) ++ .map_err(|e| MigratorError::Adapter(WalletMigrationError::from(e)))?; ++ + // Temporarily take ownership of the connection in a wrapper to perform the initial migration + // table setup. This extra adapter creation could be omitted if `RusqliteAdapter` provided an + // accessor for the connection that it wraps, or if it provided a mechanism to query to +@@ -644,6 +654,12 @@ + .borrow() + .execute("PRAGMA foreign_keys = ON", []) + .map_err(|e| MigratorError::Adapter(WalletMigrationError::from(e)))?; ++ ++ // OneKey: databases migrated before the scan floor get its shard views. ++ if target_migrations.is_empty() { ++ super::db::rebuild_onekey_shard_views(wdb.conn.borrow_mut()) ++ .map_err(|e| MigratorError::Adapter(WalletMigrationError::from(e)))?; ++ } + + // Now that the migration succeeded, check whether the seed is relevant to the wallet. + // We can only check this if we have migrated as far as `full_account_ids::MIGRATION_ID`, +--- a/src/wallet/scanning.rs ++++ b/src/wallet/scanning.rs +@@ -549,14 +549,22 @@ + WHERE shard.subtree_end_height IS NOT NULL + AND shard.subtree_end_height <= :pruning_floor + AND (commitment_tree_position >> :shard_height) = shard.shard_index ++ -- OneKey: every account's birthday, a paused one's included; a gap ++ -- below the scan floor still leaves this shard's witnesses incomplete. + AND shard.shard_index NOT IN ( +- SELECT shard_index FROM v_{table_prefix}_shard_unscanned_ranges ++ SELECT shard_index FROM v_{table_prefix}_shard_scan_ranges ++ WHERE priority > :scanned ++ AND block_range_end > (SELECT MIN(birthday_height) FROM accounts) + ) + )", + ); + conn.execute( + &sql, +- named_params![":pruning_floor": pruning_floor, ":shard_height": shard_height], ++ named_params![ ++ ":pruning_floor": pruning_floor, ++ ":shard_height": shard_height, ++ ":scanned": priority_code(&ScanPriority::Scanned), ++ ], + )?; + Ok(()) + } --- a/src/wallet.rs +++ b/src/wallet.rs @@ -127,7 +127,7 @@ @@ -121,3 +324,12 @@ anchor_height, confirmations_policy, &mut account_balances, +@@ -3227,7 +3231,7 @@ + conn: &rusqlite::Connection, + ) -> Result, rusqlite::Error> { + conn.query_row( +- "SELECT MIN(birthday_height) AS wallet_birthday FROM accounts", ++ &format!("SELECT {} AS wallet_birthday", db::ONEKEY_SCAN_FLOOR_SQL), + [], + |row| { + row.get::<_, Option>(0) diff --git a/chain-runtimes/zcash/patches/zcash_protocol-0.10.5-nu7-mainnet.patch b/chain-runtimes/zcash/patches/zcash_protocol-0.10.5-nu7-mainnet.patch new file mode 100644 index 000000000..5f16ecdc4 --- /dev/null +++ b/chain-runtimes/zcash/patches/zcash_protocol-0.10.5-nu7-mainnet.patch @@ -0,0 +1,38 @@ +--- a/src/consensus.rs ++++ b/src/consensus.rs +@@ -501,7 +501,7 @@ + NetworkUpgrade::Nu6_2 => Some(BlockHeight(3_364_600)), + NetworkUpgrade::Nu6_3 => Some(BlockHeight(3_428_143)), + #[cfg(zcash_unstable = "nu7")] +- NetworkUpgrade::Nu7 => None, ++ NetworkUpgrade::Nu7 => Some(BlockHeight(3_543_405)), + } + } + } +@@ -534,7 +534,7 @@ + NetworkUpgrade::Nu6_2 => Some(BlockHeight(4_052_000)), + NetworkUpgrade::Nu6_3 => Some(BlockHeight(4_134_000)), + #[cfg(zcash_unstable = "nu7")] +- NetworkUpgrade::Nu7 => None, ++ NetworkUpgrade::Nu7 => Some(BlockHeight(4_465_026)), + } + } + } +@@ -757,7 +757,7 @@ + 0x5437_f330 => Ok(BranchId::Nu6_2), + 0x37a5_165b => Ok(BranchId::Nu6_3), + #[cfg(zcash_unstable = "nu7")] +- 0xffff_ffff => Ok(BranchId::Nu7), ++ 0x7719_0ad9 => Ok(BranchId::Nu7), + _ => Err("Unknown consensus branch ID"), + } + } +@@ -778,7 +778,7 @@ + BranchId::Nu6_2 => 0x5437_f330, + BranchId::Nu6_3 => 0x37a5_165b, + #[cfg(zcash_unstable = "nu7")] +- BranchId::Nu7 => 0xffff_ffff, ++ BranchId::Nu7 => 0x7719_0ad9, + } + } + }