Skip to content

Commit

Permalink
Regtest network (#5)
Browse files Browse the repository at this point in the history
Use regtest network
  • Loading branch information
alexeykoren authored Oct 29, 2024
1 parent 5fef3cc commit 0e4a83d
Show file tree
Hide file tree
Showing 22 changed files with 545 additions and 325 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/lints.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Lints

# We only run these lints on trial-merges of PRs to reduce noise.
on: pull_request

jobs:

fmt:
name: Rustfmt
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: cargo fmt -- --check

clippy:
name: Clippy (MSRV)
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Clippy
uses: auguwu/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
working-directory: ${{ inputs.target }}
deny: warnings
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 6 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
FROM ubuntu:22.04
FROM rust:1.76.0

# Set up Rust and cargo
RUN apt-get update && apt-get install git build-essential clang curl unzip -y

RUN curl https://sh.rustup.rs -sSf | bash -s -- -y

ENV PATH="/root/.cargo/bin:${PATH}"

# Download and extract the zcash cached chain to /etc/zebra-test
WORKDIR /etc

RUN curl -O https://qedit-public.s3.eu-central-1.amazonaws.com/zsa/zcash-state.zip

RUN unzip zcash-state.zip -d zebra-test
RUN apt-get update && apt-get install git build-essential clang -y

# Checkout and build custom branch of the zebra repository
RUN git clone https://github.com/QED-it/zebra.git

WORKDIR zebra

RUN git switch singlenode-network-txv5
RUN git switch zsa1

RUN cargo build --release --package zebrad --bin zebrad --features="getblocktemplate-rpcs"

EXPOSE 18232

COPY regtest-config.toml regtest-config.toml

# Run the zebra node
ENTRYPOINT ["target/release/zebrad", "-c", "singlenode-config.toml"]
ENTRYPOINT ["target/release/zebrad", "-c", "regtest-config.toml"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Currently, we use a custom Zebra build. There are several changes compared to th
To build and run the docker image:

```bash
docker build -t qedit/zebra-singlenode-txv5 .
docker build -t qedit/zebra-regtest-txv5 .

docker run --name zebra-node -p 18232:18232 qedit/zebra-singlenode-txv5
docker run -p 18232:18232 qedit/zebra-regtest-txv5
```

More details on how the docker file is created and synced: [Link](https://github.com/QED-it/zcash_tx_tool/blob/main/Dockerfile)
Expand Down
10 changes: 0 additions & 10 deletions example_config.toml

This file was deleted.

19 changes: 19 additions & 0 deletions regtest-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[mining]
miner_address = 'tmLTZegcJN5zaufWQBARHkvqC62mTumm3jR'

[network]
network = "Regtest"

# This section may be omitted when testing only Canopy
[network.testnet_parameters.activation_heights]
# Configured activation heights must be greater than or equal to 1,
# block height 0 is reserved for the Genesis network upgrade in Zebra
NU5 = 1

# This section may be omitted if a persistent Regtest chain state is desired
[state]
ephemeral = true

# This section may be omitted if it's not necessary to send transactions to Zebra's mempool
[rpc]
listen_addr = "0.0.0.0:18232"
16 changes: 4 additions & 12 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
mod test;

use crate::commands::test::TestCmd;
use crate::config::AppConfig;
use abscissa_core::{Command, Configurable, FrameworkError, Runnable};
use std::path::PathBuf;
use crate::commands::test::TestCmd;

/// Application Configuration Filename
pub const CONFIG_FILE: &str = "config.toml";

/// Application subcommands need to be listed in an enum.
#[derive(clap::Parser, Command, Debug, Runnable)]
pub enum AppCmd {
Test(TestCmd)
Test(TestCmd),
}

/// Entry point for the application. It needs to be a struct to allow using subcommands!
Expand Down Expand Up @@ -73,15 +73,7 @@ impl Configurable<AppConfig> for EntryPoint {
///
/// This can be safely deleted if you don't want to override config
/// settings from command-line options.
fn process_config(
&self,
config: AppConfig,
) -> Result<AppConfig, FrameworkError> {
match &self.cmd {
// AppCmd::UnconventionalCommand(cmd) => cmd.override_config(config),
// If you don't need special overrides for some
// subcommands, you can just use a catch all
_ => Ok(config),
}
fn process_config(&self, config: AppConfig) -> Result<AppConfig, FrameworkError> {
Ok(config)
}
}
89 changes: 58 additions & 31 deletions src/commands/test.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
//! `test` - happy e2e flow that issues, transfers and burns an asset
use abscissa_core::{Command, Runnable};
use abscissa_core::config::Reader;
use orchard::keys::Scope::External;
use zcash_primitives::transaction::TxId;
use crate::components::transactions::{mine, mine_empty_blocks};
use crate::components::rpc_client::reqwest::ReqwestRpcClient;
use crate::components::transactions::create_shield_coinbase_tx;
use crate::components::transactions::sync_from_height;
use crate::components::transactions::create_transfer_tx;
use crate::components::rpc_client::reqwest::ReqwestRpcClient;
use crate::prelude::*;
use crate::components::transactions::sync_from_height;
use crate::components::transactions::{mine, mine_block, mine_empty_blocks};
use crate::components::wallet::Wallet;
use crate::config::AppConfig;
use crate::prelude::*;
use abscissa_core::config::Reader;
use abscissa_core::{Command, Runnable};
use orchard::keys::Scope::External;
use std::io::{self, Write};

use zcash_primitives::consensus::BranchId;
use zcash_primitives::transaction::TxId;

/// Run the E2E test
#[derive(clap::Parser, Command, Debug)]
pub struct TestCmd {
}
pub struct TestCmd {}

#[derive(Debug, Copy, Clone)]
struct TestBalances {
Expand All @@ -28,14 +27,10 @@ struct TestBalances {

impl TestBalances {
fn new(miner: i64, alice: i64) -> Self {
TestBalances {
miner,
alice,
}
TestBalances { miner, alice }
}

fn get(wallet: &mut Wallet) -> TestBalances {

let miner = wallet.address_for_account(0, External);
let alice = wallet.address_for_account(1, External);

Expand All @@ -44,7 +39,6 @@ impl TestBalances {
alice: wallet.balance(alice) as i64,
}
}

}

impl Runnable for TestCmd {
Expand All @@ -62,47 +56,80 @@ impl Runnable for TestCmd {
let mut balances = TestBalances::get(&mut wallet);
print_balances("=== Initial balances ===", balances);

pause();
// Uncomment the following line to pause the demo
// pause();

// --------------------- Shield miner's reward ---------------------

let shielding_tx = create_shield_coinbase_tx(miner, coinbase_txid, &mut wallet);
mine(&mut wallet, &mut rpc_client, Vec::from([ shielding_tx ]));
mine(&mut wallet, &mut rpc_client, Vec::from([shielding_tx]));

let expected_delta = TestBalances::new(500_000_000 /*coinbase_reward*/, 0);
balances = check_balances("=== Balances after shielding ===", balances, expected_delta, &mut wallet);
// 625_000_000 is the miner's coinbase reward for mining a block
let expected_delta = TestBalances::new(625_000_000, 0);
balances = check_balances(
"=== Balances after shielding ===",
balances,
expected_delta,
&mut wallet,
);

pause();
// Uncomment the following line to pause the demo
// pause();

// --------------------- Create transfer ---------------------

let amount_to_transfer_1: i64 = 2;

let transfer_tx_1 = create_transfer_tx(miner, alice, amount_to_transfer_1 as u64, &mut wallet);
mine(&mut wallet, &mut rpc_client, Vec::from([ transfer_tx_1 ]));
let transfer_tx_1 =
create_transfer_tx(miner, alice, amount_to_transfer_1 as u64, &mut wallet);
mine(&mut wallet, &mut rpc_client, Vec::from([transfer_tx_1]));

let expected_delta = TestBalances::new(-amount_to_transfer_1, amount_to_transfer_1);
check_balances("=== Balances after transfer ===", balances, expected_delta, &mut wallet);

pause();
check_balances(
"=== Balances after transfer ===",
balances,
expected_delta,
&mut wallet,
);

// Uncomment the following line to pause the demo
// pause();
}
}

#[allow(dead_code)]
fn pause() {
print!("Press Enter to continue the demo...");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
}

fn prepare_test(config: &Reader<AppConfig>, wallet: &mut Wallet, rpc_client: &mut ReqwestRpcClient) -> TxId {
fn prepare_test(
config: &Reader<AppConfig>,
wallet: &mut Wallet,
rpc_client: &mut ReqwestRpcClient,
) -> TxId {
wallet.reset();
sync_from_height(config.chain.nu5_activation_height, wallet, rpc_client);
let (_, coinbase_txid) = mine_empty_blocks(100, rpc_client); // coinbase maturity = 100
// mine Nu5 activation block
mine_block(rpc_client, BranchId::Nu5, vec![], true);
// mine 100 blocks to achieve coinbase maturity = 100
let (_, coinbase_txid) = mine_empty_blocks(100, rpc_client);
let height = match wallet.last_block_height() {
Some(bh) => bh.into(),
_ => config.chain.nu5_activation_height,
};
sync_from_height(height, wallet, rpc_client);
coinbase_txid
}

fn check_balances(header: &str, initial: TestBalances, expected_delta: TestBalances, wallet: &mut Wallet) -> TestBalances{
fn check_balances(
header: &str,
initial: TestBalances,
expected_delta: TestBalances,
wallet: &mut Wallet,
) -> TestBalances {
let actual_balances = TestBalances::get(wallet);
print_balances(header, actual_balances);
assert_eq!(actual_balances.miner, initial.miner + expected_delta.miner);
Expand All @@ -114,4 +141,4 @@ fn print_balances(header: &str, balances: TestBalances) {
info!("{}", header);
info!("Miner's balance: {}", balances.miner);
info!("Alice's balance: {}", balances.alice);
}
}
3 changes: 1 addition & 2 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod persistence;
pub mod rpc_client;
pub mod wallet;
pub mod persistence;
pub mod zebra_merkle;

pub mod transactions;

2 changes: 1 addition & 1 deletion src/components/persistence/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ impl InsertableNoteData {
spend_action_index: note.spend_action_index,
}
}
}
}
Loading

0 comments on commit 0e4a83d

Please sign in to comment.