forked from lesnikutsa/babylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinality_providers.go
67 lines (58 loc) · 2.1 KB
/
finality_providers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package keeper
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/store/prefix"
"github.com/babylonchain/babylon/x/btcstaking/types"
)
// SetFinalityProvider adds the given finality provider to KVStore
func (k Keeper) SetFinalityProvider(ctx context.Context, fp *types.FinalityProvider) {
store := k.finalityProviderStore(ctx)
fpBytes := k.cdc.MustMarshal(fp)
store.Set(fp.BtcPk.MustMarshal(), fpBytes)
}
// HasFinalityProvider checks if the finality provider exists
func (k Keeper) HasFinalityProvider(ctx context.Context, fpBTCPK []byte) bool {
store := k.finalityProviderStore(ctx)
return store.Has(fpBTCPK)
}
// GetFinalityProvider gets the finality provider with the given finality provider Bitcoin PK
func (k Keeper) GetFinalityProvider(ctx context.Context, fpBTCPK []byte) (*types.FinalityProvider, error) {
store := k.finalityProviderStore(ctx)
if !k.HasFinalityProvider(ctx, fpBTCPK) {
return nil, types.ErrFpNotFound
}
fpBytes := store.Get(fpBTCPK)
var fp types.FinalityProvider
k.cdc.MustUnmarshal(fpBytes, &fp)
return &fp, nil
}
// SlashFinalityProvider slashes a finality provider with the given PK
// A slashed finality provider will not have voting power
func (k Keeper) SlashFinalityProvider(ctx context.Context, fpBTCPK []byte) error {
fp, err := k.GetFinalityProvider(ctx, fpBTCPK)
if err != nil {
return err
}
if fp.IsSlashed() {
return types.ErrFpAlreadySlashed
}
fp.SlashedBabylonHeight = uint64(sdk.UnwrapSDKContext(ctx).HeaderInfo().Height)
btcTip := k.btclcKeeper.GetTipInfo(ctx)
if btcTip == nil {
panic(fmt.Errorf("failed to get current BTC tip"))
}
fp.SlashedBtcHeight = btcTip.Height
k.SetFinalityProvider(ctx, fp)
return nil
}
// finalityProviderStore returns the KVStore of the finality provider set
// prefix: FinalityProviderKey
// key: Bitcoin secp256k1 PK
// value: FinalityProvider object
func (k Keeper) finalityProviderStore(ctx context.Context) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
return prefix.NewStore(storeAdapter, types.FinalityProviderKey)
}