Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
Staking weight incorporation (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
n-hutton authored and ejfitzgerald committed Jun 29, 2019
1 parent 0becf90 commit 2f88722
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
4 changes: 3 additions & 1 deletion libs/ledger/include/ledger/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ class Block
Proof proof; ///< The consensus proof
/// @}

// TODO(HUT): This should be part of body since it's no longer going to be metadata
uint64_t weight = 1;

/// @name Metadata for block management
/// @{
uint64_t weight = 1;
uint64_t total_weight = 1;
bool is_loose = false;
/// @}
Expand Down
3 changes: 2 additions & 1 deletion libs/ledger/include/ledger/consensus/stake_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ledger {
class StakeSnapshot;
class EntropyGeneratorInterface;

class StakeManager : public StakeManagerInterface
class StakeManager final : public StakeManagerInterface
{
public:
using Committee = std::vector<Address>;
Expand All @@ -46,6 +46,7 @@ class StakeManager : public StakeManagerInterface
void UpdateCurrentBlock(Block const &current) override;
std::size_t GetBlockGenerationWeight(Block const &previous, Address const &address) override;
bool ShouldGenerateBlock(Block const &previous, Address const &address) override;
bool ValidMinerForBlock(Block const &previous, Address const &address) override;
/// @}

// Accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class StakeManagerInterface
virtual void UpdateCurrentBlock(Block const &current) = 0;
virtual std::size_t GetBlockGenerationWeight(Block const &previous, Address const &address) = 0;
virtual bool ShouldGenerateBlock(Block const &previous, Address const &address) = 0;
virtual bool ValidMinerForBlock(Block const &previous, Address const &address) = 0;
/// @}

private:
Expand Down
20 changes: 20 additions & 0 deletions libs/ledger/src/chain/block_coordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ BlockCoordinator::State BlockCoordinator::OnSynchronised(State current, State pr
next_block_->body.block_number = current_block_->body.block_number + 1;
next_block_->body.miner = mining_address_;

if (stake_)
{
next_block_->weight = stake_->GetBlockGenerationWeight(*current_block_, mining_address_);
}

// Attach current DAG state
if (dag_)
{
Expand Down Expand Up @@ -469,6 +474,21 @@ BlockCoordinator::State BlockCoordinator::OnPreExecBlockValidation()
return fail("No previous block in chain");
}

// Check that the weight as given by the proof is correct
if (stake_)
{
if (!stake_->ValidMinerForBlock(*previous, current_block_->body.miner))
{
return fail("Block signed by miner deemed invalid by the staking mechanism");
}

if (current_block_->weight !=
stake_->GetBlockGenerationWeight(*previous, current_block_->body.miner))
{
return fail("Incorrect stake weight found for block");
}
}

// Check: Ensure the block number is continuous
uint64_t const expected_block_number = previous->body.block_number + 1u;
if (expected_block_number != current_block_->body.block_number)
Expand Down
7 changes: 1 addition & 6 deletions libs/ledger/src/chain/main_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,8 @@ BlockStatus MainChain::AddBlock(Block const &blk)
// create a copy of the block
auto block = std::make_shared<Block>(blk);

// update the weight based on the proof and the number of transactions
block->weight = 1;
// At this point we assume that the weight has been correctly set by the miner
block->total_weight = 1;
for (auto const &slice : block->body.slices)
{
block->weight += slice.size();
}

// pass the block to the
auto const status = InsertBlock(block);
Expand Down
13 changes: 13 additions & 0 deletions libs/ledger/src/consensus/stake_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,18 @@ void StakeManager::ResetInternal(StakeSnapshotPtr &&snapshot, std::size_t commit
current_block_index_ = 0;
}

bool StakeManager::ValidMinerForBlock(Block const &previous, Address const &address)
{
auto const committee = GetCommittee(previous);

if (!committee || committee->empty())
{
FETCH_LOG_WARN(LOGGING_NAME, "Unable to determine committee for block validation");
return false;
}

return std::find((*committee).begin(), (*committee).end(), address) != (*committee).end();
}

} // namespace ledger
} // namespace fetch

0 comments on commit 2f88722

Please sign in to comment.