YPC  0.2.0
data_hash_interface.h
1 #pragma once
2 #include "ypc/core_t/analyzer/internal/is_multi_datasource.h"
3 #include "ypc/core_t/analyzer/var/data_hash_var.h"
4 #include "ypc/core_t/analyzer/var/data_source_var.h"
5 #include "ypc/stbox/ebyte.h"
6 #include "ypc/stbox/stx_status.h"
7 
8 namespace ypc {
9 namespace internal {
10 
11 template <typename Crypto, typename DataSession,
12  bool has_multi_datasource = is_multi_datasource<DataSession>::value>
13 class data_hash_interface : virtual public data_hash_var,
14  virtual public data_source_var<DataSession> {
15 protected:
16  void set_data_hash() {
17  stbox::bytes joint_bytes;
18  std::vector<stbox::bytes> rs;
19  for (uint32_t i = 0; i < data_source_var<DataSession>::m_datasource.size();
20  ++i) {
21  auto t = data_source_var<DataSession>::m_datasource[i]->data_hash();
22  rs.push_back(t);
23  }
24 
25  std::sort(rs.begin(), rs.end());
26  rs.erase(std::unique(rs.begin(), rs.end()), rs.end());
27 
28  for (uint32_t i = 0; i < rs.size(); ++i) {
29  joint_bytes += rs[i];
30  LOG(INFO) << i << "-th data with data hash: " << rs[i];
31  }
32 
33  Crypto::hash_256(joint_bytes, data_hash_var::m_data_hash);
34 #ifdef DEBUG
35  LOG(INFO) << "final data hash: " << data_hash_var::m_data_hash;
36 #endif
37  }
38 };
39 
40 template <typename Crypto>
42 protected:
43  void set_data_hash() {}
44 };
45 
46 template <typename Crypto, typename DataSession>
47 class data_hash_interface<Crypto, DataSession, false>
48  : virtual public data_hash_var,
49  virtual public data_source_var<DataSession> {
51 
52 protected:
53  void set_data_hash() {
54  data_hash_var::m_data_hash = data_source_var_t::m_datasource->data_hash();
55  }
56 };
57 
58 } // namespace internal
59 } // namespace ypc
ypc::internal::data_hash_interface
Definition: data_hash_interface.h:13
ypc::internal::data_source_var
Definition: data_source_var.h:10
ypc::internal::data_hash_var
Definition: data_hash_var.h:6
ypc::internal::noinput_data_stream
Definition: noinput_data_stream.h:8
ypc::crypto::crypto_pack
Definition: crypto_pack.h:14