Skip to content

Commit

Permalink
message board sync to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2 committed Sep 16, 2024
2 parents e062465 + dae657e commit bb0e9df
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 169 deletions.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]

[[example]]
name = "simple-sync"
path = "examples/simple-sync.rs"

[[example]]
name = "message-board-sync"
path = "examples/message-board-sync.rs"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = 3
Expand Down Expand Up @@ -54,7 +62,7 @@ tonic = { version = "0.12", default-features = false, features = [
] }

# Used in Native tests
tokio = { version = "1.0", features = ["rt", "macros"], optional = true }
tokio = { version = "1.0", features = ["rt", "macros", "rt-multi-thread"], optional = true }
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", default-features = false, features = ["unstable", "orchard"], optional = true }

getrandom = { version = "0.2", features = ["js"] }
Expand Down
77 changes: 77 additions & 0 deletions examples/message-board-sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::sync::Once;

use std::num::NonZeroU32;
use webz_core::Wallet;
use zcash_primitives::consensus::Network;
static INIT: Once = Once::new();
const SAPLING_EFVK: &'static str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz";
pub fn initialize() {
INIT.call_once(|| {
webz_core::init::start();
});
}
#[tokio::main]
async fn main() {
use zcash_keys::keys::UnifiedFullViewingKey;
use zcash_primitives::{consensus, constants};
let db_cache = tempfile::tempdir().unwrap();
let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap();

initialize();
let url = "https://zec.rocks:443";
let c = tonic::transport::Channel::from_shared(url).unwrap();

let tls = tonic::transport::ClientTlsConfig::new()
.domain_name("zec.rocks")
.with_webpki_roots();
let channel = c.tls_config(tls).unwrap();

#[cfg(feature = "sqlite-db")]
let wallet_db = {
use zcash_client_sqlite::{
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
};

let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
let mut wallet_db = WalletDb::for_path(&_db_data, consensus::Network::MainNetwork).unwrap();
init_blockmeta_db(&mut db_cache).unwrap();
init_wallet_db(&mut wallet_db, None).unwrap();
wallet_db
};

#[cfg(not(feature = "sqlite-db"))]
let wallet_db = zcash_client_memory::MemoryWalletDb::new(
consensus::Network::MainNetwork,
webz_core::PRUNING_DEPTH,
);

let mut w = Wallet::new(
wallet_db,
channel.connect().await.unwrap(),
Network::MainNetwork,
NonZeroU32::try_from(1).unwrap(),
)
.unwrap();

let s = zcash_keys::encoding::decode_extended_full_viewing_key(
constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
&SAPLING_EFVK.trim(),
)
.unwrap();

let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap();
let id = w.import_ufvk(ufvk, Some(2477329)).await.unwrap();
tracing::info!("Created account with id: {}", id);

tracing::info!("Syncing wallet");
w.sync(|scanned_to, tip| {
println!("Scanned: {}/{}", scanned_to, tip);
})
.await
.unwrap();

tracing::info!("Syncing complete :)");

let summary = w.get_wallet_summary().unwrap();
tracing::info!("Wallet summary: {:?}", summary);
}
82 changes: 82 additions & 0 deletions examples/simple-sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::sync::Once;

use std::num::NonZeroU32;
use webz_core::Wallet;
use zcash_address::ZcashAddress;
use zcash_primitives::consensus::Network;

const SEED: &str = "visit armed kite pen cradle toward reward clay marble oil write dove blind oyster silk oyster original message skate bench tone enable stadium element";
const HD_INDEX: u32 = 0;
const BIRTHDAY: Option<u32> = Some(2577329);

static INIT: Once = Once::new();
pub fn initialize() {
INIT.call_once(|| {
webz_core::init::start();
});
}

#[cfg(feature = "native")]
#[tokio::main]
async fn main() {
let db_cache = tempfile::tempdir().unwrap();
let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap();

initialize();
let url = "https://testnet.zec.rocks:443";
let c = tonic::transport::Channel::from_shared(url).unwrap();

let tls = tonic::transport::ClientTlsConfig::new()
.domain_name("testnet.zec.rocks")
.with_webpki_roots();
let channel = c.tls_config(tls).unwrap();

#[cfg(feature = "sqlite-db")]
let wallet_db = {
use zcash_client_sqlite::{
chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb,
};

let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap();
let mut wallet_db = WalletDb::for_path(&_db_data, Network::TestNetwork).unwrap();
init_blockmeta_db(&mut db_cache).unwrap();
init_wallet_db(&mut wallet_db, None).unwrap();
wallet_db
};

#[cfg(not(feature = "sqlite-db"))]
let wallet_db =
zcash_client_memory::MemoryWalletDb::new(Network::TestNetwork, webz_core::PRUNING_DEPTH);

let mut w = Wallet::new(
wallet_db,
channel.connect().await.unwrap(),
Network::TestNetwork,
NonZeroU32::try_from(1).unwrap(),
)
.unwrap();

let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
tracing::info!("Created account with id: {}", id);

tracing::info!("Syncing wallet");
w.sync(|scanned_to, tip| {
println!("Scanned: {}/{}", scanned_to, tip);
})
.await
.unwrap();

tracing::info!("Syncing complete :)");

let summary = w.get_wallet_summary().unwrap();
tracing::info!("Wallet summary: {:?}", summary);

tracing::info!("Proposing a transaction");
let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu");

w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap();
tracing::info!("Transaction proposed");

let summary = w.get_wallet_summary().unwrap();
tracing::info!("Wallet summary: {:?}", summary);
}
14 changes: 10 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ build:
test-web:
WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std"

test-native:
cargo test -r -- --nocapture
example-native:
cargo run -r --example simple-sync

test-sqlite:
cargo test -r --features="sqlite-db" -- --nocapture
example-sqlite:
cargo run -r --example simple-sync --features="sqlite-db"

example-message-board:
cargo run -r --example message-board-sync

example-message-board-sqlite:
cargo run -r --example message-board-sync --features="sqlite-db"

check:
cargo check
3 changes: 0 additions & 3 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ use zcash_client_backend::proposal::Proposal;

const BATCH_SIZE: u32 = 10000;

// type Proposal =
// zcash_client_backend::proposal::Proposal<FeeRule, zcash_client_backend::wallet::NoteId>;

/// # A Zcash wallet
///
/// A wallet is a set of accounts that can be synchronized together with the blockchain.
Expand Down
Loading

0 comments on commit bb0e9df

Please sign in to comment.