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

beefy test #1992

Merged
merged 2 commits into from
Mar 7, 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
29 changes: 17 additions & 12 deletions core/consensus/beefy/impl/beefy_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,24 +428,29 @@ namespace kagome::network {
}
auto next_session = sessions_.upper_bound(beefy_finalized_ + 1);
if (next_session == sessions_.begin()) {
SL_VERBOSE(log_, "can't vote: no sessions");
return outcome::success();
if (next_session == sessions_.end()) {
SL_VERBOSE(log_, "can't vote: no sessions");
return outcome::success();
}
++next_session;
}
auto session = std::prev(next_session);
auto grandpa_finalized = block_tree_->getLastFinalized().number;
auto target = session->first;
if (next_session != sessions_.end()
and grandpa_finalized >= next_session->first) {
target = next_session->first;
session = next_session;
} else if (target <= beefy_finalized_) {
auto diff = grandpa_finalized - beefy_finalized_ + 1;
target = beefy_finalized_
+ std::max<primitives::BlockNumber>(
min_delta_, math::nextHighPowerOf2(diff / 2));
if (next_session != sessions_.end() and target >= next_session->first) {
if (target <= beefy_finalized_) {
if (next_session != sessions_.end()
and grandpa_finalized >= next_session->first) {
target = next_session->first;
session = next_session;
} else {
auto diff = grandpa_finalized - beefy_finalized_ + 1;
target = beefy_finalized_
+ std::max<primitives::BlockNumber>(
min_delta_, math::nextHighPowerOf2(diff / 2));
if (next_session != sessions_.end() and target >= next_session->first) {
target = next_session->first;
session = next_session;
}
}
}
if (target > grandpa_finalized) {
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/beefy/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace kagome::consensus::beefy {
SCALE_TIE(2);

std::vector<crypto::EcdsaPublicKey> validators;
AuthoritySetId id;
AuthoritySetId id = 0;

std::optional<AuthorityIndex> find(
const crypto::EcdsaPublicKey &key) const {
Expand Down
152 changes: 144 additions & 8 deletions test/core/consensus/beefy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#include <libp2p/basic/scheduler.hpp>

#include "common/main_thread_pool.hpp"
#include "consensus/beefy/digest.hpp"
#include "consensus/beefy/impl/beefy_impl.hpp"
#include "consensus/beefy/impl/beefy_thread_pool.hpp"
#include "consensus/beefy/sig.hpp"
#include "crypto/ecdsa/ecdsa_provider_impl.hpp"
#include "crypto/hasher/hasher_impl.hpp"
#include "mock/core/application/app_state_manager_mock.hpp"
Expand All @@ -26,20 +28,26 @@
#include "testutil/lazy.hpp"
#include "testutil/prepare_loggers.hpp"

using kagome::beefyMmrDigest;
using kagome::TestThreadPool;
using kagome::application::AppStateManagerMock;
using kagome::application::ChainSpecMock;
using kagome::blockchain::BlockTreeMock;
using kagome::common::Buffer;
using kagome::common::MainPoolHandler;
using kagome::common::MainThreadPool;
using kagome::consensus::Timeline;
using kagome::consensus::TimelineMock;
using kagome::consensus::beefy::AuthoritySetId;
using kagome::consensus::beefy::BeefyGossipMessage;
using kagome::consensus::beefy::BeefyJustification;
using kagome::consensus::beefy::Commitment;
using kagome::consensus::beefy::ConsensusDigest;
using kagome::consensus::beefy::kMmr;
using kagome::consensus::beefy::MmrRootHash;
using kagome::consensus::beefy::SignedCommitment;
using kagome::consensus::beefy::ValidatorSet;
using kagome::consensus::beefy::VoteMessage;
using kagome::crypto::EcdsaKeypair;
using kagome::crypto::EcdsaProviderImpl;
using kagome::crypto::EcdsaSeed;
Expand Down Expand Up @@ -96,7 +104,8 @@ struct Timer : libp2p::basic::Scheduler {
};

struct BeefyPeer {
EcdsaKeypair keys_;
bool vote_ = true;
std::shared_ptr<EcdsaKeypair> keys_;
BlockNumber finalized_ = 0;
std::vector<BlockNumber> justifications_;

Expand Down Expand Up @@ -137,13 +146,20 @@ struct BeefyTest : testing::Test {
EcdsaSeed seed;
seed[0] = i;
seed[1] = 1;
peer.keys_ = ecdsa_->generateKeypair(seed, {}).value();
EXPECT_CALL(*peer.keystore_, getBeefKeyPair(_))
.WillRepeatedly(Return(
std::make_pair(std::make_shared<EcdsaKeypair>(peer.keys_), i)));
peer.keys_ = std::make_shared<EcdsaKeypair>(
ecdsa_->generateKeypair(seed, {}).value());
EXPECT_CALL(*peer.keystore_, getBeefKeyPair(_)).WillRepeatedly([&, i]() {
return peer.vote_ ? std::make_optional(std::make_pair(peer.keys_, i))
: std::nullopt;
});

std::shared_ptr<AppStateManagerMock> app_state_manager_ =
std::make_shared<AppStateManagerMock>();
EXPECT_CALL(*app_state_manager_, atPrepare(_))
.Times(testing::AnyNumber());
EXPECT_CALL(*app_state_manager_, atLaunch(_)).Times(testing::AnyNumber());
EXPECT_CALL(*app_state_manager_, atShutdown(_))
.Times(testing::AnyNumber());
std::shared_ptr<MainThreadPool> main_thread_pool_ =
std::make_shared<MainThreadPool>(TestThreadPool{io_});
std::shared_ptr<MainPoolHandler> main_pool_handler_ =
Expand All @@ -153,7 +169,9 @@ struct BeefyTest : testing::Test {
std::shared_ptr<BeefyThreadPool> beefy_thread_pool_ =
std::make_shared<BeefyThreadPool>(TestThreadPool{io_});

EXPECT_CALL(*app_state_manager_, atLaunch(_));
EXPECT_CALL(*peer.block_tree_, bestBlock()).WillRepeatedly([&] {
return blocks_.back().blockInfo();
});

EXPECT_CALL(*peer.block_tree_, getLastFinalized()).WillRepeatedly([&] {
return blocks_[peer.finalized_].blockInfo();
Expand Down Expand Up @@ -205,7 +223,7 @@ struct BeefyTest : testing::Test {
ValidatorSet genesisVoters() {
ValidatorSet voters;
for (auto &peer : peers_) {
voters.validators.emplace_back(peer.keys_.public_key);
voters.validators.emplace_back(peer.keys_->public_key);
}
return voters;
}
Expand Down Expand Up @@ -314,7 +332,6 @@ TEST_F(BeefyTest, beefy_finalizing_blocks) {
}

TEST_F(BeefyTest, lagging_validators) {
min_delta_ = 1;
makePeers(3);
generate_blocks_and_sync(62, 30);

Expand Down Expand Up @@ -360,3 +377,122 @@ TEST_F(BeefyTest, lagging_validators) {
// block #60
expect(all(), {60});
}

TEST_F(BeefyTest, correct_beefy_payload) {
min_delta_ = 2;
makePeers(4);
generate_blocks_and_sync(12, 20);

// Alice, Bob, Charlie will vote on good payloads
// Dave will vote on bad mmr roots
peers_[3].vote_ = false;

// with 3 good voters and 1 bad one, consensus should happen and best blocks
// produced.
finalize_block_and_wait_for_beefy(1, {1});
finalize_block_and_wait_for_beefy(10, {9});

// now 2 good validators and 1 bad one are voting
finalize({0, 1, 3}, 11);
Commitment commitment{{}, 11, 0};
VoteMessage vote{
commitment,
peers_[3].keys_->public_key,
ecdsa_->signPrehashed(prehash(commitment), peers_[3].keys_->secret_key)
.value(),
};
for (auto &peer : peers_) {
peer.beefy_->onMessage(vote);
}
loop();
// verify consensus is _not_ reached
expect(all(), {});

// 3rd good validator catches up and votes as well
finalize({2}, 11);
rebroadcast();
loop();
// verify consensus is reached
expect(all(), {11});
}

TEST_F(BeefyTest, beefy_importing_justifications) {
makePeers(1);
auto &peer = peers_[0];
peer.vote_ = false;
generate_blocks_and_sync(3, 10);
finalize_block_and_wait_for_beefy(3, {});
auto justify = [&](BlockNumber block_number, AuthoritySetId set) {
auto mmr = beefyMmrDigest(blocks_[block_number]);
Commitment commitment{{{kMmr, Buffer{*mmr}}}, block_number, set};
auto sig =
ecdsa_->signPrehashed(prehash(commitment), peer.keys_->secret_key)
.value();
peer.beefy_->onJustification(
blocks_[block_number].hash(),
{Buffer{scale::encode(
BeefyJustification{SignedCommitment{commitment, {sig}}})
.value()}});
};
EXPECT_EQ(peer.beefy_->finalized(), 0);

// Import block 2 with valid justification.
justify(2, 0);
loop();
EXPECT_EQ(peer.beefy_->finalized(), 2);

// Import block 3 with invalid justification (incorrect validator set).
justify(3, 10);
loop();
EXPECT_EQ(peer.beefy_->finalized(), 2);
}

// TODO(turuslan): #1651, fetch justifications
// TEST_F(BeefyTest, on_demand_beefy_justification_sync) {}

TEST_F(BeefyTest, should_initialize_voter_at_genesis) {
makePeers(1);
// push 15 blocks with `AuthorityChange` digests every 10 blocks
generate_blocks_and_sync(15, 10);
// finalize 10 without justifications
finalize(all(), 10);
loop();
// Test initialization at session boundary.
// verify voter initialized with two sessions starting at blocks 1 and 10
// verify next vote target is mandatory block 1
expect(all(), {1, 10});
}

TEST_F(BeefyTest, should_initialize_voter_at_custom_genesis) {
genesis_ = 7;
makePeers(1);
// push 15 blocks with `AuthorityChange` digests every 10 blocks
generate_blocks_and_sync(10, 10);
// finalize 10 without justifications
finalize(all(), 10);
loop();
// Test initialization at session boundary.
// verify voter initialized with two sessions starting at blocks 7 and 10
// verify next vote target is mandatory block 7
expect(all(), {7, 10});

// TODO(turuslan): #1651, multiple beefy genesis
}

TEST_F(BeefyTest, beefy_finalizing_after_pallet_genesis) {
genesis_ = 15;
makePeers(2);
// push 42 blocks including `AuthorityChange` digests every 10 blocks.
generate_blocks_and_sync(42, 10);
// GRANDPA finalize blocks leading up to BEEFY pallet genesis -> BEEFY should
// finalize nothing.
finalize_block_and_wait_for_beefy(14, {});
// GRANDPA finalize block #16 -> BEEFY should finalize #15 (genesis mandatory)
// and #16.
finalize_block_and_wait_for_beefy(16, {15, 16});
// GRANDPA finalize #21 -> BEEFY finalize #20 (mandatory) and #21
finalize_block_and_wait_for_beefy(21, {20, 21});
}

// TODO(turuslan): #1651, report equivocation
// TEST_F(BeefyTest, beefy_reports_equivocations) {}
Loading