forked from babylonlabs-io/babylon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvotes.go
109 lines (95 loc) · 3.33 KB
/
votes.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package keeper
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/runtime"
"cosmossdk.io/store/prefix"
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/babylonlabs-io/babylon/x/finality/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k Keeper) SetSig(ctx context.Context, height uint64, fpBtcPK *bbn.BIP340PubKey, sig *bbn.SchnorrEOTSSig) {
store := k.voteHeightStore(ctx, height)
store.Set(fpBtcPK.MustMarshal(), sig.MustMarshal())
}
func (k Keeper) HasSig(ctx context.Context, height uint64, fpBtcPK *bbn.BIP340PubKey) bool {
store := k.voteHeightStore(ctx, height)
return store.Has(fpBtcPK.MustMarshal())
}
func (k Keeper) GetSig(ctx context.Context, height uint64, fpBtcPK *bbn.BIP340PubKey) (*bbn.SchnorrEOTSSig, error) {
if uint64(sdk.UnwrapSDKContext(ctx).HeaderInfo().Height) < height {
return nil, types.ErrHeightTooHigh
}
store := k.voteHeightStore(ctx, height)
sigBytes := store.Get(fpBtcPK.MustMarshal())
if len(sigBytes) == 0 {
return nil, types.ErrVoteNotFound
}
sig, err := bbn.NewSchnorrEOTSSig(sigBytes)
if err != nil {
panic(fmt.Errorf("failed to unmarshal EOTS signature: %w", err))
}
return sig, nil
}
// GetSigSet gets all EOTS signatures at a given height
func (k Keeper) GetSigSet(ctx context.Context, height uint64) map[string]*bbn.SchnorrEOTSSig {
store := k.voteHeightStore(ctx, height)
iter := store.Iterator(nil, nil)
defer iter.Close()
// if there is no vote on this height, return nil
if !iter.Valid() {
return nil
}
sigs := map[string]*bbn.SchnorrEOTSSig{}
for ; iter.Valid(); iter.Next() {
fpBTCPK, err := bbn.NewBIP340PubKey(iter.Key())
if err != nil {
// failing to unmarshal finality provider's BTC PK in KVStore is a programming error
panic(fmt.Errorf("%w: %w", bbn.ErrUnmarshal, err))
}
sig, err := bbn.NewSchnorrEOTSSig(iter.Value())
if err != nil {
// failing to unmarshal EOTS sig in KVStore is a programming error
panic(fmt.Errorf("failed to unmarshal EOTS signature: %w", err))
}
sigs[fpBTCPK.MarshalHex()] = sig
}
return sigs
}
// GetVoters gets returns a map of voters' BTC PKs to the given height
func (k Keeper) GetVoters(ctx context.Context, height uint64) map[string]struct{} {
store := k.voteHeightStore(ctx, height)
iter := store.Iterator(nil, nil)
defer iter.Close()
// if there is no vote on this height, return nil
if !iter.Valid() {
return nil
}
voterBTCPKs := map[string]struct{}{}
for ; iter.Valid(); iter.Next() {
// accumulate voterBTCPKs
fpBTCPK, err := bbn.NewBIP340PubKey(iter.Key())
if err != nil {
// failing to unmarshal finality provider's BTC PK in KVStore is a programming error
panic(fmt.Errorf("%w: %w", bbn.ErrUnmarshal, err))
}
voterBTCPKs[fpBTCPK.MarshalHex()] = struct{}{}
}
return voterBTCPKs
}
// voteHeightStore returns the KVStore of the votes
// prefix: VoteKey
// key: (block height || finality provider PK)
// value: EOTS sig
func (k Keeper) voteHeightStore(ctx context.Context, height uint64) prefix.Store {
prefixedStore := k.voteStore(ctx)
return prefix.NewStore(prefixedStore, sdk.Uint64ToBigEndian(height))
}
// voteStore returns the KVStore of the votes
// prefix: VoteKey
// key: (prefix)
// value: EOTS sig
func (k Keeper) voteStore(ctx context.Context) prefix.Store {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
return prefix.NewStore(storeAdapter, types.VoteKey)
}