|
|
@@ -21,8 +21,8 @@ use std::{env, str::FromStr};
|
|
|
use darkfi::{tx::Transaction, Result};
|
|
|
use darkfi_sdk::{
|
|
|
crypto::{
|
|
|
- merkle_prelude::*, pallas, pasta_prelude::*, poseidon_hash, MerkleNode, Nullifier, TokenId,
|
|
|
- MONEY_CONTRACT_ID,
|
|
|
+ merkle_prelude::*, pallas, pasta_prelude::*, poseidon_hash, Coin, MerkleNode, Nullifier,
|
|
|
+ TokenId, MONEY_CONTRACT_ID,
|
|
|
},
|
|
|
ContractCall,
|
|
|
};
|
|
|
@@ -31,8 +31,8 @@ use log::info;
|
|
|
use rand::{prelude::IteratorRandom, rngs::OsRng, Rng};
|
|
|
|
|
|
use darkfi_money_contract::{
|
|
|
- client::{build_transfer_tx, Coin, EncryptedNote, OwnCoin},
|
|
|
- MoneyFunction,
|
|
|
+ client::{transfer_v1::TransferCallBuilder, MoneyNote, OwnCoin},
|
|
|
+ MoneyFunction::TransferV1 as MoneyTransfer,
|
|
|
};
|
|
|
|
|
|
mod harness;
|
|
|
@@ -56,26 +56,33 @@ async fn alice2alice_random_amounts() -> Result<()> {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ // Initialize harness
|
|
|
let mut th = MoneyTestHarness::new().await?;
|
|
|
- let token_id = TokenId::from(pallas::Base::random(&mut OsRng));
|
|
|
- let contract_id = *MONEY_CONTRACT_ID;
|
|
|
|
|
|
- let mut owncoins = vec![];
|
|
|
-
|
|
|
- let (airdrop_tx, airdrop_params) = th.airdrop(ALICE_AIRDROP, token_id, &th.alice_kp.public)?;
|
|
|
+ info!(target: "money", "[Faucet] ===================================================");
|
|
|
+ info!(target: "money", "[Faucet] Building Money::Transfer params for Alice's airdrop");
|
|
|
+ info!(target: "money", "[Faucet] ===================================================");
|
|
|
+ let contract_id = *MONEY_CONTRACT_ID;
|
|
|
+ let (airdrop_tx, airdrop_params) = th.airdrop(ALICE_AIRDROP, th.alice_kp.public)?;
|
|
|
|
|
|
+ info!(target: "money", "[Faucet] ==========================");
|
|
|
+ info!(target: "money", "[Faucet] Executing Alice airdrop tx");
|
|
|
+ info!(target: "money", "[Faucet] ==========================");
|
|
|
th.faucet_state.read().await.verify_transactions(&[airdrop_tx.clone()], true).await?;
|
|
|
- th.faucet_merkle_tree.append(&MerkleNode::from(airdrop_params.outputs[0].coin));
|
|
|
-
|
|
|
+ th.faucet_merkle_tree.append(&MerkleNode::from(airdrop_params.outputs[0].coin.inner()));
|
|
|
+ info!(target: "money", "[Alice] ==========================");
|
|
|
+ info!(target: "money", "[Alice] Executing Alice airdrop tx");
|
|
|
+ info!(target: "money", "[Alice] ==========================");
|
|
|
th.alice_state.read().await.verify_transactions(&[airdrop_tx.clone()], true).await?;
|
|
|
- th.alice_merkle_tree.append(&MerkleNode::from(airdrop_params.outputs[0].coin));
|
|
|
- let leaf_position = th.alice_merkle_tree.witness().unwrap();
|
|
|
+ th.alice_merkle_tree.append(&MerkleNode::from(airdrop_params.outputs[0].coin.inner()));
|
|
|
|
|
|
- let ciphertext = airdrop_params.outputs[0].ciphertext.clone();
|
|
|
- let ephem_public = airdrop_params.outputs[0].ephem_public;
|
|
|
- let e_note = EncryptedNote { ciphertext, ephem_public };
|
|
|
- let note = e_note.decrypt(&th.alice_kp.secret)?;
|
|
|
+ assert!(th.faucet_merkle_tree.root(0).unwrap() == th.alice_merkle_tree.root(0).unwrap());
|
|
|
|
|
|
+ // Gather new owncoins
|
|
|
+ let mut owncoins = vec![];
|
|
|
+ let leaf_position = th.alice_merkle_tree.witness().unwrap();
|
|
|
+ let note: MoneyNote = airdrop_params.outputs[0].note.decrypt(&th.alice_kp.secret)?;
|
|
|
+ let token_id = note.token_id;
|
|
|
owncoins.push(OwnCoin {
|
|
|
coin: Coin::from(airdrop_params.outputs[0].coin),
|
|
|
note: note.clone(),
|
|
|
@@ -84,35 +91,48 @@ async fn alice2alice_random_amounts() -> Result<()> {
|
|
|
leaf_position,
|
|
|
});
|
|
|
|
|
|
+ // Execute transactions loop
|
|
|
for i in 0..n {
|
|
|
- info!(target: "money", "Building Alice2Alice transfer tx {}", i);
|
|
|
-
|
|
|
- info!(target: "money", "Alice coins: {}", owncoins.len());
|
|
|
+ info!(target: "money", "[Alice] ===============================================");
|
|
|
+ info!(target: "money", "[Alice] Building Money::Transfer params for transfer {}", i);
|
|
|
+ info!(target: "money", "[Alice] Alice coins: {}", owncoins.len());
|
|
|
for (i, c) in owncoins.iter().enumerate() {
|
|
|
- info!(target: "money", "\t coin {} value: {}", i, c.note.value);
|
|
|
+ info!(target: "money", "[Alice] \t coin {} value: {}", i, c.note.value);
|
|
|
}
|
|
|
-
|
|
|
let amount = rand::thread_rng().gen_range(1..ALICE_AIRDROP);
|
|
|
- info!(target: "money", "Sending: {}", amount);
|
|
|
-
|
|
|
- let (params, proofs, secret_keys, spent_coins) = build_transfer_tx(
|
|
|
- &th.alice_kp,
|
|
|
- &th.alice_kp.public,
|
|
|
- amount,
|
|
|
+ info!(target: "money", "[Alice] Sending: {}", amount);
|
|
|
+ info!(target: "money", "[Alice] ===============================================");
|
|
|
+ let call_debris = TransferCallBuilder {
|
|
|
+ keypair: th.alice_kp,
|
|
|
+ recipient: th.alice_kp.public,
|
|
|
+ value: amount,
|
|
|
token_id,
|
|
|
- pallas::Base::zero(),
|
|
|
- pallas::Base::zero(),
|
|
|
- pallas::Base::random(&mut OsRng),
|
|
|
- &owncoins,
|
|
|
- &th.alice_merkle_tree,
|
|
|
- &th.mint_zkbin,
|
|
|
- &th.mint_pk,
|
|
|
- &th.burn_zkbin,
|
|
|
- &th.burn_pk,
|
|
|
- false,
|
|
|
- )?;
|
|
|
-
|
|
|
- let mut data = vec![MoneyFunction::Transfer as u8];
|
|
|
+ rcpt_spend_hook: pallas::Base::zero(),
|
|
|
+ rcpt_user_data: pallas::Base::zero(),
|
|
|
+ rcpt_user_data_blind: pallas::Base::random(&mut OsRng),
|
|
|
+ change_spend_hook: pallas::Base::zero(),
|
|
|
+ change_user_data: pallas::Base::zero(),
|
|
|
+ change_user_data_blind: pallas::Base::random(&mut OsRng),
|
|
|
+ coins: owncoins.clone(),
|
|
|
+ tree: th.alice_merkle_tree.clone(),
|
|
|
+ mint_zkbin: th.mint_zkbin.clone(),
|
|
|
+ mint_pk: th.mint_pk.clone(),
|
|
|
+ burn_zkbin: th.burn_zkbin.clone(),
|
|
|
+ burn_pk: th.burn_pk.clone(),
|
|
|
+ clear_input: false,
|
|
|
+ }
|
|
|
+ .build()?;
|
|
|
+ let (params, proofs, secret_keys, spent_coins) = (
|
|
|
+ call_debris.params,
|
|
|
+ call_debris.proofs,
|
|
|
+ call_debris.signature_secrets,
|
|
|
+ call_debris.spent_coins,
|
|
|
+ );
|
|
|
+
|
|
|
+ info!(target: "money", "[Alice] ============================");
|
|
|
+ info!(target: "money", "[Alice] Building payment tx to Alice");
|
|
|
+ info!(target: "money", "[Alice] ============================");
|
|
|
+ let mut data = vec![MoneyTransfer as u8];
|
|
|
params.encode(&mut data)?;
|
|
|
let calls = vec![ContractCall { contract_id, data }];
|
|
|
let proofs = vec![proofs];
|
|
|
@@ -125,18 +145,22 @@ async fn alice2alice_random_amounts() -> Result<()> {
|
|
|
owncoins.retain(|x| x != &spent);
|
|
|
}
|
|
|
|
|
|
- // Apply the state transition
|
|
|
+ // Verify transaction
|
|
|
+ info!(target: "money", "[Faucet] ================================");
|
|
|
+ info!(target: "money", "[Faucet] Executing Alice2Alice payment tx");
|
|
|
+ info!(target: "money", "[Faucet] ================================");
|
|
|
+ th.faucet_state.read().await.verify_transactions(&[tx.clone()], true).await?;
|
|
|
+ for output in ¶ms.outputs {
|
|
|
+ th.faucet_merkle_tree.append(&MerkleNode::from(output.coin.inner()));
|
|
|
+ }
|
|
|
+ info!(target: "money", "[Alice] ================================");
|
|
|
+ info!(target: "money", "[Alice] Executing Alice2Alice payment tx");
|
|
|
+ info!(target: "money", "[Alice] ================================");
|
|
|
th.alice_state.read().await.verify_transactions(&[tx.clone()], true).await?;
|
|
|
-
|
|
|
- // Gather new owncoins
|
|
|
+ // Gather new owncoins and apply the state transitions
|
|
|
for output in params.outputs {
|
|
|
- let e_note = EncryptedNote {
|
|
|
- ciphertext: output.ciphertext.clone(),
|
|
|
- ephem_public: output.ephem_public,
|
|
|
- };
|
|
|
- let note = e_note.decrypt(&th.alice_kp.secret)?;
|
|
|
-
|
|
|
- th.alice_merkle_tree.append(&MerkleNode::from(output.coin));
|
|
|
+ th.alice_merkle_tree.append(&MerkleNode::from(output.coin.inner()));
|
|
|
+ let note: MoneyNote = output.note.decrypt(&th.alice_kp.secret)?;
|
|
|
let leaf_position = th.alice_merkle_tree.witness().unwrap();
|
|
|
|
|
|
let owncoin = OwnCoin {
|
|
|
@@ -152,11 +176,14 @@ async fn alice2alice_random_amounts() -> Result<()> {
|
|
|
|
|
|
owncoins.push(owncoin);
|
|
|
}
|
|
|
+
|
|
|
+ assert!(th.faucet_merkle_tree.root(0).unwrap() == th.alice_merkle_tree.root(0).unwrap());
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
#[async_std::test]
|
|
|
async fn alice2alice_random_amounts_multiplecoins() -> Result<()> {
|
|
|
init_logger()?;
|
|
|
@@ -305,3 +332,4 @@ async fn alice2alice_random_amounts_multiplecoins() -> Result<()> {
|
|
|
|
|
|
Ok(())
|
|
|
}
|
|
|
+*/
|