Skip to content

Commit

Permalink
finality: refactor voting power table and APIs (#222)
Browse files Browse the repository at this point in the history
Second half of #24 

This PR moves the voting power table/cache and relevant APIs to
`x/finality`. This includes the following

- [x] move voting power table KV store to `x/finality`
- [x] move voting power dist cache to `x/finality`
- [x] move 4 APIs `ActiveFinalityProvidersAtHeight`,
`FinalityProviderPowerAtHeight`, `FinalityProviderCurrentPower`,
`ActivatedHeight` to `x/finality`
- [x] fix fuzz tests
- [x] fix e2e tests
- [x] fix doc
- [x] fix changelog

Note that this affects many LoCs since it touches a bunch of proto
files. The actual modification is much smaller than it looks like.
  • Loading branch information
SebastianElvis authored Oct 25, 2024
1 parent 2b726b2 commit 1be8537
Show file tree
Hide file tree
Showing 59 changed files with 9,578 additions and 10,070 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### State Machine Breaking

* [#224](https://github.com/babylonlabs-io/babylon/pull/224) Make injected checkpoint a standard tx
* [#216](https://github.com/babylonlabs-io/babylon/pull/216) move voting power distribution
update algorithm to `x/finality`
* [#217](https://github.com/babylonlabs-io/babylon/pull/217) Move voting power table to finality module
* [#207](https://github.com/babylonlabs-io/babylon/pull/207) Rename total voting power
to total bonded sat
* [#204](https://github.com/babylonlabs-io/babylon/pull/204) Add babylon finality
Expand Down
45 changes: 0 additions & 45 deletions client/query/btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,48 +121,3 @@ func (c *QueryClient) BTCDelegation(stakingTxHashHex string) (*btcstakingtypes.Q

return resp, err
}

// ActiveFinalityProvidersAtHeight queries the BTCStaking module for all finality providers
// with non-zero voting power at a given height
func (c *QueryClient) ActiveFinalityProvidersAtHeight(height uint64, pagination *sdkquerytypes.PageRequest) (*btcstakingtypes.QueryActiveFinalityProvidersAtHeightResponse, error) {
var resp *btcstakingtypes.QueryActiveFinalityProvidersAtHeightResponse
err := c.QueryBTCStaking(func(ctx context.Context, queryClient btcstakingtypes.QueryClient) error {
var err error
req := &btcstakingtypes.QueryActiveFinalityProvidersAtHeightRequest{
Height: height,
Pagination: pagination,
}
resp, err = queryClient.ActiveFinalityProvidersAtHeight(ctx, req)
return err
})

return resp, err
}

// FinalityProviderPowerAtHeight queries the BTCStaking module for the power of a finality provider at a given height
func (c *QueryClient) FinalityProviderPowerAtHeight(fpBtcPkHex string, height uint64) (*btcstakingtypes.QueryFinalityProviderPowerAtHeightResponse, error) {
var resp *btcstakingtypes.QueryFinalityProviderPowerAtHeightResponse
err := c.QueryBTCStaking(func(ctx context.Context, queryClient btcstakingtypes.QueryClient) error {
var err error
req := &btcstakingtypes.QueryFinalityProviderPowerAtHeightRequest{
FpBtcPkHex: fpBtcPkHex,
Height: height,
}
resp, err = queryClient.FinalityProviderPowerAtHeight(ctx, req)
return err
})

return resp, err
}

func (c *QueryClient) ActivatedHeight() (*btcstakingtypes.QueryActivatedHeightResponse, error) {
var resp *btcstakingtypes.QueryActivatedHeightResponse
err := c.QueryBTCStaking(func(ctx context.Context, queryClient btcstakingtypes.QueryClient) error {
var err error
req := &btcstakingtypes.QueryActivatedHeightRequest{}
resp, err = queryClient.ActivatedHeight(ctx, req)
return err
})

return resp, err
}
45 changes: 45 additions & 0 deletions client/query/finality.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,51 @@ func (c *QueryClient) QueryFinality(f func(ctx context.Context, queryClient fina
return f(ctx, queryClient)
}

// ActiveFinalityProvidersAtHeight queries the BTCStaking module for all finality providers
// with non-zero voting power at a given height
func (c *QueryClient) ActiveFinalityProvidersAtHeight(height uint64, pagination *sdkquerytypes.PageRequest) (*finalitytypes.QueryActiveFinalityProvidersAtHeightResponse, error) {
var resp *finalitytypes.QueryActiveFinalityProvidersAtHeightResponse
err := c.QueryFinality(func(ctx context.Context, queryClient finalitytypes.QueryClient) error {
var err error
req := &finalitytypes.QueryActiveFinalityProvidersAtHeightRequest{
Height: height,
Pagination: pagination,
}
resp, err = queryClient.ActiveFinalityProvidersAtHeight(ctx, req)
return err
})

return resp, err
}

// FinalityProviderPowerAtHeight queries the BTCStaking module for the power of a finality provider at a given height
func (c *QueryClient) FinalityProviderPowerAtHeight(fpBtcPkHex string, height uint64) (*finalitytypes.QueryFinalityProviderPowerAtHeightResponse, error) {
var resp *finalitytypes.QueryFinalityProviderPowerAtHeightResponse
err := c.QueryFinality(func(ctx context.Context, queryClient finalitytypes.QueryClient) error {
var err error
req := &finalitytypes.QueryFinalityProviderPowerAtHeightRequest{
FpBtcPkHex: fpBtcPkHex,
Height: height,
}
resp, err = queryClient.FinalityProviderPowerAtHeight(ctx, req)
return err
})

return resp, err
}

func (c *QueryClient) ActivatedHeight() (*finalitytypes.QueryActivatedHeightResponse, error) {
var resp *finalitytypes.QueryActivatedHeightResponse
err := c.QueryFinality(func(ctx context.Context, queryClient finalitytypes.QueryClient) error {
var err error
req := &finalitytypes.QueryActivatedHeightRequest{}
resp, err = queryClient.ActivatedHeight(ctx, req)
return err
})

return resp, err
}

// FinalityParams queries the finality module parameters
func (c *QueryClient) FinalityParams() (*finalitytypes.Params, error) {
var resp *finalitytypes.QueryParamsResponse
Expand Down
25 changes: 0 additions & 25 deletions proto/babylon/btcstaking/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package babylon.btcstaking.v1;
import "gogoproto/gogo.proto";
import "babylon/btcstaking/v1/params.proto";
import "babylon/btcstaking/v1/btcstaking.proto";
import "babylon/btcstaking/v1/incentive.proto";
import "babylon/btcstaking/v1/events.proto";

option go_package = "github.com/babylonlabs-io/babylon/x/btcstaking/types";
Expand All @@ -17,36 +16,12 @@ message GenesisState {
repeated FinalityProvider finality_providers = 2;
// btc_delegations all the btc delegations in the state.
repeated BTCDelegation btc_delegations = 3;
// voting_powers the voting power of every finality provider at every block height.
repeated VotingPowerFP voting_powers = 4;
// block_height_chains the block height of babylon and bitcoin.
repeated BlockHeightBbnToBtc block_height_chains = 5;
// btc_delegators contains all the btc delegators with the associated finality provider.
repeated BTCDelegator btc_delegators = 6;
// all the events and its indexes.
repeated EventIndex events = 7;
// vp_dst_cache is the table of all providers voting power with the total at one specific block.
// TODO: remove this after not storing in the keeper store it anymore.
repeated VotingPowerDistCacheBlkHeight vp_dst_cache = 8;
}

// VotingPowerFP contains the information about the voting power
// of an finality provider in a specific block height.
message VotingPowerFP {
// block_height is the height of the block the voting power was stored.
uint64 block_height = 1;
// fp_btc_pk the finality provider btc public key.
bytes fp_btc_pk = 2 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.BIP340PubKey" ];
// voting_power is the power of the finality provider at this specific block height.
uint64 voting_power = 3;
}

// VotingPowerDistCacheBlkHeight the total voting power of the finality providers at one specific block height
message VotingPowerDistCacheBlkHeight {
// block_height is the height of the block the voting power distribution cached was stored.
uint64 block_height = 1;
// vp_distribution the finality providers distribution cache at that height.
VotingPowerDistCache vp_distribution = 2;
}

// BlockHeightBbnToBtc stores the btc <-> bbn block.
Expand Down
62 changes: 0 additions & 62 deletions proto/babylon/btcstaking/v1/incentive.proto

This file was deleted.

111 changes: 1 addition & 110 deletions proto/babylon/btcstaking/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,6 @@ service Query {
option (google.api.http).get = "/babylon/btcstaking/v1/btc_delegations/{status}";
}

// ActiveFinalityProvidersAtHeight queries finality providers with non zero voting power at given height.
rpc ActiveFinalityProvidersAtHeight(QueryActiveFinalityProvidersAtHeightRequest) returns (QueryActiveFinalityProvidersAtHeightResponse) {
option (google.api.http).get = "/babylon/btcstaking/v1/finality_providers/{height}";
}

// FinalityProviderPowerAtHeight queries the voting power of a finality provider at a given height
rpc FinalityProviderPowerAtHeight(QueryFinalityProviderPowerAtHeightRequest) returns (QueryFinalityProviderPowerAtHeightResponse) {
option (google.api.http).get = "/babylon/btcstaking/v1/finality_providers/{fp_btc_pk_hex}/power/{height}";
}

// FinalityProviderCurrentPower queries the voting power of a finality provider at the current height
rpc FinalityProviderCurrentPower(QueryFinalityProviderCurrentPowerRequest) returns (QueryFinalityProviderCurrentPowerResponse) {
option (google.api.http).get = "/babylon/btcstaking/v1/finality_providers/{fp_btc_pk_hex}/power";
}

// ActivatedHeight queries the height when BTC staking protocol is activated, i.e., the first height when
// there exists 1 finality provider with voting power
rpc ActivatedHeight(QueryActivatedHeightRequest) returns (QueryActivatedHeightResponse) {
option (google.api.http).get = "/babylon/btcstaking/v1/activated_height";
}

// FinalityProviderDelegations queries all BTC delegations of the given finality provider
rpc FinalityProviderDelegations(QueryFinalityProviderDelegationsRequest) returns (QueryFinalityProviderDelegationsResponse) {
option (google.api.http).get = "/babylon/btcstaking/v1/finality_providers/{fp_btc_pk_hex}/delegations";
Expand Down Expand Up @@ -140,92 +119,6 @@ message QueryBTCDelegationsResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryFinalityProviderPowerAtHeightRequest is the request type for the
// Query/FinalityProviderPowerAtHeight RPC method.
message QueryFinalityProviderPowerAtHeightRequest {
// fp_btc_pk_hex is the hex str of Bitcoin secp256k1 PK of the finality provider that
// this BTC delegation delegates to
// the PK follows encoding in BIP-340 spec
string fp_btc_pk_hex = 1;

// height is used for querying the given finality provider's voting power at this height
uint64 height = 2;
}

// QueryFinalityProviderPowerAtHeightResponse is the response type for the
// Query/FinalityProviderPowerAtHeight RPC method.
message QueryFinalityProviderPowerAtHeightResponse {
// voting_power is the voting power of the finality provider
uint64 voting_power = 1;
}

// QueryFinalityProviderCurrentPowerRequest is the request type for the
// Query/FinalityProviderCurrentPower RPC method.
message QueryFinalityProviderCurrentPowerRequest {
// fp_btc_pk_hex is the hex str of Bitcoin secp256k1 PK of the finality provider that
// this BTC delegation delegates to
// the PK follows encoding in BIP-340 spec
string fp_btc_pk_hex = 1;
}

// QueryFinalityProviderCurrentPowerResponse is the response type for the
// Query/FinalityProviderCurrentPower RPC method.
message QueryFinalityProviderCurrentPowerResponse {
// height is the current height
uint64 height = 1;
// voting_power is the voting power of the finality provider
uint64 voting_power = 2;
}

// QueryActiveFinalityProvidersAtHeightRequest is the request type for the
// Query/ActiveFinalityProvidersAtHeight RPC method.
message QueryActiveFinalityProvidersAtHeightRequest {
// height defines at which Babylon height to query the finality providers info.
uint64 height = 1;

// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

// ActiveFinalityProvidersAtHeightResponse wraps the FinalityProvider with metadata.
message ActiveFinalityProvidersAtHeightResponse {
// btc_pk is the Bitcoin secp256k1 PK of thisfinality provider
// the PK follows encoding in BIP-340 spec
string btc_pk_hex = 1 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.BIP340PubKey" ];
// height is the queried Babylon height
uint64 height = 2;
// voting_power is the voting power of this finality provider at the given height
uint64 voting_power = 3;
// slashed_babylon_height indicates the Babylon height when
// the finality provider is slashed.
// if it's 0 then the finality provider is not slashed
uint64 slashed_babylon_height = 4;
// slashed_btc_height indicates the BTC height when
// the finality provider is slashed.
// if it's 0 then the finality provider is not slashed
uint32 slashed_btc_height = 5;
// jailed defines whether the finality provider is detected jailed
bool jailed = 6;
}

// QueryActiveFinalityProvidersAtHeightResponse is the response type for the
// Query/ActiveFinalityProvidersAtHeight RPC method.
message QueryActiveFinalityProvidersAtHeightResponse {
// finality_providers contains all the queried finality providersn.
repeated ActiveFinalityProvidersAtHeightResponse finality_providers = 1;

// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryActivatedHeightRequest is the request type for the Query/ActivatedHeight RPC method.
message QueryActivatedHeightRequest {}

// QueryActivatedHeightResponse is the response type for the Query/ActivatedHeight RPC method.
message QueryActivatedHeightResponse {
uint64 height = 1;
}

// QueryFinalityProviderDelegationsRequest is the request type for the
// Query/FinalityProviderDelegations RPC method.
message QueryFinalityProviderDelegationsRequest {
Expand Down Expand Up @@ -375,8 +268,6 @@ message FinalityProviderResponse {
uint32 slashed_btc_height = 7;
// height is the queried Babylon height
uint64 height = 8;
// voting_power is the voting power of this finality provider at the given height
uint64 voting_power = 9;
// jailed defines whether the finality provider is jailed
bool jailed = 10;
bool jailed = 9;
}
Loading

0 comments on commit 1be8537

Please sign in to comment.