From 33a7befbe81d73b1170e0ee38a42916c3a505584 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Wed, 16 Aug 2023 13:32:26 -0700 Subject: [PATCH] simplify tails reader Signed-off-by: Andrew Whitehead --- indy-credx/src/services/prover.rs | 4 +- indy-credx/src/services/tails.rs | 129 ++++++++++-------------------- 2 files changed, 43 insertions(+), 90 deletions(-) diff --git a/indy-credx/src/services/prover.rs b/indy-credx/src/services/prover.rs index 5f07ee1..ab72f33 100644 --- a/indy-credx/src/services/prover.rs +++ b/indy-credx/src/services/prover.rs @@ -18,7 +18,7 @@ use indy_data_types::anoncreds::{ }; use indy_utils::{Qualifiable, Validatable}; -use super::tails::TailsReader; +use super::tails::TailsFileReader; pub fn create_link_secret() -> Result { LinkSecret::new().map_err(err_map!(Unexpected)) @@ -254,7 +254,7 @@ pub fn create_presentation( } pub fn create_or_update_revocation_state( - tails_reader: TailsReader, + tails_reader: TailsFileReader, revoc_reg_def: &RevocationRegistryDefinition, rev_reg_delta: &RevocationRegistryDelta, rev_reg_idx: u32, diff --git a/indy-credx/src/services/tails.rs b/indy-credx/src/services/tails.rs index 7421f5a..e2f83ad 100644 --- a/indy-credx/src/services/tails.rs +++ b/indy-credx/src/services/tails.rs @@ -1,7 +1,7 @@ -use std::cell::RefCell; +use std::cell::{RefCell, RefMut}; use std::fmt::Debug; use std::fs::File; -use std::io::{BufRead, BufReader, BufWriter, Read, Seek, SeekFrom, Write}; +use std::io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; use indy_utils::base58; @@ -17,25 +17,45 @@ use crate::error::Error; const TAILS_BLOB_TAG_SZ: u8 = 2; const TAIL_SIZE: usize = Tail::BYTES_REPR_SIZE; -#[derive(Debug)] -pub struct TailsReader { - inner: Box>, +pub struct TailsFileReader { + file: RefCell>>, + path: PathBuf, } -impl TailsReader { - pub(crate) fn new(inner: TR) -> Self { +impl TailsFileReader { + pub fn new>(path: P) -> Self { Self { - inner: Box::new(RefCell::new(inner)), + file: RefCell::new(None), + path: path.into(), + } + } + + fn opened(&self) -> Result>, io::Error> { + let mut inner = self.file.borrow_mut(); + if inner.is_none() { + inner.replace(BufReader::new(File::open(&self.path)?)); } + Ok(RefMut::map(inner, |file| file.as_mut().unwrap())) + } + + fn read(&self, pos: i64, buf: &mut [u8]) -> Result<(), io::Error> { + let mut file = self.opened()?; + let offset = pos - file.stream_position()? as i64; + file.seek_relative(offset)?; + file.read_exact(buf)?; + Ok(()) } } -pub trait TailsReaderImpl: Debug + Send { - fn hash(&mut self) -> Result, Error>; - fn read(&mut self, size: usize, offset: usize) -> Result, Error>; +impl Debug for TailsFileReader { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TailsFileReader") + .field("path", &self.path) + .finish_non_exhaustive() + } } -impl RevocationTailsAccessor for TailsReader { +impl RevocationTailsAccessor for TailsFileReader { fn access_tail( &self, tail_id: u32, @@ -43,18 +63,15 @@ impl RevocationTailsAccessor for TailsReader { ) -> std::result::Result<(), ClError> { trace!("access_tail >>> tail_id: {:?}", tail_id); - let tail_bytes = self - .inner - .borrow_mut() - .read( - TAIL_SIZE, - TAIL_SIZE * tail_id as usize + TAILS_BLOB_TAG_SZ as usize, - ) - .map_err(|e| { - error!("IO error reading tails file: {e}"); - ClError::new(ClErrorKind::InvalidState, "Could not read from tails file") - })?; - + let mut tail_bytes = [0u8; TAIL_SIZE]; + self.read( + TAIL_SIZE as i64 * tail_id as i64 + TAILS_BLOB_TAG_SZ as i64, + &mut tail_bytes, + ) + .map_err(|e| { + error!("IO error reading tails file: {e}"); + ClError::new(ClErrorKind::InvalidState, "Could not read from tails file") + })?; let tail = Tail::from_bytes(tail_bytes.as_slice())?; accessor(&tail); @@ -63,70 +80,6 @@ impl RevocationTailsAccessor for TailsReader { } } -#[derive(Debug)] -pub struct TailsFileReader { - path: String, - file: Option>, - hash: Option>, -} - -impl TailsFileReader { - pub fn new(path: &str) -> TailsReader { - TailsReader::new(Self { - path: path.to_owned(), - file: None, - hash: None, - }) - } - - pub fn open(&mut self) -> Result<&mut BufReader, Error> { - if self.file.is_none() { - let file = File::open(self.path.clone())?; - self.file.replace(BufReader::new(file)); - } - Ok(self.file.as_mut().unwrap()) - } - - pub fn close(&mut self) { - self.file.take(); - } -} - -impl TailsReaderImpl for TailsFileReader { - fn hash(&mut self) -> Result, Error> { - if let Some(hash) = self.hash.as_ref() { - return Ok(hash.clone()); - } - - let file = self.open()?; - file.seek(SeekFrom::Start(0))?; - let mut hasher = Sha256::default(); - - loop { - let buf = file.fill_buf()?; - let len = buf.len(); - if len == 0 { - break; - } - hasher.update(&buf); - file.consume(len); - } - let hash = hasher.finalize().to_vec(); - self.hash.replace(hash.clone()); - Ok(hash) - } - - fn read(&mut self, size: usize, offset: usize) -> Result, Error> { - let mut buf = vec![0u8; size]; - - let file = self.open()?; - file.seek(SeekFrom::Start(offset as u64))?; - file.read_exact(buf.as_mut_slice())?; - - Ok(buf) - } -} - pub trait TailsWriter: std::fmt::Debug { fn write( &mut self,