Skip to content

Commit

Permalink
added e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraccaman committed Aug 28, 2024
1 parent e9069fe commit 68b5d50
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/scripts/e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"e2e::wallet_tests::wallet_encrypted_key_cmds": 1,
"e2e::wallet_tests::wallet_encrypted_key_cmds_env_var": 1,
"e2e::wallet_tests::wallet_unencrypted_key_cmds": 1,
"e2e::ledger_tests::masp_txs_and_queries": 82
"e2e::ledger_tests::masp_txs_and_queries": 82,
"e2e::ledger_tests::offline_sign": 20
}
27 changes: 24 additions & 3 deletions crates/tests/src/e2e/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! E2E test helpers

use std::fs::{File, OpenOptions};
use std::fs::{self, File, OpenOptions};
use std::future::Future;
use std::io::Write;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -270,7 +270,7 @@ pub fn find_keypair(
.unwrap()
.1;
let key = format!("{}{}", sk, pk);
common::SecretKey::from_str(&key).map_err(|e| {
common::SecretKey::from_str(sk).map_err(|e| {
eyre!(format!(
"Key: {} parsed from {}, Error: {}\n\nOutput: {}",
key, matched, e, unread
Expand Down Expand Up @@ -727,3 +727,24 @@ pub fn find_gaia_address(

Ok(matched.trim().to_string())
}

pub fn find_offline_file(
dir: &Path,
extension: &str,
) -> Result<Option<PathBuf>> {
// Read the directory entries
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();

if path.is_file() {
if let Some(file_extension) = path.extension() {
if file_extension == extension {
return Ok(Some(path));
}
}
}
}

Ok(None)
}
103 changes: 101 additions & 2 deletions crates/tests/src/e2e/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ use setup::constants::*;
use setup::Test;

use super::helpers::{
epochs_per_year_from_min_duration, get_height, get_pregenesis_wallet,
wait_for_block_height, wait_for_wasm_pre_compile,
epochs_per_year_from_min_duration, find_keypair, find_offline_file,
get_height, get_pregenesis_wallet, wait_for_block_height,
wait_for_wasm_pre_compile,
};
use super::setup::{set_ethereum_bridge_mode, working_dir, NamadaCmd};
use crate::e2e::helpers::{
Expand Down Expand Up @@ -123,6 +124,104 @@ fn run_ledger() -> Result<()> {
Ok(())
}

#[test]
fn offline_sign() -> Result<()> {
let test = setup::single_node_net()?;

set_ethereum_bridge_mode(
&test,
&test.net.chain_id,
Who::Validator(0),
ethereum_bridge::ledger::Mode::Off,
None,
);

// 1. Run the ledger node
let mut ledger =
start_namada_ledger_node_wait_wasm(&test, Some(0), Some(40))?;
ledger.exp_string("Committed block hash")?;
let _bg_ledger = ledger.background();

let validator_one_rpc = get_actor_rpc(&test, Who::Validator(0));

let output_folder = test.test_dir.path().to_string_lossy().to_string();

// 2. Dump a transfer tx
let tx_args = apply_use_device(vec![
"transparent-transfer",
"--source",
BERTHA,
"--target",
ALBERT,
"--token",
NAM,
"--amount",
"10.1",
"--gas-price",
"0.00090",
"--signing-keys",
BERTHA_KEY,
"--node",
&validator_one_rpc,
"--dump-tx",
"--output-folder-path",
&output_folder,
]);
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.assert_success();

let offline_tx = find_offline_file(test.test_dir.path(), "tx")
.unwrap()
.expect("Offline tx should be found.")
.to_path_buf()
.display()
.to_string();

let bertha_address = find_address(&test, BERTHA).unwrap().to_string();
let bertha_sk = find_keypair(&test, BERTHA_KEY).unwrap().to_string();

// 3. Offline sign a transfer tx
let tx_args = apply_use_device(vec![
"utils",
"sign-offline",
"--data-path",
&offline_tx,
"--address",
&bertha_address,
"--secret-keys",
&bertha_sk,
"--output-folder-path",
&output_folder,
]);
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.assert_success();

let offline_sig = find_offline_file(test.test_dir.path(), "sig")
.unwrap()
.expect("Offline signature should be found.")
.to_path_buf()
.display()
.to_string();

let tx_args = apply_use_device(vec![
"tx",
"--owner",
BERTHA_KEY,
"--tx-path",
&offline_tx,
"--signatures",
&offline_sig,
"--node",
&validator_one_rpc,
"--gas-payer",
BERTHA_KEY,
]);
let mut client = run!(test, Bin::Client, tx_args, Some(40))?;
client.assert_success();

Ok(())
}

/// In this test we:
/// 1. Run 2 genesis validator ledger nodes and 1 non-validator node
/// 2. Cross over epoch to check for consensus with multiple nodes
Expand Down

0 comments on commit 68b5d50

Please sign in to comment.