<commit_before>////////////////////////////////////////////////////////////////////////////////
/// @brief vocbase traversals
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014-2015 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Michael Hackstein
/// @author Max Neunhoeffer
/// @author Copyright 2014-2015, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#include "Traverser.h"

#include "Basics/Thread.h"

using namespace std;
using namespace triagens::basics;

class Searcher : public Thread {

    Traverser* _traverser;
    Traverser::ThreadInfo& _myInfo;
    Traverser::ThreadInfo& _peerInfo;
    Traverser::VertexId _start;
    Traverser::ExpanderFunction _expander;
    string _id;

  public:

    Searcher (Traverser* traverser, Traverser::ThreadInfo& myInfo,
              Traverser::ThreadInfo& peerInfo, Traverser::VertexId start,
              Traverser::ExpanderFunction expander, string id)
      : Thread(id), _traverser(traverser), _myInfo(myInfo),
        _peerInfo(peerInfo), _start(start), _expander(expander), _id(id) {
    }

////////////////////////////////////////////////////////////////////////////////
/// @brief Insert a neighbor to the todo list.
////////////////////////////////////////////////////////////////////////////////

  private:

    void insertNeighbor (Traverser::VertexId& neighbor,
                         Traverser::VertexId& predecessor,
                         Traverser::EdgeId& edge,
                         Traverser::EdgeWeight weight) {

      std::lock_guard<std::mutex> guard(_myInfo._mutex);
      Traverser::Step* s = _myInfo._pq.lookup(neighbor);

      // Not found, so insert it:
      if (s == nullptr) {
        _myInfo._pq.insert(neighbor,
                           Traverser::Step(neighbor, predecessor,
                                           weight, edge));
        return;
      }
      if (s->_done) {
        return;
      }
      if (s->_weight > weight) {
        _myInfo._pq.lowerWeight(neighbor, weight);
      }
    }

////////////////////////////////////////////////////////////////////////////////
/// @brief Lookup our current vertex in the data of our peer.
////////////////////////////////////////////////////////////////////////////////

    void lookupPeer (Traverser::VertexId& vertex,
                     Traverser::EdgeWeight weight) {

      std::lock_guard<std::mutex> guard(_peerInfo._mutex);
      Traverser::Step* s = _peerInfo._pq.lookup(vertex);
      if (s == nullptr) {
        // Not found, nothing more to do
        return;
      }
      Traverser::EdgeWeight total = s->_weight + weight;

      // Update the highscore:
      std::lock_guard<std::mutex> guard2(_traverser->_resultMutex);
      if (!_traverser->_highscoreSet || total < _traverser->_highscore) {
        _traverser->_highscoreSet = true;
        _traverser->_highscore = total;
      }

      // Now the highscore is set!

      // Did we find a solution together with the other thread?
      if (s->_done) {
        if (total <= _traverser->_highscore) {
          _traverser->_intermediate = vertex;
          _traverser->_bingo = true;
        }
        // We found a way, but somebody else found a better way, so
        // this is not the shortest path
        return;
      }

      // Did we find a solution on our own? This is for the single thread
      // case and for the case that the other thread is too slow to even
      // finish its own start vertex!
      if (s->_weight == 0) {
        // We have found the target, we have finished all vertices with
        // a smaller weight than this one (and did not succeed), so this
        // must be a best solution:
        _traverser->_intermediate = vertex;
        _traverser->_bingo = true;
      }
    }

////////////////////////////////////////////////////////////////////////////////
/// @brief Search graph starting at Start following edges of the given
/// direction only
////////////////////////////////////////////////////////////////////////////////

  public:

    virtual void run () {

      Traverser::VertexId v;
      Traverser::Step s;
      bool b = _myInfo._pq.popMinimal(v, s, true);

      std::vector<Traverser::Step> neighbors;

      // Iterate while no bingo found and
      // there still is a vertex on the stack.
      while (!_traverser->_bingo && b) {
        neighbors.clear();
        _expander(v, neighbors);
        for (auto& neighbor : neighbors) {
          insertNeighbor(neighbor._vertex, v, neighbor._edge,
                         s._weight + neighbor._weight);
        }
        lookupPeer(v, s._weight);

        std::lock_guard<std::mutex> guard(_myInfo._mutex);
        Traverser::Step* s2 = _myInfo._pq.lookup(v);
        s2->_done = true;
        b = _myInfo._pq.popMinimal(v, s, true);
      }
    }
};

////////////////////////////////////////////////////////////////////////////////
/// @brief return the shortest path between the start and target vertex.
////////////////////////////////////////////////////////////////////////////////

Traverser::Path* Traverser::shortestPath (VertexId const& start,
                               VertexId const& target) {

  // For the result:
  std::deque<VertexId> r_vertices;
  std::deque<VertexId> r_edges;
  _highscoreSet = false;
  _highscore = 0;
  _bingo = false;

  // Forward with initialization:
  string empty;
  ThreadInfo forward;
  forward._pq.insert(start, Step(start, empty, 0, empty));

  // backward with initialization:
  ThreadInfo backward;
  backward._pq.insert(target, Step(target, empty, 0, empty));

  // Now the searcher threads:
  Searcher forwardSearcher(this, forward, backward, start,
                           _forwardExpander, "Forward");
  Searcher backwardSearcher(this, backward, forward, target,
                            _backwardExpander, "Backward");
  forwardSearcher.start();
  backwardSearcher.start();
  forwardSearcher.join();
  backwardSearcher.join();

  if (!_bingo || _intermediate == "") {
    return nullptr;
  }

  Step* s = forward._pq.lookup(_intermediate);
  r_vertices.push_back(_intermediate);

  // FORWARD Go path back from intermediate -> start.
  // Insert all vertices and edges at front of vector
  // Do NOT! insert the intermediate vertex
  while (s->_predecessor != "") {
    r_edges.push_front(s->_edge);
    r_vertices.push_front(s->_predecessor);
    s = forward._pq.lookup(s->_predecessor);
  }

  // BACKWARD Go path back from intermediate -> target.
  // Insert all vertices and edges at back of vector
  // Also insert the intermediate vertex
  s = backward._pq.lookup(_intermediate);
  while (s->_predecessor != "") {
    r_edges.push_back(s->_edge);
    r_vertices.push_back(s->_predecessor);
    s = backward._pq.lookup(s->_predecessor);
  }
  return new Path(r_vertices, r_edges, _highscore);
};

<commit_msg>Disable bidirectional search for now.<commit_after>////////////////////////////////////////////////////////////////////////////////
/// @brief vocbase traversals
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014-2015 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Michael Hackstein
/// @author Max Neunhoeffer
/// @author Copyright 2014-2015, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#include "Traverser.h"

#include "Basics/Thread.h"

using namespace std;
using namespace triagens::basics;

class Searcher : public Thread {

    Traverser* _traverser;
    Traverser::ThreadInfo& _myInfo;
    Traverser::ThreadInfo& _peerInfo;
    Traverser::VertexId _start;
    Traverser::ExpanderFunction _expander;
    string _id;

  public:

    Searcher (Traverser* traverser, Traverser::ThreadInfo& myInfo,
              Traverser::ThreadInfo& peerInfo, Traverser::VertexId start,
              Traverser::ExpanderFunction expander, string id)
      : Thread(id), _traverser(traverser), _myInfo(myInfo),
        _peerInfo(peerInfo), _start(start), _expander(expander), _id(id) {
    }

////////////////////////////////////////////////////////////////////////////////
/// @brief Insert a neighbor to the todo list.
////////////////////////////////////////////////////////////////////////////////

  private:

    void insertNeighbor (Traverser::VertexId& neighbor,
                         Traverser::VertexId& predecessor,
                         Traverser::EdgeId& edge,
                         Traverser::EdgeWeight weight) {

      std::lock_guard<std::mutex> guard(_myInfo._mutex);
      Traverser::Step* s = _myInfo._pq.lookup(neighbor);

      // Not found, so insert it:
      if (s == nullptr) {
        _myInfo._pq.insert(neighbor,
                           Traverser::Step(neighbor, predecessor,
                                           weight, edge));
        return;
      }
      if (s->_done) {
        return;
      }
      if (s->_weight > weight) {
        _myInfo._pq.lowerWeight(neighbor, weight);
      }
    }

////////////////////////////////////////////////////////////////////////////////
/// @brief Lookup our current vertex in the data of our peer.
////////////////////////////////////////////////////////////////////////////////

    void lookupPeer (Traverser::VertexId& vertex,
                     Traverser::EdgeWeight weight) {

      std::lock_guard<std::mutex> guard(_peerInfo._mutex);
      Traverser::Step* s = _peerInfo._pq.lookup(vertex);
      if (s == nullptr) {
        // Not found, nothing more to do
        return;
      }
      Traverser::EdgeWeight total = s->_weight + weight;

      // Update the highscore:
      std::lock_guard<std::mutex> guard2(_traverser->_resultMutex);
      if (!_traverser->_highscoreSet || total < _traverser->_highscore) {
        _traverser->_highscoreSet = true;
        _traverser->_highscore = total;
      }

      // Now the highscore is set!

      // Did we find a solution together with the other thread?
      if (s->_done) {
        if (total <= _traverser->_highscore) {
          _traverser->_intermediate = vertex;
          _traverser->_bingo = true;
        }
        // We found a way, but somebody else found a better way, so
        // this is not the shortest path
        return;
      }

      // Did we find a solution on our own? This is for the single thread
      // case and for the case that the other thread is too slow to even
      // finish its own start vertex!
      if (s->_weight == 0) {
        // We have found the target, we have finished all vertices with
        // a smaller weight than this one (and did not succeed), so this
        // must be a best solution:
        _traverser->_intermediate = vertex;
        _traverser->_bingo = true;
      }
    }

////////////////////////////////////////////////////////////////////////////////
/// @brief Search graph starting at Start following edges of the given
/// direction only
////////////////////////////////////////////////////////////////////////////////

  public:

    virtual void run () {

      Traverser::VertexId v;
      Traverser::Step s;
      bool b = _myInfo._pq.popMinimal(v, s, true);

      std::vector<Traverser::Step> neighbors;

      // Iterate while no bingo found and
      // there still is a vertex on the stack.
      while (!_traverser->_bingo && b) {
        neighbors.clear();
        _expander(v, neighbors);
        for (auto& neighbor : neighbors) {
          insertNeighbor(neighbor._vertex, v, neighbor._edge,
                         s._weight + neighbor._weight);
        }
        lookupPeer(v, s._weight);

        std::lock_guard<std::mutex> guard(_myInfo._mutex);
        Traverser::Step* s2 = _myInfo._pq.lookup(v);
        s2->_done = true;
        b = _myInfo._pq.popMinimal(v, s, true);
      }
    }
};

////////////////////////////////////////////////////////////////////////////////
/// @brief return the shortest path between the start and target vertex.
////////////////////////////////////////////////////////////////////////////////

