Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: add create dkg command #484

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func New() *cobra.Command {
newBootnodeCmd(RunBootnode),
newCreateClusterCmd(runCreateCluster),
newDKGCmd(dkg.Run),
newCreateCmd(
newCreateDKGCmd(runCreateDKG),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

charon create dkg just doesn't feel right to me

Copy link
Contributor

@xenowits xenowits May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean you can do a dkg (distributed key gen) but creating a dkg doesn't make sense to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed for:

  • charon define dkg -> cluster_definition.json
  • charon seal -> cluster_lock.json
  • charon run

Or for local cluster flow:

  • charon define local
  • charon seal
  • charon run

But this this was rejected for:

  • charon create dkg -> cluster_definition.json
  • charon dkg -> cluster_lock.json
  • charon run

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is building a cluster_definition even part of a dkg?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's an input to a dkg, if i'm not wrong

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or we can do something like:
charon create dkg-cluster
or
charon create dkg-config

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should have been discussed as part of the ticket grooming. I don't think we should stop the merge of this because we do not agree on the command name. Let's create an issue and discuss it there the definete command name

),
)
}

Expand Down
31 changes: 31 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © 2022 Obol Labs Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import "github.com/spf13/cobra"

func newCreateCmd(cmds ...*cobra.Command) *cobra.Command {
root := &cobra.Command{
Use: "create",
Short: "Create different artifacts required for running a Charon cluster",
}

root.AddCommand(cmds...)

titledHelp(root)

return root
}
97 changes: 97 additions & 0 deletions cmd/createdkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright © 2022 Obol Labs Inc.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"context"
crand "crypto/rand"
"encoding/json"
"os"
"path"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/cluster"
)

type createDKGConfig struct {
OutputDir string
Name string
NumValidators int
Threshold int
FeeRecipient string
WithdrawalAddress string
ForkVersion string
OperatorENRs []string
}

func newCreateDKGCmd(runFunc func(context.Context, createDKGConfig) error) *cobra.Command {
var config createDKGConfig

cmd := &cobra.Command{
Use: "dkg",
Short: "Configure DKG by creating a cluster definition file",
Long: `Create a cluster definition file that will be used by all participants of a DKG.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runFunc(cmd.Context(), config)
},
}

bindCreateDKGFlags(cmd.Flags(), &config)

return cmd
}

func bindCreateDKGFlags(flags *pflag.FlagSet, config *createDKGConfig) {
flags.StringVar(&config.Name, "name", "", "Optional cosmetic cluster name")
flags.StringVar(&config.OutputDir, "output-dir", ".", "The folder to write the output cluster_definition.json file to.")
flags.IntVar(&config.NumValidators, "num-validators", 1, "The number of distributed validators the cluster will manage (32ETH staked for each).")
flags.IntVarP(&config.Threshold, "threshold", "t", 3, "The threshold required for signature reconstruction. Minimum is n-(ceil(n/3)-1).")
flags.StringVar(&config.FeeRecipient, "fee_recipient_address", "", "Optional Ethereum address of the fee recipient")
flags.StringVar(&config.WithdrawalAddress, "withdrawal_address", "", "Withdrawal Ethereum address")
flags.StringVar(&config.ForkVersion, "fork_version", "", "Optional hex fork version identifying the target network/chain")
flags.StringSliceVar(&config.OperatorENRs, "operator_enrs", nil, "Comma-separated list of each operator's Charon ENR address")
}

func runCreateDKG(_ context.Context, conf createDKGConfig) error {
var operators []cluster.Operator
for _, opENR := range conf.OperatorENRs {
operators = append(operators, cluster.Operator{
ENR: opENR,
})
}

def := cluster.NewDefinition(conf.Name, conf.NumValidators, conf.Threshold, conf.FeeRecipient, conf.WithdrawalAddress,
conf.ForkVersion, operators, crand.Reader)

b, err := json.Marshal(def)
if err != nil {
return errors.Wrap(err, "marshal definition")
}

if err := os.MkdirAll(conf.OutputDir, 0o755); err != nil {
return errors.Wrap(err, "create output dir")
}

if err := os.WriteFile(path.Join(conf.OutputDir, "cluster_definition.json"), b, 0o400); err != nil {
return errors.Wrap(err, "write definition")
}

return nil
}