Skip to content

Commit

Permalink
Cadence 1.0 Multi-Staging (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored May 15, 2024
1 parent 300d082 commit 2f95230
Show file tree
Hide file tree
Showing 19 changed files with 2,573 additions and 644 deletions.
20 changes: 11 additions & 9 deletions internal/cadence/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/onflow/flowkit/v2/output"

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

type lintFlagsCollection struct{}
Expand Down Expand Up @@ -202,7 +203,15 @@ func (r *lintResult) String() string {

total := numErrors + numWarnings
if total > 0 {
sb.WriteString(aurora.Colorize(fmt.Sprintf("%d %s (%d %s, %d %s)", total, pluralize("problem", total), numErrors, pluralize("error", numErrors), numWarnings, pluralize("warning", numWarnings)), color).String())
sb.WriteString(aurora.Colorize(fmt.Sprintf(
"%d %s (%d %s, %d %s)",
total,
util.Pluralize("problem", total),
numErrors,
util.Pluralize("error", numErrors),
numWarnings,
util.Pluralize("warning", numWarnings),
), color).String())
} else {
sb.WriteString(aurora.Green("Lint passed").String())
}
Expand All @@ -219,18 +228,11 @@ func (r *lintResult) Oneliner() string {
total := numErrors + numWarnings

if total > 0 {
return fmt.Sprintf("%d %s (%d %s, %d %s)", total, pluralize("problem", total), numErrors, pluralize("error", numErrors), numWarnings, pluralize("warning", numWarnings))
return fmt.Sprintf("%d %s (%d %s, %d %s)", total, util.Pluralize("problem", total), numErrors, util.Pluralize("error", numErrors), numWarnings, util.Pluralize("warning", numWarnings))
}
return "Lint passed"
}

func (r *lintResult) ExitCode() int {
return r.exitCode
}

func pluralize(word string, count int) string {
if count == 1 {
return word
}
return word + "s"
}
45 changes: 39 additions & 6 deletions internal/migrate/get_staged_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"

"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/output"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,24 +65,56 @@ func getStagedCode(
return nil, fmt.Errorf("error getting address by contract name: %w", err)
}

cName, err := cadence.NewString(contractName)
location := common.NewAddressLocation(nil, common.Address(addr), contractName)
code, err := getStagedContractCode(context.Background(), flow, location)
if err != nil {
return nil, fmt.Errorf("error creating cadence string: %w", err)
return nil, err
}

// If the contract is not staged, return nil
if code == nil {
return scripts.NewScriptResult(cadence.NewOptional(nil)), nil
}

caddr := cadence.NewAddress(addr)
return scripts.NewScriptResult(cadence.NewOptional(cadence.String(code))), nil
}

func getStagedContractCode(
ctx context.Context,
flow flowkit.Services,
location common.AddressLocation,
) ([]byte, error) {
cAddr := cadence.BytesToAddress(location.Address.Bytes())
cName, err := cadence.NewString(location.Name)
if err != nil {
return nil, fmt.Errorf("failed to get cadence string from contract name: %w", err)
}

value, err := flow.ExecuteScript(
context.Background(),
flowkit.Script{
Code: templates.GenerateGetStagedContractCodeScript(MigrationContractStagingAddress(flow.Network().Name)),
Args: []cadence.Value{caddr, cName},
Args: []cadence.Value{cAddr, cName},
},
flowkit.LatestScriptQuery,
)
if err != nil {
return nil, fmt.Errorf("error executing script: %w", err)
return nil, err
}

optValue, ok := value.(cadence.Optional)
if !ok {
return nil, fmt.Errorf("invalid script return value type: %T", value)
}

if optValue.Value == nil {
return nil, nil
}

strValue, ok := optValue.Value.(cadence.String)
if !ok {
return nil, fmt.Errorf("invalid script return value type: %T", value)
}

return scripts.NewScriptResult(value), nil
return []byte(strValue), nil
}
2 changes: 1 addition & 1 deletion internal/migrate/get_staged_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Test_GetStagedCode(t *testing.T) {

contractAddr := cadence.NewAddress(account.Address)
assert.Equal(t, contractAddr.String(), actualContractAddressArg.String())
}).Return(cadence.NewString(string(testContract.Source)))
}).Return(cadence.NewOptional(cadence.String(testContract.Source)), nil)

result, err := getStagedCode(
[]string{testContract.Name},
Expand Down
8 changes: 4 additions & 4 deletions internal/migrate/is_validated.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ import (
"github.com/onflow/flow-cli/internal/util"
)

//go:generate mockery --name GitHubRepositoriesService --output ./mocks --case underscore
type GitHubRepositoriesService interface {
//go:generate mockery --name gitHubRepositoriesService --inpackage --testonly --case underscore
type gitHubRepositoriesService interface {
GetContents(ctx context.Context, owner string, repo string, path string, opt *github.RepositoryContentGetOptions) (fileContent *github.RepositoryContent, directoryContent []*github.RepositoryContent, resp *github.Response, err error)
DownloadContents(ctx context.Context, owner string, repo string, filepath string, opt *github.RepositoryContentGetOptions) (io.ReadCloser, error)
}

type validator struct {
repoService GitHubRepositoriesService
repoService gitHubRepositoriesService
state *flowkit.State
logger output.Logger
network config.Network
Expand Down Expand Up @@ -102,7 +102,7 @@ func isValidated(
return v.validate(contractName)
}

func newValidator(repoService GitHubRepositoriesService, network config.Network, state *flowkit.State, logger output.Logger) *validator {
func newValidator(repoService gitHubRepositoriesService, network config.Network, state *flowkit.State, logger output.Logger) *validator {
return &validator{
repoService: repoService,
state: state,
Expand Down
3 changes: 1 addition & 2 deletions internal/migrate/is_validated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/stretchr/testify/require"

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

Expand All @@ -47,7 +46,7 @@ func Test_IsValidated(t *testing.T) {
// Helper function to test the isValidated function
// with all of the necessary mocks
testIsValidatedWithStatuses := func(statuses []contractUpdateStatus) (command.Result, error) {
mockClient := mocks.NewGitHubRepositoriesService(t)
mockClient := newMockGitHubRepositoriesService(t)

// mock github file download
data, _ := json.Marshal(statuses)
Expand Down
2 changes: 1 addition & 1 deletion internal/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
getStagedCodeCommand.AddToParent(Cmd)
IsStagedCommand.AddToParent(Cmd)
listStagedContractsCommand.AddToParent(Cmd)
stageContractCommand.AddToParent(Cmd)
stageCommand.AddToParent(Cmd)
unstageContractCommand.AddToParent(Cmd)
stateCommand.AddToParent(Cmd)
IsValidatedCommand.AddToParent(Cmd)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions internal/migrate/mock_staging_service_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions internal/migrate/mock_staging_validator_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2f95230

Please sign in to comment.