forked from babylonchain/babylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc_delegations.go
36 lines (32 loc) · 1.13 KB
/
btc_delegations.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
package keeper
import (
"context"
"cosmossdk.io/store/prefix"
"github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/cosmos/cosmos-sdk/runtime"
)
func (k Keeper) setBTCDelegation(ctx context.Context, btcDel *types.BTCDelegation) {
store := k.btcDelegationStore(ctx)
stakingTxHash := btcDel.MustGetStakingTxHash()
btcDelBytes := k.cdc.MustMarshal(btcDel)
store.Set(stakingTxHash[:], btcDelBytes)
}
func (k Keeper) getBTCDelegation(ctx context.Context, stakingTxHash chainhash.Hash) *types.BTCDelegation {
store := k.btcDelegationStore(ctx)
btcDelBytes := store.Get(stakingTxHash[:])
if len(btcDelBytes) == 0 {
return nil
}
var btcDel types.BTCDelegation
k.cdc.MustUnmarshal(btcDelBytes, &btcDel)
return &btcDel
}
// btcDelegationStore returns the KVStore of the BTC delegations
// prefix: BTCDelegationKey
// key: BTC delegation's staking tx hash
// value: BTCDelegation
func (k Keeper) btcDelegationStore(ctx context.Context) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
return prefix.NewStore(storeAdapter, types.BTCDelegationKey)
}