Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
refactor: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantpaez committed Sep 14, 2023
1 parent 4e99e17 commit 5b31ec1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
17 changes: 17 additions & 0 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/NethermindEth/eigenlayer/cli/prompter"
"github.com/NethermindEth/eigenlayer/pkg/daemon"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -20,10 +21,14 @@ func UpdateCmd(d daemon.Daemon, p prompter.Prompter) *cobra.Command {
cmd := cobra.Command{
Use: "update",
RunE: func(cmd *cobra.Command, args []string) error {
log.Info("Pulling package...")
pullResult, err := d.PullUpdate(instanceID, url, daemon.PullTarget{Version: version, Commit: commit})
if err != nil {
return err
}
log.Info("Package pulled successfully")
logVersionChange(pullResult.OldVersion, pullResult.NewVersion)
logCommitChange(pullResult.OldCommit, pullResult.NewCommit)
printOptionsTable(cmd.OutOrStdout(), pullResult.OldOptions, pullResult.MergedOptions)
return err
},
Expand All @@ -37,6 +42,18 @@ func UpdateCmd(d daemon.Daemon, p prompter.Prompter) *cobra.Command {
return &cmd
}

func logVersionChange(oldVersion, newVersion string) {
if newVersion != "" {
log.Infof("Package version changed: %s -> %s", oldVersion, newVersion)
}
}

func logCommitChange(oldCommit, newCommit string) {
if newCommit != "" {
log.Infof("Package commit changed from %s -> %s", oldCommit, newCommit)
}
}

type tableOptionItem struct {
name string
old string
Expand Down
23 changes: 16 additions & 7 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Daemon interface {
// calling Pull all is ready to call Install.
Pull(url string, ref PullTarget, force bool) (PullResult, error)

// PullUpdate downloads a node software package from the given URL and returns
// the result of merging both packages configs.
PullUpdate(instanceID string, url string, ref PullTarget) (PullUpdateResult, error)

// Install downloads and installs a node software package using the provided options,
Expand Down Expand Up @@ -150,23 +152,30 @@ type PullResult struct {
}

type PullUpdateResult struct {
// Version is the version of the pulled package.
Version string
// OldVersion is the version of the old package.
OldVersion string

// SpecVersion is the version of the Eigenlayer AVS Node Specification the instance
// targets. The version must match the regex `^\d+\.\d+\.\d+$`.
SpecVersion string
// NweVersion is the version of the new package.
NewVersion string

// Commit hash of the pulled package.
Commit string
// OldCommit is the commit hash of the old package.
OldCommit string

// NewCommit is the commit hash of the new package.
NewCommit string

// HasPlugin is true if the package has a plugin.
HasPlugin bool

// OldOptions is the list of options of the old package.
OldOptions []Option

// NewOptions is the list of options of the new package.
NewOptions []Option

// MergedOptions is the list of options of the new package merged with the
// old package. These options the ones that will be used for the new instance
// and should be filled by the user.
MergedOptions []Option

// HardwareRequirements is the hardware requirements specified in the package manifest.
Expand Down
37 changes: 23 additions & 14 deletions pkg/daemon/egn_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,8 @@ func (d *EgnDaemon) PullUpdate(instanceID string, url string, ref PullTarget) (P
if err != nil {
return PullUpdateResult{}, err
}
ok, err := pkgHandler.CommitPrecedence(instance.Commit, ref.Commit)
if err != nil {
return PullUpdateResult{}, err
}
if !ok {
return PullUpdateResult{}, fmt.Errorf("%w: current commit %s is not previous to the update commit %s", ErrInvalidUpdateCommit, instance.Commit, ref.Commit)
}
err = pkgHandler.CheckoutCommit(ref.Commit)
if err != nil {
return PullUpdateResult{}, err
}
} else {
var latestVersion string
latestVersion, err = pkgHandler.LatestVersion()
latestVersion, err := pkgHandler.LatestVersion()
if err != nil {
return PullUpdateResult{}, err
}
Expand All @@ -386,6 +374,24 @@ func (d *EgnDaemon) PullUpdate(instanceID string, url string, ref PullTarget) (P
return PullUpdateResult{}, err
}
}
// Get new commit hash
newCommit, err := pkgHandler.CurrentCommitHash()
if err != nil {
return PullUpdateResult{}, err
}
// Check commit precedence
ok, err := pkgHandler.CommitPrecedence(instance.Commit, newCommit)
if err != nil {
return PullUpdateResult{}, err
}
if !ok {
return PullUpdateResult{}, fmt.Errorf("%w: current commit %s is not previous to the update commit %s", ErrInvalidUpdateCommit, instance.Commit, ref.Commit)
}
// Get new version
newVersion, err := pkgHandler.CurrentVersion()
if err != nil {
return PullUpdateResult{}, err
}

// Get new options
profileNew, err := pkgHandler.Profile(instance.Profile)
Expand Down Expand Up @@ -426,7 +432,10 @@ func (d *EgnDaemon) PullUpdate(instanceID string, url string, ref PullTarget) (P
}

return PullUpdateResult{
Version: ref.Version,
OldVersion: instance.Version,
NewVersion: newVersion,
OldCommit: instance.Commit,
NewCommit: newCommit,
OldOptions: optionsOld,
NewOptions: optionsNew,
MergedOptions: mergedOptions,
Expand Down

0 comments on commit 5b31ec1

Please sign in to comment.