-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathgenesis_bls.go
67 lines (58 loc) · 2.06 KB
/
genesis_bls.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"
"cosmossdk.io/store/prefix"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/babylonlabs-io/babylon/v2/crypto/bls12381"
"github.com/babylonlabs-io/babylon/v2/x/checkpointing/types"
)
// SetGenBlsKeys registers BLS keys with each validator at genesis
func (k Keeper) SetGenBlsKeys(ctx context.Context, genKeys []*types.GenesisKey) error {
for _, key := range genKeys {
addr, err := sdk.ValAddressFromBech32(key.ValidatorAddress)
if err != nil {
return err
}
exists := k.RegistrationState(ctx).Exists(addr)
if exists {
return fmt.Errorf("a validator's BLS key has already been registered. Duplicate address: %s", key.ValidatorAddress)
}
ok := key.BlsKey.Pop.IsValid(*key.BlsKey.Pubkey, key.ValPubkey)
if !ok {
return fmt.Errorf("Proof-of-Possession is not valid. Pop: %s", key.BlsKey.Pop.String())
}
err = k.RegistrationState(ctx).CreateRegistration(*key.BlsKey.Pubkey, addr)
if err != nil {
return fmt.Errorf("failed to register a BLS key. Val Addr: %s", key.ValidatorAddress)
}
}
return nil
}
// GetBlsKeys gets all the BLS keys stored.
// This function is called in ExportGenesis
// NOTE: validator ed25519 pub key and PoP are not stored in the module
// but used on InitGenesis for validation. Make sure to populate these fields
// before using the exported data as input in the InitGenesis logic.
func (k Keeper) GetBlsKeys(ctx context.Context) ([]*types.GenesisKey, error) {
genKeys := make([]*types.GenesisKey, 0)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.BlsKeyToAddrPrefix)
iter := store.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
blsKey := new(bls12381.PublicKey)
if err := blsKey.Unmarshal(iter.Key()); err != nil {
return nil, err
}
valAddr := sdk.ValAddress(iter.Value())
genKeys = append(genKeys, &types.GenesisKey{
ValidatorAddress: valAddr.String(),
BlsKey: &types.BlsKey{
Pubkey: blsKey,
},
})
}
return genKeys, nil
}