Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykoren committed Dec 12, 2024
1 parent bf7a6ec commit 3e3954c
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 75 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The **Zcash Transaction Tool** is designed to create and send Zcash transactions

This repository includes a simple Zebra Docker image that incorporates the OrchardZSA version of Zebra and runs in regtest mode.

WARNING: This tool is not a wallet and should not be used as a wallet. This tool is in the early stages of development and should not be used in production environments.

## Table of Contents

- [Features](#features)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use abscissa_core::{Command, Runnable};

use crate::components::wallet::Wallet;
use crate::components::user::User;
use crate::prelude::*;

/// Clean state
Expand All @@ -13,7 +13,7 @@ impl Runnable for CleanCmd {
/// Run the `clean` subcommand.
fn run(&self) {
let config = APP.config();
let mut wallet = Wallet::new(&config.wallet.seed_phrase, &config.wallet.miner_seed_phrase);
let mut wallet = User::new(&config.wallet.seed_phrase, &config.wallet.miner_seed_phrase);

wallet.reset();
}
Expand Down
23 changes: 16 additions & 7 deletions src/commands/test_balances.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::components::wallet::Wallet;
use crate::components::user::User;
use crate::prelude::info;
use orchard::keys::Scope::External;
use orchard::note::AssetBase;
Expand All @@ -14,11 +14,11 @@ impl TestBalances {
TestBalances { account0, account1 }
}

pub(crate) fn get_zec(wallet: &mut Wallet) -> TestBalances {
Self::get_asset(AssetBase::native(), wallet)
pub(crate) fn get_zec(user: &mut User) -> TestBalances {
Self::get_asset(AssetBase::native(), user)
}

pub(crate) fn get_asset(asset: AssetBase, wallet: &mut Wallet) -> TestBalances {
pub(crate) fn get_asset(asset: AssetBase, wallet: &mut User) -> TestBalances {
let address0 = wallet.address_for_account(0, External);
let address1 = wallet.address_for_account(1, External);

Expand All @@ -37,9 +37,9 @@ pub(crate) fn check_balances(
asset: AssetBase,
initial: TestBalances,
expected_delta: TestBalances,
wallet: &mut Wallet,
user: &mut User,
) -> TestBalances {
let actual_balances = TestBalances::get_asset(asset, wallet);
let actual_balances = TestBalances::get_asset(asset, user);
print_balances(header, asset, actual_balances);
assert_eq!(
actual_balances.account0,
Expand All @@ -54,7 +54,16 @@ pub(crate) fn check_balances(

pub(crate) fn print_balances(header: &str, asset: AssetBase, balances: TestBalances) {
info!("{}", header);
info!("AssetBase: {}", hex::encode(asset.to_bytes()).as_str());
if asset.is_native().into() {
info!("AssetBase: Native ZEC");
} else {
let trimmed_asset_base = hex::encode(asset.to_bytes())
.as_str()
.chars()
.take(8)
.collect::<String>();
info!("AssetBase: {}", trimmed_asset_base);
}
info!("Account 0 balance: {}", balances.account0);
info!("Account 1 balance: {}", balances.account1);
}
10 changes: 3 additions & 7 deletions src/commands/test_orchard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::components::transactions::mine;
use crate::components::transactions::{
create_shield_coinbase_transaction, mine_empty_blocks, sync_from_height,
};
use crate::components::wallet::Wallet;
use crate::components::user::User;
use crate::prelude::*;

/// Run the E2E test
Expand All @@ -24,7 +24,7 @@ impl Runnable for TestOrchardCmd {
fn run(&self) {
let config = APP.config();
let mut rpc_client = ReqwestRpcClient::new(config.network.node_url());
let mut wallet = Wallet::random(&config.wallet.miner_seed_phrase);
let mut wallet = User::random(&config.wallet.miner_seed_phrase);

wallet.reset();

Expand Down Expand Up @@ -88,11 +88,7 @@ impl Runnable for TestOrchardCmd {
}
}

fn prepare_test(
target_height: u32,
wallet: &mut Wallet,
rpc_client: &mut ReqwestRpcClient,
) -> TxId {
fn prepare_test(target_height: u32, wallet: &mut User, rpc_client: &mut ReqwestRpcClient) -> TxId {
sync_from_height(target_height, wallet, rpc_client);
let activate = wallet.last_block_height().is_none();
let (_, coinbase_txid) = mine_empty_blocks(100, rpc_client, activate); // coinbase maturity = 100
Expand Down
14 changes: 7 additions & 7 deletions src/commands/test_orchard_zsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::components::transactions::sync_from_height;
use crate::components::transactions::{
create_burn_transaction, create_issue_transaction, create_transfer_transaction, mine,
};
use crate::components::wallet::Wallet;
use crate::components::user::User;
use crate::prelude::*;

/// Run the E2E test
Expand All @@ -21,7 +21,7 @@ impl Runnable for TestOrchardZSACmd {
fn run(&self) {
let config = APP.config();
let mut rpc_client = ReqwestRpcClient::new(config.network.node_url());
let mut wallet = Wallet::random(&config.wallet.miner_seed_phrase);
let mut wallet = User::random(&config.wallet.miner_seed_phrase);

wallet.reset();

Expand Down Expand Up @@ -122,17 +122,17 @@ impl Runnable for TestOrchardZSACmd {

// --------------------- Finalization ---------------------
// TODO - uncomment when finalization is implemented
// let finalization_tx = create_finalization_transaction(asset_description.clone(), &mut wallet);
// let finalization_tx = create_finalization_transaction(asset_description.clone(), &mut user);
// mine(
// &mut wallet,
// &mut user,
// &mut rpc_client,
// Vec::from([finalization_tx]),
// false,
// );
//
// let invalid_issue_tx = create_issue_transaction(issuer, 2000, asset_description, &mut wallet);
// let invalid_issue_tx = create_issue_transaction(issuer, 2000, asset_description, &mut user);
// mine(
// &mut wallet,
// &mut user,
// &mut rpc_client,
// Vec::from([invalid_issue_tx]),
// false,
Expand All @@ -142,6 +142,6 @@ impl Runnable for TestOrchardZSACmd {
}
}

fn prepare_test(target_height: u32, wallet: &mut Wallet, rpc_client: &mut ReqwestRpcClient) {
fn prepare_test(target_height: u32, wallet: &mut User, rpc_client: &mut ReqwestRpcClient) {
sync_from_height(target_height, wallet, rpc_client);
}
2 changes: 1 addition & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod persistence;
pub mod rpc_client;
pub mod wallet;
pub mod user;
pub mod zebra_merkle;

pub mod transactions;
2 changes: 1 addition & 1 deletion src/components/rpc_client/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl RpcClient for MockZcashNode {
.get(txid)
.ok_or(io::Error::new(ErrorKind::NotFound, "Transaction not found").into())
.map(|tx_string| {
Transaction::read(&hex::decode(tx_string).unwrap()[..], BranchId::Nu5).unwrap()
Transaction::read(&hex::decode(tx_string).unwrap()[..], BranchId::Nu6).unwrap()
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/rpc_client/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl RpcClient for ReqwestRpcClient {
let tx_hex: String =
self.request(&RpcRequest::new_with_params("getrawtransaction", params))?;
let tx_bytes = hex::decode(tx_hex).unwrap();
Ok(Transaction::read(tx_bytes.as_slice(), BranchId::Nu5).unwrap())
Ok(Transaction::read(tx_bytes.as_slice(), BranchId::Nu6).unwrap())
}

fn get_block_template(&self) -> Result<BlockTemplate, Box<dyn Error>> {
Expand Down
44 changes: 21 additions & 23 deletions src/components/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::rpc_client::{BlockProposal, BlockTemplate, RpcClient};
use crate::components::wallet::Wallet;
use crate::components::user::User;
use crate::components::zebra_merkle::{
block_commitment_from_parts, AuthDataRoot, Root, AUTH_COMMITMENT_PLACEHOLDER,
};
Expand All @@ -21,28 +21,27 @@ use zcash_primitives::transaction::fees::zip317::{FeeError, FeeRule};
use zcash_primitives::transaction::{Transaction, TxId};
use zcash_proofs::prover::LocalTxProver;

/// Mine a block with the given transactions and sync the wallet
/// Mine a block with the given transactions and sync the user
pub fn mine(
wallet: &mut Wallet,
wallet: &mut User,
rpc_client: &mut dyn RpcClient,
txs: Vec<Transaction>,
activate: bool,
) {
let (_, _) = mine_block(rpc_client, BranchId::Nu5, txs, activate);
let (_, _) = mine_block(rpc_client, txs, activate);
sync(wallet, rpc_client);
}

/// Mine a block with the given transactions and return the block height and coinbase txid
pub fn mine_block(
rpc_client: &mut dyn RpcClient,
branch_id: BranchId,
txs: Vec<Transaction>,
activate: bool,
) -> (u32, TxId) {
let block_template = rpc_client.get_block_template().unwrap();
let block_height = block_template.height;

let block_proposal = template_into_proposal(block_template, branch_id, txs, activate);
let block_proposal = template_into_proposal(block_template, txs, activate);
let coinbase_txid = block_proposal.transactions.first().unwrap().txid();

rpc_client.submit_block(block_proposal).unwrap();
Expand All @@ -60,10 +59,10 @@ pub fn mine_empty_blocks(
panic!("num_blocks must be greater than 0")
}

let (block_height, coinbase_txid) = mine_block(rpc_client, BranchId::Nu5, vec![], activate);
let (block_height, coinbase_txid) = mine_block(rpc_client, vec![], activate);

for _ in 1..num_blocks {
mine_block(rpc_client, BranchId::Nu5, vec![], false);
mine_block(rpc_client, vec![], false);
}

(block_height, coinbase_txid)
Expand All @@ -73,7 +72,7 @@ pub fn mine_empty_blocks(
pub fn create_shield_coinbase_transaction(
recipient: Address,
coinbase_txid: TxId,
wallet: &mut Wallet,
wallet: &mut User,
) -> Transaction {
info!("Shielding coinbase output from tx {}", coinbase_txid);

Expand Down Expand Up @@ -106,17 +105,17 @@ pub fn create_shield_coinbase_transaction(
build_tx(tx)
}

/// Sync the wallet with the node
pub fn sync(wallet: &mut Wallet, rpc: &mut dyn RpcClient) {
/// Sync the user with the node
pub fn sync(wallet: &mut User, rpc: &mut dyn RpcClient) {
let current_height = match wallet.last_block_height() {
None => 0,
Some(height) => height.add(1).into(),
};
sync_from_height(current_height, wallet, rpc);
}

/// Sync the wallet with the node from the given height
pub fn sync_from_height(from_height: u32, wallet: &mut Wallet, rpc: &mut dyn RpcClient) {
/// Sync the user with the node from the given height
pub fn sync_from_height(from_height: u32, wallet: &mut User, rpc: &mut dyn RpcClient) {
info!("Starting sync from height {}", from_height);

let wallet_last_block_height = wallet.last_block_height().map_or(0, |h| h.into());
Expand All @@ -129,8 +128,8 @@ pub fn sync_from_height(from_height: u32, wallet: &mut Wallet, rpc: &mut dyn Rpc
loop {
match rpc.get_block(next_height) {
Ok(block) => {
// if block.prev_hash != wallet.last_block_hash
// Fork management is not implemented since block.prev_hash rpc is not yet implemented in Zebra
// if block.prev_hash != user.last_block_hash
// Fork management is not implemented

info!(
"Adding transactions from block {} at height {}",
Expand Down Expand Up @@ -165,7 +164,7 @@ pub fn create_transfer_transaction(
recipient: Address,
amount: u64,
asset: AssetBase,
wallet: &mut Wallet,
wallet: &mut User,
) -> Transaction {
info!("Transfer {} zatoshi", amount);

Expand Down Expand Up @@ -221,7 +220,7 @@ pub fn create_burn_transaction(
arsonist: Address,
amount: u64,
asset: AssetBase,
wallet: &mut Wallet,
wallet: &mut User,
) -> Transaction {
info!("Burn {} zatoshi", amount);

Expand Down Expand Up @@ -268,7 +267,7 @@ pub fn create_issue_transaction(
recipient: Address,
amount: u64,
asset_desc: Vec<u8>,
wallet: &mut Wallet,
wallet: &mut User,
) -> Transaction {
info!("Issue {} asset", amount);
let mut tx = create_tx(wallet);
Expand All @@ -285,7 +284,7 @@ pub fn create_issue_transaction(
}

/// Create a transaction that issues a new asset
pub fn create_finalization_transaction(asset_desc: Vec<u8>, wallet: &mut Wallet) -> Transaction {
pub fn create_finalization_transaction(asset_desc: Vec<u8>, wallet: &mut User) -> Transaction {
info!("Finalize asset");
let mut tx = create_tx(wallet);
tx.init_issuance_bundle::<FeeError>(wallet.issuance_key(), asset_desc.clone(), None)
Expand All @@ -298,15 +297,14 @@ pub fn create_finalization_transaction(asset_desc: Vec<u8>, wallet: &mut Wallet)
/// Convert a block template and a list of transactions into a block proposal
pub fn template_into_proposal(
block_template: BlockTemplate,
branch_id: BranchId,
mut txs: Vec<Transaction>,
activate: bool,
) -> BlockProposal {
let coinbase = Transaction::read(
hex::decode(block_template.coinbase_txn.data)
.unwrap()
.as_slice(),
branch_id,
BranchId::Nu6,
)
.unwrap();

Expand Down Expand Up @@ -366,14 +364,14 @@ pub fn template_into_proposal(
}
}

fn create_tx(wallet: &Wallet) -> Builder<'_, RegtestNetwork, ()> {
fn create_tx(wallet: &User) -> Builder<'_, RegtestNetwork, ()> {
let build_config = BuildConfig::Zsa {
sapling_anchor: None,
orchard_anchor: wallet.orchard_anchor(),
};
let tx = Builder::new(
REGTEST_NETWORK,
/*wallet.last_block_height().unwrap()*/ BlockHeight::from_u32(1_842_420),
/*user.last_block_height().unwrap()*/ BlockHeight::from_u32(1_842_420),
build_config,
);
tx
Expand Down
Loading

0 comments on commit 3e3954c

Please sign in to comment.