Skip to content

Commit

Permalink
cmd: write lock file in create cluster (#566)
Browse files Browse the repository at this point in the history
Write a cluster lock file when running charon create cluster.

Rename cluster_lock.json to cluster-lock.json as per suggestion from Oisin.

category: feature
ticket: #528
  • Loading branch information
corverroos committed May 19, 2022
1 parent fa5d640 commit ec66445
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestCmdFlags(t *testing.T) {
Enabled: nil,
Disabled: nil,
},
LockFile: ".charon/cluster_lock.json",
LockFile: ".charon/cluster-lock.json",
DataDir: "from_env",
MonitoringAddr: "127.0.0.1:16001",
ValidatorAPIAddr: "127.0.0.1:16002",
Expand Down
70 changes: 68 additions & 2 deletions cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"net"
Expand All @@ -35,13 +36,16 @@ import (

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/cluster"
"github.com/obolnetwork/charon/eth2util/deposit"
"github.com/obolnetwork/charon/eth2util/keystore"
"github.com/obolnetwork/charon/p2p"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/tbls/tblsconv"
)

const clusterName = "local"

const scriptTmpl = `#!/usr/bin/env bash
# This script run a charon node using the p2pkey in the
Expand Down Expand Up @@ -367,11 +371,73 @@ func writeDepositData(conf clusterConfig, secrets []*bls_sig.SecretKey) error {
return nil
}

func writeLock(_ clusterConfig, _ []tbls.TSS, _ []p2p.Peer) error {
// TODO(corver): Create lock file.
// writeLock creates a cluster lock and writes it to disk.
func writeLock(conf clusterConfig, dvs []tbls.TSS, peers []p2p.Peer) error {
lock, err := newLock(conf, dvs, peers)
if err != nil {
return err
}

b, err := json.MarshalIndent(lock, "", " ")
if err != nil {
return errors.Wrap(err, "marshal cluster lock")
}

lockPath := path.Join(conf.ClusterDir, "cluster-lock.json")
err = os.WriteFile(lockPath, b, 0o400) // read-only
if err != nil {
return errors.Wrap(err, "write cluster lock")
}

return nil
}

// newLock returns a new unsigned cluster lock.
func newLock(conf clusterConfig, dvs []tbls.TSS, peers []p2p.Peer) (cluster.Lock, error) {
var ops []cluster.Operator
for _, p := range peers {
enrStr, err := p2p.EncodeENR(p.ENR)
if err != nil {
return cluster.Lock{}, err
}

ops = append(ops, cluster.Operator{ENR: enrStr})
}

var vals []cluster.DistValidator
for _, dv := range dvs {
pk, err := tblsconv.KeyToCore(dv.PublicKey())
if err != nil {
return cluster.Lock{}, err
}

var pubshares [][]byte
for i := 0; i < dv.NumShares(); i++ {
share, err := dv.PublicShare(i + 1) // Shares are 1-indexed.
if err != nil {
return cluster.Lock{}, err
}
b, err := share.MarshalBinary()
if err != nil {
return cluster.Lock{}, errors.Wrap(err, "marshal pubshare")
}
pubshares = append(pubshares, b)
}

vals = append(vals, cluster.DistValidator{
PubKey: string(pk),
PubShares: pubshares,
})
}

def := cluster.NewDefinition(clusterName, len(dvs), conf.Threshold, "", "", "", ops, rand.Reader)

return cluster.Lock{
Definition: def,
Validators: vals,
}, nil
}

// newPeer returns a new peer, generating a p2pkey and ENR and node directory and run script in the process.
func newPeer(conf clusterConfig, peerIdx int, nextPort func() int) (p2p.Peer, error) {
tcp := net.TCPAddr{
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newRunCmd(runFunc func(context.Context, app.Config) error) *cobra.Command {
}

func bindRunFlags(flags *pflag.FlagSet, config *app.Config) {
flags.StringVar(&config.LockFile, "lock-file", ".charon/cluster_lock.json", "The path to the cluster lock file defining distributed validator cluster")
flags.StringVar(&config.LockFile, "lock-file", ".charon/cluster-lock.json", "The path to the cluster lock file defining distributed validator cluster")
flags.StringVar(&config.BeaconNodeAddr, "beacon-node-endpoint", "http://localhost/", "Beacon node endpoint URL")
flags.StringVar(&config.ValidatorAPIAddr, "validator-api-address", "127.0.0.1:16002", "Listening address (ip and port) for validator-facing traffic proxying the beacon-node API")
flags.StringVar(&config.MonitoringAddr, "monitoring-address", "127.0.0.1:16001", "Listening address (ip and port) for the monitoring API (prometheus, pprof)")
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/TestCreateCluster_simnet_files.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
"cluster-lock.json",
"deposit-data.json",
"node0",
"node1",
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/TestCreateCluster_splitkeys_files.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
"cluster-lock.json",
"deposit-data.json",
"node0",
"node1",
Expand Down
2 changes: 1 addition & 1 deletion dkg/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func writeLock(datadir string, lock cluster.Lock) error {
return errors.Wrap(err, "marshal lock")
}

err = os.WriteFile(path.Join(datadir, "cluster_lock.json"), b, 0o444) // Read-only
err = os.WriteFile(path.Join(datadir, "cluster-lock.json"), b, 0o444) // Read-only
if err != nil {
return errors.Wrap(err, "write lock")
}
Expand Down
2 changes: 1 addition & 1 deletion dkg/dkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func testDKG(t *testing.T, def cluster.Definition, p2pKeys []*ecdsa.PrivateKey)
secretShares[i] = append(secretShares[i], key)
}

lockFile, err := os.ReadFile(path.Join(dataDir, "cluster_lock.json"))
lockFile, err := os.ReadFile(path.Join(dataDir, "cluster-lock.json"))
require.NoError(t, err)

var lock cluster.Lock
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Flags:
-h, --help Help for run
--jaeger-address string Listening address for jaeger tracing
--jaeger-service string Service name used for jaeger tracing (default "charon")
--lock-file string The path to the cluster lock file defining distributed validator cluster (default ".charon/cluster_lock.json")
--lock-file string The path to the cluster lock file defining distributed validator cluster (default ".charon/cluster-lock.json")
--log-format string Log format; console, logfmt or json (default "console")
--log-level string Log level; debug, info, warn or error (default "info")
--monitoring-address string Listening address (ip and port) for the monitoring API (prometheus, pprof) (default "127.0.0.1:16001")
Expand Down

0 comments on commit ec66445

Please sign in to comment.