Traverser::Path* Traverser::shortestPath (VertexId const& start,
                                          VertexId const& target) {

  // For the result:
  std::deque<VertexId> r_vertices;
  std::deque<VertexId> r_edges;
  _highscoreSet = false;
  _highscore = 0;
  _bingo = false;

  // Forward with initialization:
  string empty;
  ThreadInfo forward;
  forward._pq.insert(start, Step(start, empty, 0, empty));

  // backward with initialization:
  ThreadInfo backward;
  backward._pq.insert(target, Step(target, empty, 0, empty));

  // Now the searcher threads:
  Searcher forwardSearcher(this, forward, backward, start,
                           _forwardExpander, "Forward");
  Searcher backwardSearcher(this, backward, forward, target,
                            _backwardExpander, "Backward");
  forwardSearcher.start();
  //backwardSearcher.start();
  forwardSearcher.join();
  //backwardSearcher.join();

  if (!_bingo || _intermediate == "") {
    return nullptr;
  }

  Step* s = forward._pq.lookup(_intermediate);
  r_vertices.push_back(_intermediate);

  // FORWARD Go path back from intermediate -> start.
  // Insert all vertices and edges at front of vector
  // Do NOT! insert the intermediate vertex
  while (s->_predecessor != "") {
    r_edges.push_front(s->_edge);
    r_vertices.push_front(s->_predecessor);
    s = forward._pq.lookup(s->_predecessor);
  }

  // BACKWARD Go path back from intermediate -> target.
  // Insert all vertices and edges at back of vector
  // Also insert the intermediate vertex
  s = backward._pq.lookup(_intermediate);
  while (s->_predecessor != "") {
    r_edges.push_back(s->_edge);
    r_vertices.push_back(s->_predecessor);
    s = backward._pq.lookup(s->_predecessor);
  }
  return new Path(r_vertices, r_edges, _highscore);
};

<|endoftext|>


<commit_before>#include "gru.h"

#include <sstream>
#include <gflags/gflags.h>
#include <boost/algorithm/string.hpp>
#include <vector>
#include "logging.h"
#include "util.h"

DECLARE_int32(galaxy_deploy_step);
DECLARE_string(minion_path);
DECLARE_string(nexus_server_list);
DECLARE_string(nexus_root_path);
DECLARE_string(master_path);
DECLARE_bool(enable_cpu_soft_limit);
DECLARE_bool(enable_memory_soft_limit);
DECLARE_string(galaxy_node_label);
DECLARE_string(galaxy_user);
DECLARE_string(galaxy_token);
DECLARE_string(galaxy_pool);
DECLARE_int32(max_minions_per_host);

namespace baidu {
namespace shuttle {

static const int64_t default_additional_map_memory = 1024l * 1024 * 1024;
static const int64_t default_additional_reduce_memory = 2048l * 1024 * 1024;
static const int default_map_additional_millicores = 0;
static const int default_reduce_additional_millicores = 500;

int Gru::additional_map_millicores = default_map_additional_millicores;
int Gru::additional_reduce_millicores = default_reduce_additional_millicores;
int64_t Gru::additional_map_memory = default_additional_map_memory;
int64_t Gru::additional_reduce_memory = default_additional_reduce_memory;

Gru::Gru(::baidu::galaxy::sdk::AppMaster* galaxy, JobDescriptor* job,
         const std::string& job_id, WorkMode mode) :
        galaxy_(galaxy), job_(job), job_id_(job_id), mode_(mode) {
    mode_str_ = ((mode == kReduce) ? "reduce" : "map");
    minion_name_ = job->name() + "_" + mode_str_;
}

Status Gru::Start() {
    ::baidu::galaxy::sdk::SubmitJobRequest galaxy_job;
    galaxy_job.user.user = FLAGS_galaxy_user;
    galaxy_job.user.token = FLAGS_galaxy_token;
    galaxy_job.hostname = ::baidu::common::util::GetLocalHostName();
    galaxy_job.job.deploy.pools.push_back(FLAGS_galaxy_pool);
    galaxy_job.job.name = minion_name_ + "@minion";
    galaxy_job.job.type = ::baidu::galaxy::sdk::kJobBatch;
    galaxy_job.job.deploy.replica = (mode_ == kReduce) ? job_->reduce_capacity() : job_->map_capacity();
    galaxy_job.job.deploy.step = FLAGS_galaxy_deploy_step;
    galaxy_job.job.deploy.interval = 1;
    galaxy_job.job.deploy.max_per_host = FLAGS_max_minions_per_host;
    galaxy_job.job.deploy.update_break_count = 0;
    galaxy_job.job.version = "1.0.0";
    galaxy_job.job.run_user = "galaxy";
    if (!FLAGS_galaxy_node_label.empty()) {
        galaxy_job.job.deploy.tag = FLAGS_galaxy_node_label;
    }
    ::baidu::galaxy::sdk::PodDescription & pod_desc = galaxy_job.job.pod;
    pod_desc.workspace_volum.size = (3L << 30);
    pod_desc.workspace_volum.medium = ::baidu::galaxy::sdk::kDisk;
    pod_desc.workspace_volum.exclusive = false;
    pod_desc.workspace_volum.readonly = false;
    pod_desc.workspace_volum.use_symlink = false;
    pod_desc.workspace_volum.dest_path = "/home/shuttle";
    pod_desc.workspace_volum.type = ::baidu::galaxy::sdk::kEmptyDir;

    ::baidu::galaxy::sdk::TaskDescription task_desc;
    if (mode_str_ == "map") {
        task_desc.cpu.milli_core = job_->millicores() + additional_map_millicores;
    } else {
        task_desc.cpu.milli_core = job_->millicores() + additional_reduce_millicores;
    }
    task_desc.memory.size = job_->memory() +
        ((mode_ == kReduce) ? additional_reduce_memory : additional_map_memory);
    std::string app_package;
    std::vector<std::string> cache_archive_list;
    int file_size = job_->files().size();
    for (int i = 0; i < file_size; ++i) {
        const std::string& file = job_->files(i);
        if (boost::starts_with(file, "hdfs://")) {
            cache_archive_list.push_back(file);
        } else {
            app_package = file;
        }
    }
    std::stringstream ss;
    if (!job_->input_dfs().user().empty()) {
        ss << "hadoop_job_ugi=" << job_->input_dfs().user()
           << "," << job_->input_dfs().password()
           << " fs_default_name=hdfs://" << job_->input_dfs().host()
           << ":" << job_->input_dfs().port() << " ";
    }
    for (size_t i = 0; i < cache_archive_list.size(); i++) {
        ss << "cache_archive_" << i << "=" << cache_archive_list[i] << " ";
    }
    ss << "app_package=" << app_package
       << " ./minion_boot.sh -jobid=" << job_id_ << " -nexus_addr=" << FLAGS_nexus_server_list
       << " -master_nexus_path=" << FLAGS_nexus_root_path + FLAGS_master_path
       << " -work_mode=" << ((mode_ == kMapOnly) ? "map-only" : mode_str_);
    std::stringstream ss_stop;
    ss_stop << "source hdfs_env.sh; ./minion -jobid=" << job_id_ << " -nexus_addr=" << FLAGS_nexus_server_list
            << " -master_nexus_path=" << FLAGS_nexus_root_path + FLAGS_master_path
            << " -work_mode=" << ((mode_ == kMapOnly) ? "map-only" : mode_str_)
            << " -kill_task";
    task_desc.exe_package.package.source_path = FLAGS_minion_path;
    task_desc.exe_package.package.dest_path = ".";
    task_desc.exe_package.package.version = "1.0";
    task_desc.exe_package.start_cmd = ss.str().c_str();
    task_desc.exe_package.stop_cmd = ss_stop.str().c_str();
    task_desc.tcp_throt.recv_bps_quota = (50L << 20);
    task_desc.tcp_throt.send_bps_quota = (50L << 20);
    task_desc.blkio.weight = 100;
    ::baidu::galaxy::sdk::PortRequired port_req;
    port_req.port = "dynamic";
    port_req.port_name = "NFS_CLIENT_PORT";
    task_desc.ports.push_back(port_req);
    port_req.port = "dynamic";
    port_req.port_name = "MINION_PORT";
    task_desc.ports.push_back(port_req);
    if (FLAGS_enable_cpu_soft_limit) {
        task_desc.cpu.excess = true;
    } else {
        task_desc.cpu.excess = false;
    }
    if (FLAGS_enable_memory_soft_limit) {
        task_desc.memory.excess = true;
    } else {
        task_desc.memory.excess = false;
    }
    pod_desc.tasks.push_back(task_desc);
    std::string minion_id;
    ::baidu::galaxy::sdk::SubmitJobResponse rsps;
    if (galaxy_->SubmitJob(galaxy_job, &rsps)) {
        minion_id = rsps.jobid;
        LOG(INFO, "galaxy job id: %s", minion_id.c_str());
        minion_id_ = minion_id;
        galaxy_job_ = galaxy_job;
        if (minion_id_.empty()) {
            LOG(INFO, "can not get galaxy job id");
            return kGalaxyError;
        }
        return kOk;
    } else {
        LOG(WARNING, "galaxy error: %s", rsps.error_code.reason.c_str());
    }
    return kGalaxyError;
}

Status Gru::Kill() {
    LOG(INFO, "kill galaxy job: %s", minion_id_.c_str());
    if (minion_id_.empty()) {
        return kOk;
    }
    ::baidu::galaxy::sdk::RemoveJobRequest rqst;
    ::baidu::galaxy::sdk::RemoveJobResponse rsps;
    rqst.jobid = minion_id_;
    rqst.user = galaxy_job_.user;
    rqst.hostname = ::baidu::common::util::GetLocalHostName();
    if (galaxy_->RemoveJob(rqst, &rsps)) {
        return kOk;
    } else {
        LOG(WARNING, "galaxy error: %s", rsps.error_code.reason.c_str());
    }
    return kGalaxyError;
}

Status Gru::Update(const std::string& priority,
                   int capacity) {
    ::baidu::galaxy::sdk::JobDescription job_desc = galaxy_job_.job;
    if (!priority.empty()) {
        //job_desc.priority = priority;
    }
    if (capacity != -1) {
        job_desc.deploy.replica = capacity;
    }
    ::baidu::galaxy::sdk::UpdateJobRequest rqst;
    ::baidu::galaxy::sdk::UpdateJobResponse rsps;
    rqst.user = galaxy_job_.user;
    rqst.job = job_desc;
    rqst.jobid = minion_id_;
    rqst.operate = ::baidu::galaxy::sdk::kUpdateJobDefault;
    rqst.hostname = ::baidu::common::util::GetLocalHostName();
    if (galaxy_->UpdateJob(rqst, &rsps)) {
        if (!priority.empty()) {
            //galaxy_job_.priority = priority;
        }
        if (capacity != -1) {
            galaxy_job_.job.deploy.replica = capacity;
        }
        return kOk;
    } else {
        LOG(WARNING, "galaxy error: %s", rsps.error_code.reason.c_str());
    }
    return kGalaxyError;
}

}
}

<commit_msg>set deploy step<commit_after>#include "gru.h"

#include <sstream>
#include <gflags/gflags.h>
#include <boost/algorithm/string.hpp>
#include <vector>
#include "logging.h"
#include "util.h"

DECLARE_int32(galaxy_deploy_step);
DECLARE_string(minion_path);
DECLARE_string(nexus_server_list);
DECLARE_string(nexus_root_path);
DECLARE_string(master_path);
DECLARE_bool(enable_cpu_soft_limit);
DECLARE_bool(enable_memory_soft_limit);
DECLARE_string(galaxy_node_label);
DECLARE_string(galaxy_user);
DECLARE_string(galaxy_token);
DECLARE_string(galaxy_pool);
DECLARE_int32(max_minions_per_host);

