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

refactor: modify enr error message #493

Merged
merged 5 commits into from
May 6, 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
6 changes: 5 additions & 1 deletion cmd/enr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package cmd
import (
"fmt"
"io"
"io/fs"

"github.com/spf13/cobra"

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

Expand Down Expand Up @@ -50,7 +52,9 @@ func newEnrCmd(runFunc func(io.Writer, p2p.Config, string) error) *cobra.Command
// runNewENR loads the p2pkey from disk and prints the ENR for the provided config.
func runNewENR(w io.Writer, config p2p.Config, dataDir string) error {
key, err := p2p.LoadPrivKey(dataDir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return errors.New("ENR private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(dataDir))) //nolint:revive
} else if err != nil {
return err
}

Expand Down
37 changes: 37 additions & 0 deletions cmd/enr_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 (
"io"
"os"
"testing"

"github.com/stretchr/testify/require"

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

func TestRunNewEnr(t *testing.T) {
temp, err := os.MkdirTemp("", "")
require.NoError(t, err)

got := runNewENR(io.Discard, p2p.Config{}, temp)
expected := errors.New("ENR private key not found. If this is your first time running this client, create one with `charon create enr`.", z.Str("enr_path", p2p.KeyPath(temp)))
require.Equal(t, expected.Error(), got.Error())
}
2 changes: 1 addition & 1 deletion p2p/k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func KeyPath(datadir string) string {
func LoadPrivKey(dataDir string) (*ecdsa.PrivateKey, error) {
key, err := crypto.LoadECDSA(KeyPath(dataDir))
if err != nil {
return nil, errors.Wrap(err, "load key")
return nil, errors.Wrap(err, "load priv key")
}

return key, nil
Expand Down