Skip to content

Commit

Permalink
Wallet works for both para and standalone again, but needs a little c…
Browse files Browse the repository at this point in the history
…leanup.
  • Loading branch information
JoshOrndorff committed Mar 23, 2024
1 parent c6c5026 commit 120d2c0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
10 changes: 9 additions & 1 deletion wallet/src/amoeba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ use tuxedo_core::{
ConstraintChecker,
};

pub async fn amoeba_demo<Checker: ConstraintChecker + From<OuterConstraintChecker>>(
pub async fn amoeba_demo(parachain: bool, client: &HttpClient) -> anyhow::Result<()> {
if parachain {
amoeba_demo_helper::<crate::ParachainConstraintChecker>(client).await
} else {
amoeba_demo_helper::<crate::OuterConstraintChecker>(client).await
}
}

pub async fn amoeba_demo_helper<Checker: ConstraintChecker + From<OuterConstraintChecker>>(
client: &HttpClient,
) -> anyhow::Result<()> {
// Construct a simple amoeba spawning transaction (no signature required)
Expand Down
8 changes: 6 additions & 2 deletions wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub struct Cli {
/// RPC endpoint of the node that this wallet will connect to.
pub endpoint: String,

#[arg(long, short)]
#[arg(long, short('d'))]
/// Path where the wallet data is stored. Default value is platform specific.
pub path: Option<PathBuf>,
pub base_path: Option<PathBuf>,

#[arg(long, verbatim_doc_comment)]
/// Skip the initial sync that the wallet typically performs with the node.
Expand All @@ -40,6 +40,10 @@ pub struct Cli {
/// The keystore will contain the development key Shawn.
pub dev: bool,

/// Use the Parachain template encoding instead of the regular node template encoding.
#[arg(long, short, verbatim_doc_comment)]
pub parachain: bool,

#[command(subcommand)]
pub command: Option<Command>,
}
Expand Down
59 changes: 27 additions & 32 deletions wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ use cli::{Cli, Command};
/// We don't want the wallet to depend on the huge parachain codebase,
/// So we just recreate this one little type here.
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone)]
pub enum ParachainConstraintChecker<C> {
Normal(C),
pub enum ParachainConstraintChecker {
Normal(OuterConstraintChecker),
Parachain,
}

impl<C: Clone + std::fmt::Debug + Encode + Decode> SimpleConstraintChecker
for ParachainConstraintChecker<C>
{
impl SimpleConstraintChecker for ParachainConstraintChecker {
type Error = ();

fn check(
Expand All @@ -51,8 +49,8 @@ impl<C: Clone + std::fmt::Debug + Encode + Decode> SimpleConstraintChecker
}
}

impl<C> From<C> for ParachainConstraintChecker<C> {
fn from(c: C) -> Self {
impl From<OuterConstraintChecker> for ParachainConstraintChecker {
fn from(c: OuterConstraintChecker) -> Self {
ParachainConstraintChecker::Normal(c)
}
}
Expand All @@ -73,7 +71,7 @@ async fn main() -> anyhow::Result<()> {
// Setup the data paths.
let data_path = match tmp {
true => temp_dir(),
_ => cli.path.unwrap_or_else(default_data_path),
_ => cli.base_path.unwrap_or_else(default_data_path),
};
let keystore_path = data_path.join("keystore");
let db_path = data_path.join("wallet_database");
Expand Down Expand Up @@ -117,25 +115,30 @@ async fn main() -> anyhow::Result<()> {

if !sled::Db::was_recovered(&db) {
// This is a new instance, so we need to apply the genesis block to the database.
sync::apply_block::<_, ParachainConstraintChecker<OuterConstraintChecker>>(
&db,
node_genesis_block,
node_genesis_hash,
&keystore_filter,
)
.await?;
if cli.parachain {
sync::apply_block::<_, ParachainConstraintChecker>(
&db,
node_genesis_block,
node_genesis_hash,
&keystore_filter,
)
.await?;
} else {
sync::apply_block::<_, OuterConstraintChecker>(
&db,
node_genesis_block,
node_genesis_hash,
&keystore_filter,
)
.await?;
}
}

// Synchronize the wallet with attached node unless instructed otherwise.
if cli.no_sync {
log::warn!("Skipping sync with node. Using previously synced information.")
} else {
sync::synchronize::<_, ParachainConstraintChecker<OuterConstraintChecker>>(
&db,
&client,
&keystore_filter,
)
.await?;
sync::synchronize(cli.parachain, &db, &client, &keystore_filter).await?;

log::info!(
"Wallet database synchronized with node to height {:?}",
Expand All @@ -145,14 +148,9 @@ async fn main() -> anyhow::Result<()> {

// Dispatch to proper subcommand
match cli.command {
Some(Command::AmoebaDemo) => {
amoeba::amoeba_demo::<ParachainConstraintChecker<OuterConstraintChecker>>(&client).await
}
Some(Command::AmoebaDemo) => amoeba::amoeba_demo(cli.parachain, &client).await,
// Command::MultiSigDemo => multi_sig::multi_sig_demo(&client).await,
Some(Command::MintCoins(args)) => {
money::mint_coins::<ParachainConstraintChecker<OuterConstraintChecker>>(&client, args)
.await
}
Some(Command::MintCoins(args)) => money::mint_coins(cli.parachain, &client, args).await,
Some(Command::VerifyCoin { output_ref }) => {
println!("Details of coin {}:", hex::encode(output_ref.encode()));

Expand All @@ -175,10 +173,7 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
Some(Command::SpendCoins(args)) => {
money::spend_coins::<ParachainConstraintChecker<OuterConstraintChecker>>(
&db, &client, &keystore, args,
)
.await
money::spend_coins(cli.parachain, &db, &client, &keystore, args).await
}
Some(Command::InsertKey { seed }) => crate::keystore::insert_key(&keystore, &seed),
Some(Command::GenerateKey { password }) => {
Expand Down
31 changes: 29 additions & 2 deletions wallet/src/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,19 @@ use tuxedo_core::{
};

/// Create and send a transaction that mints the coins on the network
pub async fn mint_coins<Checker: ConstraintChecker + From<OuterConstraintChecker>>(
pub async fn mint_coins(
parachain: bool,
client: &HttpClient,
args: MintCoinArgs,
) -> anyhow::Result<()> {
if parachain {
mint_coins_helper::<crate::ParachainConstraintChecker>(client, args).await
} else {
mint_coins_helper::<crate::OuterConstraintChecker>(client, args).await
}
}

pub async fn mint_coins_helper<Checker: ConstraintChecker + From<OuterConstraintChecker>>(
client: &HttpClient,
args: MintCoinArgs,
) -> anyhow::Result<()> {
Expand Down Expand Up @@ -63,8 +75,23 @@ pub async fn mint_coins<Checker: ConstraintChecker + From<OuterConstraintChecker
Ok(())
}

//TODO Could I use a macro or a higher-order function to write all of these?
/// Create and send a transaction that spends coins on the network
pub async fn spend_coins<Checker: ConstraintChecker + From<OuterConstraintChecker>>(
pub async fn spend_coins(
parachain: bool,
db: &Db,
client: &HttpClient,
keystore: &LocalKeystore,
args: SpendArgs,
) -> anyhow::Result<()> {
if parachain {
spend_coins_helper::<crate::ParachainConstraintChecker>(db, client, keystore, args).await
} else {
spend_coins_helper::<crate::OuterConstraintChecker>(db, client, keystore, args).await
}
}

pub async fn spend_coins_helper<Checker: ConstraintChecker + From<OuterConstraintChecker>>(
db: &Db,
client: &HttpClient,
keystore: &LocalKeystore,
Expand Down
15 changes: 14 additions & 1 deletion wallet/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,23 @@ pub(crate) fn open_db(
Ok(db)
}

pub(crate) async fn synchronize<F: Fn(&OuterVerifier) -> bool>(
parachain: bool,
db: &Db,
client: &HttpClient,
filter: &F,
) -> anyhow::Result<()> {
if parachain {
synchronize_helper::<F, crate::ParachainConstraintChecker>(db, client, filter).await
} else {
synchronize_helper::<F, crate::OuterConstraintChecker>(db, client, filter).await
}
}

/// Synchronize the local database to the database of the running node.
/// The wallet entirely trusts the data the node feeds it. In the bigger
/// picture, that means run your own (light) node.
pub(crate) async fn synchronize<F: Fn(&OuterVerifier) -> bool, C: ConstraintChecker>(
pub(crate) async fn synchronize_helper<F: Fn(&OuterVerifier) -> bool, C: ConstraintChecker>(
db: &Db,
client: &HttpClient,
filter: &F,
Expand Down

0 comments on commit 120d2c0

Please sign in to comment.