Skip to content

Commit

Permalink
Merge pull request #1086 from AloeareV/shield_post_merge
Browse files Browse the repository at this point in the history
Get internals of complete_and_broadcast working with shielding proposals
  • Loading branch information
fluidvanadium authored May 15, 2024
2 parents dd8f941 + d219faa commit bb265da
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 222 deletions.
216 changes: 111 additions & 105 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ members = [
resolver = "2"

[workspace.dependencies]
zcash_address = { git = "https://github.com/zingolabs/librustzcash.git", tag = "calculate_tx_async" }
zcash_client_backend = { git = "https://github.com/zingolabs/librustzcash.git", tag = "calculate_tx_async", features = ["lightwalletd-tonic", "orchard", "transparent-inputs"] }
zcash_encoding = { git = "https://github.com/zingolabs/librustzcash.git", tag = "calculate_tx_async" }
zcash_keys = { git = "https://github.com/zingolabs/librustzcash.git", tag = "calculate_tx_async", features = ["orchard"] }
zcash_address = { git = "https://github.com/zingolabs/librustzcash.git", tag = "custom_notusk_to_taddr-1-g239c894b" }
zcash_client_backend = { git = "https://github.com/zingolabs/librustzcash.git", tag = "custom_notusk_to_taddr-1-g239c894b", features = ["lightwalletd-tonic", "orchard", "transparent-inputs"] }
zcash_encoding = { git = "https://github.com/zingolabs/librustzcash.git", tag = "custom_notusk_to_taddr-1-g239c894b" }
zcash_keys = { git = "https://github.com/zingolabs/librustzcash.git", tag = "custom_notusk_to_taddr-1-g239c894b", features = ["orchard"] }
zcash_note_encryption = "0.4"
zcash_primitives = { git = "https://github.com/zingolabs/librustzcash.git", tag = "calculate_tx_async" }
zcash_proofs = { git = "https://github.com/zingolabs/librustzcash.git", tag = "calculate_tx_async" }
zcash_protocol = { git = "https://github.com/zingolabs/librustzcash.git", tag = "calculate_tx_async" }
zcash_primitives = { git = "https://github.com/zingolabs/librustzcash.git", tag = "custom_notusk_to_taddr-1-g239c894b" }
zcash_proofs = { git = "https://github.com/zingolabs/librustzcash.git", tag = "custom_notusk_to_taddr-1-g239c894b" }
zcash_protocol = { git = "https://github.com/zingolabs/librustzcash.git", tag = "custom_notusk_to_taddr-1-g239c894b" }
sapling-crypto = "0.1.2"
orchard = "0.8"
zip32 = "0.1"
Expand Down
5 changes: 5 additions & 0 deletions integration-tests/tests/chain_generic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use zcash_client_backend::ShieldedProtocol::Orchard;
use zcash_client_backend::ShieldedProtocol::Sapling;

use zingo_testutils::chain_generic_tests::fixtures::propose_and_broadcast_value_to_pool;
use zingo_testutils::chain_generic_tests::fixtures::send_shield_cycle;
use zingo_testutils::chain_generic_tests::fixtures::send_value_to_pool;

use libtonode_environment::LibtonodeEnvironment;
Expand Down Expand Up @@ -33,6 +34,10 @@ async fn libtonode_propose_and_broadcast_40_000_to_sapling() {
async fn libtonode_propose_and_broadcast_40_000_to_orchard() {
propose_and_broadcast_value_to_pool::<LibtonodeEnvironment>(40_000, Shielded(Orchard)).await;
}
#[tokio::test]
async fn libtonode_send_shield_cycle() {
send_shield_cycle::<LibtonodeEnvironment>(4).await;
}

pub(crate) mod libtonode_environment {
use zcash_client_backend::PoolType;
Expand Down
47 changes: 41 additions & 6 deletions zingo-testutils/src/chain_generic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ pub mod fixtures {
use crate::chain_generic_tests::conduct_chain::ConductChain;

/// runs a send-to-receiver and receives it in a chain-generic context
pub async fn propose_and_broadcast_value_to_pool<TE>(send_value: u64, pooltype: PoolType)
pub async fn propose_and_broadcast_value_to_pool<CC>(send_value: u64, pooltype: PoolType)
where
TE: ConductChain,
CC: ConductChain,
{
let mut environment = TE::setup().await;
let mut environment = CC::setup().await;

println!("chain set up, funding client now");

Expand Down Expand Up @@ -130,12 +130,47 @@ pub mod fixtures {
);
}

/// sends back and forth several times, including sends to transparent
pub async fn send_shield_cycle<CC>(n: u64)
where
CC: ConductChain,
{
let mut environment = CC::setup().await;
let primary = environment
.fund_client(1_000_000 + (n + 6) * MARGINAL_FEE.into_u64())
.await;
let primary_address = primary.get_base_address(Shielded(Orchard)).await;

let secondary = environment.create_client().await;
let secondary_address = secondary.get_base_address(Transparent).await;

for _ in 0..n {
primary
.send_from_send_inputs(vec![(secondary_address.as_str(), 100_000, None)])
.await
.unwrap();
environment.bump_chain().await;
secondary.do_sync(false).await.unwrap();
dbg!(secondary.do_balance().await);
secondary.quick_shield().await.unwrap();
environment.bump_chain().await;
secondary.do_sync(false).await.unwrap();
dbg!(secondary.do_balance().await);
secondary
.send_from_send_inputs(vec![(primary_address.as_str(), 50_000, None)])
.await
.unwrap();
primary.do_sync(false).await.unwrap();
dbg!(primary.do_balance().await);
}
}

/// creates a proposal, sends it and receives it (upcoming: compares that it was executed correctly) in a chain-generic context
pub async fn send_value_to_pool<TE>(send_value: u64, pooltype: PoolType)
pub async fn send_value_to_pool<CC>(send_value: u64, pooltype: PoolType)
where
TE: ConductChain,
CC: ConductChain,
{
let mut environment = TE::setup().await;
let mut environment = CC::setup().await;

dbg!("chain set up, funding client now");

Expand Down
1 change: 1 addition & 0 deletions zingolib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ getset = "0.1.2"
test-case = "3.3.1"
proptest = "1.4.0"
thiserror = { workspace = true }
hdwallet = "0.4.1"

[dev-dependencies]
portpicker = "0.1.0"
Expand Down
12 changes: 9 additions & 3 deletions zingolib/src/blaze/block_management_reorg_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,9 @@ mod tests {
.handle_reorgs_and_populate_block_mangement_data(
start_block,
end_block,
Arc::new(RwLock::new(TxMapAndMaybeTrees::new_with_witness_trees())),
Arc::new(RwLock::new(
TxMapAndMaybeTrees::new_with_witness_trees_address_free(),
)),
reorg_transmitter,
)
.await;
Expand Down Expand Up @@ -890,7 +892,9 @@ mod tests {
.handle_reorgs_and_populate_block_mangement_data(
start_block,
end_block,
Arc::new(RwLock::new(TxMapAndMaybeTrees::new_with_witness_trees())),
Arc::new(RwLock::new(
TxMapAndMaybeTrees::new_with_witness_trees_address_free(),
)),
reorg_transmitter,
)
.await;
Expand Down Expand Up @@ -986,7 +990,9 @@ mod tests {
.handle_reorgs_and_populate_block_mangement_data(
start_block,
end_block,
Arc::new(RwLock::new(TxMapAndMaybeTrees::new_with_witness_trees())),
Arc::new(RwLock::new(
TxMapAndMaybeTrees::new_with_witness_trees_address_free(),
)),
reorg_transmitter,
)
.await;
Expand Down
28 changes: 7 additions & 21 deletions zingolib/src/lightclient/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,13 @@ impl LightClient {
Ok(proposal)
}

fn get_transparent_addresses(
&self,
) -> Result<Vec<zcash_primitives::legacy::TransparentAddress>, ProposeShieldError> {
let secp = secp256k1::Secp256k1::new();
Ok(self
.wallet
fn get_transparent_addresses(&self) -> Vec<zcash_primitives::legacy::TransparentAddress> {
self.wallet
.wallet_capability()
.transparent_child_keys()
.map_err(|_e| {
ProposeShieldError::Component(
zcash_client_backend::data_api::error::Error::DataSource(
TxMapAndMaybeTreesTraitError::NoSpendCapability,
),
)
})?
.transparent_child_addresses()
.iter()
.map(|(_index, sk)| {
#[allow(deprecated)]
zcash_primitives::legacy::keys::pubkey_to_address(&sk.public_key(&secp))
})
.collect::<Vec<_>>())
.map(|(_index, sk)| *sk)
.collect::<Vec<_>>()
}

/// The shield operation consumes a proposal that transfers value
Expand Down Expand Up @@ -208,7 +194,7 @@ impl LightClient {
&input_selector,
// don't shield dust
NonNegativeAmount::const_from_u64(10_000),
&self.get_transparent_addresses()?,
&self.get_transparent_addresses(),
// review! do we want to require confirmations?
// make it configurable?
0,
Expand Down Expand Up @@ -257,7 +243,7 @@ mod shielding {
async fn get_transparent_addresses() {
let basic_client = create_basic_client().await;
assert_eq!(
basic_client.get_transparent_addresses().unwrap(),
basic_client.get_transparent_addresses(),
[zcash_primitives::legacy::TransparentAddress::PublicKeyHash(
[
161, 138, 222, 242, 254, 121, 71, 105, 93, 131, 177, 31, 59, 185, 120, 148,
Expand Down
21 changes: 20 additions & 1 deletion zingolib/src/lightclient/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ impl LightClient {
pub mod send_with_proposal {
use std::{convert::Infallible, ops::DerefMut as _};

use hdwallet::traits::Deserialize as _;
use nonempty::NonEmpty;

use zcash_client_backend::proposal::Proposal;
use secp256k1::SecretKey;
use zcash_client_backend::wallet::NoteId;
use zcash_client_backend::zip321::TransactionRequest;
use zcash_client_backend::{proposal::Proposal, wallet::TransparentAddressMetadata};
use zcash_keys::keys::UnifiedSpendingKey;
use zcash_primitives::transaction::TxId;

Expand Down Expand Up @@ -248,6 +250,22 @@ pub mod send_with_proposal {

let step = proposal.steps().first();

// The 'UnifiedSpendingKey' we create is not a 'proper' USK, in that the
// transparent key it contains is not the account spending key, but the
// externally-scoped derivative key. The goal is to fix this, but in the
// interim we use this special-case logic.
fn usk_to_tkey(
unified_spend_key: &UnifiedSpendingKey,
t_metadata: &TransparentAddressMetadata,
) -> SecretKey {
hdwallet::ExtendedPrivKey::deserialize(&unified_spend_key.transparent().to_bytes())
.expect("This a hack to do a type conversion, and will not fail")
.derive_private_key(t_metadata.address_index().into())
// This is unwrapped in librustzcash, so I'm not too worried about it
.expect("private key derivation failed")
.private_key
}

let build_result =
zcash_client_backend::data_api::wallet::calculate_proposed_transaction(
self.wallet
Expand All @@ -265,6 +283,7 @@ pub mod send_with_proposal {
proposal.min_target_height(),
&[],
step,
Some(usk_to_tkey),
)
.map_err(CompleteAndBroadcastError::Calculation)?;
let txid = self
Expand Down
8 changes: 6 additions & 2 deletions zingolib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,13 @@ impl LightWallet {
));
};
let transaction_metadata_set = if wc.can_spend_from_all_pools() {
Arc::new(RwLock::new(TxMapAndMaybeTrees::new_with_witness_trees()))
Arc::new(RwLock::new(TxMapAndMaybeTrees::new_with_witness_trees(
wc.transparent_child_addresses().clone(),
)))
} else {
Arc::new(RwLock::new(TxMapAndMaybeTrees::new_treeless()))
Arc::new(RwLock::new(TxMapAndMaybeTrees::new_treeless(
wc.transparent_child_addresses().clone(),
)))
};
let transaction_context =
TransactionContext::new(&config, Arc::new(wc), transaction_metadata_set);
Expand Down
10 changes: 5 additions & 5 deletions zingolib/src/wallet/keys/extended_transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ impl KeyIndex {
}

/// Generate KeyIndex from raw index value.
pub fn from_index(i: u32) -> Result<Self, Error> {
pub fn from_index(i: u32) -> Self {
if i < HARDENED_KEY_START_INDEX {
Ok(KeyIndex::Normal(i))
KeyIndex::Normal(i)
} else {
Ok(KeyIndex::Hardened(i))
KeyIndex::Hardened(i)
}
}
}

impl From<u32> for KeyIndex {
fn from(index: u32) -> Self {
KeyIndex::from_index(index).expect("KeyIndex")
KeyIndex::from_index(index)
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ fn test_commutativity_of_key_derivation_mechanisms() {
// pk ---> pk_i

// initial key derivation material
let i = KeyIndex::from_index(42).unwrap();
let i = KeyIndex::from_index(42);
let sk = ExtendedPrivKey::with_seed(&[0xcd; 64]).unwrap();

// sk -> sk_i -> pk_i derivation
Expand Down
Loading

0 comments on commit bb265da

Please sign in to comment.