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

Add Cadence V0.42->V1.0 Update Checker to staging #1469

Merged
merged 22 commits into from
Mar 29, 2024
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
10 changes: 6 additions & 4 deletions internal/cadence/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

"errors"

"github.com/onflow/flow-cli/internal/util"

cdclint "github.com/onflow/cadence-tools/lint"
cdctests "github.com/onflow/cadence-tools/test/helpers"
"github.com/onflow/cadence/runtime/ast"
Expand Down Expand Up @@ -67,8 +69,8 @@ func newLinter(state *flowkit.State) *linter {

// Create checker configs for both standard and script
// Scripts have a different stdlib than contracts and transactions
l.checkerStandardConfig = l.newCheckerConfig(newStandardLibrary())
l.checkerScriptConfig = l.newCheckerConfig(newScriptStandardLibrary())
l.checkerStandardConfig = l.newCheckerConfig(util.NewStandardLibrary())
l.checkerScriptConfig = l.newCheckerConfig(util.NewScriptStandardLibrary())

return l
}
Expand Down Expand Up @@ -152,10 +154,10 @@ func (l *linter) lintFile(
}

// Create a new checker config with the given standard library
func (l *linter) newCheckerConfig(lib standardLibrary) *sema.Config {
func (l *linter) newCheckerConfig(lib util.StandardLibrary) *sema.Config {
return &sema.Config{
BaseValueActivationHandler: func(_ common.Location) *sema.VariableActivation {
return lib.baseValueActivation
return lib.BaseValueActivation
},
AccessCheckMode: sema.AccessCheckModeStrict,
PositionInfoEnabled: true, // Must be enabled for linters
Expand Down
59 changes: 57 additions & 2 deletions internal/migrate/stage_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package migrate

import (
"context"
"errors"
"fmt"
"strings"

"github.com/manifoldco/promptui"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/contract-updater/lib/go/templates"
flowsdk "github.com/onflow/flow-go-sdk"
"github.com/onflow/flowkit/v2"
Expand All @@ -35,7 +39,9 @@ import (
"github.com/onflow/flow-cli/internal/command"
)

var stageContractflags struct{}
var stageContractflags struct {
SkipValidation bool `default:"false" flag:"skip-validation" info:"Do not validate the contract code against staged dependencies"`
}

var stageContractCommand = &command.Command{
Cmd: &cobra.Command{
Expand All @@ -51,7 +57,7 @@ var stageContractCommand = &command.Command{
func stageContract(
args []string,
globalFlags command.GlobalFlags,
_ output.Logger,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
) (command.Result, error) {
Expand Down Expand Up @@ -82,6 +88,55 @@ func stageContract(
return nil, fmt.Errorf("failed to get account by contract name: %w", err)
}

// Validate the contract code by default
if !stageContractflags.SkipValidation {
logger.StartProgress("Validating contract code against any staged dependencies")
validator := newStagingValidator(flow, state)

var missingDependenciesErr *missingDependenciesError
contractLocation := common.NewAddressLocation(nil, common.Address(account.Address), contractName)
err = validator.ValidateContractUpdate(contractLocation, common.StringLocation(contract.Location), replacedCode)

logger.StopProgress()

// Errors when the contract's dependencies have not been staged yet are non-fatal
// This is because the contract may be dependent on contracts that are not yet staged
// and we do not want to require in-order staging of contracts
// Instead, we will prompt the user to continue staging the contract. Other errors
// will be fatal and require manual intervention using the --skip-validation flag if desired
if errors.As(err, &missingDependenciesErr) {
infoMessage := strings.Builder{}
infoMessage.WriteString("Validation cannot be performed as some of your contract's dependencies could not be found (have they been staged yet?)\n")
for _, contract := range missingDependenciesErr.MissingContracts {
infoMessage.WriteString(fmt.Sprintf(" - %s\n", contract))
}
infoMessage.WriteString("\nYou may still stage your contract, however it will be unable to be migrated until the missing contracts are staged by their respective owners. It is important to monitor the status of your contract using the `flow migrate is-validated` command\n")
logger.Error(infoMessage.String())

continuePrompt := promptui.Select{
Label: "Do you wish to continue staging your contract?",
Items: []string{"Yes", "No"},
}

_, result, err := continuePrompt.Run()
if err != nil {
return nil, err
}

if result == "No" {
return nil, fmt.Errorf("staging cancelled")
}
} else if err != nil {
logger.Error(validator.prettyPrintError(err, common.StringLocation(contract.Location)))
return nil, fmt.Errorf("errors were found while validating the contract code, and your contract HAS NOT been staged, you can use the --skip-validation flag to bypass this check")
} else {
logger.Info("No issues found while validating contract code\n")
logger.Info("DISCLAIMER: Pre-staging validation checks are not exhaustive and do not guarantee the contract will work as expected, please monitor the status of your contract using the `flow migrate is-validated` command\n")
}
} else {
logger.Info("Skipping contract code validation, you may monitor the status of your contract using the `flow migrate is-validated` command\n")
}

tx, res, err := flow.SendTransaction(
context.Background(),
transactions.SingleAccountRole(*account),
Expand Down
6 changes: 6 additions & 0 deletions internal/migrate/stage_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func Test_StageContract(t *testing.T) {
BlockHeight: 1,
}, nil)

// disable validation
stageContractflags.SkipValidation = true

result, err := stageContract(
[]string{testContract.Name},
command.GlobalFlags{
Expand All @@ -97,6 +100,9 @@ func Test_StageContract(t *testing.T) {
srv.Mock,
state,
)
// reset flags
stageContractflags.SkipValidation = false

assert.NoError(t, err)
assert.NotNil(t, result)
})
Expand Down
Loading
Loading