Gzip the output

This commit is contained in:
Aditya Kulkarni 2020-04-12 20:23:03 -07:00
parent d0e7a5f635
commit 796663c971
4 changed files with 30 additions and 11 deletions

1
Cargo.lock generated
View File

@ -2558,6 +2558,7 @@ dependencies = [
"http 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "pairing 0.14.2 (git+https://github.com/adityapk00/librustzcash.git?rev=98f9bda32957a6d7f0011c9a6adec13b5b80ea94)",

View File

@ -22,6 +22,7 @@ rust-embed = { version = "5.1.0", features = ["debug-embed"] }
rand = "0.7.2" rand = "0.7.2"
sodiumoxide = "0.2.5" sodiumoxide = "0.2.5"
ring = "0.16.9" ring = "0.16.9"
libflate = "0.1"
tonic = { version = "0.1.1", features = ["tls", "tls-roots"] } tonic = { version = "0.1.1", features = ["tls", "tls-roots"] }
bytes = "0.4" bytes = "0.4"

View File

@ -414,14 +414,22 @@ impl LightClient {
pub fn attempt_recover_seed(config: &LightClientConfig) -> Result<String, String> { pub fn attempt_recover_seed(config: &LightClientConfig) -> Result<String, String> {
use std::io::prelude::*; use std::io::prelude::*;
use byteorder::{LittleEndian, ReadBytesExt,}; use byteorder::{LittleEndian, ReadBytesExt};
use libflate::gzip::Decoder;
use bip39::{Mnemonic, Language}; use bip39::{Mnemonic, Language};
use zcash_primitives::serialize::Vector; use zcash_primitives::serialize::Vector;
let mut reader = BufReader::new(File::open(config.get_wallet_path()).unwrap()); let mut inp = BufReader::new(File::open(config.get_wallet_path()).unwrap());
let version = reader.read_u64::<LittleEndian>().unwrap(); let version = inp.read_u64::<LittleEndian>().unwrap();
println!("Reading wallet version {}", version); 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<dyn Read> = if version <= 4 {
Box::new(inp)
} else {
Box::new(Decoder::new(inp).unwrap())
};
let encrypted = if version >= 4 { let encrypted = if version >= 4 {
reader.read_u8().unwrap() > 0 reader.read_u8().unwrap() > 0
} else { } else {

View File

@ -11,6 +11,7 @@ use log::{info, warn, error};
use protobuf::parse_from_bytes; use protobuf::parse_from_bytes;
use libflate::{gzip::{Decoder, Encoder}, finish::AutoFinishUnchecked};
use secp256k1::SecretKey; use secp256k1::SecretKey;
use bip39::{Mnemonic, Language}; use bip39::{Mnemonic, Language};
@ -132,7 +133,7 @@ pub struct LightWallet {
impl LightWallet { impl LightWallet {
pub fn serialized_version() -> u64 { pub fn serialized_version() -> u64 {
return 4; return 5;
} }
fn get_taddr_from_bip39seed(config: &LightClientConfig, bip39_seed: &[u8], pos: u32) -> SecretKey { fn get_taddr_from_bip39seed(config: &LightClientConfig, bip39_seed: &[u8], pos: u32) -> SecretKey {
@ -230,8 +231,8 @@ impl LightWallet {
}) })
} }
pub fn read<R: Read>(mut reader: R, config: &LightClientConfig) -> io::Result<Self> { pub fn read<R: Read>(mut inp: R, config: &LightClientConfig) -> io::Result<Self> {
let version = reader.read_u64::<LittleEndian>()?; let version = inp.read_u64::<LittleEndian>()?;
if version > LightWallet::serialized_version() { if version > LightWallet::serialized_version() {
let e = format!("Don't know how to read wallet version {}. Do you have the latest version?", version); let e = format!("Don't know how to read wallet version {}. Do you have the latest version?", version);
error!("{}", e); error!("{}", e);
@ -239,6 +240,13 @@ impl LightWallet {
} }
info!("Reading wallet version {}", version); 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<dyn Read> = if version <= 4 {
Box::new(inp)
} else {
Box::new(Decoder::new(inp).unwrap())
};
let encrypted = if version >= 4 { let encrypted = if version >= 4 {
reader.read_u8()? > 0 reader.read_u8()? > 0
@ -329,14 +337,17 @@ impl LightWallet {
}) })
} }
pub fn write<W: Write>(&self, mut writer: W) -> io::Result<()> { pub fn write<W: Write>(&self, mut out: W) -> io::Result<()> {
if self.encrypted && self.unlocked { if self.encrypted && self.unlocked {
return Err(Error::new(ErrorKind::InvalidInput, return Err(Error::new(ErrorKind::InvalidInput,
format!("Cannot write while wallet is unlocked while encrypted."))); format!("Cannot write while wallet is unlocked while encrypted.")));
} }
// Write the version // Write the version
writer.write_u64::<LittleEndian>(LightWallet::serialized_version())?; out.write_u64::<LittleEndian>(LightWallet::serialized_version())?;
// Gzip encoder
let mut writer = AutoFinishUnchecked::new(Encoder::new(out).unwrap());
// Write if it is locked // Write if it is locked
writer.write_u8(if self.encrypted {1} else {0})?; 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 // While writing the birthday, get it from the fn so we recalculate it properly
// in case of rescans etc... // in case of rescans etc...
writer.write_u64::<LittleEndian>(self.get_birthday())?; writer.write_u64::<LittleEndian>(self.get_birthday())
Ok(())
} }
pub fn note_address(hrp: &str, note: &SaplingNoteData) -> Option<String> { pub fn note_address(hrp: &str, note: &SaplingNoteData) -> Option<String> {