From 796663c971f422ab3f848962df2e479f74cc021a Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Sun, 12 Apr 2020 20:23:03 -0700 Subject: [PATCH] Gzip the output --- Cargo.lock | 1 + lib/Cargo.toml | 1 + lib/src/lightclient.rs | 14 +++++++++++--- lib/src/lightwallet.rs | 25 +++++++++++++++++-------- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d37bd0..5d52d19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2558,6 +2558,7 @@ dependencies = [ "http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "json 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libflate 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "log4rs 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "pairing 0.14.2 (git+https://github.com/adityapk00/librustzcash.git?rev=98f9bda32957a6d7f0011c9a6adec13b5b80ea94)", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index d594da2..6e4e4a2 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -22,6 +22,7 @@ rust-embed = { version = "5.1.0", features = ["debug-embed"] } rand = "0.7.2" sodiumoxide = "0.2.5" ring = "0.16.9" +libflate = "0.1" tonic = { version = "0.1.1", features = ["tls", "tls-roots"] } bytes = "0.4" diff --git a/lib/src/lightclient.rs b/lib/src/lightclient.rs index c65754d..5fa9ee8 100644 --- a/lib/src/lightclient.rs +++ b/lib/src/lightclient.rs @@ -414,14 +414,22 @@ impl LightClient { pub fn attempt_recover_seed(config: &LightClientConfig) -> Result { use std::io::prelude::*; - use byteorder::{LittleEndian, ReadBytesExt,}; + use byteorder::{LittleEndian, ReadBytesExt}; + use libflate::gzip::Decoder; use bip39::{Mnemonic, Language}; use zcash_primitives::serialize::Vector; - let mut reader = BufReader::new(File::open(config.get_wallet_path()).unwrap()); - let version = reader.read_u64::().unwrap(); + let mut inp = BufReader::new(File::open(config.get_wallet_path()).unwrap()); + let version = inp.read_u64::().unwrap(); println!("Reading wallet version {}", version); + // After version 5, we're writing the rest of the file as a compressed stream (gzip) + let mut reader: Box = if version <= 4 { + Box::new(inp) + } else { + Box::new(Decoder::new(inp).unwrap()) + }; + let encrypted = if version >= 4 { reader.read_u8().unwrap() > 0 } else { diff --git a/lib/src/lightwallet.rs b/lib/src/lightwallet.rs index 4ff89de..76e2cc3 100644 --- a/lib/src/lightwallet.rs +++ b/lib/src/lightwallet.rs @@ -11,6 +11,7 @@ use log::{info, warn, error}; use protobuf::parse_from_bytes; +use libflate::{gzip::{Decoder, Encoder}, finish::AutoFinishUnchecked}; use secp256k1::SecretKey; use bip39::{Mnemonic, Language}; @@ -132,7 +133,7 @@ pub struct LightWallet { impl LightWallet { pub fn serialized_version() -> u64 { - return 4; + return 5; } fn get_taddr_from_bip39seed(config: &LightClientConfig, bip39_seed: &[u8], pos: u32) -> SecretKey { @@ -230,8 +231,8 @@ impl LightWallet { }) } - pub fn read(mut reader: R, config: &LightClientConfig) -> io::Result { - let version = reader.read_u64::()?; + pub fn read(mut inp: R, config: &LightClientConfig) -> io::Result { + let version = inp.read_u64::()?; if version > LightWallet::serialized_version() { let e = format!("Don't know how to read wallet version {}. Do you have the latest version?", version); error!("{}", e); @@ -239,6 +240,13 @@ impl LightWallet { } info!("Reading wallet version {}", version); + + // After version 5, we're writing the rest of the file as a compressed stream (gzip) + let mut reader: Box = if version <= 4 { + Box::new(inp) + } else { + Box::new(Decoder::new(inp).unwrap()) + }; let encrypted = if version >= 4 { reader.read_u8()? > 0 @@ -329,14 +337,17 @@ impl LightWallet { }) } - pub fn write(&self, mut writer: W) -> io::Result<()> { + pub fn write(&self, mut out: W) -> io::Result<()> { if self.encrypted && self.unlocked { return Err(Error::new(ErrorKind::InvalidInput, format!("Cannot write while wallet is unlocked while encrypted."))); } // Write the version - writer.write_u64::(LightWallet::serialized_version())?; + out.write_u64::(LightWallet::serialized_version())?; + + // Gzip encoder + let mut writer = AutoFinishUnchecked::new(Encoder::new(out).unwrap()); // Write if it is locked writer.write_u8(if self.encrypted {1} else {0})?; @@ -385,9 +396,7 @@ impl LightWallet { // While writing the birthday, get it from the fn so we recalculate it properly // in case of rescans etc... - writer.write_u64::(self.get_birthday())?; - - Ok(()) + writer.write_u64::(self.get_birthday()) } pub fn note_address(hrp: &str, note: &SaplingNoteData) -> Option {