103 lines
3.2 KiB
Rust
Raw Normal View History

use std::collections::BTreeMap;
2024-08-09 15:34:27 +04:00
use cl::{BalanceWitness, NoteWitness, NullifierSecret};
2024-08-23 03:06:15 +04:00
use common::{mmr::MMR, new_account, BoundTx, SignedBoundTx, StateWitness, Tx, ZONE_CL_FUNDS_UNIT};
use executor::ZoneNotes;
use ledger::constraint::ConstraintProof;
2024-08-09 15:34:27 +04:00
#[test]
fn test_deposit() {
let mut rng = rand::thread_rng();
2024-08-17 20:45:56 +04:00
let mut alice = new_account(&mut rng);
let alice_vk = alice.verifying_key().to_bytes();
let alice_cl_sk = NullifierSecret::random(&mut rng);
2024-08-09 15:34:27 +04:00
let zone_start = ZoneNotes::new_with_balances("ZONE", BTreeMap::new(), &mut rng);
2024-08-09 15:34:27 +04:00
let deposit = common::Deposit {
2024-08-17 20:45:56 +04:00
to: alice_vk,
2024-08-09 15:34:27 +04:00
amount: 78,
};
2024-08-23 03:06:15 +04:00
let zone_end = zone_start.clone().run(Tx::Deposit(deposit)).0;
2024-08-09 15:34:27 +04:00
let alice_deposit = cl::InputWitness::from_output(
2024-08-27 01:32:53 +04:00
cl::OutputWitness::new(
NoteWitness::stateless(
2024-08-09 15:34:27 +04:00
78,
*ZONE_CL_FUNDS_UNIT,
ConstraintProof::nop_constraint(), // alice should demand a tx inclusion proof for the deposit
2024-08-27 01:32:53 +04:00
&mut rng,
2024-08-09 15:34:27 +04:00
),
2024-08-17 20:45:56 +04:00
alice_cl_sk.commit(),
2024-08-09 15:34:27 +04:00
),
2024-08-17 20:45:56 +04:00
alice_cl_sk,
2024-08-09 15:34:27 +04:00
);
let deposit_ptx = cl::PartialTxWitness {
inputs: vec![zone_start.state_input_witness(), alice_deposit],
outputs: vec![zone_end.state_note, zone_end.fund_note],
balance_blinding: BalanceWitness::random_blinding(&mut rng),
2024-08-09 15:34:27 +04:00
};
2024-08-17 20:45:56 +04:00
let signed_deposit = SignedBoundTx::sign(
BoundTx {
tx: Tx::Deposit(deposit),
bind: alice_deposit.note_commitment(),
},
&mut alice,
);
let constraint_proofs = BTreeMap::from_iter([
2024-08-09 15:34:27 +04:00
(
zone_start.state_input_witness().nullifier(),
2024-08-09 15:34:27 +04:00
executor::prove_zone_stf(
zone_start.state.clone(),
2024-08-17 20:45:56 +04:00
vec![(signed_deposit, deposit_ptx.input_witness(1))], // bind it to the deposit note)],
deposit_ptx.input_witness(0), // input state note (input #0)
2024-08-09 15:34:27 +04:00
deposit_ptx.output_witness(0), // output state note (output #0)
deposit_ptx.output_witness(1), // output funds note (output #1)
),
),
(
alice_deposit.nullifier(),
ledger::ConstraintProof::prove_nop(
alice_deposit.nullifier(),
deposit_ptx.commit().root(),
),
2024-08-09 15:34:27 +04:00
),
]);
let note_commitments = vec![
zone_start.state_note.commit_note(),
2024-08-09 15:34:27 +04:00
alice_deposit.note_commitment(),
];
let deposit_proof = ledger::partial_tx::ProvedPartialTx::prove(
&deposit_ptx,
constraint_proofs,
&note_commitments,
)
.expect("deposit proof failed");
2024-08-09 15:34:27 +04:00
assert!(deposit_proof.verify());
assert_eq!(deposit_proof.ptx.outputs[0], zone_end.state_note.commit());
assert_eq!(
zone_end.state_note.note.state,
2024-08-09 15:34:27 +04:00
StateWitness {
2024-08-17 20:45:56 +04:00
balances: BTreeMap::from_iter([(alice_vk, 78)]),
2024-08-23 03:06:15 +04:00
included_txs: {
let mut mmr = MMR::new();
mmr.push(&Tx::Deposit(deposit).to_bytes());
mmr
},
zone_metadata: zone_start.state.zone_metadata,
2024-08-09 15:34:27 +04:00
}
.commit()
.0
);
assert!(deposit_ptx.balance().is_zero());
2024-08-09 15:34:27 +04:00
}