Skip to content

Commit

Permalink
Add CLI tool to generate an identity file (#3167)
Browse files Browse the repository at this point in the history
## Motivation
Closes #3157 

## Changes
Change the ensureIdentity to public EnsureIdentity,
Add a CLI tool that calls EnsureIdentity

## DevOps Notes
<!-- Please uncheck these items as applicable to make DevOps aware of changes that may affect releases -->
- [x] This PR does not require configuration changes (e.g., environment variables, GitHub secrets, VM resources)
- [x] This PR does not affect public APIs
- [x] This PR does not rely on a new version of external services (PoET, elasticsearch, etc.)
- [x] This PR does not make changes to log messages (which monitoring infrastructure may rely on)
  • Loading branch information
orgr committed Apr 28, 2022
1 parent ff84e8d commit 0549224
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ COPY . .
# And compile the project
RUN make build
RUN make harness
RUN make gen-p2p-identity

#In this last stage, we start from a fresh Alpine image, to reduce the image size and not ship the Go compiler in our production artifacts.
FROM linux AS spacemesh
Expand All @@ -94,6 +95,7 @@ COPY --from=server_builder /go/src/github.com/spacemeshos/go-spacemesh/build/go-
COPY --from=server_builder /go/src/github.com/spacemeshos/go-spacemesh/build/libgpu-setup.so /bin/
# TODO(nkryuchkov): uncomment when go-svm is imported
#COPY --from=server_builder /go/src/github.com/spacemeshos/go-spacemesh/build/libsvm.so /bin/
COPY --from=server_builder /go/src/github.com/spacemeshos/go-spacemesh/build/gen-p2p-identity /bin/

ENTRYPOINT ["/bin/go-harness"]
EXPOSE 7513
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ build: go-spacemesh
get-libs: get-gpu-setup #get-svm
.PHONY: get-libs

gen-p2p-identity:
cd $@ ; go build -o $(BIN_DIR)$@$(EXE) $(GOTAGS) .
hare p2p: get-libs
cd cmd/$@ ; go build -o $(BIN_DIR)go-$@$(EXE) $(GOTAGS) .
cd $@ ; go build -o $(BIN_DIR)go-$@$(EXE) $(GOTAGS) .
go-spacemesh: get-libs
go build -o $(BIN_DIR)$@$(EXE) $(LDFLAGS) $(GOTAGS) .
harness: get-libs
cd cmd/integration ; go build -o $(BIN_DIR)go-$@$(EXE) $(GOTAGS) .
.PHONY: hare p2p harness go-spacemesh
.PHONY: hare p2p harness go-spacemesh gen-p2p-identity

tidy:
go mod tidy
Expand Down
31 changes: 31 additions & 0 deletions gen-p2p-identity/gen_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"os"

"github.com/spacemeshos/go-spacemesh/p2p"
)

func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %v <path/to/output_dir>\n", os.Args[0])
os.Exit(1)
}

dir := os.Args[1]
_, err := p2p.EnsureIdentity(dir)
if err != nil {
fmt.Printf("failed generating identity: %v\n", err)
os.Exit(1)
}

identityStr, err := p2p.PrettyIdentityInfoFromDir(dir)
if err != nil {
fmt.Printf("failed fetching identity from file: %v\n", err)
os.Exit(1)
}

// Print ID
fmt.Printf("%s\n", identityStr)
}
2 changes: 1 addition & 1 deletion p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Config struct {
// New initializes libp2p host configured for spacemesh.
func New(ctx context.Context, logger log.Log, cfg Config, opts ...Opt) (*Host, error) {
logger.Info("starting libp2p host with config %+v", cfg)
key, err := ensureIdentity(cfg.DataDir)
key, err := EnsureIdentity(cfg.DataDir)
if err != nil {
return nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion p2p/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ func identityInfoFromDir(dir string) (*identityInfo, error) {
return &info, nil
}

func ensureIdentity(dir string) (crypto.PrivKey, error) {
// PrettyIdentityInfoFromDir returns a printable ID from a given identity directory.
func PrettyIdentityInfoFromDir(dir string) (string, error) {
identityInfo, err := identityInfoFromDir(dir)
return identityInfo.ID.Pretty(), err
}

// EnsureIdentity generates an identity key file in given directory.
func EnsureIdentity(dir string) (crypto.PrivKey, error) {
// TODO add crc check
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, fmt.Errorf("ensure that directory %s exist: %w", dir, err)
Expand Down
2 changes: 1 addition & 1 deletion p2p/idetity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestPersistedIdentity(t *testing.T) {
dir := t.TempDir()
_, err := ensureIdentity(dir)
_, err := EnsureIdentity(dir)
require.NoError(t, err)

info, err := identityInfoFromDir(dir)
Expand Down

0 comments on commit 0549224

Please sign in to comment.