Skip to content
Open
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
38 changes: 38 additions & 0 deletions app/proposerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"strconv"
"strings"

eth2api "github.com/attestantio/go-eth2-client/api"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/app/z"
Expand Down Expand Up @@ -188,6 +191,41 @@ func DiffSummaryFields(lines []string) []z.Field {
}
}

// RegistrationFeeRecipientFunc returns the fee recipients the initial proposer config is derived from, as on charon run
// start: the builder registration service over the lock's registrations and the overrides file, so a registration
// override newer than the lock's wins. An invalid lock registration is an error, as on charon run.
func RegistrationFeeRecipientFunc(ctx context.Context, lock cluster.Lock, overridesFile string) (func(core.PubKey) string, error) {
var (
regs = make([]*eth2api.VersionedSignedValidatorRegistration, 0, len(lock.Validators))
fees = make(map[core.PubKey]string, len(lock.Validators))
)

lockFees := lock.FeeRecipientAddresses()

for i, val := range lock.Validators {
pubkey, err := core.PubKeyFromBytes(val.PubKey)
if err != nil {
return nil, errors.Wrap(err, "core pubkey", z.Int("validator", i))
}

fees[pubkey] = lockFees[i]

reg, err := val.Eth2Registration()
if err != nil {
return nil, errors.Wrap(err, "convert builder registration", z.Int("validator", i))
}
Comment thread
KaloyanTanev marked this conversation as resolved.
Resolved

regs = append(regs, reg)
}

svc, err := NewBuilderRegistrationService(ctx, overridesFile, eth2p0.Version(lock.ForkVersion), regs, fees, nil, lock.LockHash)
if err != nil {
return nil, err
}

return svc.FeeRecipient, nil
}

