message: add back resolve blockdate
diff: @@ -27,6 +27,7 @@ use storage_units::utils::error::StorageError;
  use storage_units::{packfile, indexfile, reffile};
  use pack::{packreader_init, packreader_block_next};
  +use std::cmp::Ordering;
  #[derive(Debug)]
  pub enum Error {
  @@ -242,6 +243,64 @@ pub fn block_read(storage: &Storage, hash: &BlockHash) -> Option<RawBlock> {
  }
  }
  +enum ReverseSearch {
  + Continue,
  + Found,
  + Abort,
  +}
  +
  +fn previous_block(storage: &Storage, block: &Block) -> Block {
  + let prev_hash = block.get_header().get_previous_header();
  + let blk = blob::read(&storage, &header_to_blockhash(&prev_hash)).unwrap().decode().unwrap();
  + blk
  +}
  +
  +fn block_reverse_search_from_tip<F>(storage: &Storage, first_block: &Block, find: F) -> Result<Option<Block>>
  + where F: Fn(&Block) -> Result<ReverseSearch>
  +{
  + let mut current_blk = first_block.clone();
  + loop {
  + match find(&current_blk)? {
  + ReverseSearch::Continue => {
  + let blk = previous_block(&storage, &current_blk);
  + current_blk = blk;
  + },
  + ReverseSearch::Found => { return Ok(Some(current_blk)) },
  + ReverseSearch::Abort => { return Ok(None) },
  + };
  + }
  +}
  +
  +pub fn resolve_date_to_blockhash(storage: &Storage, tip: &BlockHash, date: &BlockDate) -> Result<Option<BlockHash>> {
  + let epoch = date.get_epochid();
  + match epoch::epoch_open_packref(&storage.config, epoch) {
  + Ok(mut handle) => {
  + let slotid = match date {
  + BlockDate::Boundary(_) => 0,
  + BlockDate::Normal(sid) => sid.slotid,
  + };
  + let r = handle.getref_at_index(slotid as u32)?;
  + Ok(r)
  + },
  + Err(_) => {
  + let tip_rblk = block_read(&storage, tip);
  + match tip_rblk {
  + None => return Ok(None),
  + Some(rblk) => {
  + let blk = rblk.decode()?;
  + let found = block_reverse_search_from_tip(storage, &blk, |x|
  + match x.get_header().get_blockdate().cmp(date) {
  + Ordering::Equal => Ok(ReverseSearch::Found),
  + Ordering::Greater => Ok(ReverseSearch::Continue),
  + Ordering::Less => Ok(ReverseSearch::Abort),
  + })?;
  + Ok(found.map(|x| header_to_blockhash(&x.get_header().compute_hash())))
  + }
  + }
  + },
  + }
  +}
  +
  /// packing parameters
  ///
  /// optionally set the maximum number of blobs in this pack
  