Skip to content

Commit

Permalink
Distinguish cumulative weight from difficulty, and reorg using weight
Browse files Browse the repository at this point in the history
Include further PoW validation on uncle
  • Loading branch information
thaerkh committed Oct 24, 2018
1 parent 669aaf2 commit dfb1a4b
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 71 deletions.
19 changes: 13 additions & 6 deletions src/blockchain_db/blockchain_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ void BlockchainDB::add_transaction(const crypto::hash& blk_hash, const transacti
}

uint64_t BlockchainDB::add_block(const block& nephew, const size_t& nephew_size, const block& uncle, const size_t& uncle_size,
const difficulty_type& cumulative_difficulty, const uint64_t& coins_generated,
const std::vector<transaction>& txs)
const difficulty_type& cumulative_difficulty, const difficulty_type& cumulative_weight,
const uint64_t& coins_generated, const std::vector<transaction>& txs)
{
block_txn_start(false);
uint64_t new_height = add_block_raw(nephew, nephew_size, cumulative_difficulty, coins_generated, txs);
add_uncle(uncle, uncle_size, cumulative_difficulty, coins_generated, get_block_hash(uncle), new_height - 1);
uint64_t new_height = add_block_raw(nephew, nephew_size, cumulative_difficulty, cumulative_weight, coins_generated, txs);
add_uncle(uncle, uncle_size, cumulative_difficulty, cumulative_weight, coins_generated, get_block_hash(uncle), new_height - 1);
block_txn_stop();

++num_calls;
Expand All @@ -204,6 +204,7 @@ uint64_t BlockchainDB::add_block(const block& nephew, const size_t& nephew_size,
uint64_t BlockchainDB::add_block_raw( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
, const difficulty_type& cumulative_weight
, const uint64_t& coins_generated
, const std::vector<transaction>& txs
)
Expand Down Expand Up @@ -236,7 +237,7 @@ uint64_t BlockchainDB::add_block_raw( const block& blk

// call out to subclass implementation to add the block & metadata
time1 = epee::misc_utils::get_tick_count();
add_block(blk, block_size, cumulative_difficulty, coins_generated, blk_hash);
add_block(blk, block_size, cumulative_difficulty, cumulative_weight, coins_generated, blk_hash);
TIME_MEASURE_FINISH(time1);
time_add_block1 += time1;

Expand All @@ -250,12 +251,13 @@ uint64_t BlockchainDB::add_block_raw( const block& blk
uint64_t BlockchainDB::add_block( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
, const difficulty_type& cumulative_weight
, const uint64_t& coins_generated
, const std::vector<transaction>& txs
)
{
block_txn_start(false);
uint64_t new_height = add_block_raw(blk, block_size, cumulative_difficulty, coins_generated, txs);
uint64_t new_height = add_block_raw(blk, block_size, cumulative_difficulty, cumulative_weight, coins_generated, txs);
block_txn_stop();
return new_height;
}
Expand Down Expand Up @@ -354,6 +356,11 @@ bool BlockchainDB::get_tx(const crypto::hash& h, cryptonote::transaction &tx) co
return true;
}

difficulty_type BlockchainDB::get_block_cumulative_weight(const crypto::hash& id) const
{
return get_block_cumulative_weight(get_block_height(id));
}

difficulty_type BlockchainDB::get_block_cumulative_difficulty(const crypto::hash& id) const
{
return get_block_cumulative_difficulty(get_block_height(id));
Expand Down
35 changes: 33 additions & 2 deletions src/blockchain_db/blockchain_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,14 @@ class BlockchainDB
* @param blk the block to be added
* @param block_size the size of the block (transactions and all)
* @param cumulative_difficulty the accumulated difficulty after this block
* @param cumulative_weight the accumulated weight including uncles after this block
* @param coins_generated the number of coins generated total after this block
* @param blk_hash the hash of the block
*/
virtual void add_block( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
, const difficulty_type& cumulative_weight
, const uint64_t& coins_generated
, const crypto::hash& blk_hash
) = 0;
Expand All @@ -378,13 +380,15 @@ class BlockchainDB
* @param uncle the uncle block to be added
* @param uncle_size the size of the uncle block (transactions and all)
* @param cumulative_difficulty the accumulated difficulty at height when this uncle was mined
* @param cumulative_weight the accumulated weight at height when this uncle was mined
* @param coins_generated the number of coins generated total after this uncle block was mined
* @param uncle_hash the hash of the uncle block
* @param height the height of where the uncle block was mined
*/
virtual void add_uncle(const block& uncle,
const size_t& uncle_size,
const difficulty_type& cumulative_difficulty,
const difficulty_type& cumulative_weight,
const uint64_t& coins_generated,
const crypto::hash& uncle_hash,
const uint64_t height) = 0;
Expand Down Expand Up @@ -801,6 +805,7 @@ class BlockchainDB
virtual uint64_t add_block( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
, const difficulty_type& cumulative_weight
, const uint64_t& coins_generated
, const std::vector<transaction>& txs
);
Expand All @@ -821,6 +826,7 @@ class BlockchainDB
virtual uint64_t add_block_raw( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
, const difficulty_type& cumulative_weight
, const uint64_t& coins_generated
, const std::vector<transaction>& txs
);
Expand All @@ -841,8 +847,8 @@ class BlockchainDB
* @return the height of the chain post-addition
*/
virtual uint64_t add_block(const block& nephew, const size_t& nephew_size, const block& uncle, const size_t& uncle_size,
const difficulty_type& cumulative_difficulty, const uint64_t& coins_generated,
const std::vector<transaction>& txs);
const difficulty_type& cumulative_difficulty, const difficulty_type& cumulative_weight,
const uint64_t& coins_generated, const std::vector<transaction>& txs);
/**
* @brief checks if a block exists
*
Expand Down Expand Up @@ -1043,6 +1049,20 @@ class BlockchainDB
*/
virtual size_t get_block_size(const uint64_t& height) const = 0;

/**
* @brief fetch a block's cumulative weight
*
* The subclass should return the cumulative weight of the block with the
* given height.
*
* If the block does not exist, the subclass should throw BLOCK_DNE
*
* @param height the height requested
*
* @return the cumulative difficulty
*/
virtual difficulty_type get_block_cumulative_weight(const uint64_t& height) const = 0;

/**
* @brief fetch a block's cumulative difficulty
*
Expand All @@ -1057,6 +1077,17 @@ class BlockchainDB
*/
virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const = 0;

/**
* @brief fetch a block's cumulative weight by hash id (main chain)
*
* If the block does not exist, the subclass should throw BLOCK_DNE
*
* @param id the hash id of the block requested
*
* @return the cumulative weight
*/
virtual difficulty_type get_block_cumulative_weight(const crypto::hash& id) const;

/**
* @brief fetch a block's cumulative difficulty by hash id (main chain)
*
Expand Down
49 changes: 30 additions & 19 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,6 @@ inline void lmdb_db_open(MDB_txn* txn, const char* name, int flags, MDB_dbi& dbi
namespace cryptonote
{

typedef struct mdb_block_info
{
uint64_t bi_height;
uint64_t bi_timestamp;
uint64_t bi_coins;
uint64_t bi_size; // a size_t really but we need 32-bit compat
difficulty_type bi_diff;
crypto::hash bi_hash;
} mdb_block_info;

typedef struct blk_height {
crypto::hash bh_hash;
uint64_t bh_height;
Expand Down Expand Up @@ -672,7 +662,8 @@ uint64_t BlockchainLMDB::get_estimated_batch_size(uint64_t batch_num_blocks, uin
}

void BlockchainLMDB::add_uncle(const block& uncle, const size_t& uncle_size, const difficulty_type& cumulative_difficulty,
const uint64_t& coins_generated, const crypto::hash& uncle_hash, uint64_t height)
const difficulty_type& cumulative_weight, const uint64_t& coins_generated,
const crypto::hash& uncle_hash, uint64_t height)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);

Expand Down Expand Up @@ -710,6 +701,7 @@ void BlockchainLMDB::add_uncle(const block& uncle, const size_t& uncle_size, con
ui.bi_size = uncle_size;
ui.bi_diff = cumulative_difficulty;
ui.bi_hash = uncle_hash;
ui.bi_weight = cumulative_weight;

MDB_val_set(val, ui);
result = mdb_cursor_put(m_cur_uncle_info, (MDB_val *)&zerokval, &val, MDB_APPENDDUP);
Expand All @@ -720,8 +712,8 @@ void BlockchainLMDB::add_uncle(const block& uncle, const size_t& uncle_size, con
m_cum_count++;
}

void BlockchainLMDB::add_block(const block& blk, const size_t& block_size, const difficulty_type& cumulative_difficulty, const uint64_t& coins_generated,
const crypto::hash& blk_hash)
void BlockchainLMDB::add_block(const block& blk, const size_t& block_size, const difficulty_type& cumulative_difficulty,
const difficulty_type& cumulative_weight, const uint64_t& coins_generated, const crypto::hash& blk_hash)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
Expand Down Expand Up @@ -769,6 +761,7 @@ void BlockchainLMDB::add_block(const block& blk, const size_t& block_size, const
bi.bi_size = block_size;
bi.bi_diff = cumulative_difficulty;
bi.bi_hash = blk_hash;
bi.bi_weight = cumulative_weight;

MDB_val_set(val, bi);
result = mdb_cursor_put(m_cur_block_info, (MDB_val *)&zerokval, &val, MDB_APPENDDUP);
Expand Down Expand Up @@ -2089,7 +2082,23 @@ size_t BlockchainLMDB::get_block_size(const uint64_t& height) const
return ret;
}

difficulty_type BlockchainLMDB::get_block_cumulative_weight(const uint64_t& height) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__ << " height: " << height);
mdb_block_info bi = get_block_info(height);
difficulty_type ret = bi.bi_weight;
return ret;
}

difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t& height) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__ << " height: " << height);
mdb_block_info bi = get_block_info(height);
difficulty_type ret = bi.bi_diff;
return ret;
}

mdb_block_info BlockchainLMDB::get_block_info(const uint64_t& height) const
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__ << " height: " << height);
check_open();
Expand All @@ -2106,10 +2115,11 @@ difficulty_type BlockchainLMDB::get_block_cumulative_difficulty(const uint64_t&
else if (get_result)
throw0(DB_ERROR("Error attempting to retrieve a cumulative difficulty from the db"));

mdb_block_info *bi = (mdb_block_info *)result.mv_data;
difficulty_type ret = bi->bi_diff;
// TODO-TK: concerns of a dangling pointer (though rest of the code seems to trust it)
mdb_block_info bi = *((mdb_block_info *)result.mv_data);

TXN_POSTFIX_RDONLY();
return ret;
return bi;
}

difficulty_type BlockchainLMDB::get_block_difficulty(const uint64_t& height) const
Expand Down Expand Up @@ -3123,8 +3133,9 @@ void BlockchainLMDB::block_txn_abort()
}
}

uint64_t BlockchainLMDB::add_block_raw(const block& blk, const size_t& block_size, const difficulty_type& cumulative_difficulty, const uint64_t& coins_generated,
const std::vector<transaction>& txs)
uint64_t BlockchainLMDB::add_block_raw(const block& blk, const size_t& block_size, const difficulty_type& cumulative_difficulty,
const difficulty_type& cumulative_weight, const uint64_t& coins_generated,
const std::vector<transaction>& txs)
{
LOG_PRINT_L3("BlockchainLMDB::" << __func__);
check_open();
Expand All @@ -3142,7 +3153,7 @@ uint64_t BlockchainLMDB::add_block_raw(const block& blk, const size_t& block_siz

try
{
BlockchainDB::add_block_raw(blk, block_size, cumulative_difficulty, coins_generated, txs);
BlockchainDB::add_block_raw(blk, block_size, cumulative_difficulty, cumulative_weight, coins_generated, txs);
}
catch (const DB_ERROR_TXN_START& e)
{
Expand Down
19 changes: 18 additions & 1 deletion src/blockchain_db/lmdb/db_lmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ typedef struct mdb_txn_cursors
#define m_cur_txpool_blob m_cursors->m_txc_txpool_blob
#define m_cur_hf_versions m_cursors->m_txc_hf_versions

typedef struct mdb_block_info
{
uint64_t bi_height;
uint64_t bi_timestamp;
uint64_t bi_coins;
uint64_t bi_size; // a size_t really but we need 32-bit compat
difficulty_type bi_diff;
crypto::hash bi_hash;
difficulty_type bi_weight;
} mdb_block_info;

typedef struct mdb_rflags
{
bool m_rf_txn;
Expand Down Expand Up @@ -213,8 +224,12 @@ class BlockchainLMDB : public BlockchainDB

virtual size_t get_block_size(const uint64_t& height) const;

virtual difficulty_type get_block_cumulative_weight(const uint64_t& height) const;

virtual difficulty_type get_block_cumulative_difficulty(const uint64_t& height) const;

virtual mdb_block_info get_block_info(const uint64_t& height) const;

virtual difficulty_type get_block_difficulty(const uint64_t& height) const;

virtual uint64_t get_block_already_generated_coins(const uint64_t& height) const;
Expand Down Expand Up @@ -280,6 +295,7 @@ class BlockchainLMDB : public BlockchainDB
virtual uint64_t add_block_raw( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
, const difficulty_type& cumulative_weight
, const uint64_t& coins_generated
, const std::vector<transaction>& txs
);
Expand Down Expand Up @@ -319,11 +335,12 @@ class BlockchainLMDB : public BlockchainDB
uint64_t get_estimated_batch_size(uint64_t batch_num_blocks, uint64_t batch_bytes) const;

virtual void add_uncle(const block& uncle, const size_t& uncle_size, const difficulty_type& cumulative_difficulty,
const uint64_t& coins_generated, const crypto::hash& uncle_hash, uint64_t height);
const difficulty_type& cumulative_weight, const uint64_t& coins_generated, const crypto::hash& uncle_hash, uint64_t height);

virtual void add_block( const block& blk
, const size_t& block_size
, const difficulty_type& cumulative_difficulty
, const difficulty_type& cumulative_weight
, const uint64_t& coins_generated
, const crypto::hash& block_hash
);
Expand Down
4 changes: 3 additions & 1 deletion src/blockchain_utilities/blockchain_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,17 @@ int import_from_file(cryptonote::core& core, const std::string& import_file_path

size_t block_size;
difficulty_type cumulative_difficulty;
difficulty_type cumulative_weight;
uint64_t coins_generated;

block_size = bp.block_size;
cumulative_difficulty = bp.cumulative_difficulty;
cumulative_weight = bp.cumulative_weight;
coins_generated = bp.coins_generated;

try
{
core.get_blockchain_storage().get_db().add_block(b, block_size, cumulative_difficulty, coins_generated, txs);
core.get_blockchain_storage().get_db().add_block(b, block_size, cumulative_difficulty, cumulative_weight, coins_generated, txs);
}
catch (const std::exception& e)
{
Expand Down
2 changes: 2 additions & 0 deletions src/blockchain_utilities/bootstrap_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ namespace cryptonote
std::vector<transaction> txs;
size_t block_size;
difficulty_type cumulative_difficulty;
difficulty_type cumulative_weight;
uint64_t coins_generated;

BEGIN_SERIALIZE()
FIELD(block)
FIELD(txs)
VARINT_FIELD(block_size)
VARINT_FIELD(cumulative_difficulty)
VARINT_FIELD(cumulative_weight)
VARINT_FIELD(coins_generated)
END_SERIALIZE()
};
Expand Down
1 change: 0 additions & 1 deletion src/cryptonote_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@

#define UNCLE_REWARD_RATIO 2
#define NEPHEW_REWARD_RATIO 20
#define UNCLE_DIFFICULTY_RATIO NEPHEW_REWARD_RATIO

#define CRYPTONOTE_REWARD_BLOCKS_WINDOW 100
#define CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1 300000
Expand Down
Loading

0 comments on commit dfb1a4b

Please sign in to comment.