/*
    This file is part of TON Blockchain Library.
    TON Blockchain Library is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.
    TON Blockchain Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License
    along with TON Blockchain Library.  If not, see .
    Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#include "td/actor/actor.h"
#include "interfaces/block-handle.h"
#include "interfaces/validator-manager.h"
namespace ton {
namespace validator {
using td::Ref;
/*
 *
 * check block proof
 * write proof
 * initialize prev, before_split, after_merge
 * initialize prev's next
 *
 */
class CheckProof : public td::actor::Actor {
 public:
  CheckProof(BlockIdExt id, td::Ref proof, td::actor::ActorId manager, td::Timestamp timeout,
             td::Promise promise, bool skip_check_signatures, td::Ref prev_key_proof = {})
      : mode_(prev_key_proof.is_null() ? m_normal : m_relproof)
      , id_(id)
      , proof_(std::move(proof))
      , old_proof_(std::move(prev_key_proof))
      , manager_(manager)
      , timeout_(timeout)
      , promise_(std::move(promise))
      , skip_check_signatures_(skip_check_signatures) {
  }
  CheckProof(BlockIdExt id, td::Ref proof, td::actor::ActorId manager, td::Timestamp timeout,
             td::Promise promise, bool skip_check_signatures, td::Ref known_state)
      : mode_(m_relstate)
      , id_(id)
      , proof_(std::move(proof))
      , manager_(manager)
      , timeout_(timeout)
      , promise_(std::move(promise))
      , state_(std::move(known_state))
      , skip_check_signatures_(skip_check_signatures) {
  }
  CheckProof(BlockIdExt id, td::Ref proof_link, td::actor::ActorId manager,
             td::Timestamp timeout, td::Promise promise)
      : mode_(m_prooflink)
      , id_(id)
      , proof_(std::move(proof_link))
      , manager_(manager)
      , timeout_(timeout)
      , promise_(std::move(promise)) {
  }
 private:
  static constexpr td::uint32 priority() {
    return 2;
  }
  void abort_query(td::Status reason);
  void finish_query();
  void alarm() override;
  void start_up() override;
  void got_block_handle(BlockHandle handle);
  void got_masterchain_state(td::Ref state);
  void process_masterchain_state();
  void check_signatures(Ref vset);
  void got_block_handle_2(BlockHandle handle);
 private:
  enum { m_normal, m_relproof, m_relstate, m_prooflink } mode_{m_normal};
  BlockIdExt id_, key_id_;
  td::Ref proof_, old_proof_;
  td::actor::ActorId manager_;
  td::Timestamp timeout_;
  td::Promise promise_;
  BlockHandle handle_;
  td::Ref state_;
  td::Ref vset_;
  Ref proof_root_, sig_root_, old_proof_root_;
  RootHash state_hash_, state_old_hash_;
  LogicalTime lt_;
  UnixTime created_at_;
  bool after_merge_, after_split_, before_split_, want_merge_, want_split_, is_key_block_;
  BlockIdExt mc_blkid_;
  std::vector prev_;
  BlockSeqno prev_key_seqno_{~0U};
  CatchainSeqno catchain_seqno_{0};
  td::uint32 validator_hash_{0};
  td::uint32 sig_count_;
  ValidatorWeight sig_weight_;
  bool skip_check_signatures_{false};
  bool sig_ok_{false};
  td::PerfWarningTimer perf_timer_{"checkproof", 0.1};
  static bool check_send_error(td::actor::ActorId SelfId, td::Status error);
  template 
  static bool check_send_error(td::actor::ActorId SelfId, td::Result& res) {
    return res.is_error() && check_send_error(std::move(SelfId), res.move_as_error());
  }
  bool fatal_error(std::string err_msg, int err_code = -666);
  bool fatal_error(td::Status error);
  bool init_parse(bool is_aux = false);
  bool is_proof() const {
    return mode_ != m_prooflink;
  }
  bool is_masterchain() const {
    return id_.is_masterchain();
  }
};
}  // namespace validator
}  // namespace ton