Electroneum
test_generator Class Reference

#include <chaingen.h>

Classes

struct  block_info
 

Public Types

enum  block_fields {
  bf_none = 0, bf_major_ver = 1 << 0, bf_minor_ver = 1 << 1, bf_timestamp = 1 << 2,
  bf_prev_id = 1 << 3, bf_miner_tx = 1 << 4, bf_tx_hashes = 1 << 5, bf_diffic = 1 << 6,
  bf_max_outs = 1 << 7, bf_hf_version = 1 << 8
}
 

Public Member Functions

 test_generator ()
 
 test_generator (const test_generator &other)
 
void get_block_chain (std::vector< block_info > &blockchain, const crypto::hash &head, size_t n) const
 
void get_last_n_block_weights (std::vector< size_t > &block_weights, const crypto::hash &head, size_t n) const
 
uint64_t get_already_generated_coins (const crypto::hash &blk_id) const
 
uint64_t get_already_generated_coins (const cryptonote::block &blk) const
 
void add_block (const cryptonote::block &blk, size_t tsx_size, std::vector< size_t > &block_weights, uint64_t already_generated_coins, uint8_t hf_version=1)
 
bool construct_block (cryptonote::block &blk, uint64_t height, const crypto::hash &prev_id, const cryptonote::account_base &miner_acc, uint64_t timestamp, uint64_t already_generated_coins, std::vector< size_t > &block_weights, const std::list< cryptonote::transaction > &tx_list, const boost::optional< uint8_t > &hf_ver=boost::none)
 
bool construct_block (cryptonote::block &blk, const cryptonote::account_base &miner_acc, uint64_t timestamp)
 
bool construct_block (cryptonote::block &blk, const cryptonote::block &blk_prev, const cryptonote::account_base &miner_acc, const std::list< cryptonote::transaction > &tx_list=std::list< cryptonote::transaction >(), const boost::optional< uint8_t > &hf_ver=boost::none)
 
bool construct_block_manually (cryptonote::block &blk, const cryptonote::block &prev_block, const cryptonote::account_base &miner_acc, int actual_params=bf_none, uint8_t major_ver=0, uint8_t minor_ver=0, uint64_t timestamp=0, const crypto::hash &prev_id=crypto::hash(), const cryptonote::difficulty_type &diffic=1, const cryptonote::transaction &miner_tx=cryptonote::transaction(), const std::vector< crypto::hash > &tx_hashes=std::vector< crypto::hash >(), size_t txs_sizes=0, size_t max_outs=999, uint8_t hf_version=1)
 
bool construct_block_manually_tx (cryptonote::block &blk, const cryptonote::block &prev_block, const cryptonote::account_base &miner_acc, const std::vector< crypto::hash > &tx_hashes, size_t txs_size)
 

Friends

class boost::serialization::access
 

Detailed Description

Definition at line 182 of file chaingen.h.

Member Enumeration Documentation

◆ block_fields

Enumerator
bf_none 
bf_major_ver 
bf_minor_ver 
bf_timestamp 
bf_prev_id 
bf_miner_tx 
bf_tx_hashes 
bf_diffic 
bf_max_outs 
bf_hf_version 

Definition at line 217 of file chaingen.h.

Constructor & Destructor Documentation

◆ test_generator() [1/2]

test_generator::test_generator ( )
inline

Definition at line 231 of file chaingen.h.

231 {}

◆ test_generator() [2/2]

test_generator::test_generator ( const test_generator other)
inline

Definition at line 232 of file chaingen.h.

232 : m_blocks_info(other.m_blocks_info) {}

Member Function Documentation

◆ add_block()

void test_generator::add_block ( const cryptonote::block blk,
size_t  tsx_size,
std::vector< size_t > &  block_weights,
uint64_t  already_generated_coins,
uint8_t  hf_version = 1 
)

Definition at line 104 of file chaingen.cpp.

105 {
106  const size_t block_weight = txs_weight + get_transaction_weight(blk.miner_tx);
107  uint64_t block_reward;
108  get_block_reward(misc_utils::median(block_weights), block_weight, already_generated_coins, block_reward, hf_version, get_block_height(blk));
109  m_blocks_info[get_block_hash(blk)] = block_info(blk.prev_id, already_generated_coins + block_reward, block_weight);
110 }
uint64_t get_transaction_weight(const transaction &tx, size_t blob_size)
unsigned __int64 uint64_t
Definition: stdint.h:136
type_vec_type median(std::vector< type_vec_type > &v)
crypto::hash get_block_hash(uint64_t height)
bool get_block_reward(size_t median_weight, size_t current_block_weight, uint64_t already_generated_coins, uint64_t &reward, uint8_t version, uint64_t current_block_height, network_type nettype)
uint64_t get_block_height(const block &b)
Here is the call graph for this function:

