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

ADR-034: Add highest voted height to finality provider #304

Merged
merged 2 commits into from
Nov 28, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Improvements

- [#304](https://github.com/babylonlabs-io/babylon/pull/304) Add highest voted height to finality provider

## v0.17.1

### Bug fixes
Expand All @@ -50,6 +54,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [278](https://github.com/babylonlabs-io/babylon/pull/278) Allow unbonding time to be min unbonding value

### Improvements

- [#264](https://github.com/babylonlabs-io/babylon/pull/264) bump docker workflow
version to 0.10.2, fix some Dockerfile issues
- [#284](https://github.com/babylonlabs-io/babylon/pull/284) Update cosmos sdk math dependency
Expand Down
6 changes: 6 additions & 0 deletions proto/babylon/btcstaking/v1/btcstaking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ message FinalityProvider {
uint32 slashed_btc_height = 7;
// jailed defines whether the finality provider is jailed
bool jailed = 8;
// highest_voted_height is the highest height for which the
// finality provider has voted
uint32 highest_voted_height = 9;
}

// FinalityProviderWithMeta wraps the FinalityProvider with metadata.
Expand All @@ -57,6 +60,9 @@ message FinalityProviderWithMeta {
uint32 slashed_btc_height = 5;
// jailed defines whether the finality provider is detected jailed
bool jailed = 6;
// highest_voted_height is the highest height for which the
// finality provider has voted
uint32 highest_voted_height = 7;
}

// BTCDelegation defines a BTC delegation
Expand Down
3 changes: 3 additions & 0 deletions proto/babylon/btcstaking/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,7 @@ message FinalityProviderResponse {
uint64 height = 8;
// jailed defines whether the finality provider is jailed
bool jailed = 9;
// highest_voted_height is the highest height for which the
// finality provider has voted
uint32 highest_voted_height = 10;
}
3 changes: 3 additions & 0 deletions proto/babylon/finality/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ message ActiveFinalityProvidersAtHeightResponse {
uint32 slashed_btc_height = 5;
// jailed defines whether the finality provider is detected jailed
bool jailed = 6;
// highest_voted_height is the highest height for which the
// finality provider has voted
uint32 highest_voted_height = 7;
}

// QueryActiveFinalityProvidersAtHeightResponse is the response type for the
Expand Down
11 changes: 11 additions & 0 deletions x/btcstaking/keeper/finality_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func (k Keeper) setFinalityProvider(ctx context.Context, fp *types.FinalityProvi
store.Set(fp.BtcPk.MustMarshal(), fpBytes)
}

// UpdateFinalityProvider update the given finality provider to KVStore
func (k Keeper) UpdateFinalityProvider(ctx context.Context, fp *types.FinalityProvider) error {
if !k.HasFinalityProvider(ctx, fp.BtcPk.MustMarshal()) {
return types.ErrFpNotFound
}

k.setFinalityProvider(ctx, fp)

return nil
}

// HasFinalityProvider checks if the finality provider exists
func (k Keeper) HasFinalityProvider(ctx context.Context, fpBTCPK []byte) bool {
store := k.finalityProviderStore(ctx)
Expand Down
4 changes: 4 additions & 0 deletions x/btcstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func FuzzFinalityProvider(f *testing.F) {
require.NoError(t, err)

AddFinalityProvider(t, ctx, *keeper, fp)
fp.HighestVotedHeight = uint32(datagen.RandomInt(r, 1000) + 1)
err = keeper.UpdateFinalityProvider(ctx, fp)
require.NoError(t, err)
fpsMap[fp.BtcPk.MarshalHex()] = fp
}

Expand All @@ -129,6 +132,7 @@ func FuzzFinalityProvider(f *testing.F) {
// check keys from map matches those in returned response
require.Equal(t, v.BtcPk.MarshalHex(), resp.FinalityProvider.BtcPk.MarshalHex())
require.Equal(t, v.Addr, resp.FinalityProvider.Addr)
require.Equal(t, v.HighestVotedHeight, resp.FinalityProvider.HighestVotedHeight)
}

// check some random non-existing guy
Expand Down
246 changes: 161 additions & 85 deletions x/btcstaking/types/btcstaking.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions x/btcstaking/types/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ func NewFinalityProviderResponse(f *FinalityProvider, bbnBlockHeight uint64) *Fi
SlashedBtcHeight: f.SlashedBtcHeight,
Jailed: f.Jailed,
Height: bbnBlockHeight,
HighestVotedHeight: f.HighestVotedHeight,
}
}
251 changes: 145 additions & 106 deletions x/btcstaking/types/query.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions x/finality/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (k Keeper) ActiveFinalityProvidersAtHeight(ctx context.Context, req *types.
VotingPower: votingPower,
SlashedBabylonHeight: finalityProvider.SlashedBabylonHeight,
SlashedBtcHeight: finalityProvider.SlashedBtcHeight,
HighestVotedHeight: finalityProvider.HighestVotedHeight,
}
finalityProvidersWithMeta = append(finalityProvidersWithMeta, &finalityProviderWithMeta)
}
Expand Down Expand Up @@ -420,6 +421,7 @@ func convertToActiveFinalityProvidersAtHeightResponse(finalityProvidersWithMeta
SlashedBabylonHeight: fpWithMeta.SlashedBabylonHeight,
SlashedBtcHeight: fpWithMeta.SlashedBtcHeight,
Jailed: fpWithMeta.Jailed,
HighestVotedHeight: fpWithMeta.HighestVotedHeight,
})
}
return activeFinalityProvidersAtHeightResponse
Expand Down
9 changes: 9 additions & 0 deletions x/finality/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ func (ms msgServer) AddFinalitySig(goCtx context.Context, req *types.MsgAddFinal
// this signature is good, add vote to DB
ms.SetSig(ctx, req.BlockHeight, fpPK, req.FinalitySig)

// update `HighestVotedHeight` if needed
if fp.HighestVotedHeight < uint32(req.BlockHeight) {
fp.HighestVotedHeight = uint32(req.BlockHeight)
err := ms.BTCStakingKeeper.UpdateFinalityProvider(ctx, fp)
if err != nil {
return nil, fmt.Errorf("failed to update the finality provider: %w", err)
}
}

// if this finality provider has signed the canonical block before,
// slash it via extracting its secret key, and emit an event
if ms.HasEvidence(ctx, req.FpBtcPk, req.BlockHeight) {
Expand Down
3 changes: 3 additions & 0 deletions x/finality/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func FuzzAddFinalitySig(f *testing.F) {
defer ctrl.Finish()

bsKeeper := types.NewMockBTCStakingKeeper(ctrl)
bsKeeper.EXPECT().UpdateFinalityProvider(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
cKeeper := types.NewMockCheckpointingKeeper(ctrl)
iKeeper := types.NewMockIncentiveKeeper(ctrl)
iKeeper.EXPECT().IndexRefundableMsg(gomock.Any(), gomock.Any()).AnyTimes()
Expand Down Expand Up @@ -310,6 +311,7 @@ func TestVoteForConflictingHashShouldRetrieveEvidenceAndSlash(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
bsKeeper := types.NewMockBTCStakingKeeper(ctrl)
bsKeeper.EXPECT().UpdateFinalityProvider(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
cKeeper := types.NewMockCheckpointingKeeper(ctrl)
iKeeper := types.NewMockIncentiveKeeper(ctrl)
iKeeper.EXPECT().IndexRefundableMsg(gomock.Any(), gomock.Any()).AnyTimes()
Expand Down Expand Up @@ -381,6 +383,7 @@ func TestDoNotPanicOnNilProof(t *testing.T) {
defer ctrl.Finish()

bsKeeper := types.NewMockBTCStakingKeeper(ctrl)
bsKeeper.EXPECT().UpdateFinalityProvider(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
cKeeper := types.NewMockCheckpointingKeeper(ctrl)
iKeeper := types.NewMockIncentiveKeeper(ctrl)
iKeeper.EXPECT().IndexRefundableMsg(gomock.Any(), gomock.Any()).AnyTimes()
Expand Down
4 changes: 3 additions & 1 deletion x/finality/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package types
import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
etypes "github.com/babylonlabs-io/babylon/x/epoching/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type BTCStakingKeeper interface {
Expand All @@ -20,6 +21,7 @@ type BTCStakingKeeper interface {
ClearPowerDistUpdateEvents(ctx context.Context, btcHeight uint32)
JailFinalityProvider(ctx context.Context, fpBTCPK []byte) error
UnjailFinalityProvider(ctx context.Context, fpBTCPK []byte) error
UpdateFinalityProvider(ctx context.Context, fp *bstypes.FinalityProvider) error
}

type CheckpointingKeeper interface {
Expand Down
14 changes: 14 additions & 0 deletions x/finality/types/mocked_keepers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading