Skip to content

Commit

Permalink
Merge branch 'lambda-main' into pretty-table
Browse files Browse the repository at this point in the history
  • Loading branch information
juan518munoz committed Nov 28, 2023
2 parents 0589670 + 1de56fa commit b3e89fb
Show file tree
Hide file tree
Showing 13 changed files with 695 additions and 77 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: CI
on:
push:
branches:
- main
pull_request:
types: [opened, repoened, synchronize]

jobs:
test:
name: Test Rust ${{matrix.toolchain}} on ${{matrix.os}}
runs-on: ${{matrix.os}}-latest
strategy:
fail-fast: false
matrix:
toolchain: [stable, nightly]
os: [ubuntu]
steps:
- uses: actions/checkout@main
- name: Install rust
uses: actions-rs/toolchain@v1
with:
toolchain: ${{matrix.toolchain}}
override: true
- name: Test
uses: actions-rs/cargo@v1
env:
RUSTFLAGS: -C debug-assertions
with:
command: test
args: --release

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- name: Install minimal stable with clippy
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: clippy
override: true

- name: Clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all --all-targets -- -D clippy::all -D warnings

rustfmt:
name: rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- name: Install minimal stable with rustfmt
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt
override: true

- name: rustfmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
43 changes: 43 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-toml
- id: pretty-format-json
- id: check-added-large-files
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: detect-private-key
- repo: https://github.com/hackaugusto/pre-commit-cargo
rev: v1.0.0
hooks:
# Allows cargo fmt to modify the source code prior to the commit
- id: cargo
name: Cargo fmt
args: ["+stable", "fmt", "--all"]
stages: [commit]
# Requires code to be properly formatted prior to pushing upstream
- id: cargo
name: Cargo fmt --check
args: ["+stable", "fmt", "--all", "--check"]
stages: [push, manual]
- id: cargo
name: Cargo check --all-targets
args: ["+stable", "check", "--all-targets"]
- id: cargo
name: Cargo check --all-targets --no-default-features
args: ["+stable", "check", "--all-targets", "--no-default-features"]
- id: cargo
name: Cargo check --all-targets --all-features
args: ["+stable", "check", "--all-targets", "--all-features"]
# Unlike fmt, clippy will not be automatically applied
- id: cargo
name: Cargo clippy
args: ["+nightly", "clippy", "--workspace", "--", "--deny", "clippy::all", "--deny", "warnings"]
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ rust-version = "1.67"
[features]
default = ["std"]
std = ["crypto/std", "objects/std"]
testing = ["objects/testing"]
testing = ["objects/testing", "mock"]

[dependencies]
clap = { version = "4.3" , features = ["derive"] }
crypto = { package = "miden-crypto", git = "https://github.com/0xPolygonMiden/crypto", branch = "next", default-features = false }
lazy_static = "1.4.0"
objects = { package = "miden-objects", git = "https://github.com/0xPolygonMiden/miden-base", branch = "main", features = ["serde"] }
miden_lib = { package = "miden-lib", git = "https://github.com/0xPolygonMiden/miden-base", branch = "main", default-features = false}
miden_lib = { package = "miden-lib", git = "https://github.com/0xPolygonMiden/miden-base", branch = "main", default-features = false }
mock = { package = "miden-mock", git = "https://github.com/0xPolygonMiden/miden-base", branch = "main", default-features = false, optional = true }
rusqlite = { version = "0.29.0", features = ["bundled"] }
rusqlite_migration = { version = "1.0" }
rand = { version="0.8.5" }
Expand All @@ -28,4 +29,5 @@ serde_json = { version = "1.0", features = ["raw_value"] }
cli-table = "0.4.7"

[dev-dependencies]
ctor = "0.2.5"
uuid = { version = "1.6.1", features = ["serde", "v4"] }
mock = { package = "miden-mock", git = "https://github.com/0xPolygonMiden/miden-base", branch = "main", default-features = false }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# miden-client
# miden-client
23 changes: 12 additions & 11 deletions src/cli/account.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use cli_table::{print_stdout, Cell, Style, Table};
use crypto::{dsa::rpo_falcon512::KeyPair, Felt};
use miden_client::{Client, ClientConfig};
use miden_client::Client;
use miden_lib::{faucets, AuthScheme};
use objects::{accounts::AccountType, assets::TokenSymbol};
use rand::Rng;
Expand Down Expand Up @@ -56,13 +56,13 @@ pub enum AccountTemplate {
}