namespace baidu {
namespace shuttle {

static const int64_t default_additional_map_memory = 1024l * 1024 * 1024;
static const int64_t default_additional_reduce_memory = 2048l * 1024 * 1024;
static const int default_map_additional_millicores = 0;
static const int default_reduce_additional_millicores = 500;

int Gru::additional_map_millicores = default_map_additional_millicores;
int Gru::additional_reduce_millicores = default_reduce_additional_millicores;
int64_t Gru::additional_map_memory = default_additional_map_memory;
int64_t Gru::additional_reduce_memory = default_additional_reduce_memory;

Gru::Gru(::baidu::galaxy::sdk::AppMaster* galaxy, JobDescriptor* job,
         const std::string& job_id, WorkMode mode) :
        galaxy_(galaxy), job_(job), job_id_(job_id), mode_(mode) {
    mode_str_ = ((mode == kReduce) ? "reduce" : "map");
    minion_name_ = job->name() + "_" + mode_str_;
}

Status Gru::Start() {
    ::baidu::galaxy::sdk::SubmitJobRequest galaxy_job;
    galaxy_job.user.user = FLAGS_galaxy_user;
    galaxy_job.user.token = FLAGS_galaxy_token;
    galaxy_job.hostname = ::baidu::common::util::GetLocalHostName();
    galaxy_job.job.deploy.pools.push_back(FLAGS_galaxy_pool);
    galaxy_job.job.name = minion_name_ + "@minion";
    galaxy_job.job.type = ::baidu::galaxy::sdk::kJobBatch;
    galaxy_job.job.deploy.replica = (mode_ == kReduce) ? job_->reduce_capacity() : job_->map_capacity();
    galaxy_job.job.deploy.step = std::min((int)FLAGS_galaxy_deploy_step, (int)galaxy_job.job.deploy.replica);
    galaxy_job.job.deploy.interval = 1;
    galaxy_job.job.deploy.max_per_host = FLAGS_max_minions_per_host;
    galaxy_job.job.deploy.update_break_count = 0;
    galaxy_job.job.version = "1.0.0";
    galaxy_job.job.run_user = "galaxy";
    if (!FLAGS_galaxy_node_label.empty()) {
        galaxy_job.job.deploy.tag = FLAGS_galaxy_node_label;
    }
    ::baidu::galaxy::sdk::PodDescription & pod_desc = galaxy_job.job.pod;
    pod_desc.workspace_volum.size = (3L << 30);
    pod_desc.workspace_volum.medium = ::baidu::galaxy::sdk::kDisk;
    pod_desc.workspace_volum.exclusive = false;
    pod_desc.workspace_volum.readonly = false;
    pod_desc.workspace_volum.use_symlink = false;
    pod_desc.workspace_volum.dest_path = "/home/shuttle";
    pod_desc.workspace_volum.type = ::baidu::galaxy::sdk::kEmptyDir;

    ::baidu::galaxy::sdk::TaskDescription task_desc;
    if (mode_str_ == "map") {
        task_desc.cpu.milli_core = job_->millicores() + additional_map_millicores;
    } else {
        task_desc.cpu.milli_core = job_->millicores() + additional_reduce_millicores;
    }
    task_desc.memory.size = job_->memory() +
        ((mode_ == kReduce) ? additional_reduce_memory : additional_map_memory);
    std::string app_package;
    std::vector<std::string> cache_archive_list;
    int file_size = job_->files().size();
    for (int i = 0; i < file_size; ++i) {
        const std::string& file = job_->files(i);
        if (boost::starts_with(file, "hdfs://")) {
            cache_archive_list.push_back(file);
        } else {
            app_package = file;
        }
    }
    std::stringstream ss;
    if (!job_->input_dfs().user().empty()) {
        ss << "hadoop_job_ugi=" << job_->input_dfs().user()
           << "," << job_->input_dfs().password()
           << " fs_default_name=hdfs://" << job_->input_dfs().host()
           << ":" << job_->input_dfs().port() << " ";
    }
    for (size_t i = 0; i < cache_archive_list.size(); i++) {
        ss << "cache_archive_" << i << "=" << cache_archive_list[i] << " ";
    }
    ss << "app_package=" << app_package
       << " ./minion_boot.sh -jobid=" << job_id_ << " -nexus_addr=" << FLAGS_nexus_server_list
       << " -master_nexus_path=" << FLAGS_nexus_root_path + FLAGS_master_path
       << " -work_mode=" << ((mode_ == kMapOnly) ? "map-only" : mode_str_);
    std::stringstream ss_stop;
    ss_stop << "source hdfs_env.sh; ./minion -jobid=" << job_id_ << " -nexus_addr=" << FLAGS_nexus_server_list
            << " -master_nexus_path=" << FLAGS_nexus_root_path + FLAGS_master_path
            << " -work_mode=" << ((mode_ == kMapOnly) ? "map-only" : mode_str_)
            << " -kill_task";
    task_desc.exe_package.package.source_path = FLAGS_minion_path;
    task_desc.exe_package.package.dest_path = ".";
    task_desc.exe_package.package.version = "1.0";
    task_desc.exe_package.start_cmd = ss.str().c_str();
    task_desc.exe_package.stop_cmd = ss_stop.str().c_str();
    task_desc.tcp_throt.recv_bps_quota = (50L << 20);
    task_desc.tcp_throt.send_bps_quota = (50L << 20);
    task_desc.blkio.weight = 100;
    ::baidu::galaxy::sdk::PortRequired port_req;
    port_req.port = "dynamic";
    port_req.port_name = "NFS_CLIENT_PORT";
    task_desc.ports.push_back(port_req);
    port_req.port = "dynamic";
    port_req.port_name = "MINION_PORT";
    task_desc.ports.push_back(port_req);
    if (FLAGS_enable_cpu_soft_limit) {
        task_desc.cpu.excess = true;
    } else {
        task_desc.cpu.excess = false;
    }
    if (FLAGS_enable_memory_soft_limit) {
        task_desc.memory.excess = true;
    } else {
        task_desc.memory.excess = false;
    }
    pod_desc.tasks.push_back(task_desc);
    std::string minion_id;
    ::baidu::galaxy::sdk::SubmitJobResponse rsps;
    if (galaxy_->SubmitJob(galaxy_job, &rsps)) {
        minion_id = rsps.jobid;
        LOG(INFO, "galaxy job id: %s", minion_id.c_str());
        minion_id_ = minion_id;
        galaxy_job_ = galaxy_job;
        if (minion_id_.empty()) {
            LOG(INFO, "can not get galaxy job id");
            return kGalaxyError;
        }
        return kOk;
    } else {
        LOG(WARNING, "galaxy error: %s", rsps.error_code.reason.c_str());
    }
    return kGalaxyError;
}

Status Gru::Kill() {
    LOG(INFO, "kill galaxy job: %s", minion_id_.c_str());
    if (minion_id_.empty()) {
        return kOk;
    }
    ::baidu::galaxy::sdk::RemoveJobRequest rqst;
    ::baidu::galaxy::sdk::RemoveJobResponse rsps;
    rqst.jobid = minion_id_;
    rqst.user = galaxy_job_.user;
    rqst.hostname = ::baidu::common::util::GetLocalHostName();
    if (galaxy_->RemoveJob(rqst, &rsps)) {
        return kOk;
    } else {
        LOG(WARNING, "galaxy error: %s", rsps.error_code.reason.c_str());
    }
    return kGalaxyError;
}

Status Gru::Update(const std::string& priority,
                   int capacity) {
    ::baidu::galaxy::sdk::JobDescription job_desc = galaxy_job_.job;
    if (!priority.empty()) {
        //job_desc.priority = priority;
    }
    if (capacity != -1) {
        job_desc.deploy.replica = capacity;
    }
    ::baidu::galaxy::sdk::UpdateJobRequest rqst;
    ::baidu::galaxy::sdk::UpdateJobResponse rsps;
    rqst.user = galaxy_job_.user;
    rqst.job = job_desc;
    rqst.jobid = minion_id_;
    rqst.operate = ::baidu::galaxy::sdk::kUpdateJobDefault;
    rqst.hostname = ::baidu::common::util::GetLocalHostName();
    if (galaxy_->UpdateJob(rqst, &rsps)) {
        if (!priority.empty()) {
            //galaxy_job_.priority = priority;
        }
        if (capacity != -1) {
            galaxy_job_.job.deploy.replica = capacity;
        }
        return kOk;
    } else {
        LOG(WARNING, "galaxy error: %s", rsps.error_code.reason.c_str());
    }
    return kGalaxyError;
}

}
}

<|endoftext|>


<commit_before>#include "affinity.hh" // FIXME
#include "command.hh"
#include "progress-bar.hh"
#include "shared.hh"
#include "store-api.hh"
#include "sync.hh"
#include "thread-pool.hh"

#include <atomic>

using namespace nix;

struct CmdVerifyPaths : StorePathsCommand
{
    bool noContents = false;
    bool noSigs = false;

    CmdVerifyPaths()
    {
        mkFlag(0, "no-contents", "do not verify the contents of each store path", &noContents);
        mkFlag(0, "no-sigs", "do not verify whether each store path has a valid signature", &noSigs);
    }

    std::string name() override
    {
        return "verify-paths";
    }

    std::string description() override
    {
        return "verify the integrity of store paths";
    }

    void run(ref<Store> store, Paths storePaths) override
    {
        restoreAffinity(); // FIXME

        auto publicKeys = getDefaultPublicKeys();

        std::atomic<size_t> untrusted{0};
        std::atomic<size_t> corrupted{0};
        std::atomic<size_t> done{0};
        std::atomic<size_t> failed{0};

        ProgressBar progressBar;

        auto showProgress = [&](bool final) {
            std::string s;
            if (final)
                s = (format("checked %d paths") % storePaths.size()).str();
            else
                s = (format("[%d/%d checked") % done % storePaths.size()).str();
            if (corrupted > 0)
                s += (format(", %d corrupted") % corrupted).str();
            if (untrusted > 0)
                s += (format(", %d untrusted") % untrusted).str();
            if (failed > 0)
                s += (format(", %d failed") % failed).str();
            if (!final) s += "]";
            return s;
        };

        progressBar.updateStatus(showProgress(false));

        ThreadPool pool;

        auto doPath = [&](const Path & storePath) {
            try {
                progressBar.startActivity(format("checking ‘%s’") % storePath);

                auto info = store->queryPathInfo(storePath);

                if (!noContents) {

                    HashSink sink(info.narHash.type);
                    store->narFromPath(storePath, sink);

                    auto hash = sink.finish();

                    if (hash.first != info.narHash) {
                        corrupted = 1;
                        printMsg(lvlError,
                            format("path ‘%s’ was modified! expected hash ‘%s’, got ‘%s’")
                            % storePath % printHash(info.narHash) % printHash(hash.first));
                    }

                }

                if (!noSigs) {

                    if (!info.ultimate && !info.checkSignatures(publicKeys)) {
                        untrusted++;
                        printMsg(lvlError, format("path ‘%s’ is untrusted") % storePath);
                    }

                }

                done++;

                progressBar.updateStatus(showProgress(false));

            } catch (Error & e) {
                printMsg(lvlError, format(ANSI_RED "error:" ANSI_NORMAL " %s") % e.what());
                failed++;
            }
        };

        for (auto & storePath : storePaths)
            pool.enqueue(std::bind(doPath, storePath));

        pool.process();

        progressBar.done();

        printMsg(lvlInfo, showProgress(true));

        throw Exit(
            (corrupted ? 1 : 0) |
            (untrusted ? 2 : 0) |
            (failed ? 4 : 0));
    }
};

static RegisterCommand r1(make_ref<CmdVerifyPaths>());
<commit_msg>Add "nix verify-store" command<commit_after>#include "affinity.hh" // FIXME
#include "command.hh"
#include "progress-bar.hh"
#include "shared.hh"
#include "store-api.hh"
#include "sync.hh"
#include "thread-pool.hh"

#include <atomic>

using namespace nix;