// InitialProposerConfig returns the unsigned revision 0 document: fee recipients from the effective builder
// registrations falling back to the lock, gas limit and builder from the seed flags.
func InitialProposerConfig(lock cluster.Lock, seed proposerconfig.Seed, feeRecipientFunc func(core.PubKey) string) (proposerconfig.Document, error) {
Expand Down
6 changes: 3 additions & 3 deletions cluster/proposerconfig/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Apply(lock cluster.Lock, c Config, d Delta) (Config, error) {
return Config{}, err
}

equal, err := sameConfig(c, res)
equal, err := SameConfig(c, res)
if err != nil {
return Config{}, err
}
Expand Down Expand Up @@ -173,8 +173,8 @@ func applyBuilder(b *Builder, d Delta) (*Builder, error) {
return &res, nil
}

// sameConfig returns whether a and b have the same canonical form.
func sameConfig(a, b Config) (bool, error) {
// SameConfig returns whether a and b have the same canonical form.
func SameConfig(a, b Config) (bool, error) {
ha, err := Document{Config: a}.Hash()
if err != nil {
return false, err
Expand Down
37 changes: 37 additions & 0 deletions cluster/proposerconfig/proposerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,40 @@ func normaliseAddress(addr string) (string, error) {

return strings.ToLower(checksummed), nil
}

// Normalise returns a copy of c with fee recipients lowercased after EIP-55 validation and
// proposer_config keys lowercased. Empty fee recipients are kept.
func Normalise(c Config) (Config, error) {
norm := func(s Settings) (Settings, error) {
if s.FeeRecipient == "" {
return s, nil
}

fee, err := normaliseAddress(s.FeeRecipient)
if err != nil {
return Settings{}, err
}

s.FeeRecipient = fee

return s, nil
}

def, err := norm(c.DefaultConfig)
if err != nil {
return Config{}, errors.Wrap(err, "default config")
}

res := Config{DefaultConfig: def, ProposerConfig: make(map[string]Settings, len(c.ProposerConfig))}

for k, v := range c.ProposerConfig {
s, err := norm(v)
if err != nil {
return Config{}, errors.Wrap(err, "proposer config entry", z.Str("pubkey", k))
}

res.ProposerConfig[strings.ToLower(k)] = s
}

return res, nil
}
85 changes: 85 additions & 0 deletions cmd/cmd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"io"
"os"
"strings"
"testing"
"time"

Expand All @@ -17,6 +18,7 @@ import (
"github.com/obolnetwork/charon/app/featureset"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/cluster/proposerconfig"
"github.com/obolnetwork/charon/dkg"
"github.com/obolnetwork/charon/p2p"
"github.com/obolnetwork/charon/testutil"
)
Expand Down Expand Up @@ -276,6 +278,89 @@ func TestProposerConfigSeedFlags(t *testing.T) {
})
}

func TestDKGProposerConfigSeedFlags(t *testing.T) {
execute := func(t *testing.T, args ...string) (dkg.Config, error) {
t.Helper()

var conf dkg.Config

root := newRootCmd(newDKGCmd(func(_ context.Context, config dkg.Config) error {
conf = config
return nil
}))
root.SetArgs(append([]string{"dkg"}, args...))

return conf, root.Execute()
}

t.Run("defaults", func(t *testing.T) {
conf, err := execute(t)
require.NoError(t, err)
require.Equal(t, proposerconfig.Seed{GasLimit: 200_000_000, BuilderBoostFactor: 100}, conf.ProposerConfigSeed)
})

t.Run("flags parse", func(t *testing.T) {
conf, err := execute(t,
"--proposer-config-gas-limit=60000000",
"--proposer-config-builder-urls=https://a.example.com",
"--proposer-config-builder-min-bid=1",
"--proposer-config-builder-boost-factor=90",
"--proposer-config-builder-max-execution-payment=2",
)
require.NoError(t, err)
require.Equal(t, proposerconfig.Seed{
GasLimit: 60_000_000,
BuilderURLs: []string{"https://a.example.com"},
BuilderMinBid: 1,
BuilderBoostFactor: 90,
BuilderMaxExecutionPayment: 2,
NonDefault: []string{
"proposer-config-gas-limit", "proposer-config-builder-urls", "proposer-config-builder-min-bid",
"proposer-config-builder-boost-factor", "proposer-config-builder-max-execution-payment",
},
}, conf.ProposerConfigSeed)
})

t.Run("builder value without urls", func(t *testing.T) {
_, err := execute(t, "--proposer-config-builder-min-bid=1")
require.ErrorContains(t, err, "require proposer-config-builder-urls")
})

t.Run("usage says the values are signed", func(t *testing.T) {
cmd := newDKGCmd(nil)
for _, flag := range []string{"proposer-config-gas-limit", "proposer-config-builder-urls", "proposer-config-builder-min-bid", "proposer-config-builder-boost-factor", "proposer-config-builder-max-execution-payment"} {
usage := cmd.Flags().Lookup(flag).Usage
require.Contains(t, usage, "Signed into the cluster's initial proposer config", flag)
require.Contains(t, usage, "charon alpha edit proposer-config", flag)
require.NotContains(t, usage, "disregarded", flag)
}
})

t.Run("nickname still validated", func(t *testing.T) {
_, err := execute(t, "--nickname="+strings.Repeat("a", 33))
require.ErrorContains(t, err, "--nickname exceeds 32 character limit")
})
}

func TestAddValidatorsProposerConfigFlags(t *testing.T) {
var conf addValidatorsConfig

root := newRootCmd(newAddValidatorsCmd(func(_ context.Context, config addValidatorsConfig) error {
conf = config
return nil
}))

root.SetArgs([]string{"add-validators"})
require.NoError(t, root.Execute())
require.Equal(t, ".charon/vc-config/proposer-config.json", conf.ProposerConfigFile)
require.Equal(t, ".charon/builder_registrations_overrides.json", conf.OverridesFile)

root.SetArgs([]string{"add-validators", "--proposer-config-file=pc.json", "--overrides-file=o.json"})
require.NoError(t, root.Execute())
require.Equal(t, "pc.json", conf.ProposerConfigFile)
require.Equal(t, "o.json", conf.OverridesFile)
}

func TestFlagsToLogFields(t *testing.T) {
set := pflag.NewFlagSet("test", pflag.PanicOnError)
bindLokiFlags(set, &log.Config{})
Expand Down
69 changes: 68 additions & 1 deletion cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/obolnetwork/charon/app/obolapi"
"github.com/obolnetwork/charon/app/z"
"github.com/obolnetwork/charon/cluster"
"github.com/obolnetwork/charon/cluster/proposerconfig"
"github.com/obolnetwork/charon/core"
"github.com/obolnetwork/charon/core/consensus/protocols"
"github.com/obolnetwork/charon/eth2util"
Expand Down Expand Up @@ -91,6 +92,9 @@ type clusterConfig struct {
ExecutionEngineAddr string

Zipped bool

// ProposerConfigSeed seeds the gas limit and builder of the proposer config signed at revision 1.
ProposerConfigSeed proposerconfig.Seed
}

func newCreateClusterCmd(runFunc func(context.Context, io.Writer, clusterConfig) error) *cobra.Command {
Expand All @@ -109,6 +113,7 @@ func newCreateClusterCmd(runFunc func(context.Context, io.Writer, clusterConfig)
bindClusterFlags(cmd.Flags(), &conf)
bindInsecureFlags(cmd.Flags(), &conf.InsecureKeys)
bindNetworkFlags(cmd.Flags(), &conf.Network, &conf.testnetConfig)
bindProposerConfigSeedFlags(cmd, &conf.ProposerConfigSeed, proposerConfigSignedUsage)

wrapPreRunE(cmd, func(cmd *cobra.Command, _ []string) error {
thresholdPresent := cmd.Flags().Lookup("threshold").Changed
Expand Down Expand Up @@ -166,7 +171,13 @@ func bindInsecureFlags(flags *pflag.FlagSet, insecureKeys *bool) {
}

func runCreateCluster(ctx context.Context, w io.Writer, conf clusterConfig) error {
var err error
// Reject a bad seed before any work: it is signed by every operator.
seed := conf.ProposerConfigSeed

err := seed.Validate()
if err != nil {
return err
}

// Map prater to goerli to ensure backwards compatibility with older cluster definitions and cluster locks.
if conf.Network == eth2util.Prater {
Expand Down Expand Up @@ -351,6 +362,11 @@ func runCreateCluster(ctx context.Context, w io.Writer, conf clusterConfig) erro
lock.NodeSignatures = append(lock.NodeSignatures, nodeSig)
}

proposerConfig, err := signProposerConfig(lock, def.FeeRecipientAddresses(), conf.ProposerConfigSeed, nodeKeys)
if err != nil {
return err
}

// dashboardURL is the Launchpad dashboard url for a given lock file.
// If empty, either conf.Publish wasn't specified or there was a processing error in publishing
// the generated lock file.
Expand All @@ -367,6 +383,10 @@ func runCreateCluster(ctx context.Context, w io.Writer, conf clusterConfig) erro
return err
}

if err = writeProposerConfig(proposerConfig, lock, conf.ClusterDir); err != nil {
return err
}

if conf.Zipped {
if err = app.BundleOutput(conf.ClusterDir, "cluster.tar.gz"); err != nil {
return err
Expand Down Expand Up @@ -718,6 +738,51 @@ func writeLock(lock cluster.Lock, clusterDir string, numNodes int) error {
return nil
}

// signProposerConfig returns the lock's revision 1 proposer config signed by every operator key.
func signProposerConfig(lock cluster.Lock, feeRecipients []string, seed proposerconfig.Seed, nodeKeys []*k1.PrivateKey) (proposerconfig.Document, error) {
cfg, err := proposerconfig.NewConfig(lock, feeRecipients, seed.GasLimit, seed.Builder())
if err != nil {
return proposerconfig.Document{}, errors.Wrap(err, "proposer config")
}

doc := proposerconfig.Document{
Version: proposerconfig.Version,
LockHash: lock.LockHash,
Revision: 1,
Config: cfg,
}

for i, key := range nodeKeys {
sig, err := proposerconfig.Sign(doc, key)
if err != nil {
return proposerconfig.Document{}, errors.Wrap(err, "sign proposer config", z.Int("operator", i))
}

doc.Signatures = append(doc.Signatures, proposerconfig.Signature{Operator: i, Signature: sig})
}

err = proposerconfig.Verify(doc, lock)
if err != nil {
return proposerconfig.Document{}, errors.Wrap(err, "verify signed proposer config")
}

return doc, nil
}

// writeProposerConfig writes the proposer config to every node's vc-config directory, keyed by its pubshares.
func writeProposerConfig(doc proposerconfig.Document, lock cluster.Lock, clusterDir string) error {
for i := range lock.Operators {
path := filepath.Join(nodeDir(clusterDir, i), "vc-config", "proposer-config.json")

err := proposerconfig.Write(path, doc, lock, cluster.NodeIdx{PeerIdx: i, ShareIdx: i + 1})
if err != nil {
return errors.Wrap(err, "write proposer config", z.Int("node", i))
}
}

return nil
}

// getValidators returns distributed validators from the provided dv public keys and keyshares.
// It creates new peers from the provided config and saves validator keys to disk for each peer.
func getValidators(
Expand Down Expand Up @@ -1004,6 +1069,8 @@ func writeOutput(out io.Writer, splitKeys bool, clusterDir string, numNodes int,
_, _ = sb.WriteString("│ ├─ charon-enr-private-key\tCharon networking private key for node authentication\n")
_, _ = sb.WriteString("│ ├─ cluster-lock.json\t\tCluster lock defines the cluster lock file which is signed by all nodes\n")

_, _ = sb.WriteString("│ ├─ vc-config/proposer-config.json\tProposer config signed by all nodes, read by charon run and the validator clients\n")

_, _ = sb.WriteString("│ ├─ deposit-data-*.json\tDeposit data files are used to activate a Distributed Validator on the DV Launchpad\n")
if keysToDisk {
_, _ = sb.WriteString("│ ├─ validator_keys\t\tValidator keystores and password\n")
Expand Down
Loading
Loading