forked from babylonlabs-io/babylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc_delegators.go
79 lines (68 loc) · 3.07 KB
/
btc_delegators.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
68
69
70
71
72
73
74
75
76
77
78
79
package keeper
import (
"context"
"github.com/cosmos/cosmos-sdk/runtime"
"cosmossdk.io/store/prefix"
"github.com/btcsuite/btcd/chaincfg/chainhash"
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
)
// getBTCDelegatorDelegationIndex gets the BTC delegation index with a given BTC PK under a given finality provider
func (k Keeper) getBTCDelegatorDelegationIndex(ctx context.Context, fpBTCPK *bbn.BIP340PubKey, delBTCPK *bbn.BIP340PubKey) *types.BTCDelegatorDelegationIndex {
fpBTCPKBytes := fpBTCPK.MustMarshal()
delBTCPKBytes := delBTCPK.MustMarshal()
store := k.btcDelegatorFpStore(ctx, fpBTCPK)
// ensure the finality provider exists
if !k.HasFinalityProvider(ctx, fpBTCPKBytes) {
return nil
}
// ensure BTC delegator exists
if !store.Has(delBTCPKBytes) {
return nil
}
// get and unmarshal
var btcDelIndex types.BTCDelegatorDelegationIndex
btcDelIndexBytes := store.Get(delBTCPKBytes)
k.cdc.MustUnmarshal(btcDelIndexBytes, &btcDelIndex)
return &btcDelIndex
}
func (k Keeper) setBTCDelegatorDelegationIndex(ctx context.Context, fpBTCPK, delBTCPK *bbn.BIP340PubKey, btcDelIndex *types.BTCDelegatorDelegationIndex) {
store := k.btcDelegatorFpStore(ctx, fpBTCPK)
btcDelIndexBytes := k.cdc.MustMarshal(btcDelIndex)
store.Set(*delBTCPK, btcDelIndexBytes)
}
// getBTCDelegatorDelegations gets the BTC delegations with a given BTC PK under a given finality provider
func (k Keeper) getBTCDelegatorDelegations(ctx context.Context, fpBTCPK *bbn.BIP340PubKey, delBTCPK *bbn.BIP340PubKey) *types.BTCDelegatorDelegations {
btcDelIndex := k.getBTCDelegatorDelegationIndex(ctx, fpBTCPK, delBTCPK)
if btcDelIndex == nil {
return nil
}
// get BTC delegation from each staking tx hash
btcDels := []*types.BTCDelegation{}
for _, stakingTxHashBytes := range btcDelIndex.StakingTxHashList {
stakingTxHash, err := chainhash.NewHash(stakingTxHashBytes)
if err != nil {
// failing to unmarshal hash bytes in DB's BTC delegation index is a programming error
panic(err)
}
btcDel := k.getBTCDelegation(ctx, *stakingTxHash)
btcDels = append(btcDels, btcDel)
}
return &types.BTCDelegatorDelegations{Dels: btcDels}
}
// btcDelegatorFpStore returns the KVStore of the BTC delegators
// prefix: BTCDelegatorKey || finality provider's Bitcoin secp256k1 PK
// key: delegator's Bitcoin secp256k1 PK
// value: BTCDelegatorDelegationIndex (a list of BTCDelegations' staking tx hashes)
func (k Keeper) btcDelegatorFpStore(ctx context.Context, fpBTCPK *bbn.BIP340PubKey) prefix.Store {
delegationStore := k.btcDelegatorStore(ctx)
return prefix.NewStore(delegationStore, fpBTCPK.MustMarshal())
}
// btcDelegatorFpStore returns the KVStore of the BTC delegators
// prefix: BTCDelegatorKey
// key: finality provider's Bitcoin secp256k1 PK || delegator's Bitcoin secp256k1 PK
// value: BTCDelegatorDelegationIndex (a list of BTCDelegations' staking tx hashes)
func (k Keeper) btcDelegatorStore(ctx context.Context) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
return prefix.NewStore(storeAdapter, types.BTCDelegatorKey)
}