struct MixVerify : virtual Args
{
    bool noContents = false;
    bool noSigs = false;

    MixVerify()
    {
        mkFlag(0, "no-contents", "do not verify the contents of each store path", &noContents);
        mkFlag(0, "no-sigs", "do not verify whether each store path has a valid signature", &noSigs);
    }

    void verifyPaths(ref<Store> store, const Paths & storePaths)
    {
        restoreAffinity(); // FIXME

        auto publicKeys = getDefaultPublicKeys();

        std::atomic<size_t> untrusted{0};
        std::atomic<size_t> corrupted{0};
        std::atomic<size_t> done{0};
        std::atomic<size_t> failed{0};

        ProgressBar progressBar;

        auto showProgress = [&](bool final) {
            std::string s;
            if (final)
                s = (format("checked %d paths") % storePaths.size()).str();
            else
                s = (format("[%d/%d checked") % done % storePaths.size()).str();
            if (corrupted > 0)
                s += (format(", %d corrupted") % corrupted).str();
            if (untrusted > 0)
                s += (format(", %d untrusted") % untrusted).str();
            if (failed > 0)
                s += (format(", %d failed") % failed).str();
            if (!final) s += "]";
            return s;
        };

        progressBar.updateStatus(showProgress(false));

        ThreadPool pool;

        auto doPath = [&](const Path & storePath) {
            try {
                progressBar.startActivity(format("checking ‘%s’") % storePath);

                auto info = store->queryPathInfo(storePath);

                if (!noContents) {

                    HashSink sink(info.narHash.type);
                    store->narFromPath(storePath, sink);

                    auto hash = sink.finish();

                    if (hash.first != info.narHash) {
                        corrupted = 1;
                        printMsg(lvlError,
                            format("path ‘%s’ was modified! expected hash ‘%s’, got ‘%s’")
                            % storePath % printHash(info.narHash) % printHash(hash.first));
                    }

                }

                if (!noSigs) {

                    if (!info.ultimate && !info.checkSignatures(publicKeys)) {
                        untrusted++;
                        printMsg(lvlError, format("path ‘%s’ is untrusted") % storePath);
                    }

                }

                done++;

                progressBar.updateStatus(showProgress(false));

            } catch (Error & e) {
                printMsg(lvlError, format(ANSI_RED "error:" ANSI_NORMAL " %s") % e.what());
                failed++;
            }
        };

        for (auto & storePath : storePaths)
            pool.enqueue(std::bind(doPath, storePath));

        pool.process();

        progressBar.done();

        printMsg(lvlInfo, showProgress(true));

        throw Exit(
            (corrupted ? 1 : 0) |
            (untrusted ? 2 : 0) |
            (failed ? 4 : 0));
    }
};

struct CmdVerifyPaths : StorePathsCommand, MixVerify
{
    CmdVerifyPaths()
    {
    }

    std::string name() override
    {
        return "verify-paths";
    }

    std::string description() override
    {
        return "verify the integrity of store paths";
    }

    void run(ref<Store> store, Paths storePaths) override
    {
        verifyPaths(store, storePaths);
    }
};

static RegisterCommand r1(make_ref<CmdVerifyPaths>());

struct CmdVerifyStore : StoreCommand, MixVerify
{
    CmdVerifyStore()
    {
    }

    std::string name() override
    {
        return "verify-store";
    }

    std::string description() override
    {
        return "verify the integrity of all paths in the Nix store";
    }

    void run(ref<Store> store) override
    {
        // FIXME: use store->verifyStore()?

        PathSet validPaths = store->queryAllValidPaths();

        verifyPaths(store, Paths(validPaths.begin(), validPaths.end()));
    }
};

static RegisterCommand r2(make_ref<CmdVerifyStore>());
<|endoftext|>


