Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add epoch to the pillar block #2773

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ include(CMakeModules/clang_format.cmake)
# cppcheck
include(CMakeModules/cppcheck.cmake)

add_custom_target(check-static DEPENDS cpp-check clang-format clang-format-check)
add_custom_target(check-static DEPENDS cpp-check)

# execute command to get git info
include(CMakeModules/git_info.cmake)
Expand Down
2 changes: 2 additions & 0 deletions libraries/common/include/common/encoding_solidity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace taraxa::util {
class EncodingSolidity {
public:
static constexpr uint8_t kStartPrefix = 32;
static constexpr uint8_t kStartPrefixSize = 1;
/// PACKING ///
template <typename... Params>
static bytes packFunctionCall(const std::string& function, const Params&... args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ class FinalChain {
*/
virtual h256 get_bridge_root(EthBlockNumber blk_num) const = 0;

/**
* @param blk_num
* @return bridge epoch
*/
virtual h256 get_bridge_epoch(EthBlockNumber blk_num) const = 0;
// TODO move out of here:

std::pair<val_t, bool> getBalance(addr_t const& addr) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ class PillarBlock {
public:
PillarBlock() = default;
PillarBlock(const dev::RLP& rlp);
PillarBlock(PbftPeriod period, h256 state_root, h256 bridge_root,
std::vector<ValidatorVoteCountChange>&& validator_votes_count_changes,
blk_hash_t previous_pillar_block_hash);
PillarBlock(PbftPeriod period, h256 state_root, blk_hash_t previous_pillar_block_hash, h256 bridge_root, u256 epoch,
std::vector<ValidatorVoteCountChange>&& validator_votes_count_changes);

PillarBlock(const PillarBlock& pillar_block);

Expand Down Expand Up @@ -79,6 +78,11 @@ class PillarBlock {
*/
const h256& getBridgeRoot() const;

/**
* @return epoch
*/
const u256& getEpoch() const;

/**
* @return pillar block rlp
*/
Expand All @@ -89,6 +93,10 @@ class PillarBlock {
*/
Json::Value getJson() const;

// this is needed for solidity encoding. So should be changed if fields are changed
static constexpr uint8_t kFieldsSize = 5;
static constexpr uint8_t kArrayPosAndSize = 2;
static constexpr uint8_t kFieldsInVoteCount = 2;
/**
* @note Solidity encoding is used for data that are sent to smart contracts
*
Expand All @@ -113,14 +121,18 @@ class PillarBlock {
// Pbft block(for period_) state root
h256 state_root_{};

// Hash of the previous pillar block as pillar blocks forms a chain
blk_hash_t previous_pillar_block_hash_{0};

// Bridge root
h256 bridge_root_{};

// Bridge epoch
u256 epoch_{};

// Delta change of validators votes count between current and latest pillar block
std::vector<ValidatorVoteCountChange> validators_votes_count_changes_{};

blk_hash_t previous_pillar_block_hash_{0};

mutable std::optional<blk_hash_t> hash_;
mutable std::shared_mutex hash_mutex_;
};
Expand Down
8 changes: 8 additions & 0 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,14 @@ class FinalChainImpl final : public FinalChain {
.code_retval);
}

h256 get_bridge_epoch(EthBlockNumber blk_num) const override {
const static auto get_bridge_epoch_method = util::EncodingSolidity::packFunctionCall("finalizedEpoch()");
return h256(call(state_api::EVMTransaction{dev::ZeroAddress, 1, kHardforksConfig.ficus_hf.bridge_contract_address,
state_api::ZeroAccount.nonce, 0, 10000000, get_bridge_epoch_method},
blk_num)
.code_retval);
}

private:
std::shared_ptr<TransactionHashes> get_transaction_hashes(std::optional<EthBlockNumber> n = {}) const {
const auto& trxs = db_->getPeriodTransactions(last_if_absent(n));
Expand Down
48 changes: 24 additions & 24 deletions libraries/core_libs/consensus/src/pillar_chain/pillar_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ PillarBlock::ValidatorVoteCountChange::ValidatorVoteCountChange(const dev::RLP&

PillarBlock::PillarBlock(const dev::RLP& rlp) : PillarBlock(util::rlp_dec<PillarBlock>(rlp)) {}

PillarBlock::PillarBlock(PbftPeriod period, h256 state_root, h256 bridge_root,
std::vector<ValidatorVoteCountChange>&& validator_votes_count_changes,
blk_hash_t previous_pillar_block_hash)
PillarBlock::PillarBlock(PbftPeriod period, h256 state_root, blk_hash_t previous_pillar_block_hash, h256 bridge_root,
u256 epoch, std::vector<ValidatorVoteCountChange>&& validator_votes_count_changes)
: pbft_period_(period),
state_root_(state_root),
previous_pillar_block_hash_(previous_pillar_block_hash),
bridge_root_(bridge_root),
validators_votes_count_changes_(std::move(validator_votes_count_changes)),
previous_pillar_block_hash_(previous_pillar_block_hash) {}
epoch_(epoch),
validators_votes_count_changes_(std::move(validator_votes_count_changes)) {}

PillarBlock::PillarBlock(const PillarBlock& pillar_block)
: pbft_period_(pillar_block.pbft_period_),
state_root_(pillar_block.state_root_),
previous_pillar_block_hash_(pillar_block.previous_pillar_block_hash_),
bridge_root_(pillar_block.bridge_root_),
validators_votes_count_changes_(pillar_block.validators_votes_count_changes_),
previous_pillar_block_hash_(pillar_block.previous_pillar_block_hash_) {}
epoch_(pillar_block.epoch_),
validators_votes_count_changes_(pillar_block.validators_votes_count_changes_) {}

dev::bytes PillarBlock::getRlp() const { return util::rlp_enc(*this); }

Expand All @@ -50,6 +51,8 @@ const h256& PillarBlock::getStateRoot() const { return state_root_; }

const h256& PillarBlock::getBridgeRoot() const { return bridge_root_; }

const u256& PillarBlock::getEpoch() const { return epoch_; }

blk_hash_t PillarBlock::getHash() const {
{
std::shared_lock lock(hash_mutex_);
Expand All @@ -67,8 +70,9 @@ Json::Value PillarBlock::getJson() const {
Json::Value res;
res["pbft_period"] = static_cast<Json::Value::UInt64>(pbft_period_);
res["state_root"] = dev::toJS(state_root_);
res["bridge_root"] = dev::toJS(bridge_root_);
res["previous_pillar_block_hash"] = dev::toJS(previous_pillar_block_hash_);
res["bridge_root"] = dev::toJS(bridge_root_);
res["epoch"] = dev::toJS(epoch_);
res["validators_vote_counts_changes"] = Json::Value(Json::arrayValue);
for (auto const& vote_count_change : validators_votes_count_changes_) {
Json::Value vote_count_change_json;
Expand All @@ -84,25 +88,21 @@ Json::Value PillarBlock::getJson() const {

dev::bytes PillarBlock::encodeSolidity() const {
dev::bytes result;
const uint8_t start_prefix = 32;
const uint8_t start_prefix_size = 1;
const uint8_t fields_size = 4;
const uint8_t array_pos_and_size = 2;
const uint8_t fields_in_vote_count_struct = 2;
result.reserve((start_prefix_size + fields_size + array_pos_and_size +
(fields_in_vote_count_struct * validators_votes_count_changes_.size())) *
result.reserve((util::EncodingSolidity::kStartPrefixSize + kFieldsSize + kArrayPosAndSize +
(kFieldsInVoteCount * validators_votes_count_changes_.size())) *
util::EncodingSolidity::kWordSize);

auto start = util::EncodingSolidity::pack(start_prefix);
auto start = util::EncodingSolidity::pack(util::EncodingSolidity::kStartPrefix);
result.insert(result.end(), start.begin(), start.end());

auto body = util::EncodingSolidity::pack(pbft_period_, state_root_, bridge_root_, previous_pillar_block_hash_);
auto body =
util::EncodingSolidity::pack(pbft_period_, state_root_, previous_pillar_block_hash_, bridge_root_, epoch_);
result.insert(result.end(), body.begin(), body.end());

const uint8_t array_position = (start_prefix_size + fields_size) * util::EncodingSolidity::kWordSize;
const uint8_t array_position =
(util::EncodingSolidity::kStartPrefixSize + kFieldsSize) * util::EncodingSolidity::kWordSize;
auto array_data = util::EncodingSolidity::pack(array_position, validators_votes_count_changes_.size());
result.insert(result.end(), array_data.begin(), array_data.end());

for (auto& vote_count_change : validators_votes_count_changes_) {
auto vote_count_change_encoded =
util::EncodingSolidity::pack(vote_count_change.addr_, vote_count_change.vote_count_change_);
Expand All @@ -116,14 +116,14 @@ PillarBlock PillarBlock::decodeSolidity(const bytes& enc) {
PillarBlock b;

uint64_t start_prefix = 0;
util::EncodingSolidity::staticUnpack(enc, start_prefix, b.pbft_period_, b.state_root_, b.bridge_root_,
b.previous_pillar_block_hash_);
util::EncodingSolidity::staticUnpack(enc, start_prefix, b.pbft_period_, b.state_root_, b.previous_pillar_block_hash_,
b.bridge_root_, b.epoch_);

uint64_t array_pos = (1 + 4) * util::EncodingSolidity::kWordSize;
uint64_t array_pos = (util::EncodingSolidity::kStartPrefixSize + kFieldsSize) * util::EncodingSolidity::kWordSize;
uint64_t array_size = 0;
bytes array_data(enc.begin() + array_pos, enc.end());
util::EncodingSolidity::staticUnpack(array_data, array_pos, array_size);
array_data = bytes(array_data.begin() + 2 * util::EncodingSolidity::kWordSize, array_data.end());
array_data = bytes(array_data.begin() + kFieldsInVoteCount * util::EncodingSolidity::kWordSize, array_data.end());

for (size_t i = 0; i < array_size; i++) {
addr_t addr;
Expand All @@ -137,7 +137,7 @@ PillarBlock PillarBlock::decodeSolidity(const bytes& enc) {
return b;
}

RLP_FIELDS_DEFINE(PillarBlock, pbft_period_, state_root_, bridge_root_, previous_pillar_block_hash_,
RLP_FIELDS_DEFINE(PillarBlock, pbft_period_, state_root_, previous_pillar_block_hash_, bridge_root_, epoch_,
validators_votes_count_changes_)

PillarBlockData::PillarBlockData(std::shared_ptr<PillarBlock> block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ std::shared_ptr<PillarBlock> PillarChainManager::createPillarBlock(
blk_hash_t previous_pillar_block_hash{}; // null block hash
auto new_vote_counts = final_chain_->dpos_validators_vote_counts(block_num);
std::vector<PillarBlock::ValidatorVoteCountChange> votes_count_changes;
h256 bridge_root = final_chain_->get_bridge_root(block_num);

// First ever pillar block
if (block_num == kFicusHfConfig.firstPillarBlockPeriod()) {
Expand Down Expand Up @@ -94,9 +93,11 @@ std::shared_ptr<PillarBlock> PillarChainManager::createPillarBlock(
votes_count_changes = getOrderedValidatorsVoteCountsChanges(new_vote_counts, current_pillar_block_vote_counts_);
}

h256 bridge_root = final_chain_->get_bridge_root(block_num);
u256 bridge_epoch = final_chain_->get_bridge_epoch(block_num);
const auto pillar_block =
std::make_shared<PillarBlock>(block_num, block_data->final_chain_blk->state_root, bridge_root,
std::move(votes_count_changes), previous_pillar_block_hash);
std::make_shared<PillarBlock>(block_num, block_data->final_chain_blk->state_root, previous_pillar_block_hash,
bridge_root, bridge_epoch, std::move(votes_count_changes));

// Check if some pillar block was not skipped
if (!isValidPillarBlock(pillar_block)) {
Expand Down
Loading
Loading