Skip to content

Commit

Permalink
Merge pull request #1547 from onflow/ianthpun/validation-check
Browse files Browse the repository at this point in the history
add contract migration check on all calls
  • Loading branch information
ianthpun authored May 16, 2024
2 parents 2f95230 + 67810c3 commit 8987859
Show file tree
Hide file tree
Showing 14 changed files with 476 additions and 350 deletions.
32 changes: 0 additions & 32 deletions cmd/flow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/onflow/flow-cli/internal/accounts"
Expand Down Expand Up @@ -57,34 +53,6 @@ func main() {
var cmd = &cobra.Command{
Use: "flow",
TraverseChildren: true,
// Messaging for Cadence 1.0 upgrade
PersistentPreRun: func(cmd *cobra.Command, args []string) {
outputFlag, _ := cmd.Flags().GetString("output")
// If output is set to json, do not append any message
if outputFlag != "json" {

width := 80
url := "https://cadence-lang.org/docs/cadence_migration_guide"

// Function to center text within a given width
centerText := func(text string, width int) string {
space := (width - len(text)) / 2
if space < 0 {
space = 0
}
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", space), text, strings.Repeat(" ", space))
}

fmt.Fprintln(os.Stderr, strings.Repeat("+", width))
fmt.Fprintln(os.Stderr, centerText("⚠ Upgrade to Cadence 1.0", width))
fmt.Fprintln(os.Stderr, centerText("The Crescendo network upgrade, including Cadence 1.0, is coming soon.", width))
fmt.Fprintln(os.Stderr, centerText("You may need to update your existing contracts to support this change.", width))
fmt.Fprintln(os.Stderr, centerText("Please visit our migration guide here:", width))
fmt.Fprintln(os.Stderr, centerText(url, width))
fmt.Fprintln(os.Stderr, strings.Repeat("+", width))

}
},
}

// quick commands
Expand Down
56 changes: 46 additions & 10 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

"github.com/dukex/mixpanel"
"github.com/getsentry/sentry-go"
"github.com/google/go-github/github"
"github.com/spf13/afero"
"github.com/spf13/cobra"

Expand All @@ -45,6 +46,7 @@ import (
"github.com/onflow/flowkit/v2/output"

"github.com/onflow/flow-cli/build"
"github.com/onflow/flow-cli/internal/migrate/validator"
"github.com/onflow/flow-cli/internal/settings"
"github.com/onflow/flow-cli/internal/util"
)
Expand Down Expand Up @@ -126,6 +128,11 @@ func (c Command) AddToParent(parent *cobra.Command) {
checkVersion(logger)
}

// check contract migrations if flag is set
if !Flags.SkipContractMigrationCheck {
checkContractMigrations(state, logger, flow)
}

// record command usage
wg := sync.WaitGroup{}
go UsageMetrics(c.Cmd, &wg)
Expand Down Expand Up @@ -424,14 +431,43 @@ func UsageMetrics(command *cobra.Command, wg *sync.WaitGroup) {

// GlobalFlags contains all global flags definitions.
type GlobalFlags struct {
Filter string
Format string
Save string
Host string
HostNetworkKey string
Log string
Network string
Yes bool
ConfigPaths []string
SkipVersionCheck bool
Filter string
Format string
Save string
Host string
HostNetworkKey string
Log string
Network string
Yes bool
ConfigPaths []string
SkipVersionCheck bool
SkipContractMigrationCheck bool
}

const migrationDataURL = "https://github.com/onflow/cadence/tree/master/migrations_data"

func checkContractMigrations(state *flowkit.State, logger output.Logger, flow flowkit.Services) {
contractStatuses, err := validator.NewValidator(github.NewClient(nil).Repositories, flow.Network(), state, logger).GetContractStatuses()
if err != nil {
// if we can't get the contract statuses, we don't check them
return
}

var failedContracts []validator.ContractUpdateStatus

for _, contract := range contractStatuses {
if contract.IsFailure() {
failedContracts = append(failedContracts, contract)
}
}

if len(failedContracts) > 0 {
fmt.Fprintf(
os.Stderr, "\n%s Heads up: We ran a check in the background to verify that your contracts are still valid for the Cadence 1.0 migration. We found %d contract(s) that have failed to migrate. \n", output.ErrorEmoji(), len(failedContracts),
)
fmt.Fprintf(
os.Stderr, "\n Please visit %s for the latest migration snapshot and information about the failure. \n", migrationDataURL,
)
}
return
}
5 changes: 3 additions & 2 deletions internal/migrate/get_staged_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/onflow/flow-cli/internal/command"
"github.com/onflow/flow-cli/internal/scripts"
"github.com/onflow/flow-cli/internal/util"
)

var getStagedCodeflags struct{}
Expand All @@ -54,13 +55,13 @@ func getStagedCode(
flow flowkit.Services,
state *flowkit.State,
) (command.Result, error) {
err := checkNetwork(flow.Network())
err := util.CheckNetwork(flow.Network())
if err != nil {
return nil, err
}

contractName := args[0]
addr, err := getAddressByContractName(state, contractName, flow.Network())
addr, err := util.GetAddressByContractName(state, contractName, flow.Network())
if err != nil {
return nil, fmt.Errorf("error getting address by contract name: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/migrate/is_staged.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/onflow/flow-cli/internal/command"
"github.com/onflow/flow-cli/internal/scripts"
"github.com/onflow/flow-cli/internal/util"
)

var isStagedflags struct{}
Expand All @@ -52,13 +53,13 @@ func isStaged(
flow flowkit.Services,
state *flowkit.State,
) (command.Result, error) {
err := checkNetwork(flow.Network())
err := util.CheckNetwork(flow.Network())
if err != nil {
return nil, err
}

contractName := args[0]
addr, err := getAddressByContractName(state, contractName, flow.Network())
addr, err := util.GetAddressByContractName(state, contractName, flow.Network())
if err != nil {
return nil, fmt.Errorf("error getting address by contract name: %w", err)
}
Expand Down
Loading

0 comments on commit 8987859

Please sign in to comment.