<commit_before>/*
  Copyright (c) 2016 Louis McCarthy
  All rights reserved.

  Licensed under MIT License, see LICENSE for full License text

  opensspro.cpp - Basic USB driver for the Starshoot Pro V2.0 Color Imager
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
#include <time.h>
#include <unistd.h>

#include "opensspro.h"

#define USB_REQ_STATUS    0x02
#define USB_REQ_SET_FRAME 0x0B
#define USB_REQ_CAPTURE   0x03
#define USB_REQ_DOWNLOAD  0x04
#define USB_REQ_ABORT     0x05
#define USB_REQ_SET_DIO   0x0C
#define USB_REQ_UNKNOWN   0x09

#define USB_TIMEOUT      1000
#define USB_RX_ENDPOINT  0x82
#define USB_CMD_ENDPOINT 0x08
#define USB_CONTROL_TYPE 0x03
#define START_BYTE       0xA5

#define IMAGE_WIDTH  3040
#define IMAGE_HEIGHT 2024
#define BUFFER_SIZE  1024

using namespace OpenSSPRO;

libusb_context* usb = NULL;

SSPRO::SSPRO()
{
    lastImage.width = IMAGE_WIDTH;
    lastImage.height = IMAGE_HEIGHT;
    lastImage.data = NULL;

    DEBUG("Searching for USB root...");
    if (usb == NULL)
    {
        int result = libusb_init(&usb);
        if (result < 0)
            ERROR("Failed to init libusb, result = %d", result);
        else
            DEBUG("Ready\n");
    }
}

bool SSPRO::Connect()
{
    DEBUG("Looking for camera...");
    if ((this->device = libusb_open_device_with_vid_pid(usb, SSPRO_VENDOR_ID, SSPRO_PRODUCT_ID)) == NULL)
    {
        DEBUG("NOT found!\n");
        return false;
    }
    DEBUG("Found it!\n");

    DEBUG("Checking for kernel driver...");
    int result;
    if (libusb_kernel_driver_active(this->device, 0) == 1)
    {
        result = libusb_detach_kernel_driver(this->device, 0);
        if (result < 0)
            ERROR("Failed to detach kernel driver, result = %d", result);
    }
    DEBUG("Done\n");

    DEBUG("Setting USB configuration...");
    result = libusb_set_configuration(this->device, 1); // Only one configuration available
    if (result < 0)
        ERROR("Failed to set configuration, result = %d", result);
    DEBUG("Done\n");

    DEBUG("Claiming USB interface...");
    result = libusb_claim_interface(this->device, 0); // Only one interface available
    if (result < 0)
        ERROR("Failed to claim interface, result = %d", result);
    DEBUG("Done\n");

    this->Init();
    return true;
}

void SSPRO::Disconnect()
{
    DEBUG("Disconnecting camera...");
    if (this->device)
        libusb_close(this->device);
    this->device = NULL;
    DEBUG("Done\n");
}

void SSPRO::GetStatus()
{
    DEBUG("Requesting camera status...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_STATUS, 0x00, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to get status, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_STATUS);
}

void SSPRO::SetupFrame()
{
    DEBUG("Setting up frame...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_SET_FRAME, 0x00, 0x00, 0x03, 0xF9 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to setup frame, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_SET_FRAME);
}

bool SSPRO::IsConnected()
{
    return (this->device != NULL);
}

bool SSPRO::GetCMDResult(unsigned char cmd)
{
    DEBUG("Getting command result...");
    int rxCount;
    unsigned char rxData[8];
    int result = libusb_bulk_transfer(this->device, USB_RX_ENDPOINT, rxData, sizeof(rxData), &rxCount, USB_TIMEOUT);
    if (result < 0)
    {
        ERROR("Failed to read command result, returned %d", result);
        return false;
    }

    if (rxCount != 8)
    {
        ERROR("Invalid packet size, %d", rxCount);
        return false;
    }

    if (rxData[0] != 0xA5)
    {
        ERROR("Invalid header, %d", rxData[0]);
        return false;
    }

    if (rxData[2] != cmd)
    {
        ERROR("Mismatched command type, RX:%d vs CMD:%d", rxData[1], cmd);
        return false;
    }

    switch (rxData[2])
    {
        case USB_REQ_STATUS:
            DEBUG("Found Status Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            capturing = (rxData[4] & 0x01) == 0x01;
            frameReady = (rxData[4] & 0x02) == 0x02;
            DEBUG("Capturing = %d, FrameReady = %d\n", capturing, frameReady);
            return true;
            break;
        case USB_REQ_SET_FRAME:
            DEBUG("Found Frame Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_CAPTURE:
            DEBUG("Found Capture Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_DOWNLOAD:
            DEBUG("Found Download Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_ABORT:
            DEBUG("Found Abort Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_SET_DIO:
            DEBUG("Found DIO Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_UNKNOWN:
            DEBUG("Found 0x09 Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        default:
            ERROR("Unknown command result, %d", rxData[1]);
            break;
    }

    return false;
}

struct rawImage* SSPRO::Capture(int ms)
{
    DEBUG("Performing a blocking capture...\n");
    if (!this->StartCapture(ms))
        return NULL;

    // Wait capture time + 100 ms
    sleep(ms+100);

    // while status == busy
    while (!this->frameReady)
    {
        this->GetStatus();
        sleep(500);
    }

    this->DownloadFrame();
    return &lastImage;
}

bool SSPRO::StartCapture(int ms)
{
    this->SetupFrame();

    DEBUG("Starting capture...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_CAPTURE, 0x01, 0x04, 0x92, 0x02 }; // 120s Color 1x1 binning
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
    {
        ERROR("Failed to capture, result = %d", result);
        return false;
    }
    DEBUG("Done\n");

    return this->GetCMDResult(USB_REQ_CAPTURE);
}

void SSPRO::CancelCapture()
{
    DEBUG("Attempting to cancel capture...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_ABORT, 0x00, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to cancel capture, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_ABORT);
}

void SSPRO::DownloadFrame()
{ // TODO: Check camera status before trying to download
    DEBUG("Requesting frame download...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_DOWNLOAD, 0x00, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
    {
        ERROR("Failed to start download, result = %d", result);
        return;
    }
    DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_DOWNLOAD);

    DEBUG("Waiting for data from camera...");
    int rxCount;
    unsigned char* rxData = (unsigned char*)malloc(BUFFER_SIZE);
    unsigned char* newImage = (unsigned char*)malloc(IMAGE_WIDTH * IMAGE_HEIGHT);
    unsigned char* pointer = newImage;
    // Loop through until we get a partial buffer of data
    do
    {
        result = libusb_bulk_transfer(this->device, USB_RX_ENDPOINT, rxData, BUFFER_SIZE, &rxCount, USB_TIMEOUT);
        if (result < 0)
        {
            ERROR("Failed to download image, result = %d", result);
            free(rxData);
            return;
        }

        memcpy(pointer, rxData, rxCount);
        pointer += BUFFER_SIZE;
    } while (rxCount == BUFFER_SIZE);
    free(rxData);
    DEBUG("Done\n");

    DEBUG("Updating lastImage...");
    if (lastImage.data)
        free(lastImage.data);
    lastImage.data = newImage;
    lastImage.width = IMAGE_WIDTH;
    lastImage.height = IMAGE_HEIGHT;
    DEBUG("Done\n");
}

void SSPRO::SetDIO()
{
    DEBUG("Setting DIO...");
    int txCount;
    unsigned char dio =0x00;
    if (fanHigh)
        dio |= 0x02;
    if (coolerOn)
        dio |= 0x01;
    unsigned char data[6] = { START_BYTE, USB_REQ_SET_DIO, dio, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to set DIO, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_SET_DIO);
}

void SSPRO::Init()
{
    DEBUG("Initializing camera...");
    fanHigh = false;
    coolerOn = true;
    frameReady = false;
    readoutSpeed = READOUT_FASTEST;
    DEBUG("Done\n");

    this->SetDIO();
}
<commit_msg>Fixed capture function sleeps, it now uses milliseconds<commit_after>/*
  Copyright (c) 2016 Louis McCarthy
  All rights reserved.

  Licensed under MIT License, see LICENSE for full License text

  opensspro.cpp - Basic USB driver for the Starshoot Pro V2.0 Color Imager
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
#include <unistd.h>

#include "opensspro.h"

#define USB_REQ_STATUS    0x02
#define USB_REQ_SET_FRAME 0x0B
#define USB_REQ_CAPTURE   0x03
#define USB_REQ_DOWNLOAD  0x04
#define USB_REQ_ABORT     0x05
#define USB_REQ_SET_DIO   0x0C
#define USB_REQ_UNKNOWN   0x09

#define USB_TIMEOUT      1000
#define USB_RX_ENDPOINT  0x82
#define USB_CMD_ENDPOINT 0x08
#define USB_CONTROL_TYPE 0x03
#define START_BYTE       0xA5

#define IMAGE_WIDTH  3040
#define IMAGE_HEIGHT 2024
#define BUFFER_SIZE  1024

using namespace OpenSSPRO;

libusb_context* usb = NULL;

SSPRO::SSPRO()
{
    lastImage.width = IMAGE_WIDTH;
    lastImage.height = IMAGE_HEIGHT;
    lastImage.data = NULL;

    DEBUG("Searching for USB root...");
    if (usb == NULL)
    {
        int result = libusb_init(&usb);
        if (result < 0)
            ERROR("Failed to init libusb, result = %d", result);
        else
            DEBUG("Ready\n");
    }
}

bool SSPRO::Connect()
{
    DEBUG("Looking for camera...");
    if ((this->device = libusb_open_device_with_vid_pid(usb, SSPRO_VENDOR_ID, SSPRO_PRODUCT_ID)) == NULL)
    {
        DEBUG("NOT found!\n");
        return false;
    }
    DEBUG("Found it!\n");

    DEBUG("Checking for kernel driver...");
    int result;
    if (libusb_kernel_driver_active(this->device, 0) == 1)
    {
        result = libusb_detach_kernel_driver(this->device, 0);
        if (result < 0)
            ERROR("Failed to detach kernel driver, result = %d", result);
    }
    DEBUG("Done\n");

    DEBUG("Setting USB configuration...");
    result = libusb_set_configuration(this->device, 1); // Only one configuration available
    if (result < 0)
        ERROR("Failed to set configuration, result = %d", result);
    DEBUG("Done\n");

    DEBUG("Claiming USB interface...");
    result = libusb_claim_interface(this->device, 0); // Only one interface available
    if (result < 0)
        ERROR("Failed to claim interface, result = %d", result);
    DEBUG("Done\n");

    this->Init();
    return true;
}

void SSPRO::Disconnect()
{
    DEBUG("Disconnecting camera...");
    if (this->device)
        libusb_close(this->device);
    this->device = NULL;
    DEBUG("Done\n");
}

void SSPRO::GetStatus()
{
    DEBUG("Requesting camera status...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_STATUS, 0x00, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to get status, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_STATUS);
}

void SSPRO::SetupFrame()
{
    DEBUG("Setting up frame...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_SET_FRAME, 0x00, 0x00, 0x03, 0xF9 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to setup frame, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_SET_FRAME);
}

bool SSPRO::IsConnected()
{
    return (this->device != NULL);
}

bool SSPRO::GetCMDResult(unsigned char cmd)
{
    DEBUG("Getting command result...");
    int rxCount;
    unsigned char rxData[8];
    int result = libusb_bulk_transfer(this->device, USB_RX_ENDPOINT, rxData, sizeof(rxData), &rxCount, USB_TIMEOUT);
    if (result < 0)
    {
        ERROR("Failed to read command result, returned %d", result);
        return false;
    }

    if (rxCount != 8)
    {
        ERROR("Invalid packet size, %d", rxCount);
        return false;
    }

    if (rxData[0] != 0xA5)
    {
        ERROR("Invalid header, %d", rxData[0]);
        return false;
    }

    if (rxData[2] != cmd)
    {
        ERROR("Mismatched command type, RX:%d vs CMD:%d", rxData[1], cmd);
        return false;
    }

    switch (rxData[2])
    {
        case USB_REQ_STATUS:
            DEBUG("Found Status Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            capturing = (rxData[4] & 0x01) == 0x01;
            frameReady = (rxData[4] & 0x02) == 0x02;
            DEBUG("Capturing = %d, FrameReady = %d\n", capturing, frameReady);
            return true;
            break;
        case USB_REQ_SET_FRAME:
            DEBUG("Found Frame Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_CAPTURE:
            DEBUG("Found Capture Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_DOWNLOAD:
            DEBUG("Found Download Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_ABORT:
            DEBUG("Found Abort Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_SET_DIO:
            DEBUG("Found DIO Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        case USB_REQ_UNKNOWN:
            DEBUG("Found 0x09 Result\n");
            DEBUG("Data: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x\n",
                    rxData[0], rxData[1], rxData[2], rxData[3], rxData[4], rxData[5], rxData[6], rxData[7]);
            return true;
            break;
        default:
            ERROR("Unknown command result, %d", rxData[1]);
            break;
    }

    return false;
}

struct rawImage* SSPRO::Capture(int ms)
{
    DEBUG("Performing a blocking capture...\n");
    if (!this->StartCapture(ms))
        return NULL;

    // Wait capture time + 100 ms
    usleep((ms+100)*1000);

    // while status == busy
    while (!this->frameReady)
    {
        this->GetStatus();
        usleep(500*1000);
    }

    this->DownloadFrame();
    return &lastImage;
}

bool SSPRO::StartCapture(int ms)
{
    this->SetupFrame();

    DEBUG("Starting capture...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_CAPTURE, 0x01, 0x04, 0x92, 0x02 }; // 120s Color 1x1 binning
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
    {
        ERROR("Failed to capture, result = %d", result);
        return false;
    }
    DEBUG("Done\n");

    return this->GetCMDResult(USB_REQ_CAPTURE);
}

void SSPRO::CancelCapture()
{
    DEBUG("Attempting to cancel capture...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_ABORT, 0x00, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to cancel capture, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_ABORT);
}

void SSPRO::DownloadFrame()
{ // TODO: Check camera status before trying to download
    DEBUG("Requesting frame download...");
    int txCount;
    unsigned char data[6] = { START_BYTE, USB_REQ_DOWNLOAD, 0x00, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
    {
        ERROR("Failed to start download, result = %d", result);
        return;
    }
    DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_DOWNLOAD);

    DEBUG("Waiting for data from camera...");
    int rxCount;
    unsigned char* rxData = (unsigned char*)malloc(BUFFER_SIZE);
    unsigned char* newImage = (unsigned char*)malloc(IMAGE_WIDTH * IMAGE_HEIGHT);
    unsigned char* pointer = newImage;
    // Loop through until we get a partial buffer of data
    do
    {
        result = libusb_bulk_transfer(this->device, USB_RX_ENDPOINT, rxData, BUFFER_SIZE, &rxCount, USB_TIMEOUT);
        if (result < 0)
        {
            ERROR("Failed to download image, result = %d", result);
            free(rxData);
            return;
        }

        memcpy(pointer, rxData, rxCount);
        pointer += BUFFER_SIZE;
    } while (rxCount == BUFFER_SIZE);
    free(rxData);
    DEBUG("Done\n");

    DEBUG("Updating lastImage...");
    if (lastImage.data)
        free(lastImage.data);
    lastImage.data = newImage;
    lastImage.width = IMAGE_WIDTH;
    lastImage.height = IMAGE_HEIGHT;
    DEBUG("Done\n");
}

void SSPRO::SetDIO()
{
    DEBUG("Setting DIO...");
    int txCount;
    unsigned char dio =0x00;
    if (fanHigh)
        dio |= 0x02;
    if (coolerOn)
        dio |= 0x01;
    unsigned char data[6] = { START_BYTE, USB_REQ_SET_DIO, dio, 0x00, 0x00, 0x00 };
    int result = libusb_bulk_transfer(this->device, USB_CMD_ENDPOINT, data, 6, &txCount, USB_TIMEOUT);
    if (result < 0)
        ERROR("Failed to set DIO, result = %d", result);
    else
        DEBUG("Done\n");

    this->GetCMDResult(USB_REQ_SET_DIO);
}

void SSPRO::Init()
{
    DEBUG("Initializing camera...");
    fanHigh = false;
    coolerOn = true;
    frameReady = false;
    readoutSpeed = READOUT_FASTEST;
    DEBUG("Done\n");

    this->SetDIO();
}
<|endoftext|>


<commit_before>/**
 * @file piper_proc.cc
 */

#include "config.h"

#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <paths.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#include "piper_proc.hh"
#include "line_buffer.hh"

using namespace std;

piper_proc::piper_proc(int pipefd, bool timestamp, const char *filename)
    : pp_fd(-1), pp_timestamp(timestamp), pp_child(-1)
{
    assert(pipefd >= 0);

    if (filename) {
        if ((this->pp_fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0600)) == -1) {
                perror("Unable to open output file for stdin");
                throw error(errno);
        }
    }
    else {
        char piper_tmpname[PATH_MAX];
        const char *tmpdir;

        if ((tmpdir = getenv("TMPDIR")) == NULL) {
            tmpdir = _PATH_VARTMP;
        }
        snprintf(piper_tmpname, sizeof(piper_tmpname),
                 "%s/lnav.piper.XXXXXX",
                 tmpdir);
        if ((this->pp_fd = mkstemp(piper_tmpname)) == -1) {
            throw error(errno);
        }

        unlink(piper_tmpname);
    }

    this->pp_child = fork();
    switch (this->pp_child) {
    case -1:
	throw error(errno);
    case 0:
	{
	    auto_fd infd(pipefd);
	    line_buffer lb;
	    off_t woff = 0;
	    off_t off = 0;
	    char *line;
	    size_t len;

	    lb.set_fd(infd);
	    while ((line = lb.read_line(off, len)) != NULL) {
                int wrc;

	    	if (timestamp) {
	    	    char time_str[64];
	    	    struct timeval tv;
	    	    char ms_str[8];

	    	    gettimeofday(&tv, NULL);
	    	    strftime(time_str, sizeof(time_str), "%FT%T", gmtime(&tv.tv_sec));
	    	    snprintf(ms_str, sizeof(ms_str), ".%03d", tv.tv_usec / 1000);
	    	    strcat(time_str, ms_str);
	    	    strcat(time_str, "Z  ");
	    	    wrc = pwrite(this->pp_fd, time_str, strlen(time_str), woff);
                    if (wrc == -1) {
                        perror("Unable to write to output file for stdin");
                        break;
                    }
                    woff += wrc;
	    	}

	    	line[len] = '\n';
		/* Need to do pwrite here since the fd is used by the main
		 * lnav process as well.
		 */
		wrc = pwrite(this->pp_fd, line, len + 1, woff);
                if (wrc == -1) {
                    perror("Unable to write to output file for stdin");
                    break;
                }
		woff += wrc;
	    }
	}
	exit(0);
	break;
    default:
	break;
    }
}

piper_proc::~piper_proc()
{
    if (this->pp_child > 0) {
	int status;

	kill(this->pp_child, SIGTERM);
	while (waitpid(this->pp_child, &status, 0) < 0 && (errno == EINTR)) {
	    ;
	}

	this->pp_child = -1;
    }
}
<commit_msg>[build] fix build issues<commit_after>/**
 * @file piper_proc.cc
 */

