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

chore: Improve external events format #169

Merged
merged 6 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Improvements

* [#169](https://github.com/babylonlabs-io/babylon/pull/169) Change external events format
* [#148](https://github.com/babylonlabs-io/babylon/pull/148) Add block results query

### Misc Improvements
Expand Down
96 changes: 52 additions & 44 deletions proto/babylon/btcstaking/v1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,40 @@ option go_package = "github.com/babylonlabs-io/babylon/x/btcstaking/types";

// EventFinalityProviderCreated is the event emitted when a finality provider is created
message EventFinalityProviderCreated {
// btc_pk is the Bitcoin secp256k1 PK of this finality provider
// the PK follows encoding in BIP-340 spec
bytes btc_pk = 1 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.BIP340PubKey" ];
// btc_pk_hex is the hex string of Bitcoin secp256k1 PK of this finality provider
string btc_pk_hex = 1;
// addr is the address to receive commission from delegations.
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
string addr = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string addr = 2;
// commission defines the commission rate of the finality provider.
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
string commission = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec"
];
// description defines the description terms for the finality provider.
cosmos.staking.v1beta1.Description description = 4;
string commission = 3;
// moniker defines a human-readable name for the finality provider.
string moniker = 4;
// identity defines an optional identity signature (ex. UPort or Keybase).
string identity = 5;
// website defines an optional website link.
string website = 6;
// security_contact defines an optional email for security contact.
string security_contact = 7;
// details define other optional details.
string details = 8;
}

// EventFinalityProviderEdited is the event emitted when a finality provider is edited
message EventFinalityProviderEdited {
// btc_pk is the Bitcoin secp256k1 PK of this finality provider
// the PK follows encoding in BIP-340 spec
bytes btc_pk = 1 [ (gogoproto.customtype) = "github.com/babylonlabs-io/babylon/types.BIP340PubKey" ];
// btc_pk_hex is the hex string of Bitcoin secp256k1 PK of this finality provider
string btc_pk_hex = 1;
// commission defines the commission rate of the finality provider.
string commission = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec"
];
// description defines the description terms for the finality provider.
cosmos.staking.v1beta1.Description description = 3;
string commission = 2;
// moniker defines a human-readable name for the finality provider.
string moniker = 3;
// identity defines an optional identity signature (ex. UPort or Keybase).
string identity = 4;
// website defines an optional website link.
string website = 5;
// security_contact defines an optional email for security contact.
string security_contact = 6;
// details define other optional details.
string details = 7;
}

// EventBTCDelegationStateUpdate is the event emitted when a BTC delegation's state is
Expand Down Expand Up @@ -98,8 +106,8 @@ message EventFinalityProviderStatusChange {
// btc_pk is the BTC public key of the finality provider
string btc_pk = 1;
// new_status is the new status that the finality provider
// is transitioned to
FinalityProviderStatus new_status = 2;
// is transitioned to, following FinalityProviderStatus
string new_status = 2;
}