impl AccountCmd {
pub fn execute(&self) -> Result<(), String> {
pub fn execute(&self, client: Client) -> Result<(), String> {
match self {
AccountCmd::List => {
list_accounts()?;
list_accounts(client)?;
}
AccountCmd::New { template, deploy } => {
new_account(template, *deploy)?;
new_account(client, template, *deploy)?;
}
AccountCmd::View { id: _ } => todo!(),
}
Expand All @@ -73,8 +73,7 @@ impl AccountCmd {
// LIST ACCOUNTS
// ================================================================================================

fn list_accounts() -> Result<(), String> {
let client = Client::new(ClientConfig::default()).map_err(|err| err.to_string())?;
fn list_accounts(client: Client) -> Result<(), String> {
let accounts = client.get_accounts().map_err(|err| err.to_string())?;

let mut rows = vec![];
Expand Down Expand Up @@ -103,9 +102,11 @@ fn list_accounts() -> Result<(), String> {
// ACCOUNT NEW
// ================================================================================================

fn new_account(template: &Option<AccountTemplate>, deploy: bool) -> Result<(), String> {
let client = Client::new(ClientConfig::default()).map_err(|err| err.to_string())?;

fn new_account(
client: Client,
template: &Option<AccountTemplate>,
deploy: bool,
) -> Result<(), String> {
if deploy {
todo!("Recording the account on chain is not supported yet");
}
Expand Down Expand Up @@ -156,10 +157,10 @@ fn new_account(template: &Option<AccountTemplate>, deploy: bool) -> Result<(), S
// TODO: Make these inserts atomic through a single transaction
client
.store()
.insert_account(&account)
.and_then(|_| client.store().insert_account_code(account.code()))
.insert_account_code(account.code())
.and_then(|_| client.store().insert_account_storage(account.storage()))
.and_then(|_| client.store().insert_account_vault(account.vault()))
.and_then(|_| client.store().insert_account(&account))
.map(|_| {
println!(
"Succesfully created and stored Account ID: {}",
Expand Down
142 changes: 142 additions & 0 deletions src/cli/input_notes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use super::Client;
use super::Parser;

use objects::notes::RecordedNote;
use objects::Digest;

#[derive(Debug, Parser, Clone)]
#[clap(about = "View input notes")]
pub enum InputNotes {
/// List input notes
#[clap(short_flag = 'l')]
List,

/// Show details of the input note for the specified note hash
#[clap(short_flag = 's')]
Show {
/// Hash of the input note to show
#[clap()]
hash: String,

/// Show note script
#[clap(short, long, default_value = "false")]
script: bool,

/// Show note vault
#[clap(short, long, default_value = "false")]
vault: bool,

/// Show note inputs
#[clap(short, long, default_value = "false")]
inputs: bool,
},
}

impl InputNotes {
pub fn execute(&self, client: Client) -> Result<(), String> {
match self {
InputNotes::List => {
list_input_notes(client)?;
}
InputNotes::Show {
hash,
script,
vault,
inputs,
} => {
show_input_note(client, hash.clone(), *script, *vault, *inputs)?;
}
}
Ok(())
}
}

// LIST INPUT NOTES
// ================================================================================================
fn list_input_notes(client: Client) -> Result<(), String> {
let notes = client.get_input_notes().map_err(|err| err.to_string())?;
print_notes_summary(&notes);
Ok(())
}

fn show_input_note(
client: Client,
hash: String,
show_script: bool,
show_vault: bool,
show_inputs: bool,
) -> Result<(), String> {
let hash = Digest::try_from(hash)
.map_err(|err| format!("Failed to parse input note hash: {}", err))?;

let note = client
.store()
.get_input_note_by_hash(hash)
.map_err(|err| err.to_string())?;

// print note summary
print_notes_summary(core::iter::once(&note));

// print note script
if show_script {
println!("{}", "-".repeat(240));
println!("Note script hash: {}", note.note().script().hash());
println!("{}", "-".repeat(240));
println!("Note Script:");
println!("{}", "-".repeat(240));
println!("{}", note.note().script().code());
};

// print note vault
if show_vault {
println!("{}", "-".repeat(240));
println!("Note vault hash: {}", note.note().vault().hash());
println!("{}", "-".repeat(240));
println!("Note Vault:");
println!("{}", "-".repeat(240));
for asset in note.note().vault().iter() {
// To do print this nicely
println!("{:?}", asset);
}
};

if show_inputs {
println!("{}", "-".repeat(240));
println!("Note inputs hash: {}", note.note().inputs().hash());
println!("{}", "-".repeat(240));
println!("Note Inputs:");
println!("{}", "-".repeat(240));
for (idx, input) in note.note().inputs().inputs().iter().enumerate() {
// To do print this nicely
println!("{idx}: {input}");
}
};

Ok(())
}

// HELPERS
// ================================================================================================
fn print_notes_summary<'a, I>(notes: I)
where
I: IntoIterator<Item = &'a RecordedNote>,
{
println!("{}", "-".repeat(240));
println!(
"{0: <66} | {1: <66} | {2: <66} | {3: <66} | {4: <15}",
"hash", "script hash", "vault hash", "inputs hash", "serial num",
);
println!("{}", "-".repeat(240));

for note in notes {
println!(
"{0: <66} | {1: <66} | {2: <66} | {3: <66} | {4: <15}",
note.note().hash(),
note.note().script().hash(),
note.note().vault().hash(),
note.note().inputs().hash(),
Digest::new(note.note().serial_num()),
);
}
println!("{}", "-".repeat(240));
}
21 changes: 20 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{Client, ClientConfig};
use clap::Parser;

mod account;
mod input_notes;
#[cfg(feature = "testing")]
mod test;

/// Root CLI struct
#[derive(Parser, Debug)]
Expand All @@ -20,13 +24,28 @@ pub struct Cli {
pub enum Command {
#[clap(subcommand)]
Account(account::AccountCmd),
#[clap(subcommand)]
InputNotes(input_notes::InputNotes),
#[cfg(feature = "testing")]
/// Insert test data into the database
TestData,
}

/// CLI entry point
impl Cli {
pub fn execute(&self) -> Result<(), String> {
// create a client
let client = Client::new(ClientConfig::default()).map_err(|err| err.to_string())?;

// execute cli command
match &self.action {
Command::Account(account) => account.execute(),
Command::Account(account) => account.execute(client),
Command::InputNotes(notes) => notes.execute(client),
#[cfg(feature = "testing")]
Command::TestData => {
test::insert_test_data(client);
Ok(())
}
}
}
}
Loading

0 comments on commit b3e89fb

Please sign in to comment.