Skip to content

Commit

Permalink
add epoch num to public randomness commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Aug 6, 2024
1 parent 7253662 commit e44fc9e
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 136 deletions.
2 changes: 2 additions & 0 deletions proto/babylon/finality/v1/finality.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ message PubRandCommit {
// commitment is the value of the commitment
// currently, it is the root of the merkle tree constructed by the public randomness
bytes commitment = 3;
// epoch_num defines the epoch number that the commit falls into
uint64 epoch_num = 4;
}

// Evidence is the evidence that a finality provider has signed finality
Expand Down
2 changes: 2 additions & 0 deletions proto/babylon/finality/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ message PubRandCommitResponse {
uint64 num_pub_rand = 1;
// commitment is the value of the commitment
bytes commitment = 2;
// epoch_num defines the epoch number that the commit falls into
uint64 epoch_num = 3;
}

// QueryListPubRandCommitRequest is the request type for the
Expand Down
9 changes: 9 additions & 0 deletions x/btcstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ func (k Keeper) BeginBlocker(ctx context.Context) error {
func (k Keeper) GetLastFinalizedEpoch(ctx context.Context) uint64 {
return k.ckptKeeper.GetLastFinalizedEpoch(ctx)
}

func (k Keeper) GetEpoch(ctx context.Context) uint64 {
epoch := k.ckptKeeper.GetEpoch(ctx)
if epoch == nil {
panic(fmt.Errorf("epoch cannot be nil"))
}

return epoch.EpochNumber
}
1 change: 0 additions & 1 deletion x/btcstaking/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var (
ErrBTCDelegationNotFound = errorsmod.Register(ModuleName, 1102, "the BTC delegation is not found")
ErrFpRegistered = errorsmod.Register(ModuleName, 1103, "the finality provider has already been registered")
ErrFpAlreadySlashed = errorsmod.Register(ModuleName, 1104, "the finality provider has already been slashed")
ErrFpNotBTCTimestamped = errorsmod.Register(ModuleName, 1105, "the finality provider is not BTC timestamped yet")
ErrBTCStakingNotActivated = errorsmod.Register(ModuleName, 1106, "the BTC staking protocol is not activated yet")
ErrBTCHeightNotFound = errorsmod.Register(ModuleName, 1107, "the BTC height is not found")
ErrReusedStakingTx = errorsmod.Register(ModuleName, 1108, "the BTC staking tx is already used")
Expand Down
19 changes: 14 additions & 5 deletions x/finality/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"time"

errorsmod "cosmossdk.io/errors"
bbn "github.com/babylonlabs-io/babylon/types"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/babylonlabs-io/babylon/x/finality/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

bbn "github.com/babylonlabs-io/babylon/types"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/babylonlabs-io/babylon/x/finality/types"
)

type msgServer struct {
Expand Down Expand Up @@ -82,7 +83,7 @@ func (ms msgServer) AddFinalitySig(goCtx context.Context, req *types.MsgAddFinal
}
fpPK := req.FpBtcPk
if ms.BTCStakingKeeper.GetVotingPower(ctx, fpPK.MustMarshal(), req.BlockHeight) == 0 {
return nil, types.ErrInvalidFinalitySig.Wrapf("the finality provider %v does not have voting power at height %d", fpPK.MustMarshal(), req.BlockHeight)
return nil, types.ErrInvalidFinalitySig.Wrapf("the finality provider %s does not have voting power at height %d", fpPK.MarshalHex(), req.BlockHeight)
}

// ensure the finality provider has not cast the same vote yet
Expand All @@ -101,6 +102,13 @@ func (ms msgServer) AddFinalitySig(goCtx context.Context, req *types.MsgAddFinal
if err != nil {
return nil, err
}
// ensure the finality provider's last randomness commit is already finalised by BTC timestamping
finalizedEpoch := ms.BTCStakingKeeper.GetLastFinalizedEpoch(ctx)
if finalizedEpoch < prCommit.EpochNum {
return nil, types.ErrPubRandCommitNotBTCTimestamped.
Wrapf("the finality provider %s last committed epoch number: %d, last finalized epoch number: %d",
fp.BtcPk.MarshalHex(), prCommit.EpochNum, finalizedEpoch)
}

// verify the finality signature message w.r.t. the public randomness commitment
// including the public randomness inclusion proof and the finality signature
Expand Down Expand Up @@ -132,7 +140,7 @@ func (ms msgServer) AddFinalitySig(goCtx context.Context, req *types.MsgAddFinal
// if this finality provider has also signed canonical block, slash it
canonicalSig, err := ms.GetSig(ctx, req.BlockHeight, fpPK)
if err == nil {
//set canonial sig
// set canonial sig
evidence.CanonicalFinalitySig = canonicalSig
// slash this finality provider, including setting its voting power to
// zero, extracting its BTC SK, and emit an event
Expand Down Expand Up @@ -206,6 +214,7 @@ func (ms msgServer) CommitPubRandList(goCtx context.Context, req *types.MsgCommi
StartHeight: req.StartHeight,
NumPubRand: req.NumPubRand,
Commitment: req.Commitment,
EpochNum: ms.BTCStakingKeeper.GetEpoch(ctx),
}

// get last public randomness commitment
Expand Down
23 changes: 12 additions & 11 deletions x/finality/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (

// x/finality module sentinel errors
var (
ErrBlockNotFound = errorsmod.Register(ModuleName, 1100, "Block is not found")
ErrVoteNotFound = errorsmod.Register(ModuleName, 1101, "vote is not found")
ErrHeightTooHigh = errorsmod.Register(ModuleName, 1102, "the chain has not reached the given height yet")
ErrPubRandNotFound = errorsmod.Register(ModuleName, 1103, "public randomness is not found")
ErrPubRandCommitNotFound = errorsmod.Register(ModuleName, 1104, "public randomness commitment is not found")
ErrNoPubRandYet = errorsmod.Register(ModuleName, 1105, "the finality provider has not committed any public randomness yet")
ErrTooFewPubRand = errorsmod.Register(ModuleName, 1106, "the request contains too few public randomness")
ErrInvalidPubRand = errorsmod.Register(ModuleName, 1107, "the public randomness list is invalid")
ErrEvidenceNotFound = errorsmod.Register(ModuleName, 1108, "evidence is not found")
ErrInvalidFinalitySig = errorsmod.Register(ModuleName, 1109, "finality signature is not valid")
ErrNoSlashableEvidence = errorsmod.Register(ModuleName, 1110, "there is no slashable evidence")
ErrBlockNotFound = errorsmod.Register(ModuleName, 1100, "Block is not found")
ErrVoteNotFound = errorsmod.Register(ModuleName, 1101, "vote is not found")
ErrHeightTooHigh = errorsmod.Register(ModuleName, 1102, "the chain has not reached the given height yet")
ErrPubRandNotFound = errorsmod.Register(ModuleName, 1103, "public randomness is not found")
ErrPubRandCommitNotFound = errorsmod.Register(ModuleName, 1104, "public randomness commitment is not found")
ErrNoPubRandYet = errorsmod.Register(ModuleName, 1105, "the finality provider has not committed any public randomness yet")
ErrTooFewPubRand = errorsmod.Register(ModuleName, 1106, "the request contains too few public randomness")
ErrInvalidPubRand = errorsmod.Register(ModuleName, 1107, "the public randomness list is invalid")
ErrEvidenceNotFound = errorsmod.Register(ModuleName, 1108, "evidence is not found")
ErrInvalidFinalitySig = errorsmod.Register(ModuleName, 1109, "finality signature is not valid")
ErrNoSlashableEvidence = errorsmod.Register(ModuleName, 1110, "there is no slashable evidence")
ErrPubRandCommitNotBTCTimestamped = errorsmod.Register(ModuleName, 1111, "the public randomness commit is not BTC timestamped yet")
)
1 change: 1 addition & 0 deletions x/finality/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type BTCStakingKeeper interface {
GetBTCStakingActivatedHeight(ctx context.Context) (uint64, error)
GetVotingPowerDistCache(ctx context.Context, height uint64) (*bstypes.VotingPowerDistCache, error)
RemoveVotingPowerDistCache(ctx context.Context, height uint64)
GetEpoch(ctx context.Context) uint64
GetLastFinalizedEpoch(ctx context.Context) uint64
RevertSluggishFinalityProvider(ctx context.Context, fpBTCPK []byte) error
}
Expand Down
108 changes: 73 additions & 35 deletions x/finality/types/finality.pb.go

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

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

0 comments on commit e44fc9e

Please sign in to comment.