// FinalityProviderStatus is the status of a finality provider.
Expand All @@ -120,16 +128,16 @@ message EventFinalityProviderStatusChange {
// Note that it is impossible for a SLASHED finality provider to
// transition to other status
enum FinalityProviderStatus {
// STATUS_INACTIVE defines a finality provider that does not have sufficient
// FINALITY_PROVIDER_STATUS_INACTIVE defines a finality provider that does not have sufficient
// delegations or does not have timestamped public randomness.
STATUS_INACTIVE = 0;
// STATUS_ACTIVE defines a finality provider that have sufficient delegations
FINALITY_PROVIDER_STATUS_INACTIVE = 0;
// FINALITY_PROVIDER_STATUS_ACTIVE defines a finality provider that have sufficient delegations
// and have timestamped public randomness.
STATUS_ACTIVE = 1;
// STATUS_JAILED defines a finality provider that is jailed due to downtime
STATUS_JAILED = 2;
// STATUS_SLASHED defines a finality provider that is slashed due to double-sign
STATUS_SLASHED = 3;
FINALITY_PROVIDER_STATUS_ACTIVE = 1;
// FINALITY_PROVIDER_STATUS_JAILED defines a finality provider that is jailed due to downtime
FINALITY_PROVIDER_STATUS_JAILED = 2;
// FINALITY_PROVIDER_STATUS_SLASHED defines a finality provider that is slashed due to double-sign
FINALITY_PROVIDER_STATUS_SLASHED = 3;
}

// EventBTCDelegationCreated is the event emitted when a BTC delegation is created
Expand All @@ -139,7 +147,7 @@ message EventBTCDelegationCreated {
// It uniquely identifies a BTC delegation
string staking_tx_hash = 1;
// version of the params used to validate the delegation
uint32 params_version = 2;
string params_version = 2;
// finality_provider_btc_pks_hex is the list of hex str of Bitcoin secp256k1 PK of
// the finality providers that this BTC delegation delegates to
// the PK follows encoding in BIP-340 spec
Expand All @@ -148,21 +156,21 @@ message EventBTCDelegationCreated {
// creates this BTC delegation the PK follows encoding in BIP-340 spec
string staker_btc_pk_hex = 4;
// staking_time is the timelock of the staking tx specified in the BTC script
uint32 staking_time = 5;
string staking_time = 5;
// staking_amount is the total amount of BTC stake in this delegation
// quantified in satoshi
uint64 staking_amount = 6;
string staking_amount = 6;
// unbonding_time is the time is timelock on unbonding tx chosen by the staker
uint32 unbonding_time = 7;
string unbonding_time = 7;
// unbonding_tx is hex encoded bytes of the unsigned unbonding tx
string unbonding_tx = 8;
// state of the BTC delegation
BTCDelegationStatus state = 9;
string state = 9;
}

// EventCovenantSignatureRecevied is the event emitted when a covenant committee
// EventCovenantSignatureReceived is the event emitted when a covenant committee
// sends valid covenant signatures for a BTC delegation
message EventCovenantSignatureRecevied{
message EventCovenantSignatureReceived{
// staking_tx_hash is the hash of the staking identifing the BTC delegation
// that this covenant signature is for
string staking_tx_hash = 1;
Expand All @@ -174,14 +182,14 @@ message EventCovenantSignatureRecevied{
string covenant_unbonding_signature_hex = 3;
}

// EventCovenantQuroumReached is the event emitted quorum of covenant committee
// EventCovenantQuorumReached is the event emitted quorum of covenant committee
// is reached for a BTC delegation
message EventCovenantQuroumReached {
message EventCovenantQuorumReached {
// staking_tx_hash is the hash of the staking identifing the BTC delegation
// that this covenant signature is for
string staking_tx_hash = 1;
// state of the BTC delegation
BTCDelegationStatus state = 2;
string state = 2;
}

// EventBTCDelegationInclusionProofReceived is the event emitted when a BTC delegation
Expand All @@ -192,12 +200,12 @@ message EventBTCDelegationInclusionProofReceived {
string staking_tx_hash = 1;
// start_height is the start BTC height of the BTC delegation
// it is the start BTC height of the timelock
uint64 start_height = 2;
string start_height = 2;
// end_height is the end height of the BTC delegation
// it is calculated by end_height = start_height + staking_time
uint64 end_height = 3;
string end_height = 3;
// state of the BTC delegation
BTCDelegationStatus state = 4;
string state = 4;
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
}

// EventBTCDelgationUnbondedEarly is the event emitted when a BTC delegation
Expand All @@ -207,7 +215,7 @@ message EventBTCDelgationUnbondedEarly {
// It uniquely identifies a BTC delegation
string staking_tx_hash = 1;
// state of the BTC delegation
BTCDelegationStatus state = 2;
string state = 2;
}

// EventBTCDelegationExpired is the event emitted when a BTC delegation
Expand All @@ -217,5 +225,5 @@ message EventBTCDelegationExpired {
// It uniquely identifies a BTC delegation
string staking_tx_hash = 1;
// state of the BTC delegation
BTCDelegationStatus state = 2;
string state = 2;
}
16 changes: 8 additions & 8 deletions x/btcstaking/keeper/power_dist_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ func (k Keeper) recordVotingPowerAndCache(goCtx context.Context, prevDc, newDc *
panic(fmt.Errorf("failed to execute after finality provider %s activated", fp.BtcPk.MarshalHex()))
}

statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_STATUS_ACTIVE)
statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_ACTIVE)
if err := sdkCtx.EventManager().EmitTypedEvent(statusChangeEvent); err != nil {
panic(fmt.Errorf(
"failed to emit FinalityProviderStatusChangeEvent with status %s: %w",
types.FinalityProviderStatus_STATUS_ACTIVE.String(), err))
types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_ACTIVE.String(), err))
}

k.Logger(sdkCtx).Info("a new finality provider becomes active", "pk", fp.BtcPk.MarshalHex())
Expand All @@ -132,11 +132,11 @@ func (k Keeper) recordVotingPowerAndCache(goCtx context.Context, prevDc, newDc *
// subscribers
newInactiveFps := newDc.FindNewInactiveFinalityProviders(prevDc)
for _, fp := range newInactiveFps {
statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_STATUS_INACTIVE)
statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_INACTIVE)
if err := sdkCtx.EventManager().EmitTypedEvent(statusChangeEvent); err != nil {
panic(fmt.Errorf(
"failed to emit FinalityProviderStatusChangeEvent with status %s: %w",
types.FinalityProviderStatus_STATUS_INACTIVE.String(), err))
types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_INACTIVE.String(), err))
}

