Electroneum
gen_block_reward Struct Reference

#include <block_reward.h>

Inheritance diagram for gen_block_reward:
Collaboration diagram for gen_block_reward:

Public Member Functions

 gen_block_reward ()
 
bool generate (std::vector< test_event_entry > &events) const
 
bool check_block_verification_context (const cryptonote::block_verification_context &bvc, size_t event_idx, const cryptonote::block &blk)
 
bool mark_invalid_block (cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
 
bool mark_checked_block (cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
 
bool check_block_rewards (cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
 
- Public Member Functions inherited from test_chain_unit_base
void register_callback (const std::string &cb_name, verify_callback cb)
 
bool verify (const std::string &cb_name, cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
 
bool check_block_verification_context (const cryptonote::block_verification_context &bvc, size_t event_idx, const cryptonote::block &)
 
bool check_tx_verification_context (const cryptonote::tx_verification_context &tvc, bool, size_t, const cryptonote::transaction &)
 
bool check_tx_verification_context_array (const std::vector< cryptonote::tx_verification_context > &tvcs, size_t, size_t, const std::vector< cryptonote::transaction > &)
 

Additional Inherited Members

- Public Types inherited from test_chain_unit_base
typedef boost::function< bool(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)> verify_callback
 
typedef std::map< std::string, verify_callbackcallbacks_map
 

Detailed Description

Definition at line 35 of file block_reward.h.

Constructor & Destructor Documentation

◆ gen_block_reward()

gen_block_reward::gen_block_reward ( )

Definition at line 120 of file block_reward.cpp.

121  : m_invalid_block_index(0)
122 {
126 }
bool check_block_rewards(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
#define REGISTER_CALLBACK_METHOD(CLASS, METHOD)
Definition: chaingen.h:830
bool mark_checked_block(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)
bool mark_invalid_block(cryptonote::core &c, size_t ev_index, const std::vector< test_event_entry > &events)

Member Function Documentation

◆ check_block_rewards()

bool gen_block_reward::check_block_rewards ( cryptonote::core c,
size_t  ev_index,
const std::vector< test_event_entry > &  events 
)

Definition at line 246 of file block_reward.cpp.

247 {
248  DEFINE_TESTS_ERROR_CONTEXT("gen_block_reward_without_txs::check_block_rewards");
249 
250  std::array<uint64_t, 7> blk_rewards;
251  blk_rewards[0] = ETN_SUPPLY >> EMISSION_SPEED_FACTOR_PER_MINUTE;
252  // Take into account the premine.
253  blk_rewards[1] = 1260000000000;
254  uint64_t cumulative_reward = blk_rewards[0] + blk_rewards[1];
255  for (size_t i = 2; i < blk_rewards.size(); ++i)
256  {
257  blk_rewards[i] = (ETN_SUPPLY - cumulative_reward) >> EMISSION_SPEED_FACTOR_PER_MINUTE;
258  cumulative_reward += blk_rewards[i];
259  }
260 
261  for (size_t i = 0; i < 5; ++i)
262  {
263  block blk_i = boost::get<block>(events[m_checked_blocks_indices[i]]);
264  CHECK_EQ(blk_rewards[i], get_tx_out_amount(blk_i.miner_tx));
265  }
266 
267  block blk_n1 = boost::get<block>(events[m_checked_blocks_indices[5]]);
268  CHECK_EQ(blk_rewards[5] + 3 * TESTS_DEFAULT_FEE, get_tx_out_amount(blk_n1.miner_tx));
269 
270  block blk_n2 = boost::get<block>(events[m_checked_blocks_indices[6]]);
271  CHECK_EQ(blk_rewards[6] + (5 + 7) * TESTS_DEFAULT_FEE, get_tx_out_amount(blk_n2.miner_tx));
272 
273  block blk_n3 = boost::get<block>(events[m_checked_blocks_indices[7]]);
274  CHECK_EQ((11 + 13) * TESTS_DEFAULT_FEE, get_tx_out_amount(blk_n3.miner_tx));
275 
276  return true;
277 }
#define EMISSION_SPEED_FACTOR_PER_MINUTE
#define TESTS_DEFAULT_FEE
Definition: chaingen.h:1061
#define ETN_SUPPLY
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CHECK_EQ(v1, v2)
Definition: chaingen.h:1058
#define DEFINE_TESTS_ERROR_CONTEXT(text)
Definition: chaingen.h:1056

◆ check_block_verification_context()

bool gen_block_reward::check_block_verification_context ( const cryptonote::block_verification_context bvc,
size_t  event_idx,
const cryptonote::block blk 
)

Definition at line 221 of file block_reward.cpp.

222 {
223  if (m_invalid_block_index == event_idx)
224  {
225  m_invalid_block_index = 0;
226  return bvc.m_verification_failed;
227  }
228  else
229  {
230  return !bvc.m_verification_failed;
231  }
232 }

◆ generate()

bool gen_block_reward::generate ( std::vector< test_event_entry > &  events) const

Definition at line 128 of file block_reward.cpp.

129 {
130  uint64_t ts_start = 1338224400;
131 
132  GENERATE_ACCOUNT(miner_account);
133  MAKE_GENESIS_BLOCK(events, blk_0, miner_account, ts_start);
134  DO_CALLBACK(events, "mark_checked_block");
135  MAKE_ACCOUNT(events, bob_account);
136 
137  // Test: miner transactions without outputs (block reward == 0)
138  block blk_0r;
139  if (!rewind_blocks(events, generator, blk_0r, blk_0, miner_account, CRYPTONOTE_REWARD_BLOCKS_WINDOW))
140  return false;
141 
142  // Test: block reward is calculated using median of the latest CRYPTONOTE_REWARD_BLOCKS_WINDOW blocks
143  DO_CALLBACK(events, "mark_invalid_block");
144  block blk_1_bad_1;
145  if (!construct_max_weight_block(generator, blk_1_bad_1, blk_0r, miner_account, CRYPTONOTE_REWARD_BLOCKS_WINDOW + 1))
146  return false;
147  events.push_back(blk_1_bad_1);
148 
149  DO_CALLBACK(events, "mark_invalid_block");
150  block blk_1_bad_2;
151  if (!construct_max_weight_block(generator, blk_1_bad_2, blk_0r, miner_account, CRYPTONOTE_REWARD_BLOCKS_WINDOW - 1))
152  return false;
153  events.push_back(blk_1_bad_2);
154 
155  block blk_1;
156  if (!construct_max_weight_block(generator, blk_1, blk_0r, miner_account))
157  return false;
158  events.push_back(blk_1);
159 
161 
162  MAKE_NEXT_BLOCK(events, blk_2, blk_1, miner_account);
163  DO_CALLBACK(events, "mark_checked_block");
164  MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_account);
165  DO_CALLBACK(events, "mark_checked_block");
166  MAKE_NEXT_BLOCK(events, blk_4, blk_3, miner_account);
167  DO_CALLBACK(events, "mark_checked_block");
168  MAKE_NEXT_BLOCK(events, blk_5, blk_4, miner_account);
169  DO_CALLBACK(events, "mark_checked_block");
170 
171  block blk_5r;
172  if (!rewind_blocks(events, generator, blk_5r, blk_5, miner_account, CRYPTONOTE_MINED_ETN_UNLOCK_WINDOW))
173  return false;
174 
175  // Test: fee increases block reward
176  transaction tx_0(construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 3 * TESTS_DEFAULT_FEE));
177  MAKE_NEXT_BLOCK_TX1(events, blk_6, blk_5r, miner_account, tx_0);
178  DO_CALLBACK(events, "mark_checked_block");
179 
180  // Test: fee from all block transactions increase block reward
181  std::list<transaction> txs_0;
182  txs_0.push_back(construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 5 * TESTS_DEFAULT_FEE));
183  txs_0.push_back(construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 7 * TESTS_DEFAULT_FEE));
184  MAKE_NEXT_BLOCK_TX_LIST(events, blk_7, blk_6, miner_account, txs_0);
185  DO_CALLBACK(events, "mark_checked_block");
186 
187  // Test: block reward == transactions fee
188  {
189  transaction tx_1 = construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 11 * TESTS_DEFAULT_FEE);
190  transaction tx_2 = construct_tx_with_fee(events, blk_5, miner_account, bob_account, MK_COINS(1), 13 * TESTS_DEFAULT_FEE);
191  size_t txs_1_weight = get_transaction_weight(tx_1) + get_transaction_weight(tx_2);
192  uint64_t txs_fee = get_tx_fee(tx_1) + get_tx_fee(tx_2);
193 
194  std::vector<size_t> block_weights;
195  generator.get_last_n_block_weights(block_weights, get_block_hash(blk_7), CRYPTONOTE_REWARD_BLOCKS_WINDOW);
196  size_t median = misc_utils::median(block_weights);
197 
198  transaction miner_tx;
199  bool r = construct_miner_tx_by_weight(miner_tx, get_block_height(blk_7) + 1, generator.get_already_generated_coins(blk_7),
200  miner_account.get_keys().m_account_address, block_weights, 2 * median - txs_1_weight, 2 * median, txs_fee);
201  if (!r)
202  return false;
203 
204  std::vector<crypto::hash> txs_1_hashes;
205  txs_1_hashes.push_back(get_transaction_hash(tx_1));
206  txs_1_hashes.push_back(get_transaction_hash(tx_2));
207 
208  block blk_8;
209  generator.construct_block_manually(blk_8, blk_7, miner_account, test_generator::bf_miner_tx | test_generator::bf_tx_hashes,
210  0, 0, 0, crypto::hash(), 0, miner_tx, txs_1_hashes, txs_1_weight);
211 
212  events.push_back(blk_8);
213  DO_CALLBACK(events, "mark_checked_block");
214  }
215 
216  DO_CALLBACK(events, "check_block_rewards");
217 
218  return true;
219 }
#define CRYPTONOTE_MINED_ETN_UNLOCK_WINDOW
#define MAKE_NEXT_BLOCK_TX1(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, TX1)
Definition: chaingen.h:849
bool get_tx_fee(const transaction &tx, uint64_t &fee)
#define MAKE_NEXT_BLOCK(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC)
Definition: chaingen.h:839
#define TESTS_DEFAULT_FEE
Definition: chaingen.h:1061
uint64_t get_transaction_weight(const transaction &tx, size_t blob_size)
transaction construct_tx_with_fee(std::vector< test_event_entry > &events, const block &blk_head, const account_base &acc_from, const var_addr_t &to, uint64_t amount, uint64_t fee)
Definition: chaingen.cpp:931
#define MAKE_GENESIS_BLOCK(VEC_EVENTS, BLK_NAME, MINER_ACC, TS)
Definition: chaingen.h:833
unsigned __int64 uint64_t
Definition: stdint.h:136
type_vec_type median(std::vector< type_vec_type > &v)
#define CRYPTONOTE_REWARD_BLOCKS_WINDOW
#define MAKE_ACCOUNT(VEC_EVENTS, account)
Definition: chaingen.h:815
crypto::hash get_transaction_hash(const transaction &t)
crypto::hash get_block_hash(uint64_t height)
POD_CLASS hash
Definition: hash.h:50
#define MK_COINS(amount)
Definition: chaingen.h:1060
#define GENERATE_ACCOUNT(account)
Definition: chaingen.h:801
#define DO_CALLBACK(VEC_EVENTS, CB_NAME)
Definition: chaingen.h:820
#define MAKE_NEXT_BLOCK_TX_LIST(VEC_EVENTS, BLK_NAME, PREV_BLOCK, MINER_ACC, TXLIST)
Definition: chaingen.h:867
uint64_t get_block_height(const block &b)
Here is the call graph for this function:

◆ mark_checked_block()

bool gen_block_reward::mark_checked_block ( cryptonote::core c,
size_t  ev_index,
const std::vector< test_event_entry > &  events 
)

Definition at line 240 of file block_reward.cpp.

241 {
242  m_checked_blocks_indices.push_back(ev_index - 1);
243  return true;
244 }

◆ mark_invalid_block()

bool gen_block_reward::mark_invalid_block ( cryptonote::core c,
size_t  ev_index,
const std::vector< test_event_entry > &  events 
)

Definition at line 234 of file block_reward.cpp.

235 {
236  m_invalid_block_index = ev_index + 1;
237  return true;
238 }

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