#include "config.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <paths.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#include "piper_proc.hh"
#include "line_buffer.hh"

using namespace std;

piper_proc::piper_proc(int pipefd, bool timestamp, const char *filename)
    : pp_fd(-1), pp_timestamp(timestamp), pp_child(-1)
{
    assert(pipefd >= 0);

    if (filename) {
        if ((this->pp_fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0600)) == -1) {
                perror("Unable to open output file for stdin");
                throw error(errno);
        }
    }
    else {
        char piper_tmpname[PATH_MAX];
        const char *tmpdir;

        if ((tmpdir = getenv("TMPDIR")) == NULL) {
            tmpdir = _PATH_VARTMP;
        }
        snprintf(piper_tmpname, sizeof(piper_tmpname),
                 "%s/lnav.piper.XXXXXX",
                 tmpdir);
        if ((this->pp_fd = mkstemp(piper_tmpname)) == -1) {
            throw error(errno);
        }

        unlink(piper_tmpname);
    }

    this->pp_child = fork();
    switch (this->pp_child) {
    case -1:
	throw error(errno);
    case 0:
	{
	    auto_fd infd(pipefd);
	    line_buffer lb;
	    off_t woff = 0;
	    off_t off = 0;
	    char *line;
	    size_t len;

	    lb.set_fd(infd);
	    while ((line = lb.read_line(off, len)) != NULL) {
                int wrc;

	    	if (timestamp) {
	    	    char time_str[64];
	    	    struct timeval tv;
	    	    char ms_str[8];

	    	    gettimeofday(&tv, NULL);
	    	    strftime(time_str, sizeof(time_str), "%FT%T", gmtime(&tv.tv_sec));
	    	    snprintf(ms_str, sizeof(ms_str), ".%03d", (int)(tv.tv_usec / 1000));
	    	    strcat(time_str, ms_str);
	    	    strcat(time_str, "Z  ");
	    	    wrc = pwrite(this->pp_fd, time_str, strlen(time_str), woff);
                    if (wrc == -1) {
                        perror("Unable to write to output file for stdin");
                        break;
                    }
                    woff += wrc;
	    	}

	    	line[len] = '\n';
		/* Need to do pwrite here since the fd is used by the main
		 * lnav process as well.
		 */
		wrc = pwrite(this->pp_fd, line, len + 1, woff);
                if (wrc == -1) {
                    perror("Unable to write to output file for stdin");
                    break;
                }
		woff += wrc;
	    }
	}
	exit(0);
	break;
    default:
	break;
    }
}

piper_proc::~piper_proc()
{
    if (this->pp_child > 0) {
	int status;

	kill(this->pp_child, SIGTERM);
	while (waitpid(this->pp_child, &status, 0) < 0 && (errno == EINTR)) {
	    ;
	}

	this->pp_child = -1;
    }
}
<|endoftext|>


<commit_before>#include "qnodeview.h"

#include <QtCore/QDebug>

#include "graphicsnode.hpp"

#include "qreactiveproxymodel.h"
#include "graphicsnodescene.hpp"
#include "graphicsnodesocket.hpp"

#include "qnodeeditorsocketmodel.h"

class QNodeViewPrivate final : public QObject
{
public:
    explicit QNodeViewPrivate(QObject* p) : QObject(p) {}

    QReactiveProxyModel     m_Proxy   {this     };
    QAbstractItemModel*     m_pModel  {Q_NULLPTR};
    QVector<GraphicsNode*>  m_lNodes  {         };
    GraphicsNodeScene       m_Scene   {this     };
    QNodeEditorSocketModel* m_pFactory{Q_NULLPTR};
};

QNodeView::QNodeView(QWidget* parent) : GraphicsNodeView(parent),
    d_ptr(new QNodeViewPrivate(this))
{
    d_ptr->m_pFactory = new QNodeEditorSocketModel(&d_ptr->m_Proxy, &d_ptr->m_Scene);

    m_pModel = d_ptr->m_pFactory; //HACK to remove

    d_ptr->m_Scene.setSceneRect(-9999, -9999, 9999, 9999);
    setScene(&d_ptr->m_Scene);
}

QNodeView::~QNodeView()
{
    delete d_ptr;
}

GraphicsNodeScene* QNodeView::scene() const
{
    return &d_ptr->m_Scene;
}

void QNodeView::setModel(QAbstractItemModel* m)
{
    d_ptr->m_pModel = m;
    d_ptr->m_Proxy.setSourceModel(m);
}

GraphicsNode* QNodeView::getNode(const QModelIndex& idx) const
{
    if (!idx.isValid())
        return Q_NULLPTR;

    const auto factoryIdx = d_ptr->m_pFactory->mapFromSource(
        d_ptr->m_Proxy.mapFromSource(idx)
    );

    Q_ASSERT(factoryIdx.isValid());

    return d_ptr->m_pFactory->getNode(factoryIdx);
}

QReactiveProxyModel* QNodeView::reactiveModel() const
{
    return &d_ptr->m_Proxy;
}

QAbstractItemModel *QNodeView::sinkSocketModel(const QModelIndex& node) const
{
    return d_ptr->m_pFactory->sinkSocketModel(node);
}

QAbstractItemModel *QNodeView::sourceSocketModel(const QModelIndex& node) const
{
    return d_ptr->m_pFactory->sourceSocketModel(node);
}

QAbstractItemModel* QNodeView::edgeModel() const
{
    return d_ptr->m_pFactory->edgeModel();
}
<commit_msg>qnodeview: Do not set the view size<commit_after>#include "qnodeview.h"

#include <QtCore/QDebug>

#include "graphicsnode.hpp"

#include "qreactiveproxymodel.h"
#include "graphicsnodescene.hpp"
#include "graphicsnodesocket.hpp"

#include "qnodeeditorsocketmodel.h"

class QNodeViewPrivate final : public QObject
{
public:
    explicit QNodeViewPrivate(QObject* p) : QObject(p) {}

    QReactiveProxyModel     m_Proxy   {this     };
    QAbstractItemModel*     m_pModel  {Q_NULLPTR};
    QVector<GraphicsNode*>  m_lNodes  {         };
    GraphicsNodeScene       m_Scene   {this     };
    QNodeEditorSocketModel* m_pFactory{Q_NULLPTR};
};

QNodeView::QNodeView(QWidget* parent) : GraphicsNodeView(parent),
    d_ptr(new QNodeViewPrivate(this))
{
    d_ptr->m_pFactory = new QNodeEditorSocketModel(&d_ptr->m_Proxy, &d_ptr->m_Scene);

    m_pModel = d_ptr->m_pFactory; //HACK to remove

    setScene(&d_ptr->m_Scene);
}

QNodeView::~QNodeView()
{
    delete d_ptr;
}

GraphicsNodeScene* QNodeView::scene() const
{
    return &d_ptr->m_Scene;
}

void QNodeView::setModel(QAbstractItemModel* m)
{
    d_ptr->m_pModel = m;
    d_ptr->m_Proxy.setSourceModel(m);
}

GraphicsNode* QNodeView::getNode(const QModelIndex& idx) const
{
    if (!idx.isValid())
        return Q_NULLPTR;

    const auto factoryIdx = d_ptr->m_pFactory->mapFromSource(
        d_ptr->m_Proxy.mapFromSource(idx)
    );

    Q_ASSERT(factoryIdx.isValid());

    return d_ptr->m_pFactory->getNode(factoryIdx);
}

QReactiveProxyModel* QNodeView::reactiveModel() const
{
    return &d_ptr->m_Proxy;
}

QAbstractItemModel *QNodeView::sinkSocketModel(const QModelIndex& node) const
{
    return d_ptr->m_pFactory->sinkSocketModel(node);
}

QAbstractItemModel *QNodeView::sourceSocketModel(const QModelIndex& node) const
{
    return d_ptr->m_pFactory->sourceSocketModel(node);
}

QAbstractItemModel* QNodeView::edgeModel() const
{
    return d_ptr->m_pFactory->edgeModel();
}
<|endoftext|>


<commit_before>// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "amount.h"
#include "chainparams.h"
#include "core_io.h"
#include "init.h"
#include "net.h"
#include "main.h"
#include "pow.h"
#include "rpcserver.h"
#include "util.h"
#ifdef ENABLE_WALLET
#include "db.h"
#include "wallet.h"
#endif

#include <stdint.h>

#include <boost/assign/list_of.hpp>

#include "json/json_spirit_utils.h"
#include "json/json_spirit_value.h"

using namespace json_spirit;
using namespace std;

/**
 * Return average network hashes per second based on the last 'lookup' blocks,
 * or from the last difficulty change if 'lookup' is nonpositive.
 * If 'height' is nonnegative, compute the estimate at the time when a given block was found.
 */
Value GetNetworkHashPS(int lookup, int height) {
    CBlockIndex *pb = chainActive.Tip();

    if (height >= 0 && height < chainActive.Height())
        pb = chainActive[height];

    if (pb == NULL || !pb->nHeight)
        return 0;

    // If lookup is -1, then use blocks since last difficulty change.
    if (lookup <= 0)
        lookup = pb->nHeight % 2016 + 1;

    // If lookup is larger than chain, then set it to chain length.
    if (lookup > pb->nHeight)
        lookup = pb->nHeight;

    CBlockIndex *pb0 = pb;
    int64_t minTime = pb0->GetBlockTime();
    int64_t maxTime = minTime;
    for (int i = 0; i < lookup; i++) {
        pb0 = pb0->pprev;
        int64_t time = pb0->GetBlockTime();
        minTime = std::min(time, minTime);
        maxTime = std::max(time, maxTime);
    }

    // In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
    if (minTime == maxTime)
        return 0;

    uint256 workDiff = pb->nChainWork - pb0->nChainWork;
    int64_t timeDiff = maxTime - minTime;

    return (int64_t)(workDiff.getdouble() / timeDiff);
}

Value getnetworkhashps(const Array& params, bool fHelp)
{
    if (fHelp || params.size() > 2)
        throw runtime_error(
            "getnetworkhashps ( blocks height )\n"
            "\nReturns the estimated network hashes per second based on the last n blocks.\n"
            "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
            "Pass in [height] to estimate the network speed at the time when a certain block was found.\n"
            "\nArguments:\n"
            "1. blocks     (numeric, optional, default=120) The number of blocks, or -1 for blocks since last difficulty change.\n"
            "2. height     (numeric, optional, default=-1) To estimate at the time of the given height.\n"
            "\nResult:\n"
            "x             (numeric) Hashes per second estimated\n"
            "\nExamples:\n"
            + HelpExampleCli("getnetworkhashps", "")
            + HelpExampleRpc("getnetworkhashps", "")
       );

    return GetNetworkHashPS(params.size() > 0 ? params[0].get_int() : 120, params.size() > 1 ? params[1].get_int() : -1);
}

#ifdef ENABLE_WALLET
Value getgenerate(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 0)
        throw runtime_error(
            "getgenerate\n"
            "\nReturn if the server is set to generate coins or not. The default is false.\n"
            "It is set with the command line argument -gen (or bata.conf setting gen)\n"
            "It can also be set with the setgenerate call.\n"
            "\nResult\n"
            "true|false      (boolean) If the server is set to generate coins or not\n"
            "\nExamples:\n"
            + HelpExampleCli("getgenerate", "")
            + HelpExampleRpc("getgenerate", "")
        );

    return GetBoolArg("-gen", false);
}

Value setgenerate(const Array& params, bool fHelp)
{
    if (fHelp || params.size() < 1 || params.size() > 2)
        throw runtime_error(
            "setgenerate generate ( genproclimit )\n"
            "\nSet 'generate' true or false to turn generation on or off.\n"
            "Generation is limited to 'genproclimit' processors, -1 is unlimited.\n"
            "See the getgenerate call for the current setting.\n"
            "\nArguments:\n"
            "1. generate         (boolean, required) Set to true to turn on generation, off to turn off.\n"
            "2. genproclimit     (numeric, optional) Set the processor limit for when generation is on. Can be -1 for unlimited.\n"
            "                    Note: in -regtest mode, genproclimit controls how many blocks are generated immediately.\n"
            "\nResult\n"
            "[ blockhashes ]     (array, -regtest only) hashes of blocks generated\n"
            "\nExamples:\n"
            "\nSet the generation on with a limit of one processor\n"
            + HelpExampleCli("setgenerate", "true 1") +
            "\nCheck the setting\n"
            + HelpExampleCli("getgenerate", "") +
            "\nTurn off generation\n"
            + HelpExampleCli("setgenerate", "false") +
            "\nUsing json rpc\n"
            + HelpExampleRpc("setgenerate", "true, 1")
        );

    if (pwalletMain == NULL)
        throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");

    bool fGenerate = true;
    if (params.size() > 0)
        fGenerate = params[0].get_bool();

    int nGenProcLimit = -1;
    if (params.size() > 1)
    {
        nGenProcLimit = params[1].get_int();
        if (nGenProcLimit == 0)
            fGenerate = false;
    }

    // -regtest mode: don't return until nGenProcLimit blocks are generated
    if (fGenerate && Params().MineBlocksOnDemand())
    {
        int nHeightStart = 0;
        int nHeightEnd = 0;
        int nHeight = 0;
        int nGenerate = (nGenProcLimit > 0 ? nGenProcLimit : 1);
        CReserveKey reservekey(pwalletMain);

        {   // Don't keep cs_main locked
            LOCK(cs_main);
            nHeightStart = chainActive.Height();
            nHeight = nHeightStart;
            nHeightEnd = nHeightStart+nGenerate;
        }
        unsigned int nExtraNonce = 0;
        Array blockHashes;
        while (nHeight < nHeightEnd)
        {
            auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
            if (!pblocktemplate.get())
                throw JSONRPCError(RPC_INTERNAL_ERROR, "Wallet keypool empty");
            CBlock *pblock = &pblocktemplate->block;
            {
                LOCK(cs_main);
                IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
            }
            while (!CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits)) {
                // Yes, there is a chance every nonce could fail to satisfy the -regtest
                // target -- 1 in 2^(2^32). That ain't gonna happen.
                ++pblock->nNonce;
            }
            CValidationState state;
            if (!ProcessNewBlock(state, NULL, pblock))
                throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
            ++nHeight;
            blockHashes.push_back(pblock->GetHash().GetHex());
        }
        return blockHashes;
    }
    else // Not -regtest: start generate thread, return immediately
    {
        mapArgs["-gen"] = (fGenerate ? "1" : "0");
        mapArgs ["-genproclimit"] = itostr(nGenProcLimit);
        GenerateBitcoins(fGenerate, pwalletMain, nGenProcLimit);
    }

    return Value::null;
}

Value gethashespersec(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 0)
        throw runtime_error(
            "gethashespersec\n"
            "\nReturns a recent hashes per second performance measurement while generating.\n"
            "See the getgenerate and setgenerate calls to turn generation on and off.\n"
            "\nResult:\n"
            "n            (numeric) The recent hashes per second when generation is on (will return 0 if generation is off)\n"
            "\nExamples:\n"
            + HelpExampleCli("gethashespersec", "")
            + HelpExampleRpc("gethashespersec", "")
        );

    if (GetTimeMillis() - nHPSTimerStart > 8000)
        return (int64_t)0;
    return (int64_t)dHashesPerSec;
}
#endif

Value getmininginfo(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 0)
        throw runtime_error(
            "getmininginfo\n"
            "\nReturns a json object containing mining-related information."
            "\nResult:\n"
            "{\n"
            "  \"blocks\": nnn,             (numeric) The current block\n"
            "  \"currentblocksize\": nnn,   (numeric) The last block size\n"
            "  \"currentblocktx\": nnn,     (numeric) The last block transaction\n"
            "  \"difficulty\": xxx.xxxxx    (numeric) The current difficulty\n"
            "  \"errors\": \"...\"          (string) Current errors\n"
            "  \"generate\": true|false     (boolean) If the generation is on or off (see getgenerate or setgenerate calls)\n"
            "  \"genproclimit\": n          (numeric) The processor limit for generation. -1 if no generation. (see getgenerate or setgenerate calls)\n"
            "  \"hashespersec\": n          (numeric) The hashes per second of the generation, or 0 if no generation.\n"
            "  \"pooledtx\": n              (numeric) The size of the mem pool\n"
            "  \"testnet\": true|false      (boolean) If using testnet or not\n"
            "  \"chain\": \"xxxx\",         (string) current network name as defined in BIP70 (main, test, regtest)\n"
            "}\n"
            "\nExamples:\n"
            + HelpExampleCli("getmininginfo", "")
            + HelpExampleRpc("getmininginfo", "")
        );

    Object obj;
    obj.push_back(Pair("blocks",           (int)chainActive.Height()));
    obj.push_back(Pair("currentblocksize", (uint64_t)nLastBlockSize));
    obj.push_back(Pair("currentblocktx",   (uint64_t)nLastBlockTx));
    obj.push_back(Pair("difficulty",       (double)GetDifficulty()));
    obj.push_back(Pair("errors",           GetWarnings("statusbar")));
    obj.push_back(Pair("genproclimit",     (int)GetArg("-genproclimit", -1)));
    obj.push_back(Pair("networkhashps",    getnetworkhashps(params, false)));
    obj.push_back(Pair("pooledtx",         (uint64_t)mempool.size()));
    obj.push_back(Pair("testnet",          Params().TestnetToBeDeprecatedFieldRPC()));
    obj.push_back(Pair("chain",            Params().NetworkIDString()));
#ifdef ENABLE_WALLET
    obj.push_back(Pair("generate",         getgenerate(params, false)));
    obj.push_back(Pair("hashespersec",     gethashespersec(params, false)));
#endif
    return obj;
}

// NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
Value prioritisetransaction(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 3)
        throw runtime_error(
            "prioritisetransaction <txid> <priority delta> <fee delta>\n"
            "Accepts the transaction into mined blocks at a higher (or lower) priority\n"
            "\nArguments:\n"
            "1. \"txid\"       (string, required) The transaction id.\n"
            "2. priority delta (numeric, required) The priority to add or subtract.\n"
            "                  The transaction selection algorithm considers the tx as it would have a higher priority.\n"
            "                  (priority of a transaction is calculated: coinage * value_in_satoshis / txsize) \n"
            "3. fee delta      (numeric, required) The fee value (in satoshis) to add (or subtract, if negative).\n"
            "                  The fee is not actually paid, only the algorithm for selecting transactions into a block\n"
            "                  considers the transaction as it would have paid a higher (or lower) fee.\n"
            "\nResult\n"
            "true              (boolean) Returns true\n"
            "\nExamples:\n"
            + HelpExampleCli("prioritisetransaction", "\"txid\" 0.0 10000")
            + HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000")
        );

    uint256 hash = ParseHashStr(params[0].get_str(), "txid");

    CAmount nAmount = params[2].get_int64();

    mempool.PrioritiseTransaction(hash, params[0].get_str(), params[1].get_real(), nAmount);
    return true;
}

// NOTE: Assumes a conclusive result; if result is inconclusive, it must be handled by caller
static Value BIP22ValidationResult(const CValidationState& state)
{
    if (state.IsValid())
        return Value::null;

    std::string strRejectReason = state.GetRejectReason();
    if (state.IsError())
        throw JSONRPCError(RPC_VERIFY_ERROR, strRejectReason);
    if (state.IsInvalid())
    {
        if (strRejectReason.empty())
            return "rejected";
        return strRejectReason;
    }
    // Should be impossible
    return "valid?";
}