k.Logger(sdkCtx).Info("a new finality provider becomes inactive", "pk", fp.BtcPk.MarshalHex())
Expand Down Expand Up @@ -263,11 +263,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
if _, ok := slashedFPs[fpBTCPKHex]; ok {
fp.IsSlashed = true

statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_STATUS_SLASHED)
statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_SLASHED)
if err := sdkCtx.EventManager().EmitTypedEvent(statusChangeEvent); err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:
IMO, it might be more performant if each of the event type come with .toEvent method which will turn your custom event struct into the cosmos event type. https://pkg.go.dev/github.com/cometbft/[email protected]/abci/types#Event
Then use the EmitEvent instead of EmitTypedEvent to save some json marshal/unmarshl

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is good to use EmitEvent because cosmos has listed EmitEvent as deprecated https://github.com/cosmos/cosmos-sdk/blob/v0.50.6/types/events.go#L45-L49

panic(fmt.Errorf(
"failed to emit FinalityProviderStatusChangeEvent with status %s: %w",
types.FinalityProviderStatus_STATUS_SLASHED.String(), err))
types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_SLASHED.String(), err))
}

continue
Expand All @@ -279,11 +279,11 @@ func (k Keeper) ProcessAllPowerDistUpdateEvents(
if _, ok := jailedFPs[fpBTCPKHex]; ok {
fp.IsJailed = true

statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_STATUS_JAILED)
statusChangeEvent := types.NewFinalityProviderStatusChangeEvent(fp.BtcPk, types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_JAILED)
if err := sdkCtx.EventManager().EmitTypedEvent(statusChangeEvent); err != nil {
panic(fmt.Errorf(
"failed to emit FinalityProviderStatusChangeEvent with status %s: %w",
types.FinalityProviderStatus_STATUS_JAILED.String(), err))
types.FinalityProviderStatus_FINALITY_PROVIDER_STATUS_JAILED.String(), err))
}
}

Expand Down
55 changes: 32 additions & 23 deletions x/btcstaking/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/hex"
"strconv"

bbn "github.com/babylonlabs-io/babylon/types"
)
Expand Down Expand Up @@ -46,18 +47,26 @@ func NewEventPowerDistUpdateWithUnjailedFP(fpBTCPK *bbn.BIP340PubKey) *EventPowe

func NewEventFinalityProviderCreated(fp *FinalityProvider) *EventFinalityProviderCreated {
return &EventFinalityProviderCreated{
BtcPk: fp.BtcPk,
Addr: fp.Addr,
Commission: fp.Commission,
Description: fp.Description,
BtcPkHex: fp.BtcPk.MarshalHex(),
Addr: fp.Addr,
Commission: fp.Commission.String(),
Moniker: fp.Description.Moniker,
Identity: fp.Description.Identity,
Website: fp.Description.Website,
SecurityContact: fp.Description.SecurityContact,
Details: fp.Description.Details,
}
}