◆ construct_block() [1/3]

bool test_generator::construct_block ( cryptonote::block blk,
uint64_t  height,
const crypto::hash prev_id,
const cryptonote::account_base miner_acc,
uint64_t  timestamp,
uint64_t  already_generated_coins,
std::vector< size_t > &  block_weights,
const std::list< cryptonote::transaction > &  tx_list,
const boost::optional< uint8_t > &  hf_ver = boost::none 
)

Definition at line 112 of file chaingen.cpp.

116 {
117  blk.major_version = hf_ver ? hf_ver.get() : CURRENT_BLOCK_MAJOR_VERSION;
118  blk.minor_version = hf_ver ? hf_ver.get() : CURRENT_BLOCK_MINOR_VERSION;
119  blk.timestamp = timestamp;
120  blk.prev_id = prev_id;
121 
122  blk.tx_hashes.reserve(tx_list.size());
123  BOOST_FOREACH(const transaction &tx, tx_list)
124  {
125  crypto::hash tx_hash;
126  get_transaction_hash(tx, tx_hash);
127  blk.tx_hashes.push_back(tx_hash);
128  }
129 
130  uint64_t total_fee = 0;
131  size_t txs_weight = 0;
132  BOOST_FOREACH(auto& tx, tx_list)
133  {
134  uint64_t fee = 0;
135  bool r = get_tx_fee(tx, fee);
136  CHECK_AND_ASSERT_MES(r, false, "wrong transaction passed to construct_block");
137  total_fee += fee;
138  txs_weight += get_transaction_weight(tx);
139  }
140 
141  blk.miner_tx = AUTO_VAL_INIT(blk.miner_tx);
142  size_t target_block_weight = txs_weight + get_transaction_weight(blk.miner_tx);
143  while (true)
144  {
145  if (!construct_miner_tx(height, misc_utils::median(block_weights), already_generated_coins, target_block_weight, total_fee, miner_acc.get_keys().m_account_address, blk.miner_tx, blobdata(), 10, hf_ver ? hf_ver.get() : 1))
146  return false;
147 
148  size_t actual_block_weight = txs_weight + get_transaction_weight(blk.miner_tx);
149  if (target_block_weight < actual_block_weight)
150  {
151  target_block_weight = actual_block_weight;
152  }
153  else if (actual_block_weight < target_block_weight)
154  {
155  size_t delta = target_block_weight - actual_block_weight;
156  blk.miner_tx.extra.resize(blk.miner_tx.extra.size() + delta, 0);
157  actual_block_weight = txs_weight + get_transaction_weight(blk.miner_tx);
158  if (actual_block_weight == target_block_weight)
159  {
160  break;
161  }
162  else
163  {
164  CHECK_AND_ASSERT_MES(target_block_weight < actual_block_weight, false, "Unexpected block size");
165  delta = actual_block_weight - target_block_weight;
166  blk.miner_tx.extra.resize(blk.miner_tx.extra.size() - delta);
167  actual_block_weight = txs_weight + get_transaction_weight(blk.miner_tx);
168  if (actual_block_weight == target_block_weight)
169  {
170  break;
171  }
172  else
173  {
174  CHECK_AND_ASSERT_MES(actual_block_weight < target_block_weight, false, "Unexpected block size");
175  blk.miner_tx.extra.resize(blk.miner_tx.extra.size() + delta, 0);
176  target_block_weight = txs_weight + get_transaction_weight(blk.miner_tx);
177  }
178  }
179  }
180  else
181  {
182  break;
183  }
184  }
185 
186  //blk.tree_root_hash = get_tx_tree_hash(blk);
187 
188  // Nonce search...
189  blk.nonce = 0;
190  while (!miner::find_nonce_for_given_block(blk, get_test_difficulty(hf_ver), height))
191  blk.timestamp++;
192 
193  add_block(blk, txs_weight, block_weights, already_generated_coins, hf_ver ? hf_ver.get() : 1);
194 
195  return true;
196 }
std::vector< crypto::hash > tx_hashes
bool get_tx_fee(const transaction &tx, uint64_t &fee)
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
uint64_t height
Definition: blockchain.cpp:91
uint64_t get_transaction_weight(const transaction &tx, size_t blob_size)
std::vector< uint8_t > extra
const account_keys & get_keys() const
Definition: account.cpp:264
unsigned __int64 uint64_t
Definition: stdint.h:136
std::string blobdata
Definition: blobdatatype.h:39
type_vec_type median(std::vector< type_vec_type > &v)
account_public_address m_account_address
Definition: account.h:43
void add_block(const cryptonote::block &blk, size_t tsx_size, std::vector< size_t > &block_weights, uint64_t already_generated_coins, uint8_t hf_version=1)
Definition: chaingen.cpp:104
crypto::hash get_transaction_hash(const transaction &t)
#define AUTO_VAL_INIT(v)
Definition: misc_language.h:53
#define CURRENT_BLOCK_MINOR_VERSION
POD_CLASS hash
Definition: hash.h:50
cryptonote::difficulty_type get_test_difficulty(const boost::optional< uint8_t > &hf_ver=boost::none)
Definition: chaingen.h:409
bool construct_miner_tx(size_t height, size_t median_weight, uint64_t already_generated_coins, size_t current_block_weight, uint64_t fee, const account_public_address &miner_address, transaction &tx, const blobdata &extra_nonce, size_t max_outs, uint8_t hard_fork_version, network_type nettype)
#define CURRENT_BLOCK_MAJOR_VERSION
Here is the call graph for this function:

◆ construct_block() [2/3]

bool test_generator::construct_block ( cryptonote::block blk,
const cryptonote::account_base miner_acc,
uint64_t  timestamp 
)

Definition at line 198 of file chaingen.cpp.

199 {
200  std::vector<size_t> block_weights;
201  std::list<cryptonote::transaction> tx_list;
202  return construct_block(blk, 0, null_hash, miner_acc, timestamp, 0, block_weights, tx_list);
203 }
bool construct_block(cryptonote::block &blk, uint64_t height, const crypto::hash &prev_id, const cryptonote::account_base &miner_acc, uint64_t timestamp, uint64_t already_generated_coins, std::vector< size_t > &block_weights, const std::list< cryptonote::transaction > &tx_list, const boost::optional< uint8_t > &hf_ver=boost::none)
Definition: chaingen.cpp:112

◆ construct_block() [3/3]

bool test_generator::construct_block ( cryptonote::block blk,
const cryptonote::block blk_prev,
const cryptonote::account_base miner_acc,
const std::list< cryptonote::transaction > &  tx_list = std::list<cryptonote::transaction>(),
const boost::optional< uint8_t > &  hf_ver = boost::none 
)

Definition at line 205 of file chaingen.cpp.

209 {
210  uint64_t height = boost::get<txin_gen>(blk_prev.miner_tx.vin.front()).height + 1;
211  crypto::hash prev_id = get_block_hash(blk_prev);
212  // Keep difficulty unchanged
213  uint64_t timestamp = blk_prev.timestamp + current_difficulty_window(hf_ver); // DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN;
214  uint64_t already_generated_coins = get_already_generated_coins(prev_id);
215  std::vector<size_t> block_weights;
217 
218  return construct_block(blk, height, prev_id, miner_acc, timestamp, already_generated_coins, block_weights, tx_list, hf_ver);
219 }
uint64_t current_difficulty_window(const boost::optional< uint8_t > &hf_ver=boost::none)
Definition: chaingen.h:410
uint64_t height
Definition: blockchain.cpp:91
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CRYPTONOTE_REWARD_BLOCKS_WINDOW
bool construct_block(cryptonote::block &blk, uint64_t height, const crypto::hash &prev_id, const cryptonote::account_base &miner_acc, uint64_t timestamp, uint64_t already_generated_coins, std::vector< size_t > &block_weights, const std::list< cryptonote::transaction > &tx_list, const boost::optional< uint8_t > &hf_ver=boost::none)
Definition: chaingen.cpp:112
uint64_t get_already_generated_coins(const crypto::hash &blk_id) const
Definition: chaingen.cpp:88
crypto::hash get_block_hash(uint64_t height)
POD_CLASS hash
Definition: hash.h:50
void get_last_n_block_weights(std::vector< size_t > &block_weights, const crypto::hash &head, size_t n) const
Definition: chaingen.cpp:78
Here is the call graph for this function:

◆ construct_block_manually()

bool test_generator::construct_block_manually ( cryptonote::block blk,
const cryptonote::block prev_block,
const cryptonote::account_base miner_acc,
int  actual_params = bf_none,
uint8_t  major_ver = 0,
uint8_t  minor_ver = 0,
uint64_t  timestamp = 0,
const crypto::hash prev_id = crypto::hash(),
const cryptonote::difficulty_type diffic = 1,
const cryptonote::transaction miner_tx = cryptonote::transaction(),
const std::vector< crypto::hash > &  tx_hashes = std::vector<crypto::hash>(),
size_t  txs_sizes = 0,
size_t  max_outs = 999,
uint8_t  hf_version = 1 
)

Definition at line 221 of file chaingen.cpp.

228 {
229  blk.major_version = actual_params & bf_major_ver ? major_ver : CURRENT_BLOCK_MAJOR_VERSION;
230  blk.minor_version = actual_params & bf_minor_ver ? minor_ver : CURRENT_BLOCK_MINOR_VERSION;
231  blk.timestamp = actual_params & bf_timestamp ? timestamp : prev_block.timestamp + DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN; // Keep difficulty unchanged
232  blk.prev_id = actual_params & bf_prev_id ? prev_id : get_block_hash(prev_block);
233  blk.tx_hashes = actual_params & bf_tx_hashes ? tx_hashes : std::vector<crypto::hash>();
234  max_outs = actual_params & bf_max_outs ? max_outs : 9999;
235  hf_version = actual_params & bf_hf_version ? hf_version : 1;
236 
237  size_t height = get_block_height(prev_block) + 1;
238  uint64_t already_generated_coins = get_already_generated_coins(prev_block);
239  std::vector<size_t> block_weights;
241  if (actual_params & bf_miner_tx)
242  {
243  blk.miner_tx = miner_tx;
244  }
245  else
246  {
247  size_t current_block_weight = txs_weight + get_transaction_weight(blk.miner_tx);
248  // TODO: This will work, until size of constructed block is less then CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE
249  if (!construct_miner_tx(height, misc_utils::median(block_weights), already_generated_coins, current_block_weight, 0, miner_acc.get_keys().m_account_address, blk.miner_tx, blobdata(), max_outs, hf_version))
250  return false;
251  }
252 
253  //blk.tree_root_hash = get_tx_tree_hash(blk);
254 
255  difficulty_type a_diffic = actual_params & bf_diffic ? diffic : get_test_difficulty(hf_version);
256  fill_nonce(blk, a_diffic, height);
257 
258  add_block(blk, txs_weight, block_weights, already_generated_coins, hf_version);
259 
260  return true;
261 }
std::vector< crypto::hash > tx_hashes
uint64_t height
Definition: blockchain.cpp:91
uint64_t get_transaction_weight(const transaction &tx, size_t blob_size)
const account_keys & get_keys() const
Definition: account.cpp:264
unsigned __int64 uint64_t
Definition: stdint.h:136
std::string blobdata
Definition: blobdatatype.h:39
type_vec_type median(std::vector< type_vec_type > &v)
#define CRYPTONOTE_REWARD_BLOCKS_WINDOW
account_public_address m_account_address
Definition: account.h:43
boost::multiprecision::uint128_t difficulty_type
Definition: difficulty.h:43
void add_block(const cryptonote::block &blk, size_t tsx_size, std::vector< size_t > &block_weights, uint64_t already_generated_coins, uint8_t hf_version=1)
Definition: chaingen.cpp:104
void fill_nonce(cryptonote::block &blk, const difficulty_type &diffic, uint64_t height)
Definition: chaingen.cpp:797
uint64_t get_already_generated_coins(const crypto::hash &blk_id) const
Definition: chaingen.cpp:88
#define CURRENT_BLOCK_MINOR_VERSION
crypto::hash get_block_hash(uint64_t height)
cryptonote::difficulty_type get_test_difficulty(const boost::optional< uint8_t > &hf_ver=boost::none)
Definition: chaingen.h:409
#define DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN
bool construct_miner_tx(size_t height, size_t median_weight, uint64_t already_generated_coins, size_t current_block_weight, uint64_t fee, const account_public_address &miner_address, transaction &tx, const blobdata &extra_nonce, size_t max_outs, uint8_t hard_fork_version, network_type nettype)
void get_last_n_block_weights(std::vector< size_t > &block_weights, const crypto::hash &head, size_t n) const
Definition: chaingen.cpp:78
uint64_t get_block_height(const block &b)
#define CURRENT_BLOCK_MAJOR_VERSION
Here is the call graph for this function:
Here is the caller graph for this function:

◆ construct_block_manually_tx()

bool test_generator::construct_block_manually_tx ( cryptonote::block blk,
const cryptonote::block prev_block,
const cryptonote::account_base miner_acc,
const std::vector< crypto::hash > &  tx_hashes,
size_t  txs_size 
)

Definition at line 263 of file chaingen.cpp.

266 {
267  return construct_block_manually(blk, prev_block, miner_acc, bf_tx_hashes, 0, 0, 0, crypto::hash(), 0, transaction(), tx_hashes, txs_weight);
268 }
bool construct_block_manually(cryptonote::block &blk, const cryptonote::block &prev_block, const cryptonote::account_base &miner_acc, int actual_params=bf_none, uint8_t major_ver=0, uint8_t minor_ver=0, uint64_t timestamp=0, const crypto::hash &prev_id=crypto::hash(), const cryptonote::difficulty_type &diffic=1, const cryptonote::transaction &miner_tx=cryptonote::transaction(), const std::vector< crypto::hash > &tx_hashes=std::vector< crypto::hash >(), size_t txs_sizes=0, size_t max_outs=999, uint8_t hf_version=1)
Definition: chaingen.cpp:221
POD_CLASS hash
Definition: hash.h:50
Here is the caller graph for this function:

◆ get_already_generated_coins() [1/2]

uint64_t test_generator::get_already_generated_coins ( const crypto::hash blk_id) const

Definition at line 88 of file chaingen.cpp.

89 {
90  auto it = m_blocks_info.find(blk_id);
91  if (it == m_blocks_info.end())
92  throw std::runtime_error("block hash wasn't found");
93 
94  return it->second.already_generated_coins;
95 }
Here is the caller graph for this function:

◆ get_already_generated_coins() [2/2]

uint64_t test_generator::get_already_generated_coins ( const cryptonote::block blk) const

Definition at line 97 of file chaingen.cpp.

98 {
99  crypto::hash blk_hash;
100  get_block_hash(blk, blk_hash);
101  return get_already_generated_coins(blk_hash);
102 }
uint64_t get_already_generated_coins(const crypto::hash &blk_id) const
Definition: chaingen.cpp:88
crypto::hash get_block_hash(uint64_t height)
POD_CLASS hash
Definition: hash.h:50
Here is the call graph for this function:

◆ get_block_chain()

void test_generator::get_block_chain ( std::vector< block_info > &  blockchain,
const crypto::hash head,
size_t  n 
) const

Definition at line 60 of file chaingen.cpp.

61 {
62  crypto::hash curr = head;
63  while (null_hash != curr && blockchain.size() < n)
64  {
65  auto it = m_blocks_info.find(curr);
66  if (m_blocks_info.end() == it)
67  {
68  throw std::runtime_error("block hash wasn't found");
69  }
70 
71  blockchain.push_back(it->second);
72  curr = it->second.prev_id;
73  }
74 
75  std::reverse(blockchain.begin(), blockchain.end());
76 }
struct rule_list head
POD_CLASS hash
Definition: hash.h:50

◆ get_last_n_block_weights()

void test_generator::get_last_n_block_weights ( std::vector< size_t > &  block_weights,
const crypto::hash head,
size_t  n 
) const

Definition at line 78 of file chaingen.cpp.

79 {
80  std::vector<block_info> blockchain;
81  get_block_chain(blockchain, head, n);
82  BOOST_FOREACH(auto& bi, blockchain)
83  {
84  block_weights.push_back(bi.block_weight);
85  }
86 }
struct rule_list head
void get_block_chain(std::vector< block_info > &blockchain, const crypto::hash &head, size_t n) const
Definition: chaingen.cpp:60
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ boost::serialization::access

friend class boost::serialization::access
friend

Definition at line 261 of file chaingen.h.


The documentation for this class was generated from the following files: