Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added timestamp to wallet #152

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub use kitties;
pub use money;
pub use poe;
pub use runtime_upgrade;
pub use timestamp;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
Expand Down
3 changes: 3 additions & 0 deletions wallet/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub enum Command {

/// Show the complete list of UTXOs known to the wallet.
ShowAllOutputs,

/// Show the latest on-chain timestamp.
ShowTimestamp,
}

#[derive(Debug, Args)]
Expand Down
9 changes: 6 additions & 3 deletions wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ async fn main() -> anyhow::Result<()> {
// The filter function that will determine whether the local database should track a given utxo
// is based on whether that utxo is privately owned by a key that is in our keystore.
let keystore_filter = |v: &OuterVerifier| -> bool {
matches![
v,
matches![v,
OuterVerifier::Sr25519Signature(Sr25519Signature { owner_pubkey }) if crate::keystore::has_key(&keystore, owner_pubkey)
]
] || matches![v, OuterVerifier::UpForGrabs(UpForGrabs)]
};
muraca marked this conversation as resolved.
Show resolved Hide resolved

if !sled::Db::was_recovered(&db) {
Expand Down Expand Up @@ -166,6 +165,10 @@ async fn main() -> anyhow::Result<()> {

Ok(())
}
Some(Command::ShowTimestamp) => {
println!("Timestamp: {}", sync::get_timestamp(&db)?);
Ok(())
}
None => {
log::info!("No Wallet Command invoked. Exiting.");
Ok(())
Expand Down
54 changes: 37 additions & 17 deletions wallet/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ use sled::Db;
use sp_core::H256;
use sp_runtime::traits::{BlakeTwo256, Hash};
use tuxedo_core::{
dynamic_typing::UtxoData,
types::{Input, OutputRef},
verifier::Sr25519Signature,
};

use jsonrpsee::http_client::HttpClient;
use runtime::{money::Coin, Block, OuterVerifier, Transaction};
use runtime::{money::Coin, timestamp::Timestamp, Block, OuterVerifier, Transaction};

/// The identifier for the blocks tree in the db.
const BLOCKS: &str = "blocks";
Expand All @@ -39,6 +40,9 @@ const UNSPENT: &str = "unspent";
/// The identifier for the spent tree in the db.
const SPENT: &str = "spent";

/// The identifier for the current timestamp in the db.
const TIMESTAMP: &str = "timestamp";

/// Open a database at the given location intended for the given genesis block.
///
/// If the database is already populated, make sure it is based on the expected genesis
Expand Down Expand Up @@ -262,23 +266,30 @@ async fn apply_transaction<F: Fn(&OuterVerifier) -> bool>(
.filter(|o| filter(&o.verifier))
.enumerate()
{
// For now the wallet only supports simple coins, so skip anything else
let amount = match output.payload.extract::<Coin<0>>() {
Ok(Coin(amount)) => amount,
Err(_) => continue,
};

let output_ref = OutputRef {
tx_hash,
index: index as u32,
};

match output.verifier {
OuterVerifier::Sr25519Signature(Sr25519Signature { owner_pubkey }) => {
// Add it to the global unspent_outputs table
add_unspent_output(db, &output_ref, &owner_pubkey, &amount)?;
// For now the wallet only supports simple coins and timestamp
match output.payload.type_id {
Coin::<0>::TYPE_ID => {
let amount = output.payload.extract::<Coin<0>>()?.0;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a good time to make the filtering we do here paramaterizeable like the verifier filter is above. Or at least make a follow up issue for it.

let output_ref = OutputRef {
tx_hash,
index: index as u32,
};

match output.verifier {
OuterVerifier::Sr25519Signature(Sr25519Signature { owner_pubkey }) => {
// Add it to the global unspent_outputs table
add_unspent_output(db, &output_ref, &owner_pubkey, &amount)?;
}
_ => return Err(anyhow!("{:?}", ())),
}
}
Timestamp::TYPE_ID => {
let timestamp = output.payload.extract::<Timestamp>()?.time;
let timestamp_tree = db.open_tree(TIMESTAMP)?;
timestamp_tree.insert([0], timestamp.encode())?;
}
_ => return Err(anyhow!("{:?}", ())),
_ => continue,
}
}

Expand Down Expand Up @@ -456,3 +467,12 @@ pub(crate) fn get_balances(db: &Db) -> anyhow::Result<impl Iterator<Item = (H256

Ok(balances.into_iter())
}

pub(crate) fn get_timestamp(db: &Db) -> anyhow::Result<u64> {
let timestamp_tree = db.open_tree(TIMESTAMP)?;
let timestamp = timestamp_tree
.get([0])?
.ok_or_else(|| anyhow!("Could not find timestamp in database."))?;
u64::decode(&mut &timestamp[..])
.map_err(|_| anyhow!("Could not decode timestamp from database."))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would go in a separate timestamp module. My idea is that logic associated with each piece (like how this is associated with the timestamp piece) will live in a dedicated file. That should make it easy to make chain-specific wallets by either including or not including that part of the wallet.

Loading