func NewEventFinalityProviderEdited(fp *FinalityProvider) *EventFinalityProviderEdited {
return &EventFinalityProviderEdited{
BtcPk: fp.BtcPk,
Commission: fp.Commission,
Description: fp.Description,
BtcPkHex: fp.BtcPk.MarshalHex(),
Commission: fp.Commission.String(),
Moniker: fp.Description.Moniker,
Identity: fp.Description.Identity,
Website: fp.Description.Website,
SecurityContact: fp.Description.SecurityContact,
Details: fp.Description.Details,
}
}

Expand All @@ -69,9 +78,9 @@ func NewInclusionProofEvent(
) *EventBTCDelegationInclusionProofReceived {
return &EventBTCDelegationInclusionProofReceived{
StakingTxHash: stakingTxHash,
StartHeight: startHeight,
EndHeight: endHeight,
State: state,
StartHeight: strconv.FormatUint(startHeight, 10),
EndHeight: strconv.FormatUint(endHeight, 10),
State: state.String(),
}
}

Expand All @@ -81,23 +90,23 @@ func NewBtcDelCreationEvent(
) *EventBTCDelegationCreated {
return &EventBTCDelegationCreated{
StakingTxHash: stakingTxHash,
ParamsVersion: btcDel.ParamsVersion,
ParamsVersion: strconv.FormatUint(uint64(btcDel.ParamsVersion), 10),
FinalityProviderBtcPksHex: btcDel.FinalityProviderKeys(),
StakerBtcPkHex: btcDel.BtcPk.MarshalHex(),
StakingTime: btcDel.StakingTime,
StakingAmount: btcDel.TotalSat,
UnbondingTime: btcDel.UnbondingTime,
StakingTime: strconv.FormatUint(uint64(btcDel.StakingTime), 10),
StakingAmount: strconv.FormatUint(btcDel.TotalSat, 10),
UnbondingTime: strconv.FormatUint(uint64(btcDel.UnbondingTime), 10),
UnbondingTx: hex.EncodeToString(btcDel.BtcUndelegation.UnbondingTx),
State: BTCDelegationStatus_PENDING,
State: BTCDelegationStatus_PENDING.String(),
}
}

func NewCovenantSignatureReceivedEvent(
btcDel *BTCDelegation,
covPK *bbn.BIP340PubKey,
unbondingTxSig *bbn.BIP340Signature,
) *EventCovenantSignatureRecevied {
return &EventCovenantSignatureRecevied{
) *EventCovenantSignatureReceived {
return &EventCovenantSignatureReceived{
StakingTxHash: btcDel.MustGetStakingTxHash().String(),
CovenantBtcPkHex: covPK.MarshalHex(),
CovenantUnbondingSignatureHex: unbondingTxSig.ToHexStr(),
Expand All @@ -107,10 +116,10 @@ func NewCovenantSignatureReceivedEvent(
func NewCovenantQuorumReachedEvent(
btcDel *BTCDelegation,
state BTCDelegationStatus,
) *EventCovenantQuroumReached {
return &EventCovenantQuroumReached{
) *EventCovenantQuorumReached {
return &EventCovenantQuorumReached{
StakingTxHash: btcDel.MustGetStakingTxHash().String(),
State: state,
State: state.String(),
}
}

Expand All @@ -119,7 +128,7 @@ func NewDelegationUnbondedEarlyEvent(
) *EventBTCDelgationUnbondedEarly {
return &EventBTCDelgationUnbondedEarly{
StakingTxHash: stakingTxHash,
State: BTCDelegationStatus_UNBONDED,
State: BTCDelegationStatus_UNBONDED.String(),
}
}

Expand All @@ -128,7 +137,7 @@ func NewExpiredDelegationEvent(
) *EventBTCDelegationExpired {
return &EventBTCDelegationExpired{
StakingTxHash: stakingTxHash,
State: BTCDelegationStatus_UNBONDED,
State: BTCDelegationStatus_UNBONDED.String(),
}
}

Expand All @@ -138,6 +147,6 @@ func NewFinalityProviderStatusChangeEvent(
) *EventFinalityProviderStatusChange {
return &EventFinalityProviderStatusChange{
BtcPk: fpPk.MarshalHex(),
NewStatus: status,
NewStatus: status.String(),
}
}
Loading