Skip to content

Commit

Permalink
cmd: ensures valid network during create dkg (#598)
Browse files Browse the repository at this point in the history
Ensures valid network during create dkg using network flag which is then used to get fork version to create cluster definition.

category: bug
ticket: #583
  • Loading branch information
dB2510 authored May 24, 2022
1 parent 516210c commit f56ceb3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cluster/test_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func NewForT(t *testing.T, dv, k, n, seed int) (Lock, []*ecdsa.PrivateKey, [][]*
Definition: NewDefinition(
"test cluster", dv, k,
testutil.RandomETHAddress(), testutil.RandomETHAddress(),
"0x0000000", ops, random),
"0x00000000", ops, random),
Validators: vals,
SignatureAggregate: nil,
}, p2pKeys, dvShares
Expand Down
21 changes: 18 additions & 3 deletions cmd/createdkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/pflag"

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

Expand All @@ -36,11 +37,19 @@ type createDKGConfig struct {
Threshold int
FeeRecipient string
WithdrawalAddress string
ForkVersion string
Network string
DKGAlgo string
OperatorENRs []string
}

var networkToForkVersion = map[string]string{
"prater": "0x00001020",
"kintsugi": "0x60000069",
"kiln": "0x70000069",
"gnosis": "0x00000064",
"mainnet": "0x00000000",
}

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

Expand All @@ -66,7 +75,7 @@ func bindCreateDKGFlags(flags *pflag.FlagSet, config *createDKGConfig) {
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.StringVar(&config.Network, "network", defaultNetwork, "Ethereum network to create validators for. Options: mainnet, prater, kintsugi, kiln, gnosis.")
flags.StringVar(&config.DKGAlgo, "dkg-algorithm", "default", "DKG algorithm to use; default, keycast, frost")
flags.StringSliceVar(&config.OperatorENRs, "operator-enrs", nil, "Comma-separated list of each operator's Charon ENR address")
}
Expand All @@ -79,8 +88,14 @@ func runCreateDKG(_ context.Context, conf createDKGConfig) error {
})
}

if !validNetworks[conf.Network] {
return errors.New("unsupported network", z.Str("network", conf.Network))
}

forkVersion := networkToForkVersion[conf.Network]

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

def.DKGAlgorithm = conf.DKGAlgo

Expand Down
27 changes: 24 additions & 3 deletions dkg/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type Config struct {
DataDir string
P2P p2p.Config
Log log.Config
Network string

TestDef *cluster.Definition
TestSigning bool
Expand All @@ -67,6 +66,11 @@ func Run(ctx context.Context, conf Config) (err error) {
return err
}

network, err := forkVersionToNetwork(def.ForkVersion)
if err != nil {
return err
}

peers, err := def.Peers()
if err != nil {
return err
Expand Down Expand Up @@ -155,12 +159,12 @@ func Run(ctx context.Context, conf Config) (err error) {
}

// Sign, exchange and aggregate Deposit Data signatures
aggSigDepositData, err := signAndAggDepositData(ctx, ex, shares, def.WithdrawalAddress, conf.Network, nodeIdx)
aggSigDepositData, err := signAndAggDepositData(ctx, ex, shares, def.WithdrawalAddress, network, nodeIdx)
if err != nil {
return err
}

return writeDepositData(aggSigDepositData, def.WithdrawalAddress, conf.Network, conf.DataDir)
return writeDepositData(aggSigDepositData, def.WithdrawalAddress, network, conf.DataDir)
}

// setupP2P returns a started libp2p tcp node and a shutdown function.
Expand Down Expand Up @@ -565,3 +569,20 @@ func waitConnect(ctx context.Context, tcpNode host.Host, p peer.ID) (time.Durati

return 0, false
}

func forkVersionToNetwork(forkVersion string) (string, error) {
switch forkVersion {
case "0x00001020":
return "prater", nil
case "0x60000069":
return "kintsugi", nil
case "0x70000069":
return "kiln", nil
case "0x00000064":
return "gnosis", nil
case "0x00000000":
return "mainnet", nil
default:
return "", errors.New("invalid fork version")
}
}

0 comments on commit f56ceb3

Please sign in to comment.