Value getblocktemplate(const Array& params, bool fHelp)
{
    if (fHelp || params.size() > 1)
        throw runtime_error(
            "getblocktemplate ( \"jsonrequestobject\" )\n"
            "\nIf the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
            "It returns data needed to construct a block to work on.\n"
            "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"

            "\nArguments:\n"
            "1. \"jsonrequestobject\"       (string, optional) A json object in the following spec\n"
            "     {\n"
            "       \"mode\":\"template\"    (string, optional) This must be set to \"template\" or omitted\n"
            "       \"capabilities\":[       (array, optional) A list of strings\n"
            "           \"support\"           (string) client side supported feature, 'longpoll', 'coinbasetxn', 'coinbasevalue', 'proposal', 'serverlist', 'workid'\n"
            "           ,...\n"
            "         ]\n"
            "     }\n"
            "\n"

            "\nResult:\n"
            "{\n"
            "  \"version\" : n,                    (numeric) The block version\n"
            "  \"previousblockhash\" : \"xxxx\",    (string) The hash of current highest block\n"
            "  \"transactions\" : [                (array) contents of non-coinbase transactions that should be included in the next block\n"
            "      {\n"
            "         \"data\" : \"xxxx\",          (string) transaction data encoded in hexadecimal (byte-for-byte)\n"
            "         \"hash\" : \"xxxx\",          (string) hash/id encoded in little-endian hexadecimal\n"
            "         \"depends\" : [              (array) array of numbers \n"
            "             n                        (numeric) transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is\n"
            "             ,...\n"
            "         ],\n"
            "         \"fee\": n,                   (numeric) difference in value between transaction inputs and outputs (in Satoshis); for coinbase transactions, this is a negative Number of the total collected block fees (ie, not including the block subsidy); if key is not present, fee is unknown and clients MUST NOT assume there isn't one\n"
            "         \"sigops\" : n,               (numeric) total number of SigOps, as counted for purposes of block limits; if key is not present, sigop count is unknown and clients MUST NOT assume there aren't any\n"
            "         \"required\" : true|false     (boolean) if provided and true, this transaction must be in the final block\n"
            "      }\n"
            "      ,...\n"
            "  ],\n"
            "  \"coinbaseaux\" : {                  (json object) data that should be included in the coinbase's scriptSig content\n"
            "      \"flags\" : \"flags\"            (string) \n"
            "  },\n"
            "  \"coinbasevalue\" : n,               (numeric) maximum allowable input to coinbase transaction, including the generation award and transaction fees (in Satoshis)\n"
            "  \"coinbasetxn\" : { ... },           (json object) information for coinbase transaction\n"
            "  \"target\" : \"xxxx\",               (string) The hash target\n"
            "  \"mintime\" : xxx,                   (numeric) The minimum timestamp appropriate for next block time in seconds since epoch (Jan 1 1970 GMT)\n"
            "  \"mutable\" : [                      (array of string) list of ways the block template may be changed \n"
            "     \"value\"                         (string) A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock'\n"
            "     ,...\n"
            "  ],\n"
            "  \"noncerange\" : \"00000000ffffffff\",   (string) A range of valid nonces\n"
            "  \"sigoplimit\" : n,                 (numeric) limit of sigops in blocks\n"
            "  \"sizelimit\" : n,                  (numeric) limit of block size\n"
            "  \"curtime\" : ttt,                  (numeric) current timestamp in seconds since epoch (Jan 1 1970 GMT)\n"
            "  \"bits\" : \"xxx\",                 (string) compressed target of next block\n"
            "  \"height\" : n                      (numeric) The height of the next block\n"
            "}\n"

            "\nExamples:\n"
            + HelpExampleCli("getblocktemplate", "")
            + HelpExampleRpc("getblocktemplate", "")
         );

    std::string strMode = "template";
    Value lpval = Value::null;
    if (params.size() > 0)
    {
        const Object& oparam = params[0].get_obj();
        const Value& modeval = find_value(oparam, "mode");
        if (modeval.type() == str_type)
            strMode = modeval.get_str();
        else if (modeval.type() == null_type)
        {
            /* Do nothing */
        }
        else
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
        lpval = find_value(oparam, "longpollid");

        if (strMode == "proposal")
        {
            const Value& dataval = find_value(oparam, "data");
            if (dataval.type() != str_type)
                throw JSONRPCError(RPC_TYPE_ERROR, "Missing data String key for proposal");

            CBlock block;
            if (!DecodeHexBlk(block, dataval.get_str()))
                throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");

            uint256 hash = block.GetHash();
            BlockMap::iterator mi = mapBlockIndex.find(hash);
            if (mi != mapBlockIndex.end()) {
                CBlockIndex *pindex = mi->second;
                if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
                    return "duplicate";
                if (pindex->nStatus & BLOCK_FAILED_MASK)
                    return "duplicate-invalid";
                return "duplicate-inconclusive";
            }

            CBlockIndex* const pindexPrev = chainActive.Tip();
            // TestBlockValidity only supports blocks built on the current Tip
            if (block.hashPrevBlock != pindexPrev->GetBlockHash())
                return "inconclusive-not-best-prevblk";
            CValidationState state;
            TestBlockValidity(state, block, pindexPrev, false, true);
            return BIP22ValidationResult(state);
        }
    }

    if (strMode != "template")
        throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");

    if (vNodes.empty())
        throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Bata is not connected!");

    if (IsInitialBlockDownload())
        throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Bata is downloading blocks...");

    static unsigned int nTransactionsUpdatedLast;

    if (lpval.type() != null_type)
    {
        // Wait to respond until either the best block changes, OR a minute has passed and there are more transactions
        uint256 hashWatchedChain;
        boost::system_time checktxtime;
        unsigned int nTransactionsUpdatedLastLP;

        if (lpval.type() == str_type)
        {
            // Format: <hashBestChain><nTransactionsUpdatedLast>
            std::string lpstr = lpval.get_str();

            hashWatchedChain.SetHex(lpstr.substr(0, 64));
            nTransactionsUpdatedLastLP = atoi64(lpstr.substr(64));
        }
        else
        {
            // NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
            hashWatchedChain = chainActive.Tip()->GetBlockHash();
            nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
        }

        // Release the wallet and main lock while waiting
#ifdef ENABLE_WALLET
        if(pwalletMain)
            LEAVE_CRITICAL_SECTION(pwalletMain->cs_wallet);
#endif
        LEAVE_CRITICAL_SECTION(cs_main);
        {
            checktxtime = boost::get_system_time() + boost::posix_time::minutes(1);

            boost::unique_lock<boost::mutex> lock(csBestBlock);
            while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning())
            {
                if (!cvBlockChange.timed_wait(lock, checktxtime))
                {
                    // Timeout: Check transactions for update
                    if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
                        break;
                    checktxtime += boost::posix_time::seconds(10);
                }
            }
        }
        ENTER_CRITICAL_SECTION(cs_main);
#ifdef ENABLE_WALLET
        if(pwalletMain)
            ENTER_CRITICAL_SECTION(pwalletMain->cs_wallet);
#endif

        if (!IsRPCRunning())
            throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
        // TODO: Maybe recheck connections/IBD and (if something wrong) send an expires-immediately template to stop miners?
    }

    // Update block
    static CBlockIndex* pindexPrev;
    static int64_t nStart;
    static CBlockTemplate* pblocktemplate;
    if (pindexPrev != chainActive.Tip() ||
        (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
    {
        // Clear pindexPrev so future calls make a new block, despite any failures from here on
        pindexPrev = NULL;

        // Store the pindexBest used before CreateNewBlock, to avoid races
        nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
        CBlockIndex* pindexPrevNew = chainActive.Tip();
        nStart = GetTime();

        // Create new block
        if(pblocktemplate)
        {
            delete pblocktemplate;
            pblocktemplate = NULL;
        }
        CScript scriptDummy = CScript() << OP_TRUE;
        pblocktemplate = CreateNewBlock(scriptDummy);
        if (!pblocktemplate)
            throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");

        // Need to update only after we know CreateNewBlock succeeded
        pindexPrev = pindexPrevNew;
    }
    CBlock* pblock = &pblocktemplate->block; // pointer for convenience

    // Update nTime
    UpdateTime(pblock, pindexPrev);
    pblock->nNonce = 0;

    static const Array aCaps = boost::assign::list_of("proposal");

    Array transactions;
    map<uint256, int64_t> setTxIndex;
    int i = 0;
    BOOST_FOREACH (CTransaction& tx, pblock->vtx)
    {
        uint256 txHash = tx.GetHash();
        setTxIndex[txHash] = i++;

        if (tx.IsCoinBase())
            continue;

        Object entry;

        entry.push_back(Pair("data", EncodeHexTx(tx)));

        entry.push_back(Pair("hash", txHash.GetHex()));

        Array deps;
        BOOST_FOREACH (const CTxIn &in, tx.vin)
        {
            if (setTxIndex.count(in.prevout.hash))
                deps.push_back(setTxIndex[in.prevout.hash]);
        }
        entry.push_back(Pair("depends", deps));

        int index_in_template = i - 1;
        entry.push_back(Pair("fee", pblocktemplate->vTxFees[index_in_template]));
        entry.push_back(Pair("sigops", pblocktemplate->vTxSigOps[index_in_template]));

        transactions.push_back(entry);
    }

    Object aux;
    aux.push_back(Pair("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end())));

    uint256 hashTarget = uint256().SetCompact(pblock->nBits);

    static Array aMutable;
    if (aMutable.empty())
    {
        aMutable.push_back("time");
        aMutable.push_back("transactions");
        aMutable.push_back("prevblock");
    }

    Object result;
    result.push_back(Pair("capabilities", aCaps));
    result.push_back(Pair("version", pblock->nVersion));
    result.push_back(Pair("previousblockhash", pblock->hashPrevBlock.GetHex()));
    result.push_back(Pair("transactions", transactions));
    result.push_back(Pair("coinbaseaux", aux));
    result.push_back(Pair("coinbasevalue", (int64_t)pblock->vtx[0].vout[0].nValue));
    result.push_back(Pair("longpollid", chainActive.Tip()->GetBlockHash().GetHex() + i64tostr(nTransactionsUpdatedLast)));
    result.push_back(Pair("target", hashTarget.GetHex()));
    result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
    result.push_back(Pair("mutable", aMutable));
    result.push_back(Pair("noncerange", "00000000ffffffff"));
    result.push_back(Pair("sigoplimit", (int64_t)MAX_BLOCK_SIGOPS));
    result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SIZE));
    result.push_back(Pair("curtime", pblock->GetBlockTime()));
    result.push_back(Pair("bits", strprintf("%08x", pblock->nBits)));
    result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));

    return result;
}

class submitblock_StateCatcher : public CValidationInterface
{
public:
    uint256 hash;
    bool found;
    CValidationState state;

    submitblock_StateCatcher(const uint256 &hashIn) : hash(hashIn), found(false), state() {};

protected:
    virtual void BlockChecked(const CBlock& block, const CValidationState& stateIn) {
        if (block.GetHash() != hash)
            return;
        found = true;
        state = stateIn;
    };
};

Value submitblock(const Array& params, bool fHelp)
{
    if (fHelp || params.size() < 1 || params.size() > 2)
        throw runtime_error(
            "submitblock \"hexdata\" ( \"jsonparametersobject\" )\n"
            "\nAttempts to submit new block to network.\n"
            "The 'jsonparametersobject' parameter is currently ignored.\n"
            "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n"

            "\nArguments\n"
            "1. \"hexdata\"    (string, required) the hex-encoded block data to submit\n"
            "2. \"jsonparametersobject\"     (string, optional) object of optional parameters\n"
            "    {\n"
            "      \"workid\" : \"id\"    (string, optional) if the server provided a workid, it MUST be included with submissions\n"
            "    }\n"
            "\nResult:\n"
            "\nExamples:\n"
            + HelpExampleCli("submitblock", "\"mydata\"")
            + HelpExampleRpc("submitblock", "\"mydata\"")
        );

    CBlock block;
    if (!DecodeHexBlk(block, params[0].get_str()))
        throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");

    uint256 hash = block.GetHash();
    bool fBlockPresent = false;
    {
        LOCK(cs_main);
        BlockMap::iterator mi = mapBlockIndex.find(hash);
        if (mi != mapBlockIndex.end()) {
            CBlockIndex *pindex = mi->second;
            if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
                return "duplicate";
            if (pindex->nStatus & BLOCK_FAILED_MASK)
                return "duplicate-invalid";
            // Otherwise, we might only have the header - process the block before returning
            fBlockPresent = true;
        }
    }

    CValidationState state;
    submitblock_StateCatcher sc(block.GetHash());
    RegisterValidationInterface(&sc);
    bool fAccepted = ProcessNewBlock(state, NULL, &block);
    UnregisterValidationInterface(&sc);
    if (fBlockPresent)
    {
        if (fAccepted && !sc.found)
            return "duplicate-inconclusive";
        return "duplicate";
    }
    if (fAccepted)
    {
        if (!sc.found)
            return "inconclusive";
        state = sc.state;
    }
    return BIP22ValidationResult(state);
}

Value estimatefee(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 1)
        throw runtime_error(
            "estimatefee nblocks\n"
            "\nEstimates the approximate fee per kilobyte\n"
            "needed for a transaction to begin confirmation\n"
            "within nblocks blocks.\n"
            "\nArguments:\n"
            "1. nblocks     (numeric)\n"
            "\nResult:\n"
            "n :    (numeric) estimated fee-per-kilobyte\n"
            "\n"
            "-1.0 is returned if not enough transactions and\n"
            "blocks have been observed to make an estimate.\n"
            "\nExample:\n"
            + HelpExampleCli("estimatefee", "6")
            );

    RPCTypeCheck(params, boost::assign::list_of(int_type));

    int nBlocks = params[0].get_int();
    if (nBlocks < 1)
        nBlocks = 1;

    CFeeRate feeRate = mempool.estimateFee(nBlocks);
    if (feeRate == CFeeRate(0))
        return -1.0;

    return ValueFromAmount(feeRate.GetFeePerK());
}

Value estimatepriority(const Array& params, bool fHelp)
{
    if (fHelp || params.size() != 1)
        throw runtime_error(
            "estimatepriority nblocks\n"
            "\nEstimates the approximate priority\n"
            "a zero-fee transaction needs to begin confirmation\n"
            "within nblocks blocks.\n"
            "\nArguments:\n"
            "1. nblocks     (numeric)\n"
            "\nResult:\n"
            "n :    (numeric) estimated priority\n"
            "\n"
            "-1.0 is returned if not enough transactions and\n"
            "blocks have been observed to make an estimate.\n"
            "\nExample:\n"
            + HelpExampleCli("estimatepriority", "6")
            );

    RPCTypeCheck(params, boost::assign::list_of(int_type));

    int nBlocks = params[0].get_int();
    if (nBlocks < 1)
        nBlocks = 1;

    return mempool.estimatePriority(nBlocks);
}
<commit_msg>Delete rpcmining.cpp<commit_after><|endoftext|>