Skip to content

Commit

Permalink
Update create pub key tx to include number of validators & aggregate …
Browse files Browse the repository at this point in the history
…key according to that
  • Loading branch information
p0p3yee committed Feb 7, 2024
1 parent a4d6ba1 commit 5bf7965
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 83 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/FairBlock/DistributedIBE v0.0.0-20230528025616-f58fb2b93eaf
github.com/armon/go-metrics v0.4.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-sdk v0.47.3
Expand Down Expand Up @@ -47,7 +48,6 @@ require (
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.44.203 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
Expand Down
2 changes: 2 additions & 0 deletions proto/fairyring/keyshare/pub_key.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ message ActivePubKey {
string publicKey = 1;
string creator = 2;
uint64 expiry = 3;
uint64 numberOfValidators = 4;
}

message QueuedPubKey {
string publicKey = 1;
string creator = 2;
uint64 expiry = 3;
uint64 numberOfValidators = 4;
}
1 change: 1 addition & 0 deletions proto/fairyring/keyshare/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ message MsgCreateLatestPubKey {
string creator = 1;
string publicKey = 2;
repeated string commitments = 3;
uint64 numberOfValidators = 4;
}

message MsgCreateLatestPubKeyResponse {}
Expand Down
11 changes: 9 additions & 2 deletions x/keyshare/client/cli/tx_latest_pub_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fairyring/x/keyshare/types"
"github.com/spf13/cast"
"strings"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -12,9 +13,9 @@ import (

func CmdCreateLatestPubKey() *cobra.Command {
cmd := &cobra.Command{
Use: "create-latest-pub-key [public-key] [commitments]",
Use: "create-latest-pub-key [public-key] [commitments] [number-of-validators]",
Short: "Create a latest public key",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {

// Get value arguments
Expand All @@ -23,6 +24,11 @@ func CmdCreateLatestPubKey() *cobra.Command {
commitmentStr := args[1]
commitments := strings.Split(commitmentStr, ",")

numberOfValidators, err := cast.ToUint64E(args[2])
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand All @@ -32,6 +38,7 @@ func CmdCreateLatestPubKey() *cobra.Command {
clientCtx.GetFromAddress().String(),
argPublicKey,
commitments,
numberOfValidators,
)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
Expand Down
12 changes: 9 additions & 3 deletions x/keyshare/keeper/msg_server_pub_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (k msgServer) CreateLatestPubKey(goCtx context.Context, msg *types.MsgCreat
return nil, types.ErrEmptyCommitments
}

if msg.NumberOfValidators == 0 {
return nil, types.ErrInvalidNumberOfValidators
}

commitments := types.Commitments{
Commitments: msg.Commitments,
}
Expand All @@ -40,9 +44,10 @@ func (k msgServer) CreateLatestPubKey(goCtx context.Context, msg *types.MsgCreat
}

var queuedPubKey = types.QueuedPubKey{
Creator: msg.Creator,
PublicKey: msg.PublicKey,
Expiry: expHeight,
Creator: msg.Creator,
PublicKey: msg.PublicKey,
Expiry: expHeight,
NumberOfValidators: msg.NumberOfValidators,
}

k.SetQueuedCommitments(
Expand Down Expand Up @@ -70,6 +75,7 @@ func (k msgServer) CreateLatestPubKey(goCtx context.Context, msg *types.MsgCreat
sdk.NewAttribute(types.QueuedPubKeyCreatedEventExpiryHeight, strconv.FormatUint(expHeight, 10)),
sdk.NewAttribute(types.QueuedPubKeyCreatedEventCreator, msg.Creator),
sdk.NewAttribute(types.QueuedPubKeyCreatedEventPubkey, msg.PublicKey),
sdk.NewAttribute(types.QueuedPubKeyCreatedEventNumberOfValidators, strconv.FormatUint(msg.NumberOfValidators, 10)),
),
)

Expand Down
16 changes: 8 additions & 8 deletions x/keyshare/keeper/msg_server_send_keyshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ func (k msgServer) SendKeyshare(goCtx context.Context, msg *types.MsgSendKeyshar
stateKeyShares = append(stateKeyShares, eachKeyShare)
}

// Get the active public key for aggregating
activePubKey, found := k.GetActivePubKey(ctx)

if !found {
return nil, types.ErrPubKeyNotFound
}

expectedThreshold := sdk.NewDecFromInt(
sdk.NewInt(types.KeyAggregationThresholdNumerator)).Quo(
sdk.NewDecFromInt(sdk.NewInt(types.KeyAggregationThresholdDenominator))).MulInt64(
int64(len(validatorList))).Ceil().TruncateInt64()
int64(activePubKey.NumberOfValidators)).Ceil().TruncateInt64()

// Emit KeyShare Submitted Event
ctx.EventManager().EmitEvent(
Expand Down Expand Up @@ -162,13 +169,6 @@ func (k msgServer) SendKeyshare(goCtx context.Context, msg *types.MsgSendKeyshar
}, nil
}

// Get the active public key for aggregating
activePubKey, found := k.pepKeeper.GetActivePubKey(ctx)

if !found {
return nil, types.ErrPubKeyNotFound
}

// Parse & append all the keyshare for aggregation
var listOfShares []distIBE.ExtractedKey
var listOfCommitment []distIBE.Commitment
Expand Down
6 changes: 5 additions & 1 deletion x/keyshare/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
if foundQk {
if qk.Expiry > height {
am.keeper.SetActivePubKey(ctx, types.ActivePubKey(qk))
am.pepKeeper.SetActivePubKey(ctx, peptypes.ActivePubKey(qk))
am.pepKeeper.SetActivePubKey(ctx, peptypes.ActivePubKey{
PublicKey: qk.PublicKey,
Creator: qk.Creator,
Expiry: qk.Expiry,
})
if foundQc {
am.keeper.SetActiveCommitments(ctx, qc)
}
Expand Down
2 changes: 1 addition & 1 deletion x/keyshare/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
ErrInvalidCommitmentLength = sdkerrors.Register(ModuleName, 1122, "invalid commitment length")
ErrInvalidVersion = sdkerrors.Register(ModuleName, 1123, "invalid version")
ErrRequestNotFound = sdkerrors.Register(ModuleName, 1124, "no request found with this identity")
ErrNoAggregatedKeyshare = sdkerrors.Register(ModuleName, 1125, "aggregated keyshare has not been generated")
ErrInvalidNumberOfValidators = sdkerrors.Register(ModuleName, 1125, "invalid number of validators")
ErrAddressAlreadyAuthorized = sdkerrors.Register(ModuleName, 1900, "address is already authorized")
ErrAuthorizedAddrNotFound = sdkerrors.Register(ModuleName, 1901, "target authorized address not found")
ErrNotAuthorizedAddrCreator = sdkerrors.Register(ModuleName, 1902, "sender is not the creator of target authorized address")
Expand Down
1 change: 1 addition & 0 deletions x/keyshare/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const (
QueuedPubKeyCreatedEventExpiryHeight = "queued-pubkey-created-expiry-height"
QueuedPubKeyCreatedEventCreator = "queued-pubkey-created-creator"
QueuedPubKeyCreatedEventPubkey = "queued-pubkey-created-pubkey"
QueuedPubKeyCreatedEventNumberOfValidators = "number-of-validators"
)

const (
Expand Down
11 changes: 8 additions & 3 deletions x/keyshare/types/messages_latest_pub_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ func NewMsgCreateLatestPubKey(
creator string,
publicKey string,
commitments []string,
numberOfValidators uint64,
) *MsgCreateLatestPubKey {
return &MsgCreateLatestPubKey{
Creator: creator,
PublicKey: publicKey,
Commitments: commitments,
Creator: creator,
PublicKey: publicKey,
Commitments: commitments,
NumberOfValidators: numberOfValidators,
}
}

Expand Down Expand Up @@ -67,5 +69,8 @@ func (msg *MsgCreateLatestPubKey) ValidateBasic() error {
return ErrInvalidCommitment.Wrapf("expected hex encoded commitment, got: %s", c)
}
}
if msg.NumberOfValidators == 0 {
return ErrInvalidNumberOfValidators.Wrapf("expected at least 1 validator, got: %d", msg.NumberOfValidators)
}
return nil
}
105 changes: 88 additions & 17 deletions x/keyshare/types/pub_key.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5bf7965

Please sign in to comment.