From 56ca685333c5291d5dceef521348e819da948eff Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Thu, 22 Feb 2024 14:51:08 -0500 Subject: [PATCH] =?UTF-8?q?mock-consensus:=20=E2=AD=90=20use=20transaction?= =?UTF-8?q?=20plan=20to=20add=20spends?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #3588. * #3788. * #3857 --- crates/core/app/tests/mock_consensus.rs | 51 ++++++----------------- crates/core/transaction/src/plan/build.rs | 21 ++++++++++ 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/crates/core/app/tests/mock_consensus.rs b/crates/core/app/tests/mock_consensus.rs index e4b12d1a08..12e87b3a5b 100644 --- a/crates/core/app/tests/mock_consensus.rs +++ b/crates/core/app/tests/mock_consensus.rs @@ -93,48 +93,23 @@ async fn mock_consensus_can_send_a_spend_action() -> anyhow::Result<()> { .witness(note.commit()) .ok_or_else(|| anyhow!("index is not witnessed"))?; - // ...and use it to craft a `Spend`. - let (spend, spend_key) = { - use {decaf377_rdsa::SigningKey, penumbra_shielded_pool::SpendPlan}; - let spend_plan = SpendPlan::new(&mut rng, note, proof.position()); - let auth_sig = test_keys::SPEND_KEY - .spend_auth_key() - .randomize(&spend_plan.randomizer) - .sign(&mut rng, [0u8; 64].as_ref()); - let spend = spend_plan.spend(&test_keys::FULL_VIEWING_KEY, auth_sig, proof, sct.root()); - let key = SigningKey::from(spend_plan.value_blinding); - (spend, key) - }; - // Next, craft a transaction, containing this `Spend`. let tx = { use { - penumbra_asset::Value, - penumbra_fee::Fee, - penumbra_num::Amount, - penumbra_transaction::{Action, Transaction, TransactionBody, TransactionParameters}, - penumbra_txhash::AuthorizingData, - }; - let transaction_parameters = TransactionParameters { - expiry_height: 0, - chain_id: "i-wonder-if-this-is-load-bearing".to_owned(), - fee: Fee(Value { - amount: Amount::zero(), - asset_id, - }), + penumbra_proto::DomainType, + penumbra_shielded_pool::SpendPlan, + penumbra_transaction::{Transaction, TransactionPlan}, }; - let transaction_body = TransactionBody { - actions: vec![Action::Spend(spend)], - transaction_parameters, - ..Default::default() - }; - let binding_sig = spend_key.sign(rng, transaction_body.auth_hash().as_bytes()); - let transaction = Transaction { - transaction_body, - binding_sig, - anchor: sct.root(), - }; - ::encode_to_vec(&transaction) + + let (viewing_key, spend_key) = (&test_keys::FULL_VIEWING_KEY, &test_keys::SPEND_KEY); + let mut plan = TransactionPlan::default(); + plan.actions + .push(SpendPlan::new(&mut rng, note, proof.position()).into()); + let witness = plan.witness_data(sct)?; + let auth = plan.authorize(rand_core::OsRng, spend_key)?; + plan.build_concurrent(viewing_key, &witness, &auth) + .await + .map(|tx| tx.encode_to_vec())? }; // Execute the transaction, and sync another mock client up to the latest snapshot. diff --git a/crates/core/transaction/src/plan/build.rs b/crates/core/transaction/src/plan/build.rs index 76d638ed7d..f304e3571e 100644 --- a/crates/core/transaction/src/plan/build.rs +++ b/crates/core/transaction/src/plan/build.rs @@ -190,4 +190,25 @@ impl TransactionPlan { // 4. Return the completed transaction. Ok(tx) } + + /// Returns a [`WitnessData`], which may be used to build this transaction. + pub fn witness_data(&self, sct: penumbra_tct::Tree) -> Result { + let anchor = sct.root(); + + let witness_note = |spend: &penumbra_shielded_pool::SpendPlan| { + let commitment = spend.note.commit(); + sct.witness(commitment) + .ok_or_else(|| anyhow::anyhow!("commitment should exist in tree")) + .map(|proof| (commitment, proof)) + }; + let state_commitment_proofs = self + .spend_plans() + .map(witness_note) + .collect::>()?; + + Ok(WitnessData { + anchor, + state_commitment_proofs, + }) + } }